-
Notifications
You must be signed in to change notification settings - Fork 46
Return 400 for malformed bulk status JSON #461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { describe, expect, it, vi, beforeEach } from "vitest"; | ||
| import { NextRequest } from "next/server"; | ||
| import { PUT } from "./route"; | ||
|
|
||
| const mockGetAuthContext = vi.fn(); | ||
| vi.mock("@/lib/auth/get-user", () => ({ | ||
| getAuthContext: (...args: unknown[]) => mockGetAuthContext(...args), | ||
| })); | ||
|
|
||
| const mockFrom = vi.fn(); | ||
|
|
||
| function makeRequest(body: string) { | ||
| return new NextRequest("http://localhost/api/applications/bulk-status", { | ||
| method: "PUT", | ||
| headers: { "Content-Type": "application/json" }, | ||
| body, | ||
| }); | ||
| } | ||
|
|
||
| describe("PUT /api/applications/bulk-status", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| mockGetAuthContext.mockResolvedValue({ | ||
| user: { id: "poster-1" }, | ||
| supabase: { from: mockFrom }, | ||
| }); | ||
| }); | ||
|
|
||
| it("returns 400 for malformed JSON without querying Supabase", async () => { | ||
| const res = await PUT(makeRequest("{")); | ||
|
|
||
| expect(res.status).toBe(400); | ||
| await expect(res.json()).resolves.toEqual({ error: "Invalid JSON body" }); | ||
| expect(mockFrom).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,16 @@ const bulkStatusSchema = z.object({ | |
| ]), | ||
| }); | ||
|
|
||
| async function parseJsonBody(request: NextRequest) { | ||
| try { | ||
| return { body: await request.json() }; | ||
| } catch { | ||
| return { | ||
| response: NextResponse.json({ error: "Invalid JSON body" }, { status: 400 }), | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| // PUT /api/applications/bulk-status - Bulk update application statuses | ||
| export async function PUT(request: NextRequest) { | ||
| try { | ||
|
|
@@ -22,7 +32,10 @@ export async function PUT(request: NextRequest) { | |
| } | ||
| const { user, supabase } = auth; | ||
|
|
||
| const body = await request.json(); | ||
| const parsed = await parseJsonBody(request); | ||
| if (parsed.response) return parsed.response; | ||
|
|
||
| const body = parsed.body; | ||
|
Comment on lines
+36
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||
| const validationResult = bulkStatusSchema.safeParse(body); | ||
|
|
||
| if (!validationResult.success) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mockFromis declared but never given a return valuemockFromis asserted to not be called in the malformed-JSON test, which is correct. However,vi.fn()returnsundefinedby default, so any future test case in this file that exercises the happy path will get an immediate crash when the route callssupabase.from(...).select(...). Consider adding a minimal chain stub (e.g.,.mockReturnValue({ select: vi.fn(), in: vi.fn(), ... })) inbeforeEachso the fixture is ready for follow-on tests.