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]>
46 lines
2.8 KiB
TypeScript
46 lines
2.8 KiB
TypeScript
/**
|
|
* 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 { 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
|
|
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 })
|
|
// 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}`)
|
|
}
|
|
|
|
// 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)
|