Fixing Three Hidden Bugs in an Unfamiliar Vehicle-Booking API
System DesignModel answerLast updated
Debug an unfamiliar Node.js vehicle-booking backend where one user's list exposes others' vehicles, new vehicles are missing, and edits revert after refresh.
You are handed a Node.js backend for a vehicle-booking app that you have never seen before. It ships with API documentation and a preset test suite, and several tests currently fail. You have limited time.
Three unrelated defects have been reported:
- Foreign vehicles in "My Cars". The endpoint meant to list only the signed-in user's vehicles also returns vehicles owned by other users.
- Added vehicle is invisible. After a user creates a vehicle, it does not show up in their list.
- Edits revert. An edit request seems to succeed, but after a page refresh the vehicle shows its old values.
The goal is to make every preset test pass without introducing regressions.
The code is not provided, so describe your approach:
- In what order do you explore the project and the failing tests?
- For each symptom, which code paths do you inspect and what are the most probable root causes?
- What does each fix look like?
- How do you verify each fix and make sure nothing else breaks?
Assume a conventional layout (routes, authentication middleware, handlers or services, a data store) and state any assumptions you make. Listing the clarifying questions you would ask is welcome, for example how the user is identified, what the data store is, whether a cache exists, and whether tests hit HTTP or service functions.
Cover all three bugs. For bug 1, also address whether the problem is purely cosmetic or something more serious.
Hints
Hint 1
Run the test suite before reading any code, and map each failing test to one of the three symptoms. Then read each endpoint against what the API documentation says it should do.
Hint 2
For the missing vehicle, decide first whether the write failed or the read failed: inspect the stored data directly after a create. Then check which fields the list filter depends on and whether a cache is serving stale data.
Hint 3
For lost edits, look at how the update finds its record and what it does when nothing matches. Check whether the modified object is actually saved back and whether the response reflects stored state or just echoes the request.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach: work from evidence, one bug at a time
- Run the suite and triage. Group failing tests by symptom (list leaks, create invisible, edit not persisted). The test names and assertions tell you which endpoint and which expected value are wrong, which is faster than reading the whole codebase.
- Skim the structure. Locate the router, the auth middleware that sets the current user (for example
req.user), the handlers or services, and the data layer. Open the API docs for the three relevant endpoints (list mine, create, update) and note the documented request, response and authorization rules. - Fix one bug, rerun the whole suite, commit. Small diffs make regressions easy to spot. If time is short, take the bug whose failing tests are most clearly localized first.
Useful clarifying questions: how the caller is identified (session, token, header), what the store is (database, file, in-memory) and whether there is a cache, whether tests reset data between runs, and whether the client re-fetches on refresh.
Bug 1: list contains other users' vehicles
Where to look: the "my vehicles" handler and the query or filter it uses.
Likely causes:
- The filter is missing, so the handler returns everything (
find({}), or returning the full array). - The filter is ineffective: wrong field name (
userIdvsownerId), a type mismatch (string vs ObjectId or number, so a strict comparison never matches or a loose one matches wrongly), an assignment=instead of===, or a filter whose result is discarded. - The owner comes from a client-supplied query or body value instead of the authenticated identity.
Fix: derive the owner from the authenticated context (req.user.id), never from client input, and filter on the stored owner field with matching types:
const mine = await Cars.find({ ownerId: req.user.id });
res.json(mine);
Beyond the symptom: this is an access-control flaw, not just display. Check sibling endpoints (get by id, update, delete) for the same gap: they should return 403 or 404 when the vehicle belongs to someone else, as the docs specify. Add a test with two users to lock this in.
Bug 2: newly created vehicle does not appear
First, split write from read. After a create call, inspect the store directly (or call get-by-id).
- If the record is not stored, the bug is in the write path.
- If it is stored but not listed, the bug is in the read path.
Write-path causes: the handler never awaits the save (responds before the write completes, or the promise is dropped), pushes into a copy of an array, or validation silently rejects the record while still returning success.
Read-path causes: the create handler does not store the owner field the list filters on (so the new record has ownerId: undefined and never matches), stores it with a different type, or a cache or memoized list is not invalidated after the write.
Fix: persist the owner from req.user.id, await the save, respond only after it succeeds (201 with the stored record), and invalidate or bypass any cache on writes:
const car = await Cars.create({ ...validated, ownerId: req.user.id });
cache.del(`cars:${req.user.id}`);
res.status(201).json(car);
Note that bugs 1 and 2 can interact: tightening the filter in bug 1 can expose a create handler that never set the owner, so rerun everything after each change.
Bug 3: edits vanish after refresh
Core idea: a success response does not prove persistence. The handler may be modifying an in-memory object or a copy and returning it, so the response looks right while the store is unchanged.
Likely causes:
- The update never saves (missing
await, missingsave(), or mutating a clone). - The lookup matches nothing (wrong id type or field), the update affects zero rows or documents, and the code ignores that count and returns 200.
- Wrong field names in the update payload, so nothing real is written.
- The response echoes the request body rather than the stored record.
Fix: locate the record by id and owner, check the result, write, and return the stored state:
const updated = await Cars.findOneAndUpdate(
{ _id: req.params.id, ownerId: req.user.id },
{ $set: allowedFields(req.body) },
{ new: true }
);
if (!updated) return res.status(404).json({ error: 'Not found' });
res.json(updated);
Including the owner in the lookup also closes the same ownership gap noted in bug 1.
Verification
- Rerun the full preset suite after each fix.
- Add small tests: two users each see only their own vehicles; create then list contains the new vehicle; update then re-fetch from the store returns the new values; updating a missing or foreign id returns the documented error.
- Confirm persistence by re-reading from the store, not from the response.
Common mistakes
- Reading the whole codebase before running the tests.
- Trusting a 200 response as proof that data was saved.
- Fixing the list filter on the client or in the response formatting instead of the query.
- Taking the owner id from the request body or query string.
- Changing several things at once, so a regression cannot be traced.
- Forgetting the cache, or forgetting to check sibling endpoints for the same ownership flaw.
Time allocation
Spend a few minutes on triage, then roughly equal time per bug, ordering by how localized each failure is. Leave the last few minutes for a full-suite run and a quick review of the diff.