From ebfedb03d93bbda8251f7d301292e65182932229 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Smoli=C5=84ski?= Date: Wed, 25 Nov 2026 21:18:09 +0100 Subject: [PATCH] Mail relay: tests count the account rules' own hooks, reference index, doctor fixture Co-Authored-By: Claude Fable 5.2 --- docs/agent/reference-site/INDEX.md | 53 +++++++++++++++++++++++++++++- scripts/reference-index.mjs | 3 ++ test/accountRulesSuite.ts | 10 ++++-- test/doctor.test.ts | 2 +- 4 files changed, 64 insertions(+), 4 deletions(-) diff --git a/docs/agent/reference-site/INDEX.md b/docs/agent/reference-site/INDEX.md index a4ef160..b227d58 100644 --- a/docs/agent/reference-site/INDEX.md +++ b/docs/agent/reference-site/INDEX.md @@ -113,7 +113,9 @@ export default defineConfig({ use: { baseURL, trace: 'retain-on-failure' }, webServer: { command: 'pnpm start', - env: { PORT: new URL(baseURL).port || '3000' }, + // E2E_MAIL_RELAY_URL: the mail service's stand-in that e2e/mail-relay.spec.ts starts (nothing listens there otherwise, + // and the other specs send through the SMTP sink, so the site never calls it) + env: { E2E_MAIL_RELAY_URL: `http://127.0.0.1:${process.env.E2E_RELAY_PORT ?? 2527}/mail`, PORT: new URL(baseURL).port || '3000' }, reuseExistingServer: process.env.E2E_REUSE_SERVER === '1', url: `${baseURL}/api/health`, }, @@ -1166,6 +1168,9 @@ Rules: AGENT.md §8a. | `e2e/global-setup.ts` | Playwright global setup: resets the test admins and clears their stored 2FA attempt counts before every run, so specs do not depend on run order (below) | | `e2e/late-staff-fixture.ts` | test fixture, on only with E2E_LATE_STAFF=1: one plugin before iapl (staff collections of the config: roles with one more value, the plugin's roles, roles of the project's own) and one after it (a staff collection without rules, one whose roles field is text, copies of staff collections under new slugs — plain, with rules of their own, one rule used for another operation, other role values, the opt-out, not staff — and a visitor collection with open sign-up) | | `e2e/late-staff.spec.ts` | the account rules on a BUILT site (run by `pnpm e2e:built` after a webpack and a Turbopack build): a staff collection a later plugin adds, staff collections whose accounts can hold `admin`, and copies of staff collections — a visitor account gets nothing, nobody raises its own role or makes an admin, a copy is never wider than the plugin's rule | +| `e2e/mail-relay-fixture.ts` | test fixture, on only with E2E_MAIL_RELAY_URL: points the mail service's client at the stand-in the spec starts; never in a real site (a real site names no address) (below) | +| `e2e/mail-relay-setup.ts` | for the mail-service spec: empties the SMTP fields and the stored token, so the next e-mail enrols and goes through the mail service (local database only) (below) | +| `e2e/mail-relay.spec.ts` | the mail service on a built site: the state block in the panel, the first e-mail through the public confirm route, no token in anything the browser receives | | `e2e/smoke.spec.ts` | the smoke tests every release must pass: pages, showcase, forms (also a file sent by e-mail), sitemap, SEO/GEO, headers, consent, 2FA | | `e2e/smtp-sink.ts` | a local mail sink for the smoke tests: reads what the site really sent (recipients, attachments) | | `e2e/totp.ts` | RFC 6238 code generator for the 2FA sign-in in the smoke test, and the address of the second test admin (below) | @@ -1191,6 +1196,52 @@ export default function globalSetup(): void { } ``` +### `e2e/mail-relay-fixture.ts` + +```ts +/** + * Test fixture, on only with E2E_MAIL_RELAY_URL (e2e/mail-relay.spec.ts): points the mail service's client at + * a stand-in the spec starts on this machine, so the spec can watch a built site enrol through its own confirm + * route and send — without the real service. Never part of a real site: the starter drops `e2e/` and the lines + * that use this file; a real site names no address (the plugin's own is used). + */ +export const E2E_MAIL_RELAY_URL = process.env.E2E_MAIL_RELAY_URL || undefined + +/** `iapl({ mail })` for the test, `undefined` (the plugin's defaults) otherwise. */ +export const e2eMail = E2E_MAIL_RELAY_URL ? { relay: { retryBackoffMs: 50, url: E2E_MAIL_RELAY_URL } } : undefined +``` + +### `e2e/mail-relay-setup.ts` + +```ts +/** + * For e2e/mail-relay.spec.ts — LOCAL / CI database only: + * pnpm payload run e2e/mail-relay-setup.ts the site has no SMTP server and no token: its next e-mail + * goes to the mail service, enrolling first + * Afterwards the spec runs e2e/create-test-admin.ts again, which points e-mail back at the tests' SMTP sink. + */ +import config from '@payload-config' +import { RELAY_KV_KEY } from '@intecion/ipal-kit' +import { isLocalDatabase } from '@intecion/ipal-kit/seed' +import { getPayload } from 'payload' + +if (process.env.NODE_ENV === 'production' && !process.env.CI) {throw new Error('Refusing: NODE_ENV=production outside CI.')} +if (!isLocalDatabase(process.env.DATABASE_URI ?? '')) {throw new Error('Refusing: DATABASE_URI is not a local database.')} + +const payload = await getPayload({ config }) +await payload.updateGlobal({ + slug: 'site-integrations', + context: { ipalIntegrationStatus: true }, + data: { emailStatus: { checkedAt: null, code: null, message: null, since: null, state: null }, emailTransport: 'smtp', smtpFromAddress: 'site@example-studio.test', smtpFromName: 'Example Studio', smtpHost: null, smtpPassword: null, smtpUser: null } as never, + overrideAccess: true, +}) +for (const key of await payload.kv.keys()) { + if (key === RELAY_KV_KEY || key.startsWith(`${RELAY_KV_KEY}:`)) {await payload.kv.delete(key)} +} +console.log('[e2e] no SMTP server and no mail-service token: the next e-mail enrols and goes through the mail service') +process.exit(0) +``` + ### `e2e/totp.ts` ```ts diff --git a/scripts/reference-index.mjs b/scripts/reference-index.mjs index 808cd4f..70d1123 100644 --- a/scripts/reference-index.mjs +++ b/scripts/reference-index.mjs @@ -65,6 +65,9 @@ export const DESCRIPTIONS = { 'e2e/global-setup.ts': 'Playwright global setup: resets the test admins and clears their stored 2FA attempt counts before every run, so specs do not depend on run order', 'e2e/two-factor-setup.spec.ts': 'browser check of "2FA setup required": the first admin of a fresh site, a new editor, the administrator\'s reset on the account page', 'e2e/smoke.spec.ts': 'the smoke tests every release must pass: pages, showcase, forms (also a file sent by e-mail), sitemap, SEO/GEO, headers, consent, 2FA', + 'e2e/mail-relay-fixture.ts': 'test fixture, on only with E2E_MAIL_RELAY_URL: points the mail service\'s client at the stand-in the spec starts; never in a real site (a real site names no address)', + 'e2e/mail-relay-setup.ts': 'for the mail-service spec: empties the SMTP fields and the stored token, so the next e-mail enrols and goes through the mail service (local database only)', + 'e2e/mail-relay.spec.ts': 'the mail service on a built site: the state block in the panel, the first e-mail through the public confirm route, no token in anything the browser receives', 'e2e/smtp-sink.ts': 'a local mail sink for the smoke tests: reads what the site really sent (recipients, attachments)', 'e2e/two-factor-back.spec.ts': 'browser test of the 2FA screens\' `back` link (twoFactor.safeBack): a prepared link — script or another host — ends on the admin root, an ordinary one is followed', 'e2e/totp.ts': 'RFC 6238 code generator for the 2FA sign-in in the smoke test, and the address of the second test admin', diff --git a/test/accountRulesSuite.ts b/test/accountRulesSuite.ts index 4545a7d..e71597a 100644 --- a/test/accountRulesSuite.ts +++ b/test/accountRulesSuite.ts @@ -29,6 +29,12 @@ import { testDatabase } from './http/db.js' import { cookiesOf, startPayloadServer, type TestServer } from './http/server.js' import { totp } from './http/totp.js' +/** + * A `beforeOperation` hook of the account rules. Every auth collection also carries the mark that tells the mail + * service what Payload's own account mail is (modules/email/relay/accountMail.ts): that one is not counted here. + */ +const ruleHook = (hook: unknown) => !(hook as Record)[Symbol.for('iapl.accountMailMark')] + const TOTP_SECRET = 'JBSWY3DPEHPK3PXP' /** A plugin the project lists AFTER iapl: its auth collections did not exist when iapl ran (review round 2, R2-3). */ @@ -484,7 +490,7 @@ export function accountRulesSuite(twoFactor: boolean): void { expect(checkPassword('abcd', { minLength: 4 })).toBeNull() expect(checkPassword('Someone@Example.com ', { email: 'someone@example.com', minLength: 4 })).toBe('email') const hooks = async (options: Record) => - (await iapl({ access: { authCollection: 'users' }, i18n, twoFactor: false, ...options } as never)({ admin: { user: 'users' }, collections: [{ slug: 'customers', auth: true, fields: [] }], secret: 'x' } as unknown as Config)).collections!.map((c) => [c.slug, c.hooks?.beforeOperation?.length ?? 0]) + (await iapl({ access: { authCollection: 'users' }, i18n, twoFactor: false, ...options } as never)({ admin: { user: 'users' }, collections: [{ slug: 'customers', auth: true, fields: [] }], secret: 'x' } as unknown as Config)).collections!.map((c) => [c.slug, (c.hooks?.beforeOperation ?? []).filter(ruleHook).length]) expect(await hooks({})).toEqual([['customers', 0], ['users', 1]]) expect(await hooks({ accounts: { password: false } })).toEqual([['customers', 0], ['users', 0]]) expect(await hooks({ accounts: false })).toEqual([['customers', 0], ['users', 0]]) @@ -621,7 +627,7 @@ export function accountRulesSuite(twoFactor: boolean): void { }) test('the switches', async () => { - const guards = (config: Config, slug: string) => of(config, slug).hooks?.beforeOperation?.length ?? 0 + const guards = (config: Config, slug: string) => (of(config, slug).hooks?.beforeOperation ?? []).filter(ruleHook).length const firstUser = (config: Config, slug: string) => (of(config, slug).endpoints || []).filter((e) => e.path === '/first-register').length const on = await build({}) // staff collections: the password rule; every auth collection: the first-user endpoint diff --git a/test/doctor.test.ts b/test/doctor.test.ts index fc6585e..a531334 100644 --- a/test/doctor.test.ts +++ b/test/doctor.test.ts @@ -701,7 +701,7 @@ describe('e-mail: transport, the mail service, Graph, the confirm route', () => test('mail-relay-route: trailing slashes or another API route keep the service from confirming the site — unless the relay is off', async () => { expect(await run(null, { 'next.config.ts': 'export default { trailingSlash: true }' })).toEqual(['mail-relay-route:warning']) - expect(await run(null, { 'src/payload.config.ts': "buildConfig({ routes: { api: '/cms-api' } })" })).toEqual(['mail-relay-route:warning']) + expect(await run(null, { 'src/payload.config.ts': "export default { plugins: [iapl({})], routes: { api: '/cms-api' } }" })).toEqual(['mail-relay-route:warning']) expect(await run(null, { 'next.config.ts': 'export default { trailingSlash: true }', 'src/payload.config.ts': 'iapl({ mail: { relay: false } })' })).toEqual([]) })