import { test, expect, seed, loginAs, api, API, type Role, v2 } from "./fixtures"; type Section = { id: string; title: string; body: string }; type Draft = { sections: Section[]; versions: { version: number; acceptances: number }[] }; type Notice = { version: number; builtIn: boolean; sections: Section[] }; type Operator = { id: string; name: string; active: boolean }; type AccessLog = { rows: { action: string; what: string; records: number | null; actor: string }[] }; type ConsentLog = { rows: { version?: number; source: string; email: string | null }[] }; test.describe("privacy notice, operators and access log (POPIA)", () => { // A version that needs accepting is published below; accept it for every seeded account // afterwards so later test files don't meet the prompt. test.afterAll(async ({ request }) => { const tokens = [...(["admin", "supervisor", "staff", "user"] as Role[]).map(r => seed().users[r].token), seed().newsPromptUser.token, seed().noticeFirstUser.token]; for (const token of tokens) { await request.post(`${API}/api/v2/me/privacy/notice/accept`, { headers: { Authorization: `Bearer ${token}` } }); } }); test("the built-in notice is shown until a version is published, with the operator register filled in", async ({ page }) => { const notice = await (await (await page.request.get(`${API}/api/v2/privacy/notice`)).json()).data as Notice; expect(notice.version).toBe(0); expect(notice.sections.find(s => s.id === "sharing")?.body).toContain("• PayFast (South Africa)"); await page.goto("/legal/privacy"); await expect(page.getByRole("heading", { name: "Privacy Policy" })).toBeVisible(); await expect(page.getByText("• PayFast (South Africa)", { exact: false })).toBeVisible(); }); test("an admin edits and publishes; operators feed the notice", async ({ page }) => { // A new operator appears in the notice's {operators} list. const op = await v2(page, "admin", "POST", "/api/v2/privacy/operators", { name: "E2E Mailer", purpose: "Sending emails", dataShared: "Email address", location: "Ireland" }); await loginAs(page, "admin"); await page.goto("/dashboard/admin/privacy?tab=notice"); const intro = page.getByLabel("1. Introduction text"); await expect(intro).toBeVisible(); await intro.fill("{org} looks after your information carefully. (E2E edit)"); await page.getByRole("button", { name: "Save draft" }).click(); await expect(page.getByText("Draft saved.")).toBeVisible(); // Nothing public changes until it's published. expect(((await (await page.request.get(`${API}/api/v2/privacy/notice`)).json()).data as Notice).version).toBe(0); await page.getByRole("button", { name: "Publish…" }).click(); await page.getByPlaceholder("e.g. We now use PayFast for online payments.").fill("Clearer introduction."); await page.getByRole("button", { name: "Publish", exact: true }).click(); await expect(page.getByText("Version 1 is published.")).toBeVisible(); await page.goto("/legal/privacy"); await expect(page.getByText("E2E Church looks after your information carefully. (E2E edit)")).toBeVisible(); await expect(page.getByText("• E2E Mailer (Ireland) — Sending emails.", { exact: false })).toBeVisible(); await expect(page.getByText(/Version 1/).first()).toBeVisible(); await v2(page, "admin", "DELETE", `/api/v2/privacy/operators/${op.id}`); }); test("after publishing its own version, the editor points out new built-in text", async ({ page }) => { // The published version came from the built-in text; put an older wording back in one changed // section so it shows as having new text. const draft = await v2(page, "admin", "GET", "/api/v2/privacy/notice/draft"); const sections = draft.sections.map(s => (s.id === "changes" ? { ...s, body: "We may update this notice from time to time." } : s)); await v2(page, "admin", "PUT", "/api/v2/privacy/notice/draft", { sections }); await loginAs(page, "admin"); await page.goto("/dashboard/admin/privacy?tab=notice"); const banner = page.getByTestId("builtin-updates"); await expect(banner).toContainText("The built-in privacy notice has been updated"); await expect(banner).toContainText("How people agree to the notice"); const marked = page.getByTestId("section-has-new-text"); await expect(marked).toHaveCount(1); await marked.getByText("New built-in text for this section — show it").click(); await marked.getByRole("button", { name: "Use the new text" }).click(); await expect(page.getByTestId("section-has-new-text")).toHaveCount(0); await page.getByRole("button", { name: "Save draft" }).click(); await expect(page.getByText("Draft saved.")).toBeVisible(); await banner.getByRole("button", { name: "Done — hide this" }).click(); await expect(page.getByTestId("builtin-updates")).toHaveCount(0); await page.reload(); await expect(page.getByLabel("1. Introduction text")).toBeVisible(); await expect(page.getByTestId("builtin-updates")).toHaveCount(0); await v2(page, "admin", "DELETE", "/api/v2/privacy/notice/draft"); }); test("someone who never accepted the notice is asked once, after login", async ({ page }) => { await page.addInitScript(token => localStorage.setItem("hope_events_token", token), seed().noticeFirstUser.token); await page.goto("/dashboard/user"); const dialog = page.getByRole("dialog", { name: "Our privacy notice" }); await expect(dialog).toBeVisible(); await expect(dialog).toContainText("It explains what we keep about you"); await dialog.getByRole("button", { name: "I accept" }).click(); await expect(dialog).toBeHidden(); await page.reload(); await expect(page.getByText("My Events").first()).toBeVisible(); await expect(page.getByRole("dialog", { name: "Our privacy notice" })).toHaveCount(0); }); test("the cookie button stays on a published notice", async ({ page }) => { await page.goto("/legal/privacy"); await expect(page.getByRole("button", { name: "Change my cookie choice" })).toBeVisible(); }); test("a significant change is accepted once, after login", async ({ page }) => { const draft = await v2(page, "admin", "GET", "/api/v2/privacy/notice/draft"); // Re-acceptance needs a summary of what changed. await expect(v2(page, "admin", "POST", "/api/v2/privacy/notice/publish", { sections: draft.sections, requiresAcceptance: true })).rejects.toThrow(/400/); const published = await v2<{ version: number }>(page, "admin", "POST", "/api/v2/privacy/notice/publish", { sections: draft.sections, requiresAcceptance: true, changeSummary: "We added WhatsApp news." }); await loginAs(page, "user"); await page.goto("/dashboard/user"); const dialog = page.getByRole("dialog", { name: "We've updated our privacy notice" }); await expect(dialog).toBeVisible(); await expect(dialog.getByText("We added WhatsApp news.")).toBeVisible(); await dialog.getByRole("button", { name: "I accept" }).click(); await expect(dialog).toBeHidden(); await page.reload(); await expect(page.getByText("My Events").first()).toBeVisible(); await expect(page.getByRole("dialog", { name: "We've updated our privacy notice" })).toHaveCount(0); const log = await v2(page, "admin", "GET", `/api/v2/privacy/consent-log?type=notice&q=${encodeURIComponent(seed().users.user.email)}`); expect(log.rows[0]).toMatchObject({ version: published.version, source: "prompt" }); }); test("bulk exports are in the access log", async ({ page }) => { const res = await page.request.post(`${API}/api/v2/reports/pdf`, { headers: { Authorization: `Bearer ${seed().users.supervisor.token}`, "Content-Type": "application/json" }, data: JSON.stringify({ title: "E2E attendee list", kind: "table", table: { columns: ["Name"], rows: [["A"], ["B"], ["C"]] } }), }); expect(res.ok()).toBe(true); await v2(page, "supervisor", "GET", `/api/v2/events/${seed().events.free}/consent`); const log = await v2(page, "admin", "GET", "/api/v2/privacy/access-log"); expect(log.rows.find(r => r.action === "report_pdf")).toMatchObject({ what: "E2E attendee list", records: 3, actor: "Sam Supervisor" }); expect(log.rows.some(r => r.action === "consent_list")).toBe(true); }); });