- 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>
123 lines
7.1 KiB
TypeScript
123 lines
7.1 KiB
TypeScript
// 1.3.0: database backups (Settings → Backups, as Cross Events) and the account page's quick links
|
|
// to the church's sites.
|
|
import assert from 'node:assert/strict';
|
|
import fs from 'node:fs';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import { Readable } from 'node:stream';
|
|
import zlib from 'node:zlib';
|
|
import { after, before, describe, test } from 'node:test';
|
|
import { Browser, startHarness, type Harness } from './support/harness.js';
|
|
import { BACKUP_FILE, backupName, backupPath, listBackups, runBackup, type Dumper } from '../src/backups/backups.js';
|
|
import { codeAt } from '../src/lib/totp.js';
|
|
|
|
let h: Harness;
|
|
let dir: string;
|
|
before(async () => {
|
|
h = await startHarness();
|
|
dir = fs.mkdtempSync(path.join(os.tmpdir(), 'accounts-backups-'));
|
|
h.ctx.config.backupDir = dir;
|
|
h.ctx.config.databaseUrl = 'postgresql://test@127.0.0.1/accounts_test';
|
|
});
|
|
after(async () => { await h.close(); fs.rmSync(dir, { recursive: true, force: true }); });
|
|
|
|
const dumpOf = (sql: string): Dumper => () => ({ stdout: Readable.from([sql]), done: Promise.resolve() });
|
|
const failing: Dumper = () => ({ stdout: Readable.from(['-- half a dump']), done: Promise.reject(new Error('pg_dump stopped (exit 1): connection refused')) });
|
|
|
|
async function adminPage() {
|
|
await h.makeAccount({ email: 'backups.admin@hope.test', password: 'Correct-horse-8', role: 'admin' });
|
|
const browser = new Browser();
|
|
const { url } = await browser.follow(`${h.publicUrl}/api/self/signin?returnTo=/admin`, (u) => u.pathname === '/signin');
|
|
const uid = url.searchParams.get('uid')!;
|
|
await h.api(browser, 'POST', `/api/v2/interaction/${uid}/password`, { email: 'backups.admin@hope.test', password: 'Correct-horse-8' });
|
|
const setup = await h.api(browser, 'POST', `/api/v2/interaction/${uid}/two-factor/setup`, { method: 'totp' });
|
|
const done = await h.api(browser, 'POST', `/api/v2/interaction/${uid}/two-factor/setup/confirm`, { method: 'totp', code: codeAt(String(setup.body.secret), Math.floor(Date.now() / 30000)) });
|
|
await browser.follow(done.body.redirectTo as string, (u) => u.pathname === '/admin' || u.pathname === '/account');
|
|
return browser;
|
|
}
|
|
|
|
describe('backups', () => {
|
|
test('a backup is the gzipped dump, named as the crosscode script names them', async () => {
|
|
const b = await runBackup(h.ctx, dumpOf('CREATE TABLE "Account" ();\n'));
|
|
assert.match(b.filename, BACKUP_FILE);
|
|
assert.equal(zlib.gunzipSync(fs.readFileSync(path.join(dir, b.filename))).toString(), 'CREATE TABLE "Account" ();\n');
|
|
assert.equal(backupName(new Date(2026, 10, 20, 2, 0, 5)), 'accounts-20261120-020005.sql.gz');
|
|
// A second one in the same second gets its own name.
|
|
const again = await runBackup(h.ctx, dumpOf('-- again\n'));
|
|
assert.notEqual(again.filename, b.filename);
|
|
});
|
|
|
|
test('a dump that fails part-way leaves no file, and says why', async () => {
|
|
const before = (await listBackups(h.ctx)).length;
|
|
await assert.rejects(runBackup(h.ctx, failing), /connection refused/);
|
|
assert.equal((await listBackups(h.ctx)).length, before);
|
|
assert.match(String(await h.ctx.settings.get('backup_last_error')), /connection refused/);
|
|
await runBackup(h.ctx, dumpOf('-- fine\n'));
|
|
assert.equal(await h.ctx.settings.get('backup_last_error'), null);
|
|
});
|
|
|
|
test('only the newest are kept; downloads only by a backup\'s own name', async () => {
|
|
await h.ctx.settings.set('backup_retain_count', '2');
|
|
await runBackup(h.ctx, dumpOf('-- newest\n'));
|
|
const left = await listBackups(h.ctx);
|
|
assert.equal(left.length, 2);
|
|
assert.ok(backupPath(h.ctx, left[0]!.filename));
|
|
assert.equal(backupPath(h.ctx, '../.env'), null);
|
|
assert.equal(backupPath(h.ctx, 'accounts-20200101-000000.sql.gz'), null);
|
|
await h.ctx.settings.set('backup_retain_count', null);
|
|
});
|
|
|
|
test('admins see, change and download them; members can\'t', async () => {
|
|
await h.makeAccount({ email: 'backups.member@hope.test', password: 'Correct-horse-9' });
|
|
const member = new Browser();
|
|
const { url } = await member.follow(`${h.publicUrl}/api/self/signin?returnTo=/account`, (u) => u.pathname === '/signin');
|
|
const step = await h.api(member, 'POST', `/api/v2/interaction/${url.searchParams.get('uid')}/password`, { email: 'backups.member@hope.test', password: 'Correct-horse-9' });
|
|
await member.follow(step.body.redirectTo as string, (u) => u.pathname === '/account');
|
|
assert.equal((await h.api(member, 'GET', '/api/v2/admin/backups')).status, 403);
|
|
|
|
const admin = await adminPage();
|
|
const list = await h.api(admin, 'GET', '/api/v2/admin/backups');
|
|
assert.equal(list.status, 200, JSON.stringify(list.body));
|
|
const items = list.body.items as { filename: string }[];
|
|
assert.equal(list.body.nightly, true);
|
|
assert.equal(list.body.retain, 14);
|
|
const file = await admin.request(`${h.publicUrl}/api/v2/admin/backups/${items[0]!.filename}/download`);
|
|
assert.equal(file.status, 200);
|
|
assert.equal(file.headers.get('cache-control'), 'no-store');
|
|
assert.ok(zlib.gunzipSync(Buffer.from(await file.arrayBuffer())).length > 0);
|
|
assert.equal((await admin.request(`${h.publicUrl}/api/v2/admin/backups/nope.sql.gz/download`)).status, 404);
|
|
assert.equal(await h.db.auditLog.count({ where: { action: 'backup_downloaded' } }), 1);
|
|
|
|
const bad = await h.api(admin, 'PUT', '/api/v2/admin/backups/settings', { retain: 0, nightly: true });
|
|
assert.equal(bad.status, 400);
|
|
const saved = await h.api(admin, 'PUT', '/api/v2/admin/backups/settings', { retain: 30, nightly: false });
|
|
assert.deepEqual(saved.body, { retain: 30, nightly: false, deleted: 0 });
|
|
assert.equal(await h.ctx.settings.bool('backup_nightly', true), false);
|
|
});
|
|
|
|
test('the nightly backup is skipped while it\'s turned off', async () => {
|
|
const { runBackupJob } = await import('../src/backups/backups.js');
|
|
await h.ctx.settings.set('backup_nightly', 'false');
|
|
assert.deepEqual(await runBackupJob(h.ctx), { skipped: true });
|
|
await h.ctx.settings.set('backup_nightly', null);
|
|
});
|
|
});
|
|
|
|
describe('quick links to the church\'s sites', () => {
|
|
test('the account page lists each connected site, Events opening its dashboard', async () => {
|
|
const browser = new Browser();
|
|
const { url } = await browser.follow(`${h.publicUrl}/api/self/signin?returnTo=/account`, (u) => u.pathname === '/signin');
|
|
const step = await h.api(browser, 'POST', `/api/v2/interaction/${url.searchParams.get('uid')}/password`, { email: 'backups.member@hope.test', password: 'Correct-horse-9' });
|
|
await browser.follow(step.body.redirectTo as string, (u) => u.pathname === '/account');
|
|
const apps = await h.api(browser, 'GET', '/api/v2/me/apps');
|
|
assert.equal(apps.status, 200, JSON.stringify(apps.body));
|
|
const links = await h.db.productLink.findMany();
|
|
assert.equal((apps.body.items as unknown[]).length, links.length);
|
|
for (const item of apps.body.items as { product: string; url: string; name: string }[]) {
|
|
assert.ok(item.url.startsWith('http'));
|
|
if (item.product === 'events') assert.match(item.url, /\/api\/auth\/signin\?returnTo=%2Fdashboard$/);
|
|
}
|
|
assert.equal((await h.api(new Browser(), 'GET', '/api/v2/me/apps')).status, 401);
|
|
});
|
|
});
|