Files
crosscode-platform/apps/events/backend/tests/privacyNotice.test.js
T
joshuaandClaude Opus 5.5 34f9e96e4e POPIA toolkit part 2: privacy notice editor, operators, consent and access logs
- Admin -> Privacy (POPIA): edit the privacy notice (built-in text as the
  start, placeholders for org/IO/operators), drafts, preview, numbered
  versions; earlier versions readable on /legal/privacy?version=N
- "Significant change" versions are accepted once after login
  (NoticePrompt); sign-up records the version accepted
- Operator register (seeded with the services the app uses), listed in
  the notice via {operators}
- Consent log (news opt-ins, notice acceptances, parental consent) and
  access log (report exports, registers, certificates, group lists, form
  answers, consent/medical lists, backups, impersonation)
- Information Officer on the contact page
- Unit tests (privacyNotice.test.js), e2e (privacy-notice.spec.ts); docs

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012mt6TCv85T9VKRq3cV7rXq
2026-10-23 19:11:20 +02:00

110 lines
5.1 KiB
JavaScript

jest.mock('../src/config/db', () => ({
privacyNotice: { findFirst: jest.fn(), findMany: jest.fn(), findUnique: jest.fn() },
operator: { findMany: jest.fn() },
appSetting: { findMany: jest.fn().mockResolvedValue([]), findUnique: jest.fn() },
}));
const prisma = require('../src/config/db');
const notice = require('../src/utils/privacyNotice');
const { reportRowCount } = require('../src/utils/accessLog');
const ctx = {
org: 'Hope Church', orgEmail: 'office@hope.test', website: 'hope.test', ioName: 'Ann IO', ioEmail: 'io@hope.test', medicalRetentionDays: '30',
operators: [
{ name: 'PayFast', location: 'South Africa', purpose: 'Online payments', dataShared: 'Name, email address' },
{ name: 'WAWP', location: null, purpose: 'Delivering WhatsApp messages', dataShared: 'Mobile number' },
],
};
beforeEach(() => jest.clearAllMocks());
describe('renderSections', () => {
test('fills placeholders and expands {operators} into one bullet each', () => {
const [s] = notice.renderSections([{ id: 'a', title: 'A', body: 'Run by {org} — ask {ioName} ({ioEmail}).\n{operators}\nUnknown {nope} stays.' }], ctx);
expect(s.body.split('\n')).toEqual([
'Run by Hope Church — ask Ann IO (io@hope.test).',
'• PayFast (South Africa) — Online payments. Receives: Name, email address.',
'• WAWP — Delivering WhatsApp messages. Receives: Mobile number.',
'Unknown {nope} stays.',
]);
});
test('{operators} inside a sentence is left alone', () => {
const [s] = notice.renderSections([{ id: 'a', title: 'A', body: 'See {operators} below' }], ctx);
expect(s.body).toBe('See {operators} below');
});
test('the built-in notice has no unfilled placeholders', () => {
const rendered = notice.renderSections(notice.defaultSections(), ctx);
for (const s of rendered) expect(s.body).not.toMatch(/\{\w+\}/);
expect(rendered.find(s => s.id === 'sharing').body).toContain('• PayFast (South Africa)');
});
});
describe('cleanSections', () => {
test('trims, makes ids from titles and keeps them unique', () => {
const out = notice.cleanSections([
{ title: ' 1. Introduction ', body: 'Hi\r\nthere ' },
{ title: 'Introduction', body: '' },
]);
expect(out).toEqual([
{ id: 'introduction', title: '1. Introduction', body: 'Hi\nthere' },
{ id: 'introduction-2', title: 'Introduction', body: '' },
]);
});
test('refuses an empty notice or a section without a title', () => {
expect(() => notice.cleanSections([])).toThrow(/at least one section/);
expect(() => notice.cleanSections([{ title: ' ', body: 'x' }])).toThrow(/needs a title/);
});
});
describe('noticePromptPending', () => {
test('nobody is asked while no version needs re-acceptance', async () => {
prisma.privacyNotice.findFirst.mockResolvedValue(null);
expect(await notice.noticePromptPending({ noticeVersionAccepted: null })).toBe(false);
});
test('asked when they accepted an older version than the newest one that needs it', async () => {
prisma.privacyNotice.findFirst.mockResolvedValue({ version: 3 });
expect(await notice.noticePromptPending({ noticeVersionAccepted: 2 })).toBe(true);
expect(await notice.noticePromptPending({ noticeVersionAccepted: null })).toBe(true);
expect(await notice.noticePromptPending({ noticeVersionAccepted: 3 })).toBe(false);
expect(await notice.noticePromptPending({ noticeVersionAccepted: 5 })).toBe(false);
expect(prisma.privacyNotice.findFirst.mock.calls[0][0].where).toEqual({ requiresAcceptance: true });
});
});
describe('publicNotice', () => {
beforeEach(() => prisma.operator.findMany.mockResolvedValue([]));
test('the built-in notice is version 0 while nothing is published', async () => {
prisma.privacyNotice.findMany.mockResolvedValue([]);
const n = await notice.publicNotice(null);
expect(n.version).toBe(0);
expect(n.builtIn).toBe(true);
expect(n.sections.length).toBeGreaterThan(10);
});
test('once published, version 0 is no longer available as a past version', async () => {
prisma.privacyNotice.findMany.mockResolvedValue([{ version: 1, publishedAt: new Date() }]);
expect(await notice.publicNotice(0)).toBeNull();
});
test('a published version, marked current only when it is the newest', async () => {
prisma.privacyNotice.findMany.mockResolvedValue([{ version: 2 }, { version: 1 }]);
prisma.privacyNotice.findUnique.mockResolvedValue({ version: 1, publishedAt: new Date('2026-10-01T10:00:00Z'), changeSummary: 'x', sections: [{ id: 'a', title: 'A', body: 'Hello' }] });
const n = await notice.publicNotice(1);
expect(n).toMatchObject({ version: 1, builtIn: false, current: false, changeSummary: 'x' });
expect(n.sections[0].body).toBe('Hello');
});
});
describe('reportRowCount', () => {
test('counts table rows, layered items and extra tables', () => {
expect(reportRowCount({ table: { rows: [[1], [2]] }, extraTables: [{ rows: [[1]] }] })).toBe(3);
expect(reportRowCount({ layered: { sections: [{ items: ['a', 'b'] }, { items: ['c'] }] } })).toBe(3);
expect(reportRowCount(null)).toBe(0);
});
});