-
Notifications
You must be signed in to change notification settings - Fork 46
Validate affiliate conversion ids before mutation queries #459
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 |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
| import { GET, POST, PUT } from "./route"; | ||
| import { DELETE, GET, POST, PUT } from "./route"; | ||
| import { NextRequest } from "next/server"; | ||
|
|
||
| // Mock auth | ||
|
|
@@ -44,6 +44,14 @@ function makePutRequest(id: string, body: Record<string, unknown>) { | |
| }); | ||
| } | ||
|
|
||
| function makeDeleteRequest(id: string, body: Record<string, unknown>) { | ||
| return new NextRequest(`http://localhost/api/affiliates/offers/${id}/conversions`, { | ||
| method: "DELETE", | ||
| headers: { "Content-Type": "application/json" }, | ||
| body: JSON.stringify(body), | ||
| }); | ||
| } | ||
|
|
||
| function makeParams(id: string) { | ||
| return { params: Promise.resolve({ id }) }; | ||
| } | ||
|
|
@@ -390,6 +398,37 @@ describe("PUT /api/affiliates/offers/[id]/conversions", () => { | |
| expect(mockCalculateCommission).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("rejects non-string conversion_id before updating conversions", async () => { | ||
| mockGetAuthContext.mockResolvedValue({ | ||
| user: { id: "user-seller", authMethod: "session" }, | ||
| }); | ||
|
|
||
| mockFrom.mockImplementation((table: string) => { | ||
| if (table === "affiliate_offers") { | ||
| return chainable({ | ||
| id: "offer-1", | ||
| seller_id: "user-seller", | ||
| commission_rate: 0.2, | ||
| commission_type: "percentage", | ||
| commission_flat_sats: 0, | ||
| }); | ||
| } | ||
| return chainable([]); | ||
| }); | ||
|
|
||
| const res = await PUT( | ||
| makePutRequest("offer-1", { | ||
| conversion_id: { id: "conv-1" }, | ||
| note: "paid elsewhere", | ||
| }), | ||
| makeParams("offer-1") | ||
| ); | ||
|
|
||
| expect(res.status).toBe(400); | ||
| await expect(res.json()).resolves.toEqual({ error: "conversion_id is required" }); | ||
| expect(mockFrom).not.toHaveBeenCalledWith("affiliate_conversions"); | ||
| }); | ||
|
|
||
| it("updates integer sale_amount_sats and recalculates commission", async () => { | ||
| mockGetAuthContext.mockResolvedValue({ | ||
| user: { id: "user-seller", authMethod: "session" }, | ||
|
|
@@ -477,6 +516,39 @@ describe("PUT /api/affiliates/offers/[id]/conversions", () => { | |
| }); | ||
| }); | ||
|
|
||
| describe("DELETE /api/affiliates/offers/[id]/conversions", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it("rejects non-string conversion_id before querying conversions", async () => { | ||
| mockGetAuthContext.mockResolvedValue({ | ||
| user: { id: "user-seller", authMethod: "session" }, | ||
| }); | ||
|
|
||
| mockFrom.mockImplementation((table: string) => { | ||
| if (table === "affiliate_offers") { | ||
| return chainable({ | ||
| id: "offer-1", | ||
| seller_id: "user-seller", | ||
| }); | ||
| } | ||
| return chainable([]); | ||
| }); | ||
|
|
||
| const res = await DELETE( | ||
| makeDeleteRequest("offer-1", { | ||
| conversion_id: ["conv-1"], | ||
| }), | ||
| makeParams("offer-1") | ||
| ); | ||
|
|
||
| expect(res.status).toBe(400); | ||
| await expect(res.json()).resolves.toEqual({ error: "conversion_id is required" }); | ||
| expect(mockFrom).not.toHaveBeenCalledWith("affiliate_conversions"); | ||
| }); | ||
| }); | ||
|
Comment on lines
+519
to
+550
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 new 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! |
||
|
|
||
| describe("PUT /api/affiliates/offers/[id]/conversions", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
|
|
||
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.
conversion_idnot testedThe new
isNonEmptyStringguard rejects whitespace-only strings (e.g.," "), which is a behavior change from the old!conversion_idcheck — a whitespace string is truthy and previously would have been forwarded to Supabase. Neither the PUT nor the DELETE new tests cover this path, so that specific validation branch has no regression coverage.