import { test, expect, seed, loginAs, api, v2 } from "./fixtures"; type RunResult = { result: Record> }; type UserRow = { name: string; email: string; isActive: boolean }; type Readiness = { score: number; checks: { key: string; ok: boolean; applies: boolean }[] }; type Breach = { id: string; status: string; regulatorNotifiedAt: string | null }; test.describe("retention, breaches and readiness (POPIA)", () => { test("the retention job warns inactive accounts first, and anonymises them 30 days after the warning", async ({ page }) => { const { result } = await v2(page, "admin", "POST", "/api/v2/privacy/retention/run"); expect(result.inactive_accounts.warned).toBeGreaterThanOrEqual(1); expect(result.inactive_accounts.anonymised).toBeGreaterThanOrEqual(1); expect(result.login_history.deleted).toBeGreaterThanOrEqual(1); const warned = await v2(page, "admin", "GET", `/api/v2/users/${seed().retention.toWarn}`); expect(warned.email).toBe("dormant@e2e.test"); const gone = await v2(page, "admin", "GET", `/api/v2/users/${seed().retention.toAnonymise}`); expect(gone.name).toBe("Deleted User"); expect(gone.email).toMatch(/@deleted\.invalid$/); // Seeded accounts (staff roles, and the recently active user) are untouched. expect((await v2(page, "admin", "GET", `/api/v2/users/${seed().users.user.id}`)).email).toBe("user@e2e.test"); }); test("retention periods are validated and saved; the notice follows them", async ({ page }) => { const state = await v2<{ policies: Record }>(page, "admin", "GET", "/api/v2/privacy/retention"); await expect(v2(page, "admin", "PUT", "/api/v2/privacy/retention", { policies: { ...state.policies, inactive_accounts: { enabled: true, months: 3 } } })).rejects.toThrow(/400/); await v2(page, "admin", "PUT", "/api/v2/privacy/retention", { policies: { ...state.policies, form_answers: { enabled: true, months: 18 } } }); await page.goto("/legal/privacy"); await expect(page.getByText("Answers to event forms are deleted 18 months after the event.", { exact: false })).toBeVisible(); }); test("a breach is recorded, reported, and drafts are ready to copy", async ({ page }) => { const breach = await v2(page, "admin", "POST", "/api/v2/privacy/breaches", { title: "Phone with the attendee list lost", discoveredAt: new Date().toISOString(), description: "A leader's phone was stolen at the camp site.", dataAffected: "Names and phone numbers of 40 campers", }); expect(breach.status).toBe("open"); await loginAs(page, "admin"); await page.goto("/dashboard/admin/privacy?tab=breaches"); await page.getByRole("button", { name: /Phone with the attendee list lost/ }).click(); await expect(page.getByText("Draft message to the people affected")).toBeVisible(); await expect(page.getByText("Personal information affected: Names and phone numbers of 40 campers")).toBeVisible(); await page.getByRole("button", { name: "Regulator told today" }).click(); await expect(page.getByRole("button", { name: "Regulator told today" })).toHaveCount(0); const list = await v2(page, "admin", "GET", "/api/v2/privacy/breaches"); expect(list.find(b => b.id === breach.id)?.regulatorNotifiedAt).toBeTruthy(); }); test("the readiness page scores the install", async ({ page }) => { const r = await v2(page, "admin", "GET", "/api/v2/privacy/readiness"); expect(r.score).toBeGreaterThan(0); expect(r.checks.find(c => c.key === "retention_reviewed")?.ok).toBe(true); expect(r.checks.find(c => c.key === "retention_running")?.ok).toBe(false); // jobs are off in e2e await loginAs(page, "admin"); await page.goto("/dashboard/admin/privacy"); await expect(page.getByText("POPIA readiness")).toBeVisible(); await expect(page.getByText(`${r.score}%`)).toBeVisible(); }); });