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("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="); }); });