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

40 lines
2.2 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 orderGet } from "./orders/[token]/route";
import { POST as ticketsPost } from "./pages/[company]/[slug]/tickets/[block]/route";
type Handler = (req: never, ctx: { params: Promise<Record<string, string>> }) => Promise<Response>;
const xff = { "x-forwarded-for": "6.6.6.6, 203.0.113.7" };
const call = (h: unknown, params: Record<string, string>, 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: { reference: "T-1" } }));
// add-event-ticket-orders: the order page and the TICKETS block post, signed out, with only the last X-Forwarded-For hop.
describe("public ticket proxies", () => {
it("GET an order by its token", async () => {
const res = await call(orderGet, { token: "tok/1" }, { headers: xff });
expect(await res.json()).toEqual({ reference: "T-1" });
const [p, opts] = mocks.callMobileApi.mock.calls[0];
expect(p).toBe("/api/public/orders/tok%2F1");
expect(opts.headers).toEqual({ "X-Forwarded-For": "203.0.113.7" });
});
it("POST a ticket request to a page block", async () => {
await call(ticketsPost, { company: "INS", slug: "day/1", block: "2" }, { method: "POST", body: JSON.stringify({ a: 1 }), headers: xff });
const [p, opts] = mocks.callMobileApi.mock.calls[0];
expect(p).toBe("/api/public/pages/INS/day%2F1/tickets/2");
expect(opts).toMatchObject({ method: "POST", body: { a: 1 }, headers: { "X-Forwarded-For": "203.0.113.7" } });
});
it("passes refusals with status and code", async () => {
mocks.callMobileApi.mockResolvedValue({ ok: false, error: "General has only 1 ticket left.", status: 409, code: "TYPE_UNAVAILABLE" });
const res = await call(ticketsPost, { company: "INS", slug: "d", block: "0" }, { method: "POST", body: "{}" });
expect(res.status).toBe(409);
expect(((await res.json()) as Record<string, unknown>).code).toBe("TYPE_UNAVAILABLE");
});
});