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
@@ -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.
|
||||
|
||||
@@ -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@')
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user