import { test, expect, seed, API } from "./fixtures"; // /api/v2 (1.51.0) against the real server and database: paged public events, the v2 error // shape, the signed-in person, and the OpenAPI reference at /docs/v2. test.describe("API v2", () => { test("public events come a page at a time; errors have a code", async ({ request }) => { const res = await request.get(`${API}/api/v2/events?limit=2`); expect(res.ok()).toBe(true); const body = await res.json(); expect(body.data).toHaveLength(2); expect(body.meta).toMatchObject({ page: 1, limit: 2 }); expect(body.meta.total).toBeGreaterThan(2); const search = await (await request.get(`${API}/api/v2/events?q=free%20evening`)).json(); expect(search.data.map((e: { title: string }) => e.title)).toEqual(["E2E Free Evening"]); const one = await (await request.get(`${API}/api/v2/events/${seed().events.paid}`)).json(); expect(one.data.ticketOptions.length).toBeGreaterThan(0); const bad = await request.get(`${API}/api/v2/events?limit=1000`); expect(bad.status()).toBe(400); expect((await bad.json()).error).toMatchObject({ code: "VALIDATION_FAILED", fields: { limit: expect.any(String) } }); const missing = await request.get(`${API}/api/v2/events/00000000-0000-4000-8000-000000000000`); expect((await missing.json()).error.code).toBe("NOT_FOUND"); }); test("me and my registrations need a token", async ({ request }) => { expect((await request.get(`${API}/api/v2/me`)).status()).toBe(401); const headers = { Authorization: `Bearer ${seed().users.user.token}` }; const me = await (await request.get(`${API}/api/v2/me`, { headers })).json(); expect(me.data).toMatchObject({ id: seed().users.user.id, role: "user" }); const regs = await (await request.get(`${API}/api/v2/me/registrations?past=true`, { headers })).json(); expect(regs.meta).toMatchObject({ page: 1, limit: 25 }); expect(regs.data.length).toBe(Math.min(25, regs.meta.total)); }); test("register for a free event, then cancel it", async ({ request }) => { const headers = { Authorization: `Bearer ${seed().users.staff.token}` }; const event = (await (await request.get(`${API}/api/v2/events/${seed().events.free}`)).json()).data; const made = await request.post(`${API}/api/v2/registrations`, { headers, data: { eventId: event.id, options: [{ eventOptionId: event.ticketOptions[0].id, quantity: 1 }] } }); expect(made.status()).toBe(201); const reg = (await made.json()).data; expect(reg).toMatchObject({ status: "paid", balance: 0, event: { id: event.id } }); const someoneElse = await request.post(`${API}/api/v2/registrations/${reg.id}/cancel`, { headers: { Authorization: `Bearer ${seed().users.user.token}` } }); expect((await someoneElse.json()).error.code).toBe("NOT_FOUND"); const cancelled = await request.post(`${API}/api/v2/registrations/${reg.id}/cancel`, { headers }); expect((await cancelled.json()).data.status).toBe("cancelled"); const again = await request.post(`${API}/api/v2/registrations/${reg.id}/cancel`, { headers }); expect((await again.json()).error).toMatchObject({ code: "VALIDATION_FAILED", message: "Registration is already cancelled" }); }); test("log in, change my details, and check a ticket in", async ({ request }) => { const bad = await request.post(`${API}/api/v2/auth/login`, { data: { identifier: seed().users.user.email, password: "wrong-password" } }); expect(bad.status()).toBe(401); expect((await bad.json()).error).toMatchObject({ code: "UNAUTHENTICATED", message: "Invalid email or password" }); const login = await (await request.post(`${API}/api/v2/auth/login`, { data: { identifier: seed().users.user.email, password: "E2e-password-123" } })).json(); expect(login.data).toMatchObject({ status: "signed_in", user: { id: seed().users.user.id, role: "user" } }); const headers = { Authorization: `Bearer ${login.data.token}` }; const changed = await (await request.patch(`${API}/api/v2/me`, { headers, data: { notificationPreference: "email" } })).json(); expect(changed.data).toMatchObject({ id: seed().users.user.id, notificationPreference: "email" }); expect(changed.token).toEqual(expect.any(String)); // A free ticket for the user, then check it in twice through v2. const event = (await (await request.get(`${API}/api/v2/events/${seed().events.free}`)).json()).data; await request.post(`${API}/api/v2/registrations`, { headers, data: { eventId: event.id, options: [{ eventOptionId: event.ticketOptions[0].id, quantity: 1 }] } }); const tickets = await (await request.get(`${API}/api/tickets/mytickets`, { headers })).json(); const qr = tickets.find((t: { eventId: string; qrCode: string }) => t.eventId === event.id).qrCode; const staff = { Authorization: `Bearer ${seed().users.staff.token}` }; expect((await request.post(`${API}/api/v2/checkin/${qr}`, { headers, data: {} })).status()).toBe(403); const look = await (await request.get(`${API}/api/v2/checkin/${qr}`, { headers: staff })).json(); expect(look.data).toMatchObject({ outcome: "found", ticket: { eventId: event.id } }); expect(look.data.ticket.remaining).toBeGreaterThan(0); const scanned = await (await request.post(`${API}/api/v2/checkin/${qr}`, { headers: staff, data: { eventId: event.id } })).json(); expect(scanned.data).toMatchObject({ outcome: "checked_in", remaining: 0 }); const again = await (await request.post(`${API}/api/v2/checkin/${qr}`, { headers: staff, data: {} })).json(); expect(again.data).toMatchObject({ outcome: "already_used", checkedIn: 0 }); expect((await (await request.post(`${API}/api/v2/checkin/NOT-A-CODE`, { headers: staff, data: {} })).json()).error.code).toBe("NOT_FOUND"); }); test("an admin can read the OpenAPI reference at /docs/v2", async ({ page, request }) => { const headers = { Authorization: `Bearer ${seed().users.user.token}` }; expect((await request.get(`${API}/api/v2/openapi.json`, { headers })).status()).toBe(403); await page.goto(`${API}/docs/v2?token=${seed().users.admin.token}`); await expect(page.getByRole("heading", { name: /Cross Events API/ })).toBeVisible(); await expect(page.locator(".opblock-summary-path", { hasText: "/events/{id}" })).toBeVisible(); // The token isn't left in the address bar. expect(page.url()).not.toContain("token="); }); });