POPIA toolkit part 3: retention, breach register, readiness

- Retention policies (Admin -> Privacy -> Retention) applied by a nightly
  `retention` job: inactive accounts anonymised after 5 years with a
  30-day warning (on by default; never staff roles or people with an
  upcoming registration), form answers, login history, payer details in
  payment notifications, audit/access logs
- User.lastLoginAt (set on login, backfilled) and inactivityWarnedAt
- Breach register (SecurityBreach) with drafts for the people affected
  and the Information Regulator
- Readiness checklist with a score
- Children's/medical flag in the access log; {retention} placeholder in
  the privacy notice
- Unit tests (retention.test.js), e2e (privacy-retention.spec.ts); docs

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012mt6TCv85T9VKRq3cV7rXq
This commit is contained in:
2026-10-23 20:25:58 +02:00
co-authored by Claude Opus 5.5
parent 7f5e31461a
commit 6907bc3bd3
32 changed files with 1441 additions and 35 deletions
+1
View File
@@ -9,6 +9,7 @@ export type Seed = {
course: { sessions: { id: string; label: string }[]; ticketId: string; qrCode: string };
unsubscribeToken: string;
newsPromptUser: { id: string; token: string };
retention: { toWarn: string; toAnonymise: string };
};
export const API = "http://localhost:5100";
+1 -1
View File
@@ -32,7 +32,7 @@ test.describe("privacy notice, operators and access log (POPIA)", () => {
const op = await api<Operator>(page, "admin", "POST", "/api/privacy/operators", { name: "E2E Mailer", purpose: "Sending emails", dataShared: "Email address", location: "Ireland" });
await loginAs(page, "admin");
await page.goto("/dashboard/admin/privacy");
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)");
+64
View File
@@ -0,0 +1,64 @@
import { test, expect, seed, loginAs, api } from "./fixtures";
type RunResult = { result: Record<string, Record<string, number>> };
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 api<RunResult>(page, "admin", "POST", "/api/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 api<UserRow>(page, "admin", "GET", `/api/users/${seed().retention.toWarn}`);
expect(warned.email).toBe("dormant@e2e.test");
const gone = await api<UserRow>(page, "admin", "GET", `/api/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 api<UserRow>(page, "admin", "GET", `/api/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 api<{ policies: Record<string, { enabled: boolean; months: number }> }>(page, "admin", "GET", "/api/privacy/retention");
await expect(api(page, "admin", "PUT", "/api/privacy/retention", { policies: { ...state.policies, inactive_accounts: { enabled: true, months: 3 } } })).rejects.toThrow(/400/);
await api(page, "admin", "PUT", "/api/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 api<Breach>(page, "admin", "POST", "/api/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 api<Breach[]>(page, "admin", "GET", "/api/privacy/breaches");
expect(list.find(b => b.id === breach.id)?.regulatorNotifiedAt).toBeTruthy();
});
test("the readiness page scores the install", async ({ page }) => {
const r = await api<Readiness>(page, "admin", "GET", "/api/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();
});
});
+10
View File
@@ -162,6 +162,16 @@ async function seed(url) {
const u = await prisma.user.create({ data: { name: 'Nomsa New', email: 'prompt@e2e.test', role: 'user', password, isActive: true, updatedAt: new Date(), newsEmail: true } });
return { id: u.id, token: generateToken(u.id, u.role, 0) };
})(),
// Retention: two accounts untouched for six years — one not yet warned, one warned 40 days
// ago (so the next run anonymises it) — and three-year-old login history.
retention: await (async () => {
const longAgo = new Date(Date.now() - 6 * 365 * 24 * 60 * 60 * 1000);
const oldUser = (email, warnedAt) => prisma.user.create({ data: { name: `Old ${email}`, email, role: 'user', password, isActive: true, createdAt: longAgo, updatedAt: longAgo, inactivityWarnedAt: warnedAt } });
const toWarn = await oldUser('dormant@e2e.test', null);
const toAnonymise = await oldUser('gone-quiet@e2e.test', new Date(Date.now() - 40 * 24 * 60 * 60 * 1000));
await prisma.securityEvent.create({ data: { userId: toWarn.id, type: 'login', createdAt: new Date(Date.now() - 3 * 365 * 24 * 60 * 60 * 1000) } });
return { toWarn: toWarn.id, toAnonymise: toAnonymise.id };
})(),
// The link a news email to the seeded user would carry (signed with JWT_SECRET).
unsubscribeToken: require(path.join(backend, 'src/utils/privacy')).unsubscribeToken({ userId: users.user.id }),
};