-
Notifications
You must be signed in to change notification settings - Fork 49
Scope bounty submissions for API key auth #393
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,83 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
| import { NextRequest } from "next/server"; | ||
|
|
||
| const mockFrom = vi.fn(); | ||
|
|
||
| const supabaseClient = { | ||
| from: mockFrom, | ||
| }; | ||
|
|
||
| vi.mock("@/lib/auth/get-user", () => ({ | ||
| getAuthContext: vi.fn(), | ||
| })); | ||
|
|
||
| import { GET } from "./route"; | ||
| import { getAuthContext } from "@/lib/auth/get-user"; | ||
|
|
||
| const mockGetAuthContext = vi.mocked(getAuthContext); | ||
|
|
||
| function makeRequest() { | ||
| return new NextRequest("http://localhost/api/bounties/bounty-1/submissions", { | ||
| method: "GET", | ||
| }); | ||
| } | ||
|
|
||
| function makeParams() { | ||
| return { params: Promise.resolve({ id: "bounty-1" }) }; | ||
| } | ||
|
|
||
| function makeBountyQuery(creatorId: string) { | ||
| return { | ||
| select: vi.fn().mockReturnThis(), | ||
| eq: vi.fn().mockReturnThis(), | ||
| single: vi.fn().mockResolvedValue({ | ||
| data: { id: "bounty-1", creator_id: creatorId }, | ||
| }), | ||
| }; | ||
| } | ||
|
|
||
| function makeSubmissionsQuery() { | ||
| return { | ||
| select: vi.fn().mockReturnThis(), | ||
| eq: vi.fn().mockReturnThis(), | ||
| order: vi.fn().mockResolvedValue({ data: [], error: null }), | ||
| }; | ||
| } | ||
|
|
||
| describe("GET /api/bounties/[id]/submissions", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| mockGetAuthContext.mockResolvedValue({ | ||
| user: { id: "user-1", authMethod: "api_key" }, | ||
| supabase: supabaseClient, | ||
| } as never); | ||
| }); | ||
|
|
||
| it("filters non-creator API key requests to the caller's submissions", async () => { | ||
| const bountyQuery = makeBountyQuery("creator-1"); | ||
| const submissionsQuery = makeSubmissionsQuery(); | ||
| mockFrom.mockImplementation((table: string) => | ||
| table === "bounties" ? bountyQuery : submissionsQuery | ||
| ); | ||
|
|
||
| const response = await GET(makeRequest(), makeParams()); | ||
|
|
||
| expect(response.status).toBe(200); | ||
| expect(submissionsQuery.eq).toHaveBeenCalledWith("bounty_id", "bounty-1"); | ||
| expect(submissionsQuery.eq).toHaveBeenCalledWith("submitter_id", "user-1"); | ||
| }); | ||
|
|
||
| it("allows bounty creators to list all submissions for their bounty", async () => { | ||
| const bountyQuery = makeBountyQuery("user-1"); | ||
| const submissionsQuery = makeSubmissionsQuery(); | ||
| mockFrom.mockImplementation((table: string) => | ||
| table === "bounties" ? bountyQuery : submissionsQuery | ||
| ); | ||
|
|
||
| const response = await GET(makeRequest(), makeParams()); | ||
|
|
||
| expect(response.status).toBe(200); | ||
| expect(submissionsQuery.eq).toHaveBeenCalledWith("bounty_id", "bounty-1"); | ||
| expect(submissionsQuery.eq).not.toHaveBeenCalledWith("submitter_id", "user-1"); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,22 +15,35 @@ export async function GET( | |
| } | ||
| const { user, supabase } = auth; | ||
|
|
||
| // RLS will filter to (submitter or bounty creator) automatically. | ||
| const { data, error } = await (supabase as any) | ||
| const { data: bounty } = await (supabase as any) | ||
| .from("bounties") | ||
| .select("id, creator_id") | ||
| .eq("id", id) | ||
| .single(); | ||
|
|
||
| if (!bounty) { | ||
| return NextResponse.json({ error: "Bounty not found" }, { status: 404 }); | ||
| } | ||
|
Comment on lines
+18
to
+26
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.
The |
||
|
|
||
| let query = (supabase as any) | ||
| .from("bounty_submissions") | ||
| .select( | ||
| ` | ||
| *, | ||
| submitter:profiles!submitter_id (id, username, full_name, avatar_url) | ||
| ` | ||
| ) | ||
| .eq("bounty_id", id) | ||
| .order("created_at", { ascending: false }); | ||
| .eq("bounty_id", id); | ||
|
|
||
| if (bounty.creator_id !== user.id) { | ||
| query = query.eq("submitter_id", user.id); | ||
| } | ||
|
|
||
| const { data, error } = await query.order("created_at", { ascending: false }); | ||
|
|
||
| if (error) { | ||
| return NextResponse.json({ error: error.message }, { status: 400 }); | ||
| } | ||
| void user; | ||
| return NextResponse.json({ data: data || [] }); | ||
| } catch { | ||
| return NextResponse.json({ error: "Unexpected error" }, { status: 500 }); | ||
|
|
||
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.
api_keyauthBoth test cases fix
authMethodto"api_key", which exercises only the service-role (RLS-bypassed) code path. The"session"path (session-scoped client, RLS active) is untested, so regressions in that path — e.g. a creator losing access to all submissions because RLS policies diverge from the new app-level logic — would not be caught. Adding a describe block for session auth with both creator and non-creator sub-cases would complete coverage.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!