M1: the reference e2e suite follows the stored 2FA verify count

The verify count now survives a restart of the site, so the specs can no longer
share one test admin across server runs: create-test-admin.ts makes two test
admins and clears their stored counts; the setup spec signs in as the second.
The whole suite passes in one run (20 passed, 1 skipped).

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-25 00:39:23 +01:00
co-authored by Claude Fable 5.2
parent 83356ab4ed
commit 81cee02e78
6 changed files with 32 additions and 55 deletions
+7 -44
View File
@@ -94,6 +94,8 @@ try {
/**
* Smoke tests against a built, seeded site: pnpm build && pnpm seed && pnpm payload run e2e/create-test-admin.ts.
* Run create-test-admin.ts again before repeating the tests within ten minutes: the 2FA verify limit (5 attempts per
* account in 10 minutes) is stored in the database, and that script clears the test admins' counts.
* E2E_BASE_URL picks the port (default 3100). Playwright starts its own server there and fails when the port is
* taken, so another project's dev server is never tested by mistake; E2E_REUSE_SERVER=1 uses a server you started.
*/
@@ -1157,55 +1159,13 @@ Rules: AGENT.md §8a.
| File | What it shows |
|---|---|
| `e2e/create-test-admin.ts` | a test admin with a known TOTP secret, and e-mail sending pointed at the tests' mail sink (local / CI database only) (below) |
| `e2e/create-test-admin.ts` | two test admins with a known TOTP secret (the specs share out the five 2FA verify attempts per account), their stored verify counts cleared, and e-mail sending pointed at the tests' mail sink (local / CI database only) |
| `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 (below) |
| `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) |
| `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/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/create-test-admin.ts`
```ts
/**
* Creates (or resets) the smoke-test admin with a known TOTP secret, so e2e/smoke.spec.ts signs in through
* the real 2FA screen instead of skipping it. LOCAL / CI database only:
* pnpm payload run e2e/create-test-admin.ts
* Values come from E2E_ADMIN_EMAIL / E2E_ADMIN_PASSWORD / E2E_TOTP_SECRET (.env.example). Not a seed: seeds
* never create users (docs/seeding.md). It also points e-mail sending at the tests' local mail sink
* (E2E_SMTP_PORT, default 2526).
*/
import config from '@payload-config'
import { isLocalDatabase } from '@intecion/ipal-kit/seed'
import { getPayload } from 'payload'
const { E2E_ADMIN_EMAIL: email, E2E_ADMIN_PASSWORD: password, E2E_TOTP_SECRET: totpSecret } = process.env
if (!email || !password || !totpSecret) {throw new Error('Set E2E_ADMIN_EMAIL, E2E_ADMIN_PASSWORD and E2E_TOTP_SECRET (.env.example).')}
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 })
const data = { email, hasTotp: true, password, roles: ['admin'], totpSecret }
const found = await payload.find({ collection: 'users', limit: 1, overrideAccess: true, where: { email: { equals: email } } })
if (found.docs[0]) {
await payload.update({ id: found.docs[0].id, collection: 'users', data: data as never, overrideAccess: true })
} else {
await payload.create({ collection: 'users', data: data as never, overrideAccess: true })
}
console.log(`[e2e] test admin ready: ${email}`)
// E-mail for the smoke tests goes to a local sink the test itself starts (e2e/smtp-sink.ts), so the test can
// read what the site sent — the form file as an attachment. Without the sink running, sends simply fail.
const smtpPort = Number(process.env.E2E_SMTP_PORT ?? 2526)
await payload.updateGlobal({
slug: 'site-integrations',
data: { emailTransport: 'smtp', smtpFromAddress: '[email protected]', smtpHost: '127.0.0.1', smtpPassword: 'e2e-sink', smtpPort, smtpUser: 'e2e' } as never,
overrideAccess: true,
})
console.log(`[e2e] e-mail sending points at the local sink 127.0.0.1:${smtpPort}`)
process.exit(0)
```
### `e2e/totp.ts`
```ts
@@ -1224,4 +1184,7 @@ export function totp(secret: string, now = Date.now()): string {
const code = (hmac.readUInt32BE(offset) & 0x7fffffff) % 1_000_000
return code.toString().padStart(6, '0')
}
/** The second smoke-test admin (create-test-admin.ts makes both): `e2e-admin@…` → `e2e-admin-2@…`. */
export const secondTestAdmin = (email: string): string => email.replace('@', '-2@')
```
+15 -7
View File
@@ -7,7 +7,10 @@
* (E2E_SMTP_PORT, default 2526).
*/
import config from '@payload-config'
import { verifyLimitKey } from '@intecion/ipal-kit'
import { isLocalDatabase } from '@intecion/ipal-kit/seed'
import { secondTestAdmin } from './totp'
import { getPayload } from 'payload'
const { E2E_ADMIN_EMAIL: email, E2E_ADMIN_PASSWORD: password, E2E_TOTP_SECRET: totpSecret } = process.env
@@ -16,14 +19,19 @@ if (process.env.NODE_ENV === 'production' && !process.env.CI) {throw new Error('
if (!isLocalDatabase(process.env.DATABASE_URI ?? '')) {throw new Error('Refusing: DATABASE_URI is not a local database.')}
const payload = await getPayload({ config })
const data = { email, hasTotp: true, password, roles: ['admin'], totpSecret }
const found = await payload.find({ collection: 'users', limit: 1, overrideAccess: true, where: { email: { equals: email } } })
if (found.docs[0]) {
await payload.update({ id: found.docs[0].id, collection: 'users', data: data as never, overrideAccess: true })
} else {
await payload.create({ collection: 'users', data: data as never, overrideAccess: true })
// Two test admins with the same password and TOTP secret. The 2FA verify limit is 5 attempts per account in
// 10 minutes and is stored in the database, so it survives a restart of the site: the smoke and back-link specs
// sign in as the first account, the setup spec as the second, and this script clears both counts — run it again
// before repeating the tests within ten minutes.
for (const address of [email, secondTestAdmin(email)]) {
const data = { email: address, hasTotp: true, password, roles: ['admin'], totpSecret }
const found = await payload.find({ collection: 'users', limit: 1, overrideAccess: true, where: { email: { equals: address } } })
const doc = found.docs[0]
? await payload.update({ id: found.docs[0].id, collection: 'users', data: data as never, overrideAccess: true })
: await payload.create({ collection: 'users', data: data as never, overrideAccess: true })
await payload.kv.delete(verifyLimitKey('users', String(doc.id)))
console.log(`[e2e] test admin ready: ${address}`)
}
console.log(`[e2e] test admin ready: ${email}`)
// E-mail for the smoke tests goes to a local sink the test itself starts (e2e/smtp-sink.ts), so the test can
// read what the site sent — the form file as an attachment. Without the sink running, sends simply fail.
+3
View File
@@ -13,3 +13,6 @@ export function totp(secret: string, now = Date.now()): string {
const code = (hmac.readUInt32BE(offset) & 0x7fffffff) % 1_000_000
return code.toString().padStart(6, '0')
}
/** The second smoke-test admin (create-test-admin.ts makes both): `e2e-admin@…` → `e2e-admin-2@…`. */
export const secondTestAdmin = (email: string): string => email.replace('@', '-2@')
+3 -2
View File
@@ -1,6 +1,6 @@
import { type APIRequestContext, expect, type Page, test } from '@playwright/test'
import { totp } from './totp'
import { secondTestAdmin, totp } from './totp'
/**
* "2FA setup required" in a real browser: an account that has not set two-step verification up sees only the setup
@@ -17,7 +17,8 @@ const EDITOR = { email: `e2e-editor-${stamp}@example.com`, password: `editor-${s
/** The admin the later tests act as: the owner made in the first test, else the smoke-test admin. */
let admin: { email: string; password: string; secret: string } | null =
process.env.E2E_ADMIN_EMAIL && process.env.E2E_ADMIN_PASSWORD && process.env.E2E_TOTP_SECRET
? { email: process.env.E2E_ADMIN_EMAIL, password: process.env.E2E_ADMIN_PASSWORD, secret: process.env.E2E_TOTP_SECRET }
? // the second test admin: the other specs spend the first one's verify attempts (5 per account in 10 minutes)
{ email: secondTestAdmin(process.env.E2E_ADMIN_EMAIL), password: process.env.E2E_ADMIN_PASSWORD, secret: process.env.E2E_TOTP_SECRET }
: null
let editorId: string | undefined
+2
View File
@@ -9,6 +9,8 @@ try {
/**
* Smoke tests against a built, seeded site: pnpm build && pnpm seed && pnpm payload run e2e/create-test-admin.ts.
* Run create-test-admin.ts again before repeating the tests within ten minutes: the 2FA verify limit (5 attempts per
* account in 10 minutes) is stored in the database, and that script clears the test admins' counts.
* E2E_BASE_URL picks the port (default 3100). Playwright starts its own server there and fails when the port is
* taken, so another project's dev server is never tested by mistake; E2E_REUSE_SERVER=1 uses a server you started.
*/
+2 -2
View File
@@ -59,12 +59,12 @@ export const DESCRIPTIONS = {
'DESIGN.md': 'the human source of the look, matching src/design/tokens.ts',
'README.md': 'how to run the reference site and what is where',
'docs/DEPLOY.md': 'the Coolify runbook: local check, app settings, MongoDB resource, env, targeted seeds, DNS, rollback',
'e2e/create-test-admin.ts': 'a test admin with a known TOTP secret, and e-mail sending pointed at the tests\' mail sink (local / CI database only)',
'e2e/create-test-admin.ts': 'two test admins with a known TOTP secret (the specs share out the five 2FA verify attempts per account), their stored verify counts cleared, and e-mail sending pointed at the tests\' mail sink (local / CI database only)',
'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/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',
'e2e/totp.ts': 'RFC 6238 code generator for the 2FA sign-in in the smoke test, and the address of the second test admin',
'ipal-doctor.json': 'the production domain and launch state for ipal-doctor',
'next.config.ts': 'security headers, enforced CSP, admin CSP, upload headers, images, no X-Powered-By',
'package.json': 'scripts (build, start, seed, tokens, generate), pinned Payload versions and overrides',