- /api (v1) answers 410 API_V1_REMOVED, except provider webhooks (/api/webhooks/*, /api/whatsapp/webhook) and links in sent messages (308s, one-click POST). 43 v1 route files and deprecation.ts removed; /docs redirects to /docs/v2; API_DOCUMENTATION.md points there. - Read-only and impersonation lists name v2 paths only. The campus scope route test now runs over v2 routes (v2CampusScope.test.ts). - Passwords hashed with Argon2id (@node-rs/argon2); bcrypt hashes still sign in and are re-hashed at the next sign-in. - e2e on /api/v2 only (v2() helper); Playwright waits on /api/v2. - MIGRATING-TO-2.md with the full v1 -> v2 table. - Two documented response shapes corrected. Backend 690 tests and e2e 102/102 (PayFast sandbox skipped) pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
69 lines
3.0 KiB
TypeScript
69 lines
3.0 KiB
TypeScript
import { test as base, expect, type Page } from "@playwright/test";
|
|
import fs from "fs";
|
|
import path from "path";
|
|
|
|
export type Role = "admin" | "supervisor" | "staff" | "user";
|
|
export type Seed = {
|
|
users: Record<Role, { id: string; name: string; email: string; token: string }>;
|
|
events: { free: string; paid: string; course: string; dropIn: string; conference: string; kids: string; serving: string; form: string; past: string };
|
|
campuses: Record<"north" | "south", { id: string; slug: string }>;
|
|
northTicketQr: string;
|
|
formOptionId: string;
|
|
course: { sessions: { id: string; label: string }[]; ticketId: string; qrCode: string };
|
|
unsubscribeToken: string;
|
|
newsPromptUser: { id: string; token: string };
|
|
noticeFirstUser: { id: string; token: string };
|
|
retention: { toWarn: string; toAnonymise: string };
|
|
};
|
|
|
|
export const API = "http://localhost:5100";
|
|
|
|
// Written by e2e/support/prepare-db.js when the e2e backend starts, so read lazily.
|
|
let cached: Seed | null = null;
|
|
export function seed(): Seed {
|
|
if (!cached) cached = JSON.parse(fs.readFileSync(path.join(__dirname, ".data", "seed.json"), "utf8")) as Seed;
|
|
return cached;
|
|
}
|
|
|
|
/** Signs the page in as a seeded user (the token goes where the app keeps it). */
|
|
export async function loginAs(page: Page, role: Role) {
|
|
await page.addInitScript((token) => {
|
|
localStorage.setItem("hope_events_token", token);
|
|
}, seed().users[role].token);
|
|
}
|
|
|
|
export const test = base.extend({
|
|
page: async ({ page }, use) => {
|
|
// Skip the cookie notice so it never covers what the tests click.
|
|
await page.addInitScript(() => localStorage.setItem("hope_events_cookie_consent", "declined"));
|
|
await use(page);
|
|
},
|
|
});
|
|
|
|
/** Calls the e2e API as a seeded user. */
|
|
export async function api<T = unknown>(page: Page, role: Role, method: string, url: string, body?: unknown): Promise<T> {
|
|
const res = await page.request.fetch(`${API}${url}`, {
|
|
method,
|
|
headers: { Authorization: `Bearer ${seed().users[role].token}`, "Content-Type": "application/json" },
|
|
data: body === undefined ? undefined : JSON.stringify(body),
|
|
});
|
|
const text = await res.text();
|
|
if (!res.ok()) throw new Error(`${method} ${url} → ${res.status()}: ${text}`);
|
|
return (text ? JSON.parse(text) : null) as T;
|
|
}
|
|
|
|
/** Calls `/api/v2` as a seeded user and returns the answer's `data` (throws on an error, like `api`). */
|
|
export async function v2<T = unknown>(page: Page, role: Role, method: string, url: string, body?: unknown): Promise<T> {
|
|
return (await api<{ data: T }>(page, role, method, url, body)).data;
|
|
}
|
|
|
|
/** Chooses an event in an EventPicker (the first on the page, or the one labelled `label`) by searching its title. */
|
|
export async function pickEvent(page: Page, title: string, label?: string) {
|
|
const button = label ? page.getByRole("button", { name: new RegExp(`^${label}:`) }) : page.getByTestId("event-picker").first().getByRole("button").first();
|
|
await button.click();
|
|
await page.getByRole("combobox", { name: /^Search/ }).fill(title);
|
|
await page.getByRole("option", { name: new RegExp(title) }).first().click();
|
|
}
|
|
|
|
export { expect };
|