app: email and WhatsApp's backend (APP-1.0-PLAN §8)
The church's one set of settings from Accounts (messaging.get; the SMTP password and WAWP token sealed with the link's secret, kept encrypted in AppSetting messaging_settings), fetched when the App links to Accounts, on Accounts' signed messaging.changed, once a day and on the admin's Fetch again. messages-send (every minute): the outbox by email (nodemailer) and WhatsApp (WAWP), STOP checked before every WhatsApp (the member and anyone with the same number), email instead for what a member asked for (never announcements), retries after 2, 4, 8 and 16 minutes, a reminder never after its slot started, nothing more than 3 days late. announcement-sends and devotional-sends queue "Also send it" at the publish time and today's devotional at each member's time. Members' choices (NotificationSettings), news consent kept from Accounts, MessageCount for the heartbeat's messages, the admin's Email and WhatsApp API with Send a test. Migration messaging (checked on PostgreSQL 18). 110 backend tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -3,11 +3,13 @@
|
||||
* tested against Accounts' provider in apps/accounts/backend/tests/authClient.test.ts), linked to a
|
||||
* church's Accounts and registered with the Hub, and a clock the tests can move.
|
||||
*/
|
||||
import type { IdentityClaims } from '@crosscode/contracts';
|
||||
import { IDENTITY_REQUEST_SCHEMA, type IdentityClaims } from '@crosscode/contracts';
|
||||
import { sealLinkValue } from '@crosscode/contracts/signing';
|
||||
import type { AuthClientModule } from '../../src/auth/accounts.js';
|
||||
import { createApp } from '../../src/app.js';
|
||||
import { createCtx, type Ctx, type Db } from '../../src/context.js';
|
||||
import { loadConfig } from '../../src/lib/config.js';
|
||||
import type { Email, Smtp, Transport, Wawp, WhatsApp } from '../../src/messaging/transport.js';
|
||||
import { memoryDb, type MemoryDb } from './memoryDb.js';
|
||||
|
||||
export const SITE = 'https://app.hope.test';
|
||||
@@ -22,8 +24,33 @@ export const MEMBER = '33333333-3333-4333-8333-333333333333';
|
||||
export type FakeAccounts = {
|
||||
next: Partial<IdentityClaims> & { sub: string };
|
||||
started: { returnTo: string; requireTwoFactor: boolean }[];
|
||||
/** Requests the App sent Accounts (`crosscode.identity-request/1`). */
|
||||
requests: Record<string, unknown>[];
|
||||
/** What `messaging.get` answers: the church's settings in the clear (sealed on the way), or an error. */
|
||||
messaging: { email: (Omit<Smtp, 'pass'> & { pass: string | null }) | null; whatsapp: Wawp | null } | Error;
|
||||
};
|
||||
|
||||
/** The link secret the tests' Accounts link has. */
|
||||
export const LINK_SECRET = 'link-secret';
|
||||
|
||||
export const CHURCH_SMTP = { host: 'smtp.hope.test', port: 587, secure: false, user: 'app@hope.test', pass: 'smtp-pass', from: 'app@hope.test' };
|
||||
export const CHURCH_WAWP = { accessToken: 'wawp-token', instanceId: 'HOPE1' };
|
||||
|
||||
/** Keeps what would have been sent; `fail` makes a channel throw. */
|
||||
export class MemoryTransport implements Transport {
|
||||
emails: (Email & { settings: Smtp })[] = [];
|
||||
whatsapps: (WhatsApp & { settings: Wawp })[] = [];
|
||||
fail: { email?: string; whatsapp?: string } = {};
|
||||
async email(settings: Smtp, msg: Email) {
|
||||
if (this.fail.email) throw new Error(this.fail.email);
|
||||
this.emails.push({ ...msg, settings });
|
||||
}
|
||||
async whatsapp(settings: Wawp, msg: WhatsApp) {
|
||||
if (this.fail.whatsapp) throw new Error(this.fail.whatsapp);
|
||||
this.whatsapps.push({ ...msg, settings });
|
||||
}
|
||||
}
|
||||
|
||||
export function claims(over: Partial<IdentityClaims> & { sub: string }): IdentityClaims {
|
||||
return {
|
||||
schema: 'crosscode.identity/1',
|
||||
@@ -68,6 +95,20 @@ function fakeAuthClient(fake: FakeAccounts): AuthClientModule {
|
||||
throw new SignInError('bad', 'BAD_TOKEN');
|
||||
}
|
||||
},
|
||||
request: async (input: Record<string, unknown>) => {
|
||||
fake.requests.push(input);
|
||||
if (input.type === 'messaging.get') {
|
||||
if (fake.messaging instanceof Error) throw fake.messaging;
|
||||
const { email, whatsapp } = fake.messaging;
|
||||
return {
|
||||
schema: IDENTITY_REQUEST_SCHEMA,
|
||||
email: email ? { ...email, pass: email.pass ? sealLinkValue(LINK_SECRET, email.pass) : null } : null,
|
||||
whatsapp: whatsapp ? { instanceId: whatsapp.instanceId, accessToken: sealLinkValue(LINK_SECRET, whatsapp.accessToken) } : null,
|
||||
updatedAt: '2026-11-30T10:00:00.000Z',
|
||||
};
|
||||
}
|
||||
return { schema: IDENTITY_REQUEST_SCHEMA, ok: true };
|
||||
},
|
||||
verifyAccountsCall: (raw: Buffer, headers: Record<string, unknown>) => {
|
||||
if (headers['x-test-signed'] !== 'yes') throw new SignInError('That call isn\'t signed by Accounts.', 'BAD_SIGNATURE');
|
||||
return JSON.parse(raw.toString('utf8'));
|
||||
@@ -85,14 +126,15 @@ export function testClock(start = '2026-12-01T06:00:00Z') {
|
||||
export async function harness(opts: { linked?: boolean; env?: Record<string, string> } = {}) {
|
||||
const db = memoryDb();
|
||||
const clock = testClock();
|
||||
const fake: FakeAccounts = { next: { sub: PASTOR }, started: [] };
|
||||
const fake: FakeAccounts = { next: { sub: PASTOR }, started: [], requests: [], messaging: { email: CHURCH_SMTP, whatsapp: CHURCH_WAWP } };
|
||||
const transport = new MemoryTransport();
|
||||
const config = loadConfig({ APP_URL: SITE, HUB_ADMIN_EMAIL: ADMIN_EMAIL.toUpperCase(), ...opts.env });
|
||||
const ctx = createCtx(config, db as unknown as Db, { authClient: async () => fakeAuthClient(fake), now: clock.now });
|
||||
const ctx = createCtx(config, db as unknown as Db, { authClient: async () => fakeAuthClient(fake), now: clock.now, transport });
|
||||
await ctx.settings.set('hub_install_id', INSTALL_ID);
|
||||
if (opts.linked !== false) {
|
||||
await db.productLink.create({ data: { installId: ACCOUNTS_INSTALL_ID, product: 'accounts', url: ACCOUNTS, publicKey: 'unused', secret: ctx.secrets.encrypt('link-secret'), connectedAt: new Date() } });
|
||||
await db.productLink.create({ data: { installId: ACCOUNTS_INSTALL_ID, product: 'accounts', url: ACCOUNTS, publicKey: 'unused', secret: ctx.secrets.encrypt(LINK_SECRET), connectedAt: new Date() } });
|
||||
}
|
||||
return { db, ctx, fake, clock, app: createApp(ctx) };
|
||||
return { db, ctx, fake, clock, transport, app: createApp(ctx) };
|
||||
}
|
||||
|
||||
export type Harness = Awaited<ReturnType<typeof harness>> & { db: MemoryDb; ctx: Ctx };
|
||||
|
||||
Reference in New Issue
Block a user