fix: validate swipe restaurant ids against room candidates

This commit is contained in:
2026-03-03 12:06:03 +08:00
parent f3d8a58603
commit 8b4ab415fd
3 changed files with 24 additions and 2 deletions
+15
View File
@@ -113,6 +113,21 @@ describe("POST /api/room/[id]/swipe", () => {
expect(res.status).toBe(400);
});
it("returns 400 when restaurantId is not in room candidates", async () => {
mockAtomicUpdate.mockImplementation(async (_id, updater) => {
const data = structuredClone(TEST_ROOM_DATA);
return updater(data);
});
const req = createRequest("/api/room/ROOM01/swipe", {
method: "POST",
body: { userId: "user-1", restaurantId: "unknown-id", action: "like" },
});
const ctx = createRouteContext({ id: "ROOM01" });
const res = await POST(req, ctx);
expect(res.status).toBe(400);
});
it("returns 404 when room not found", async () => {
mockAtomicUpdate.mockResolvedValue(null);
+3 -1
View File
@@ -23,8 +23,10 @@ export const POST = apiHandler(async (req, { params }) => {
}
const restaurantIndex = data.restaurants.findIndex((r) => r.id === rid);
if (restaurantIndex < 0) {
throw new ApiError("restaurantId 不存在于该房间候选列表", 400);
}
const alreadySwiped =
restaurantIndex >= 0 &&
restaurantIndex < (data.swipeCounts[userId] ?? 0);
if (alreadySwiped) return data;