app: the Church App's foundation; docs for Hub 2.26.1 and two years of devotionals

apps/app (not released; app@1.0.0 is built in slices, APP-1.0-PLAN §15):
- backend (Express, Prisma, pg-boss, as the Website): the Hub client
  (CCP-, product app, the active-member band in the heartbeat), links
  and church.changed, sign-in through Accounts (everyone a Member; 8 days
  renewed up to 90; every role only with 2FA; the first Owner from
  HUB_ADMIN_EMAIL), Accounts' user.updated/export/deleted, active members
  once a day, modules on and off, GET /api/v1/app. Migration init.
- frontend (Next.js 16): header, tab bar, footer, manifest and icons from
  the church profile, service worker, Add to home screen, Me, the admin's
  first dashboard.
- env.template.json, PM2 template, README, CHANGELOG, WHATS-NEW; 27 tests.

Docs: the Hub runs 2.26.1 with two years of Cross Code's devotionals; an
App gets 2 weeks, today included. HANDOVER lists the App's slices.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-01 00:20:07 +02:00
co-authored by Claude Opus 5.5
parent 74dee881e4
commit bb36e06fa4
85 changed files with 4479 additions and 53 deletions
+254
View File
@@ -0,0 +1,254 @@
import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import request from 'supertest';
import { cookieHeader, cookiesFrom, harness, MEMBER, PASTOR, SITE } from './support/harness.js';
const DAY = 24 * 3600e3;
type H = Awaited<ReturnType<typeof harness>>;
/** Signs in from `returnTo` through the stand-in Accounts; returns the callback's response and the cookies after it. */
async function signIn(h: H, returnTo = '/prayer?day=2026-12-02', cookies: Record<string, string> = {}, mfa = false) {
const start = await request(h.app).get(`/api/auth/signin?returnTo=${encodeURIComponent(returnTo)}${mfa ? '&mfa=1' : ''}`).set('Cookie', cookieHeader(cookies));
assert.equal(start.status, 303);
const after = cookiesFrom(start, cookies);
const back = await request(h.app).get('/api/auth/callback?code=abc&state=xyz').set('Cookie', cookieHeader(after));
return { start, back, cookies: cookiesFrom(back, after) };
}
const me = (h: H, cookies: Record<string, string>) => request(h.app).get('/api/auth/me').set('Cookie', cookieHeader(cookies));
describe('signing in through Accounts', () => {
it('sends people to Accounts with the page they were on, and brings them back to it', async () => {
const h = await harness();
h.fake.next = { sub: MEMBER, email: 'thandi@hope.test', name: 'Thandi Mokoena', amr: ['pwd'] };
const { start, back, cookies } = await signIn(h);
assert.match(start.headers.location!, /^https:\/\/accounts\.hope\.test\//);
assert.deepEqual(h.fake.started, [{ returnTo: '/prayer?day=2026-12-02', requireTwoFactor: false }]);
assert.equal(back.headers.location, `${SITE}/prayer?day=2026-12-02`);
assert.match(String(back.headers['set-cookie']), /cca_session=.*HttpOnly/);
const res = await me(h, cookies);
assert.equal(res.status, 200);
assert.deepEqual(res.body.member, { id: MEMBER, name: 'Thandi Mokoena', email: 'thandi@hope.test', roles: [] });
});
it('makes anyone with an account a Member, keeping a copy of their profile', async () => {
const h = await harness();
h.fake.next = { sub: MEMBER, email: 'Thandi@Hope.test', phone_number: '+27820000000', phone_number_verified: false, whatsapp_opted_out: true, amr: ['pwd'] };
await signIn(h);
const m = await h.db.member.findUnique({ where: { id: MEMBER } });
assert.equal(m!.email, 'thandi@hope.test');
assert.equal(m!.phone, null, 'only a verified number is kept');
assert.equal(m!.whatsappOptedOut, true);
assert.deepEqual(m!.roles, []);
});
it('refuses a foreign return address (back to Home instead)', async () => {
const h = await harness();
await request(h.app).get('/api/auth/signin?returnTo=https%3A%2F%2Fevil.test%2F');
assert.equal(h.fake.started[0]!.returnTo, '/');
});
it('says so when the App isn\'t linked to Accounts yet', async () => {
const h = await harness({ linked: false });
const res = await request(h.app).get('/api/auth/signin?returnTo=%2Fme');
assert.equal(res.status, 303);
assert.equal(res.headers.location, `${SITE}/auth/problem?code=ACCOUNTS_NOT_LINKED&returnTo=%2Fme`);
});
});
describe('the first Owner', () => {
it('is whoever signs in with the Hub\'s admin email (verified)', async () => {
const h = await harness();
h.fake.next = { sub: PASTOR, email: 'Pastor@Hope.test' };
const { cookies } = await signIn(h, '/admin');
const res = await me(h, cookies);
assert.deepEqual(res.body.member.roles, ['owner']);
assert.deepEqual(res.body.activeRoles, ['owner']);
assert.equal((await h.db.auditLog.findMany({ where: { action: 'first_owner_signed_in' } })).length, 1);
});
it('never from an unverified email, and never a second one', async () => {
const h = await harness();
h.fake.next = { sub: PASTOR, email_verified: false };
await signIn(h, '/');
assert.deepEqual((await h.db.member.findUnique({ where: { id: PASTOR } }))!.roles, []);
await h.db.member.create({ data: { id: MEMBER, name: 'Owner', email: 'owner@hope.test', roles: ['owner'] } });
h.fake.next = { sub: PASTOR };
await signIn(h, '/');
assert.deepEqual((await h.db.member.findUnique({ where: { id: PASTOR } }))!.roles, []);
});
it('becomes Owner even after signing in as a Member first', async () => {
const h = await harness();
h.fake.next = { sub: PASTOR, email_verified: false };
await signIn(h, '/');
h.fake.next = { sub: PASTOR };
await signIn(h, '/');
assert.deepEqual((await h.db.member.findUnique({ where: { id: PASTOR } }))!.roles, ['owner']);
});
});
describe('2FA for every role (decision 7)', () => {
it('asks Accounts once more for a second step when someone with a role heads for the admin', async () => {
const h = await harness();
await h.db.member.create({ data: { id: PASTOR, name: 'Pastor Sam', email: 'pastor@hope.test', roles: ['prayer_coordinator'] } });
h.fake.next = { sub: PASTOR, amr: ['pwd'] };
const first = await signIn(h, '/admin');
assert.equal(first.back.headers.location, `${SITE}/api/auth/signin?mfa=1&returnTo=%2Fadmin`);
assert.equal(first.cookies.cca_session, undefined);
// Accounts still came back without one: say why.
const second = await signIn(h, '/admin', {}, true);
assert.equal(second.back.headers.location, `${SITE}/auth/problem?code=TWO_FACTOR_NEEDED&returnTo=%2Fadmin`);
});
it('lets someone with a role use the App as a Member without 2FA, but not the admin', async () => {
const h = await harness();
await h.db.member.create({ data: { id: PASTOR, name: 'Pastor Sam', email: 'pastor@hope.test', roles: ['church_admin'] } });
h.fake.next = { sub: PASTOR, amr: ['pwd'] };
const { back, cookies } = await signIn(h, '/prayer');
assert.equal(back.headers.location, `${SITE}/prayer`);
const res = await me(h, cookies);
assert.deepEqual(res.body.activeRoles, []);
const admin = await request(h.app).get('/api/v1/admin/overview').set('Cookie', cookieHeader(cookies));
assert.equal(admin.status, 403);
assert.equal(admin.body.error.code, 'TWO_FACTOR_NEEDED');
});
it('opens the admin with 2FA, and refuses a Member', async () => {
const h = await harness();
await h.db.member.create({ data: { id: PASTOR, name: 'Pastor Sam', email: 'pastor@hope.test', roles: ['devotional_writer'] } });
h.fake.next = { sub: PASTOR };
const staff = await signIn(h, '/admin');
const ok = await request(h.app).get('/api/v1/admin/overview').set('Cookie', cookieHeader(staff.cookies));
assert.equal(ok.status, 200);
assert.deepEqual(ok.body.activeMembers90d, { count: 1, band: '0–100' });
assert.equal(ok.body.accounts.url, 'https://accounts.hope.test');
h.fake.next = { sub: MEMBER, email: 'm@hope.test' };
const member = await signIn(h, '/');
const no = await request(h.app).get('/api/v1/admin/overview').set('Cookie', cookieHeader(member.cookies));
assert.equal(no.status, 403);
assert.equal(no.body.error.code, 'FORBIDDEN');
const out = await request(h.app).get('/api/v1/admin/overview');
assert.equal(out.status, 401);
});
it('lets only an Owner or Church admin switch modules', async () => {
const h = await harness();
await h.db.member.create({ data: { id: PASTOR, name: 'Pastor Sam', email: 'content@hope.test', roles: ['content_admin'] } });
h.fake.next = { sub: PASTOR, email: 'content@hope.test' };
let s = await signIn(h, '/admin');
const put = (cookies: Record<string, string>, body: object) => request(h.app).put('/api/v1/admin/modules').set('Origin', SITE).set('Cookie', cookieHeader(cookies)).send(body);
assert.equal((await put(s.cookies, { events: false })).status, 403);
await h.db.member.update({ where: { id: PASTOR }, data: { roles: ['church_admin'] } });
s = await signIn(h, '/admin');
const res = await put(s.cookies, { events: false, devotionals: false });
assert.equal(res.status, 200);
assert.deepEqual(res.body.modules, ['house-of-prayer', 'prayer-requests', 'announcements']);
assert.equal((await put(s.cookies, { chat: true })).status, 400);
assert.equal((await put(s.cookies, { events: true })).body.modules.includes('events'), true);
const app = await request(h.app).get('/api/v1/app');
assert.deepEqual(app.body.modules, ['house-of-prayer', 'prayer-requests', 'announcements', 'events']);
});
});
describe('staying signed in (decision 7)', () => {
it('lasts 8 days, renewed by each visit, never past 90 days from the sign-in', async () => {
const h = await harness();
h.fake.next = { sub: MEMBER, email: 'm@hope.test', amr: ['pwd'] };
const { cookies } = await signIn(h, '/');
// Every 7 days for 12 weeks: still signed in.
for (let week = 1; week <= 12; week++) {
h.clock.add(7 * DAY);
assert.equal((await me(h, cookies)).status, 200, `week ${week}`);
}
// Day 84 + 7 = 91: past the 90-day limit, even though the last visit was a week ago.
h.clock.add(7 * DAY);
assert.equal((await me(h, cookies)).status, 401);
});
it('ends after 8 days away', async () => {
const h = await harness();
h.fake.next = { sub: MEMBER, email: 'm@hope.test', amr: ['pwd'] };
const { cookies } = await signIn(h, '/');
h.clock.add(7 * DAY);
assert.equal((await me(h, cookies)).status, 200);
h.clock.add(8 * DAY + 60e3);
assert.equal((await me(h, cookies)).status, 401);
});
it('renews the cookie too', async () => {
const h = await harness();
h.fake.next = { sub: MEMBER, email: 'm@hope.test', amr: ['pwd'] };
const { cookies } = await signIn(h, '/');
h.clock.add(3 * DAY);
const res = await me(h, cookies);
const expires = /cca_session=[^;]+;.*Expires=([^;]+)/.exec(String(res.headers['set-cookie']))?.[1];
assert.equal(new Date(expires!).toISOString().slice(0, 10), new Date(h.clock.now().getTime() + 8 * DAY).toISOString().slice(0, 10));
});
it('ends at a back-channel logout from Accounts', async () => {
const h = await harness();
h.fake.next = { sub: MEMBER, email: 'm@hope.test', amr: ['pwd'] };
const { cookies } = await signIn(h, '/');
const res = await request(h.app).post('/api/auth/backchannel-logout').type('form').send({ logout_token: JSON.stringify({ sub: MEMBER, sid: null }) });
assert.equal(res.status, 200);
assert.equal((await me(h, cookies)).status, 401);
});
});
describe('active members (§10)', () => {
it('counts a member once a day, in the church\'s timezone, only when signed in', async () => {
const h = await harness();
h.fake.next = { sub: MEMBER, email: 'm@hope.test', amr: ['pwd'] };
const { cookies } = await signIn(h, '/');
await request(h.app).get('/api/v1/app');
assert.equal((await h.db.member.findUnique({ where: { id: MEMBER } }))!.lastActiveAt, null, 'signing in alone isn\'t using the App');
await me(h, cookies);
const first = (await h.db.member.findUnique({ where: { id: MEMBER } }))!.lastActiveAt as Date;
assert.equal(first.toISOString(), h.clock.now().toISOString());
h.clock.add(3 * 3600e3);
await me(h, cookies);
assert.equal(((await h.db.member.findUnique({ where: { id: MEMBER } }))!.lastActiveAt as Date).toISOString(), first.toISOString(), 'one write a day');
// 23:00 UTC is 01:00 the next day in Johannesburg: a new day.
h.clock.set('2026-12-01T23:00:00Z');
await me(h, cookies);
assert.equal(((await h.db.member.findUnique({ where: { id: MEMBER } }))!.lastActiveAt as Date).toISOString(), '2026-12-01T23:00:00.000Z');
});
});
describe('Accounts\' calls', () => {
const call = (h: H, body: object) => request(h.app).post('/api/accounts/v1/events').set('x-test-signed', 'yes').set('Content-Type', 'application/json').send(JSON.stringify(body));
const base = { schema: 'crosscode.identity/1', callId: '44444444-4444-4444-8444-444444444444', occurredAt: '2026-12-01T06:00:00.000Z', sub: MEMBER };
it('refuses an unsigned call', async () => {
const h = await harness();
const res = await request(h.app).post('/api/accounts/v1/events').set('Content-Type', 'application/json').send('{}');
assert.equal(res.status, 401);
});
it('keeps the profile copy up to date, WhatsApp STOP included', async () => {
const h = await harness();
await h.db.member.create({ data: { id: MEMBER, name: 'Thandi', email: 'thandi@hope.test' } });
const res = await call(h, { ...base, type: 'user.updated', profile: { name: 'Thandi M', given_name: 'Thandi', family_name: 'M', email: 'T@Hope.test', email_verified: true, phone_number: '+27821112222', phone_number_verified: true, news_consent: { email: true, whatsapp: false }, whatsapp_opted_out: true } });
assert.equal(res.status, 200);
const m = await h.db.member.findUnique({ where: { id: MEMBER } });
assert.equal(m!.name, 'Thandi M');
assert.equal(m!.email, 't@hope.test');
assert.equal(m!.phone, '+27821112222');
assert.equal(m!.whatsappOptedOut, true);
});
it('exports what the App keeps about someone, and forgets them when their account is deleted', async () => {
const h = await harness();
h.fake.next = { sub: MEMBER, email: 'm@hope.test', amr: ['pwd'] };
const { cookies } = await signIn(h, '/');
const exp = await call(h, { ...base, type: 'user.export', exportId: '55555555-5555-4555-8555-555555555555' });
assert.equal(exp.body.product, 'app');
assert.equal(exp.body.sections[0].records[0].email, 'm@hope.test');
const del = await call(h, { ...base, type: 'user.deleted' });
assert.equal(del.body.status, 'deleted');
assert.equal(await h.db.member.findUnique({ where: { id: MEMBER } }), null);
assert.equal((await me(h, cookies)).status, 401);
});
});