One proxy route for /api/admin/event-leads (dot segments rejected), service, types, and /event-leads gated by BO_EVENT_LEADS. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
63 lines
3.0 KiB
TypeScript
63 lines
3.0 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({ callMobileApi: vi.fn(), getToken: vi.fn() }));
|
|
vi.mock("@/app/lib/server/mobileApi", () => ({ callMobileApi: mocks.callMobileApi }));
|
|
vi.mock("@/app/lib/server/session", () => ({ getToken: mocks.getToken }));
|
|
|
|
import { GET, POST, PUT } from "./route";
|
|
|
|
type Req = Parameters<typeof GET>[0];
|
|
const ctx = (...path: string[]) => ({ params: Promise.resolve({ path }) });
|
|
const req = (url: string, init?: RequestInit) => new Request(`http://localhost${url}`, init) as unknown as Req;
|
|
|
|
beforeEach(() => {
|
|
mocks.callMobileApi.mockReset();
|
|
mocks.getToken.mockReset();
|
|
mocks.getToken.mockResolvedValue("jwt-abc");
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { ok: 1 } });
|
|
});
|
|
|
|
describe("/api/event-leads/[...path]", () => {
|
|
it("returns 401 without a session", async () => {
|
|
mocks.getToken.mockResolvedValue(null);
|
|
const res = await GET(req("/api/event-leads/events"), ctx("events"));
|
|
expect(res.status).toBe(401);
|
|
expect(mocks.callMobileApi).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("forwards GET with the query string", async () => {
|
|
const res = await GET(req("/api/event-leads/9/leads?status=OPEN&page=0&size=20"), ctx("9", "leads"));
|
|
expect(res.status).toBe(200);
|
|
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/event-leads/9/leads?status=OPEN&page=0&size=20", { token: "jwt-abc" });
|
|
});
|
|
|
|
it("forwards POST and PUT bodies", async () => {
|
|
await POST(req("/api/event-leads/9/match", { method: "POST", body: JSON.stringify({ mobile: "0123" }) }), ctx("9", "match"));
|
|
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/event-leads/9/match", { method: "POST", body: { mobile: "0123" }, token: "jwt-abc" });
|
|
await PUT(req("/api/event-leads/9/leads/7", { method: "PUT", body: JSON.stringify({ status: "WON" }) }), ctx("9", "leads", "7"));
|
|
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/event-leads/9/leads/7", { method: "PUT", body: { status: "WON" }, token: "jwt-abc" });
|
|
});
|
|
|
|
it("rejects dot segments so the proxy cannot leave /api/admin/event-leads", async () => {
|
|
const res = await GET(req("/api/event-leads/x"), ctx("..", "users"));
|
|
expect(res.status).toBe(400);
|
|
expect(mocks.callMobileApi).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("encodes path segments", async () => {
|
|
await GET(req("/api/event-leads/a"), ctx("a/b"));
|
|
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/event-leads/a%2Fb", { token: "jwt-abc" });
|
|
});
|
|
|
|
it("passes upstream 409 with code and details", async () => {
|
|
mocks.callMobileApi.mockResolvedValue({
|
|
ok: false, error: "Already captured", status: 409, code: "ALREADY_CAPTURED", details: { eventLeadUuid: "55" },
|
|
});
|
|
const res = await POST(req("/api/event-leads/9", { method: "POST", body: "{}" }), ctx("9"));
|
|
expect(res.status).toBe(409);
|
|
const json = (await res.json()) as Record<string, unknown>;
|
|
expect(json.code).toBe("ALREADY_CAPTURED");
|
|
expect(json.details).toEqual({ eventLeadUuid: "55" });
|
|
});
|
|
});
|