Skip to content
Closed
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
30 changes: 29 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,26 @@ describe("GET /api/bounties", () => {
});
});

describe("POST /api/bounties", () => {
beforeEach(() => vi.clearAllMocks());

it("returns 400 for malformed JSON without querying Supabase", async () => {
const from = vi.fn();
(getAuthContext as any).mockResolvedValue({
user: { id: "user-1" },
supabase: { from },
});
const request = {
json: vi.fn().mockRejectedValue(new SyntaxError("Invalid JSON")),
} as any;

const response = await POST(request);

expect(response.status).toBe(400);
await expect(response.json()).resolves.toEqual({
error: "Invalid JSON body",
});
expect(from).not.toHaveBeenCalled();
});
});

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: "Invalid JSON body" },
{ status: 400 }
);
}
const parsed = createBountySchema.safeParse(body);
if (!parsed.success) {
return NextResponse.json(
Expand Down
Loading