Files
co-lite/solvway-admin-portal/app/api/public/inviteCheckinProxies.test.ts
T

45 lines
2.5 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
const mocks = vi.hoisted(() => ({ callMobileApi: vi.fn() }));
vi.mock("@/app/lib/server/mobileApi", () => ({ callMobileApi: mocks.callMobileApi }));
import { GET as inviteGet, POST as invitePost } from "./invite/[token]/route";
import { POST as inviteCheckin } from "./invite/[token]/checkin/route";
import { GET as checkinGet } from "./checkin/[token]/route";
import { POST as lookup } from "./checkin/[token]/lookup/route";
import { POST as walkIn } from "./checkin/[token]/register/route";
type Handler = (req: never, ctx: { params: Promise<{ token: string }> }) => Promise<Response>;
const ctx = { params: Promise.resolve({ token: "tok/1" }) };
const call = (h: unknown, init: RequestInit = {}) => (h as Handler)(new Request("http://localhost/x", init) as never, ctx);
const xff = { "x-forwarded-for": "6.6.6.6, 203.0.113.7" };
beforeEach(() => mocks.callMobileApi.mockReset().mockResolvedValue({ ok: true, data: { state: "OPEN" } }));
describe("public invite and check-in proxies", () => {
it.each([
["invite GET", inviteGet, "GET", "/api/public/invite/tok%2F1"],
["invite POST", invitePost, "POST", "/api/public/invite/tok%2F1"],
["invite checkin POST", inviteCheckin, "POST", "/api/public/invite/tok%2F1/checkin"],
["checkin GET", checkinGet, "GET", "/api/public/checkin/tok%2F1"],
["lookup POST", lookup, "POST", "/api/public/checkin/tok%2F1/lookup"],
["register POST", walkIn, "POST", "/api/public/checkin/tok%2F1/register"],
])("%s maps to mobile-api with only the last X-Forwarded-For hop", async (_n, h, method, path) => {
const res = await call(h, method === "GET" ? { headers: xff } : { method, body: JSON.stringify({ a: 1 }), headers: xff });
expect(res.status).toBe(200);
expect(await res.json()).toEqual({ state: "OPEN" });
const [p, opts] = mocks.callMobileApi.mock.calls[0];
expect(p).toBe(path);
expect(opts.headers).toEqual({ "X-Forwarded-For": "203.0.113.7" });
if (method === "POST") expect(opts).toMatchObject({ method: "POST", body: { a: 1 } });
else expect(opts.method).toBeUndefined();
});
it("passes upstream failures with status and code", async () => {
mocks.callMobileApi.mockResolvedValue({ ok: false, error: "This event is full", status: 409, code: "EVENT_FULL" });
const res = await call(invitePost, { method: "POST", body: "{}" });
expect(res.status).toBe(409);
expect(((await res.json()) as Record<string, unknown>).code).toBe("EVENT_FULL");
});
});