Events 3.3.0, Accounts 1.3.0, contracts 1.11.0: Accounts looks like Events, backups; sign-out and upload fixes

- Accounts: account and admin pages in Cross Events' layout (top bar, sidebar,
  a page per section, dark footer); quick links to the church's sites
  (GET /me/apps); nightly pg_dump backups with Admin -> Backups (run, keep,
  download, audited; heartbeat lastBackupAt); a fuller built-in privacy notice
  with headings and bullets; a fixed favicon. /backups/ ignored (anchored, so
  src/backups is kept).
- Contracts 1.11.0: people.removeDemo (only @example.com, never an admin).
- Events: uploads served from the folder they're saved in (a backend started
  outside backend/ answered 404 for a logo it had just saved); Branding checks
  the file loads before saving; logout waits for the sign-out call so Accounts
  signs out too; set-demo removes the last demo's people from Accounts.
Tests: Accounts 62 + e2e 8, Events 782, contracts 225; npm run check passes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-20 11:19:57 +02:00
co-authored by Claude Opus 5.5
parent 8ee64bb9c9
commit 8856d6b35d
61 changed files with 1514 additions and 162 deletions
@@ -0,0 +1,39 @@
// 3.3.0: new demo data (set-demo) removes the last demo's people from the church's Accounts
// (contracts 1.11.0 people.removeDemo), only while sign-in goes through Accounts.
const mockRequests: Record<string, unknown>[] = [];
let mockMode = 'accounts';
jest.mock('../src/config/db', () => ({}));
jest.mock('../src/accounts/client', () => ({
accounts: async () => ({
request: async (body: { subs: string[] }) => { mockRequests.push(body); return { removed: body.subs.length - 1, kept: [{ sub: body.subs[0], reason: 'not_demo' }] }; },
}),
accountsLink: async () => null,
AccountsUnavailable: class extends Error {},
}));
jest.mock('../src/accounts/mode', () => ({
...jest.requireActual('../src/accounts/mode'),
authMode: async () => mockMode,
}));
import { removeDemoPeople } from '../src/accounts/move';
const sub = (n: number) => `00000000-0000-4000-8000-${String(n).padStart(12, '0')}`;
beforeEach(() => { mockRequests.length = 0; mockMode = 'accounts'; });
test('asks Accounts to remove each old demo person once, a thousand at a time', async () => {
const subs = Array.from({ length: 1500 }, (_, i) => sub(i));
const r = await removeDemoPeople([...subs, subs[0]!, ''], 'admin-sub');
expect(mockRequests.map((q) => (q.subs as string[]).length)).toEqual([1000, 500]);
expect(mockRequests[0]).toMatchObject({ type: 'people.removeDemo', actor: 'admin-sub' });
expect(r).toEqual({ removed: 1498, kept: 2 });
});
test('nothing to ask while sign-in is Events\' own, or with nobody to remove', async () => {
mockMode = 'events';
expect(await removeDemoPeople([sub(1)])).toEqual({ removed: 0, kept: 0 });
mockMode = 'accounts';
expect(await removeDemoPeople([])).toEqual({ removed: 0, kept: 0 });
expect(mockRequests).toHaveLength(0);
});
@@ -0,0 +1,74 @@
// Branding uploads end to end (3.3.0): the real v2 upload routes (multer saving to disk) and the
// real file serving, as app.js mounts them. The logo and favicon saved are the files the returned
// address serves, wherever the backend was started from (before 3.3.0 a backend started outside
// backend/ answered 404 for a logo it had just saved). Only the database is a stand-in (no admin
// yet, so the upload is allowed as during setup).
import express from 'express';
import fs from 'fs';
import os from 'os';
import path from 'path';
import request from 'supertest';
import { v2Router } from '../src/v2';
/* eslint-disable @typescript-eslint/no-require-imports -- CommonJS */
const { serveUploads } = require('../src/utils/uploads');
/* eslint-enable @typescript-eslint/no-require-imports */
function site() {
const app = express();
app.use(express.json());
app.use('/api/v2', v2Router());
app.use('/api/uploads', serveUploads());
return app;
}
// Any model, any call: nothing found, no users (as before setup).
jest.mock('../src/config/db', () => {
const anything: unknown = new Proxy(function () { /* callable */ }, {
get: (_t, key) => (key === 'then' ? undefined : key === 'count' ? async () => 0 : anything),
apply: async () => null,
});
return anything;
});
// The real upload folder of this checkout (as multer writes it), cleaned up after.
const BRANDING = path.join(__dirname, '..', 'public', 'uploads', 'branding');
const made: string[] = [];
afterAll(() => { for (const f of made) fs.rmSync(path.join(BRANDING, f), { force: true }); });
const PNG = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==', 'base64');
describe('branding uploads', () => {
const startedIn = process.cwd();
// PM2 or a script may start the backend from anywhere: the served files mustn't depend on it.
beforeAll(() => process.chdir(fs.mkdtempSync(path.join(os.tmpdir(), 'events-cwd-'))));
afterAll(() => process.chdir(startedIn));
test.each([['logo', 'logo.png'], ['favicon', 'icon.png']])('a %s upload is served at the address it answers with', async (kind, name) => {
const app = site();
const up = await request(app).post(`/api/v2/uploads/${kind}`).attach('image', PNG, { filename: name, contentType: 'image/png' });
expect(up.status).toBe(200);
const saved = String(up.body.data.path);
expect(saved).toMatch(new RegExp(`^/uploads/branding/${kind}-[\\w-]+\\.png$`));
made.push(path.basename(saved));
expect(fs.existsSync(path.join(BRANDING, path.basename(saved)))).toBe(true);
const got = await request(app).get(new URL(up.body.data.url).pathname);
expect(got.status).toBe(200);
expect(Buffer.compare(got.body as Buffer, PNG)).toBe(0);
});
test('app.js serves uploads from that same folder, never one relative to where it started', () => {
const source = fs.readFileSync(path.join(__dirname, '..', 'src', 'app.js'), 'utf8');
expect(source).toContain("require('./utils/uploads').serveUploads()");
expect(source).not.toMatch(/express\.static\(\s*['"`]/);
});
test('a file that isn\'t an image is refused, and nothing is saved', async () => {
const app = site();
const before = fs.existsSync(BRANDING) ? fs.readdirSync(BRANDING).length : 0;
const up = await request(app).post('/api/v2/uploads/logo').attach('image', Buffer.from('<html>'), { filename: 'page.html', contentType: 'text/html' });
expect(up.status).toBe(400);
expect((fs.existsSync(BRANDING) ? fs.readdirSync(BRANDING).length : 0)).toBe(before);
});
});