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, DELETE } from "./events/[uuid]/[...path]/route"; const req = (url = "http://localhost/x", init?: RequestInit) => new Request(url, init) as never; const p = (uuid: string, ...path: string[]) => ({ params: Promise.resolve({ uuid, path }) }); beforeEach(() => { mocks.callMobileApi.mockReset().mockResolvedValue({ ok: true, data: { ok: 1 } }); mocks.getToken.mockReset().mockResolvedValue("jwt"); }); describe("event guest proxies", () => { it("require a session", async () => { mocks.getToken.mockResolvedValue(null); expect((await GET(req(), p("e", "guests"))).status).toBe(401); expect((await POST(req(), p("e", "guests"))).status).toBe(401); expect((await DELETE(req(), p("e", "guests", "3"))).status).toBe(401); expect(mocks.callMobileApi).not.toHaveBeenCalled(); }); it("forward guests, check-in, attendance, preview, send and delete with the token", async () => { await GET(req(), p("e1", "guests")); await GET(req(), p("e1", "checkin")); await GET(req(), p("e1", "attendance")); await POST(req("http://localhost/x?preview=true", { method: "POST", body: '{"guests":[]}' }), p("e1", "guests")); await POST(req("http://localhost/x", { method: "POST", body: '{"guests":[]}' }), p("e1", "guests")); await POST(req("http://localhost/x", { method: "POST", body: '{"allUnsent":true}' }), p("e1", "guests", "send")); await DELETE(req(), p("e1", "guests", "7")); expect(mocks.callMobileApi.mock.calls).toEqual([ ["/api/admin/events/e1/guests", { token: "jwt" }], ["/api/admin/events/e1/checkin", { token: "jwt" }], ["/api/admin/events/e1/attendance", { token: "jwt" }], ["/api/admin/events/e1/guests?preview=true", { method: "POST", body: { guests: [] }, token: "jwt" }], ["/api/admin/events/e1/guests", { method: "POST", body: { guests: [] }, token: "jwt" }], ["/api/admin/events/e1/guests/send", { method: "POST", body: { allUnsent: true }, token: "jwt" }], ["/api/admin/events/e1/guests/7", { method: "DELETE", token: "jwt" }], ]); }); it("answers a delete with an empty success body", async () => { mocks.callMobileApi.mockResolvedValue({ ok: true, data: undefined }); const res = await DELETE(req(), p("e1", "guests", "7")); expect(res.status).toBe(200); expect(await res.json()).toEqual({}); }); it("passes the upstream error code and status through", async () => { mocks.callMobileApi.mockResolvedValue({ ok: false, error: "Registered", status: 409, code: "GUEST_REGISTERED" }); const res = await DELETE(req(), p("e1", "guests", "7")); expect(res.status).toBe(409); expect(await res.json()).toEqual({ error: "Registered", code: "GUEST_REGISTERED" }); }); it("refuse paths other than guests, checkin and attendance", async () => { for (const path of [["registration-links"], ["guests", ".."], []]) { expect((await GET(req(), p("e1", ...path))).status).toBe(400); } expect(mocks.callMobileApi).not.toHaveBeenCalled(); }); it("refuse an event id with path characters before forwarding", async () => { for (const uuid of ["..", "a/b", "a%2Fb", "a b", "a?x=1", ""]) { expect((await GET(req(), p(uuid, "guests"))).status).toBe(400); expect((await DELETE(req(), p(uuid, "guests", "3"))).status).toBe(400); } expect(mocks.callMobileApi).not.toHaveBeenCalled(); await GET(req(), p("3f2a-9c", "guests")); expect(mocks.callMobileApi).toHaveBeenCalledTimes(1); }); });