Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
43 lines
2.0 KiB
TypeScript
43 lines
2.0 KiB
TypeScript
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 } from "./tickets/[code]/route";
|
|
import { POST } from "./tickets/[code]/checkin/route";
|
|
|
|
const req = (init?: RequestInit) => new Request("http://localhost/x", init) as never;
|
|
const p = (code: string) => ({ params: Promise.resolve({ code }) });
|
|
|
|
beforeEach(() => {
|
|
mocks.callMobileApi.mockReset().mockResolvedValue({ ok: true, data: { state: "VALID" } });
|
|
mocks.getToken.mockReset().mockResolvedValue("jwt");
|
|
});
|
|
|
|
// add-event-tickets: the staff ticket view and door check-in go to mobile-api with the session token.
|
|
describe("staff ticket proxies", () => {
|
|
it("forward the view and the check-in with the token", async () => {
|
|
await GET(req(), p("c/1"));
|
|
await POST(req({ method: "POST", body: '{"name":"Lim Wei"}' }), p("c1"));
|
|
expect(mocks.callMobileApi.mock.calls).toEqual([
|
|
["/api/admin/tickets/c%2F1", { token: "jwt" }],
|
|
["/api/admin/tickets/c1/checkin", { method: "POST", body: { name: "Lim Wei" }, token: "jwt" }],
|
|
]);
|
|
});
|
|
|
|
it("answer 401 without a session and never call upstream", async () => {
|
|
mocks.getToken.mockResolvedValue(undefined);
|
|
expect((await GET(req(), p("c1"))).status).toBe(401);
|
|
expect((await POST(req({ method: "POST", body: "{}" }), p("c1"))).status).toBe(401);
|
|
expect(mocks.callMobileApi).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("pass a refusal with its status and code", async () => {
|
|
mocks.callMobileApi.mockResolvedValue({ ok: false, status: 409, error: "NOT_EVENT_DAY", code: "NOT_EVENT_DAY" });
|
|
const res = await POST(req({ method: "POST", body: "{}" }), p("c1"));
|
|
expect(res.status).toBe(409);
|
|
expect(((await res.json()) as Record<string, unknown>).code).toBe("NOT_EVENT_DAY");
|
|
});
|
|
});
|