Files
crosscode-platform/apps/accounts/backend/tests/messaging.test.ts
T
joshuaandClaude Opus 5.5 70c471666b Accounts sign-in looks like the church's site; Events sends its logo and colours
- @crosscode/ui: Shell with the church's colour wash, logo (or its initial)
  and name, footer; Card edged in the church's colour; Heading with an
  icon tile. Icons on every sign-in step.
- contracts 1.9.0: churchLookSchema (logo, accent colour) in import.begin;
  church.branding request. Accounts fills (move) or replaces (button) the
  church profile's name, logo and colours.
- Events 3.1.0: src/accounts/look.ts (logo as a full address), sent with a
  move; Settings -> Sign-in "Use this site's logo and colours".
Tests: Accounts 55, contracts 218, Events 774, test:accounts 16.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 21:50:02 +02:00

126 lines
8.1 KiB
TypeScript

// Accounts 1.2.0: sign-in never fails with a 500 when a code can't be sent, WhatsApp codes fall back
// to email, and a linked product can check, test and set Accounts' email and WhatsApp
// (crosscode.identity-request/1 messaging.*, contracts 1.9.0).
import assert from 'node:assert/strict';
import crypto from 'node:crypto';
import { after, before, beforeEach, describe, test } from 'node:test';
import { ACCOUNTS_REQUEST_PATH, IDENTITY_REQUEST_SCHEMA } from '@crosscode/contracts';
import { ed25519PublicToPem, generateEd25519, LINK_HEADERS, newLinkSecret, sealLinkValue, signedHeaders } from '@crosscode/contracts/signing';
import { Browser, startHarness, type Harness } from './support/harness.js';
import type { MemoryMessenger } from '../src/lib/messages.js';
const EVENTS_ID = '6d1f0b2c-8a3e-4c5d-9e7f-1a2b3c4d5e6f';
let h: Harness;
let secret: string;
let messenger: MemoryMessenger;
before(async () => {
h = await startHarness();
messenger = h.messenger as MemoryMessenger;
secret = newLinkSecret();
await h.db.productLink.create({ data: { installId: EVENTS_ID, product: 'events', url: 'https://events.hope.test', publicKey: ed25519PublicToPem(generateEd25519().publicKey), secret: h.ctx.secrets.encrypt(secret), connectedAt: new Date() } });
});
after(async () => { await h.close(); });
beforeEach(() => { messenger.emailOn = true; messenger.whatsappOn = false; messenger.failHost = null; });
async function ask(body: Record<string, unknown>) {
const raw = JSON.stringify({ schema: IDENTITY_REQUEST_SCHEMA, requestId: crypto.randomUUID(), actor: null, ...body });
const res = await fetch(`${h.publicUrl}${ACCOUNTS_REQUEST_PATH}`, { method: 'POST', body: raw, headers: { 'content-type': 'application/json', ...signedHeaders(LINK_HEADERS, { senderId: EVENTS_ID, secret, rawBody: raw }) } });
return { status: res.status, body: (await res.json()) as Record<string, unknown> };
}
const smtp = (over: Record<string, unknown> = {}) => ({ host: 'smtp.hope.test', port: 587, secure: false, user: 'events@hope.test', pass: sealLinkValue(secret, 'mail-pass'), from: 'events@hope.test', ...over });
describe('a code that can\'t be sent', () => {
test('without email, the page says so (503 with a message), never a 500', async () => {
messenger.emailOn = false;
await h.makeAccount({ email: 'unverified@hope.test', password: 'Correct-horse-1', emailVerified: false });
const r = await h.signIn(new Browser(), 'unverified@hope.test', 'Correct-horse-1');
// The password step answers with the error of the next step: the email code couldn't go.
assert.ok('pending' in r);
assert.equal(r.pending.error && (r.pending.error as { code: string }).code, 'CODE_NOT_SENT', JSON.stringify(r.pending));
});
test('a WhatsApp code that can\'t go is sent by email', async () => {
await h.makeAccount({ email: 'wa@hope.test', password: 'Correct-horse-1', phone: '+27821230000', twoFactorMethod: 'whatsapp' });
const r = await h.signIn(new Browser(), 'wa@hope.test', 'Correct-horse-1');
assert.ok('pending' in r);
assert.equal(r.pending.stage, 'two_factor', JSON.stringify(r.pending));
assert.match(String(r.pending.sentTo), /@hope\.test$/);
assert.ok(messenger.lastCode('wa@hope.test'));
});
test('forgot password says email isn\'t set up, for any address', async () => {
messenger.emailOn = false;
const res = await h.api(new Browser(), 'POST', '/api/v2/password/forgot', { email: 'nobody@hope.test' });
assert.equal(res.status, 503);
assert.equal((res.body.error as { code: string }).code, 'EMAIL_NOT_READY');
});
});
describe('messaging.* from a product', () => {
test('status says what can be sent, without secrets', async () => {
messenger.emailOn = false;
const r = await ask({ type: 'messaging.status' });
assert.equal(r.status, 200, JSON.stringify(r.body));
assert.deepEqual(r.body.email, { ready: false, host: null, from: null });
assert.equal(JSON.stringify(r.body).includes('pass'), false);
});
test('test sends with the settings given and saves nothing; a failure says why', async () => {
const ok = await ask({ type: 'messaging.test', email: { settings: smtp(), to: 'office@hope.test' } });
assert.deepEqual(ok.body.email, { ok: true, error: null, saved: false });
assert.deepEqual(messenger.tried.at(-1), { kind: 'email', settings: { host: 'smtp.hope.test', port: 587, secure: false, user: 'events@hope.test', pass: 'mail-pass', from: 'events@hope.test' }, to: 'office@hope.test' });
assert.equal(await h.ctx.settings.get('smtp_host'), null);
messenger.failHost = 'bad.hope.test';
const bad = await ask({ type: 'messaging.test', email: { settings: smtp({ host: 'bad.hope.test' }), to: 'office@hope.test' } });
assert.equal((bad.body.email as { ok: boolean }).ok, false);
assert.match(String((bad.body.email as { error: string }).error), /ECONNREFUSED/);
});
test('set saves only what worked, with the password encrypted; null keeps the saved password', async () => {
messenger.failHost = 'bad.hope.test';
const failed = await ask({ type: 'messaging.set', email: { settings: smtp({ host: 'bad.hope.test' }), to: 'office@hope.test' } });
assert.equal((failed.body.email as { saved: boolean }).saved, false);
assert.equal(await h.ctx.settings.get('smtp_host'), null);
const saved = await ask({ type: 'messaging.set', email: { settings: smtp(), to: 'office@hope.test' }, whatsapp: { settings: { accessToken: sealLinkValue(secret, 'wawp-token'), instanceId: 'INST1' }, to: '+27821234567' } });
assert.deepEqual([saved.body.email, saved.body.whatsapp].map((x) => (x as { saved: boolean }).saved), [true, true]);
assert.equal(await h.ctx.settings.get('smtp_pass'), 'mail-pass');
const row = await h.db.appSetting.findUnique({ where: { key: 'smtp_pass' } });
assert.notEqual(row!.value, 'mail-pass', 'encrypted at rest');
assert.equal(await h.ctx.settings.get('wawp_instance_id'), 'INST1');
const kept = await ask({ type: 'messaging.set', email: { settings: smtp({ pass: null, from: 'office@hope.test' }), to: 'office@hope.test' } });
assert.equal((kept.body.email as { saved: boolean }).saved, true);
assert.equal(await h.ctx.settings.get('smtp_pass'), 'mail-pass');
assert.equal(await h.ctx.settings.get('smtp_from'), 'office@hope.test');
});
test('people.ensure still makes the account when its email can\'t go, and says so', async () => {
messenger.emailOn = false;
const r = await ask({ type: 'people.ensure', name: 'No Mail', email: 'nomail@hope.test', phone: null, returnUrl: 'https://events.hope.test/' });
assert.equal(r.status, 200, JSON.stringify(r.body));
assert.equal(r.body.created, true);
assert.equal(r.body.emailSent, false);
});
});
describe('the church\'s look from a product (1.9.0)', () => {
test('a move fills only what\'s empty; the button replaces it', async () => {
await h.ctx.settings.set('church_profile', JSON.stringify({ name: 'Hope Family Church', shortName: 'Hope' }));
const look = { name: 'Other Name', primaryColour: '#123456', accentColour: '#abcdef', logoUrl: 'https://events.hope.test/uploads/logo.png' };
assert.equal((await ask({ type: 'church.branding', church: look, overwrite: false })).status, 200);
const filled = await h.ctx.settings.json<{ name: string; branding: { logoUrl: string; colours: { primary: string; accent: string } } }>('church_profile', {} as never);
assert.equal(filled.name, 'Hope Family Church', 'kept');
assert.equal(filled.branding.logoUrl, 'https://events.hope.test/uploads/logo.png');
assert.equal(filled.branding.colours.primary, '#123456');
await ask({ type: 'church.branding', church: { ...look, primaryColour: '#654321', logoUrl: 'https://events.hope.test/new.png' }, overwrite: true });
const replaced = await h.ctx.settings.json<{ name: string; branding: { logoUrl: string; colours: { primary: string } } }>('church_profile', {} as never);
assert.deepEqual([replaced.name, replaced.branding.logoUrl, replaced.branding.colours.primary], ['Other Name', 'https://events.hope.test/new.png', '#654321']);
// The sign-in pages get it.
const b = await h.api(new Browser(), 'GET', '/api/v2/branding');
assert.equal(b.body.logoUrl, 'https://events.hope.test/new.png');
});
});