Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/app/api/bounties/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ vi.mock("@/lib/supabase/server", () => ({
createClient: vi.fn(),
}));

import { GET } from "./route";
vi.mock("@/lib/auth/get-user", () => ({
getAuthContext: vi.fn(),
}));

import { GET, POST } from "./route";
import { createClient } from "@/lib/supabase/server";
import { getAuthContext } from "@/lib/auth/get-user";

function makeReq(url: string) {
return { nextUrl: new URL(url) } as any;
Expand Down Expand Up @@ -60,3 +65,22 @@ describe("GET /api/bounties", () => {
});
});

describe("POST /api/bounties", () => {
it("returns 400 for malformed JSON body and never calls Supabase", async () => {
(getAuthContext as any).mockResolvedValue({
user: { id: "u1", email: "a@b.com" },
supabase: {},
});

const malformedReq = {
json: vi.fn().mockRejectedValue(new Error("Unexpected token")),
} as any;

const res = await POST(malformedReq);

expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toMatch(/malformed json/i);
});
});

10 changes: 9 additions & 1 deletion src/app/api/bounties/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,15 @@ export async function POST(request: NextRequest) {
}
const { user, supabase } = auth;

const body = await request.json();
let body: unknown;
try {
body = await request.json();
} catch {
return NextResponse.json(
{ error: "Malformed JSON body" },
{ status: 400 }
);
}
const parsed = createBountySchema.safeParse(body);
if (!parsed.success) {
return NextResponse.json(
Expand Down
Loading