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
+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.
*/