A church moved sign-in to Accounts without email set up there: codes and password links couldn't be sent, sign-in failed with a 500, nobody could get in. - contracts 1.9.0: messaging.status/test/set (SMTP, WAWP; secrets sealed); people.ensure answers emailSent. - Accounts 1.2.0: SendError instead of a 500; WhatsApp codes fall back to email; forgot password says email isn't set up; setup only offers methods that work; messaging.* (test, save only what worked); test WhatsApp in admin; WAWP from .env. - Events 3.1.0: Settings -> Sign-in shows what Accounts can send and copies this site's SMTP/WAWP into it (secrets stay on the server), test then save; Move disabled and refused (EMAIL_NOT_READY) without email. Tests: Accounts 54, contracts 218, Events 774, test:accounts 16 (also on Postgres). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
108 lines
6.6 KiB
TypeScript
108 lines
6.6 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);
|
|
});
|
|
});
|