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 as getLinks } from "./events/[uuid]/registration-links/route"; import { PUT as putLink } from "./events/[uuid]/registration-links/[token]/route"; import { GET as getMine } from "./my-registration-links/route"; import { GET as getNotice, PUT as putNotice } from "./companies/[id]/privacy-notice/route"; const req = (init?: RequestInit) => new Request("http://localhost/x", init) as never; const p = (v: T) => ({ params: Promise.resolve(v) }); beforeEach(() => { mocks.callMobileApi.mockReset().mockResolvedValue({ ok: true, data: { ok: 1 } }); mocks.getToken.mockReset().mockResolvedValue("jwt"); }); describe("registration admin proxies", () => { it("require a session", async () => { mocks.getToken.mockResolvedValue(null); for (const res of [await getLinks(req(), p({ uuid: "e" })), await getMine(), await getNotice(req(), p({ id: "1" }))]) { expect(res.status).toBe(401); } expect(mocks.callMobileApi).not.toHaveBeenCalled(); }); it("forward to the admin endpoints with the token", async () => { await getLinks(req(), p({ uuid: "e1" })); await putLink(req({ method: "PUT", body: '{"enabled":false}' }), p({ uuid: "e1", token: "t1" })); await getMine(); await getNotice(req(), p({ id: "2" })); await putNotice(req({ method: "PUT", body: '{"textEn":"a","textMs":"b"}' }), p({ id: "2" })); expect(mocks.callMobileApi.mock.calls).toEqual([ ["/api/admin/events/e1/registration-links", { token: "jwt" }], ["/api/admin/events/e1/registration-links/t1", { method: "PUT", body: { enabled: false }, token: "jwt" }], ["/api/admin/my-registration-links", { token: "jwt" }], ["/api/admin/companies/2/privacy-notice", { token: "jwt" }], ["/api/admin/companies/2/privacy-notice", { method: "PUT", body: { textEn: "a", textMs: "b" }, token: "jwt" }], ]); }); });