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:
co-authored by
Claude Fable 5.2
parent
83356ab4ed
commit
81cee02e78
@@ -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@')
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user