import { describe, it, expect, vi, beforeEach } from "vitest"; const mocks = vi.hoisted(() => ({ callMobileApi: vi.fn(), callMobileApiBinary: vi.fn() })); vi.mock("@/app/lib/server/mobileApi", () => ({ callMobileApi: mocks.callMobileApi, callMobileApiBinary: mocks.callMobileApiBinary })); import { GET as ticketGet } from "./tickets/[token]/route"; import { POST as consentPost } from "./tickets/[token]/consent/route"; import { GET as qrGet } from "./tickets/[token]/qr.png/route"; import { POST as attendeePost } from "./orders/[token]/tickets/[ticketId]/attendee/route"; type Handler = (req: never, ctx: { params: Promise> }) => Promise; const xff = { "x-forwarded-for": "6.6.6.6, 203.0.113.7" }; const call = (h: unknown, params: Record, init: RequestInit = {}) => (h as Handler)(new Request("http://localhost/x", init) as never, { params: Promise.resolve(params) }); beforeEach(() => { mocks.callMobileApi.mockReset().mockResolvedValue({ ok: true, data: { state: "VALID" } }); mocks.callMobileApiBinary.mockReset().mockResolvedValue({ ok: true, data: { body: new Uint8Array([137, 80, 78, 71]).buffer, contentType: "image/png" } }); }); // add-event-tickets: the ticket page, its consent, its QR PNG and naming an attendee, signed out, with only the last X-Forwarded-For hop. describe("public ticket code proxies", () => { it("GET a ticket by its code", async () => { const res = await call(ticketGet, { token: "c/1" }, { headers: xff }); expect(await res.json()).toEqual({ state: "VALID" }); expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/public/tickets/c%2F1", { headers: { "X-Forwarded-For": "203.0.113.7" } }); }); it("POST a consent", async () => { await call(consentPost, { token: "c1" }, { method: "POST", body: '{"consent":true}', headers: xff }); expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/public/tickets/c1/consent", { method: "POST", body: { consent: true }, headers: { "X-Forwarded-For": "203.0.113.7" } }); }); it("POST an attendee to an order's ticket", async () => { await call(attendeePost, { token: "t/1", ticketId: "7" }, { method: "POST", body: '{"name":"Tan"}', headers: xff }); expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/public/orders/t%2F1/tickets/7/attendee", { method: "POST", body: { name: "Tan" }, headers: { "X-Forwarded-For": "203.0.113.7" } }); }); it("refuses a ticket id that is not a number without calling upstream", async () => { const res = await call(attendeePost, { token: "t1", ticketId: "7/x" }, { method: "POST", body: "{}" }); expect(res.status).toBe(404); expect(mocks.callMobileApi).not.toHaveBeenCalled(); }); it("passes the QR PNG bytes and type, private, with the client hop", async () => { const res = await call(qrGet, { token: "c1" }, { headers: xff }); expect(res.headers.get("Content-Type")).toBe("image/png"); expect(res.headers.get("Cache-Control")).toBe("private, max-age=300"); expect(new Uint8Array(await res.arrayBuffer())).toEqual(new Uint8Array([137, 80, 78, 71])); expect(mocks.callMobileApiBinary).toHaveBeenCalledWith("/api/public/tickets/c1/qr.png", { headers: { "X-Forwarded-For": "203.0.113.7" } }); }); it("answers 404 for a QR that is not there", async () => { mocks.callMobileApiBinary.mockResolvedValue({ ok: false, status: 404, error: "x" }); expect((await call(qrGet, { token: "c1" })).status).toBe(404); }); });