fix: resolve TODO.md security/perf/test-coverage backlog
Fixes the 13-item codebase health scan backlog: wraps meal-plan generation in a transaction, adds missing userId/GIN indexes, fixes an IPv6-parsing gap in the webhook SSRF guard (and an identical duplicated bug in the AI URL-import path, now consolidated onto one implementation), paginates the collections list, dedupes the AI recipe Zod schemas, wires up Stripe tier sync, rate-limits AI key rotation, gets `pnpm typecheck` actually working, and adds test coverage for the previously-untested admin/webhooks routes. Two flagged issues (collection removeRecipeId IDOR, tier-limit race) turned out to already be fixed/non-issues on inspection — noted in TODO.md rather than silently dropped. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import { NextRequest } from "next/server";
|
||||
|
||||
const mockSession = { user: { id: "user-1" } };
|
||||
|
||||
vi.mock("@/lib/api-auth", () => ({
|
||||
requireSession: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/validate-webhook-url", () => ({
|
||||
validateWebhookUrl: vi.fn().mockResolvedValue(null),
|
||||
}));
|
||||
|
||||
const { mockSelectChain, mockInsertValues } = vi.hoisted(() => {
|
||||
const mockSelectChain = {
|
||||
from: vi.fn().mockReturnThis(),
|
||||
where: vi.fn().mockResolvedValue([]),
|
||||
};
|
||||
return { mockSelectChain, mockInsertValues: vi.fn().mockResolvedValue(undefined) };
|
||||
});
|
||||
|
||||
vi.mock("@epicure/db", () => ({
|
||||
db: {
|
||||
select: vi.fn(() => mockSelectChain),
|
||||
insert: vi.fn(() => ({ values: mockInsertValues })),
|
||||
},
|
||||
webhooks: { id: "id", userId: "user_id", url: "url", events: "events", active: "active", createdAt: "created_at" },
|
||||
eq: vi.fn((a, b) => ({ a, b, op: "eq" })),
|
||||
}));
|
||||
|
||||
const { requireSession } = await import("@/lib/api-auth");
|
||||
const { validateWebhookUrl } = await import("@/lib/validate-webhook-url");
|
||||
import { GET, POST } from "../route";
|
||||
|
||||
function makeRequest(method: string, body?: unknown) {
|
||||
return new NextRequest("http://localhost/api/v1/webhooks", {
|
||||
method,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: body ? JSON.stringify(body) : undefined,
|
||||
});
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.mocked(requireSession).mockResolvedValue({ session: mockSession as never, response: null });
|
||||
vi.mocked(validateWebhookUrl).mockResolvedValue(null);
|
||||
mockSelectChain.where.mockResolvedValue([]);
|
||||
});
|
||||
|
||||
describe("GET /api/v1/webhooks", () => {
|
||||
it("returns 401 when not authenticated", async () => {
|
||||
vi.mocked(requireSession).mockResolvedValue({
|
||||
session: null,
|
||||
response: new Response(JSON.stringify({ error: "Unauthorized" }), { status: 401 }),
|
||||
} as never);
|
||||
|
||||
const res = await GET();
|
||||
expect(res.status).toBe(401);
|
||||
});
|
||||
|
||||
it("returns 200 with the user's webhooks", async () => {
|
||||
mockSelectChain.where.mockResolvedValue([{ id: "wh-1", userId: "user-1" }]);
|
||||
const res = await GET();
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json() as unknown[];
|
||||
expect(body).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /api/v1/webhooks", () => {
|
||||
const validBody = { url: "https://example.com/hook", events: ["recipe.created"] };
|
||||
|
||||
it("returns 400 on validation error", async () => {
|
||||
const res = await POST(makeRequest("POST", { url: "" }));
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
|
||||
it("returns 400 when the URL fails SSRF validation", async () => {
|
||||
vi.mocked(validateWebhookUrl).mockResolvedValue("Webhook URL must not point to a private or reserved address");
|
||||
const res = await POST(makeRequest("POST", validBody));
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
|
||||
it("returns 401 when not authenticated", async () => {
|
||||
vi.mocked(requireSession).mockResolvedValue({
|
||||
session: null,
|
||||
response: new Response(JSON.stringify({ error: "Unauthorized" }), { status: 401 }),
|
||||
} as never);
|
||||
|
||||
const res = await POST(makeRequest("POST", validBody));
|
||||
expect(res.status).toBe(401);
|
||||
});
|
||||
|
||||
it("returns 201 and creates the webhook", async () => {
|
||||
const res = await POST(makeRequest("POST", validBody));
|
||||
expect(res.status).toBe(201);
|
||||
const body = await res.json() as { url: string; secret: string };
|
||||
expect(body.url).toBe(validBody.url);
|
||||
expect(body.secret).toBeTruthy();
|
||||
expect(mockInsertValues).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user