/** * A locked staff account answers its login exactly like a wrong password (accounts.lockedAnswer, 1.18.0; IAWL * idea 6). Shared by lockedAnswer.test.ts (default, 2FA on), lockedAnswer2faOff.test.ts (2FA off) and * lockedAnswerPayload.test.ts (`lockedAnswer: 'payload'`): one Payload per file. * Database: SQLite in memory, or IPAL_TEST_POSTGRES_URI / IPAL_TEST_MONGO_URI (test/http/db.ts). */ import type { Payload } from 'payload' import { payloadTotp } from '@clocklimited/payload-2fa' import { lexicalEditor } from '@payloadcms/richtext-lexical' import { getPayload } from 'payload' import { afterAll, beforeAll } from 'vitest' import { iapl } from '../src/plugin.js' import { buildTestConfig } from './helpers.js' import { testDatabase } from './http/db.js' import { startPayloadServer, type TestServer } from './http/server.js' export const PASSWORD = 'a-long-test-password-1' export type Answer = { body: string; status: number } export function lockedAnswerSite({ lockedAnswer, twoFactor = true }: { lockedAnswer?: 'payload'; twoFactor?: boolean } = {}) { const state = {} as { payload: Payload; server: TestServer } beforeAll(async () => { const config = buildTestConfig({ admin: { user: 'users' }, collections: [ // a visitor collection with Payload's own lock: not staff, not changed { slug: 'customers', access: { create: () => true }, auth: { maxLoginAttempts: 3 }, fields: [] }, { slug: 'media', fields: [], upload: { staticDir: '/tmp/iapl-locked-answer-media' } }, ], db: testDatabase('lockedanswer'), editor: lexicalEditor(), plugins: [ iapl({ access: { authCollection: 'users' }, ...(lockedAnswer ? { accounts: { lockedAnswer } } : {}), i18n: { defaultLocale: 'en', locales: [{ code: 'en', label: 'English' }] }, twoFactor: twoFactor ? { issuer: 'Example', plugin: payloadTotp } : false, }), ], secret: 'integration-test-secret-0123456789', telemetry: false, }) state.payload = await getPayload({ config }) state.server = await startPayloadServer(config) await state.payload.create({ collection: 'users' as never, data: { email: 'owner@example.com', password: PASSWORD, roles: ['admin'] } as never }) await state.payload.create({ collection: 'customers' as never, data: { email: 'client@example.com', password: PASSWORD } as never }) }, 120_000) afterAll(async () => { await state.server?.close() await state.payload?.destroy?.() }) const restLogin = async (collection: string, email: string, password: string, ip?: string): Promise => { const res = await fetch(`${state.server.url}/api/${collection}/login`, { body: JSON.stringify({ email, password }), headers: { 'content-type': 'application/json', ...(ip ? { 'x-real-ip': ip } : {}) }, method: 'POST', }) return { body: await res.text(), status: res.status } } const gqlLogin = async (mutation: string, email: string, password: string) => { const res = await fetch(`${state.server.url}/api/graphql`, { body: JSON.stringify({ query: `mutation { ${mutation}(email: ${JSON.stringify(email)}, password: ${JSON.stringify(password)}) { token } }` }), headers: { 'content-type': 'application/json' }, method: 'POST', }) const body = (await res.json()) as { errors?: { extensions?: { name?: string; statusCode?: number }; message: string }[] } const e = body.errors?.[0] return { name: e?.extensions?.name, message: e?.message, status: res.status, statusCode: e?.extensions?.statusCode } } /** * Locks `email` with wrong passwords. A staff collection: since 1.18.0 (accounts.signInLimit) Payload's lock is at 50 * failures and one IP may fail 5 times per address, so the lock comes from a spray of 50 IPs, one failure each. A * visitor collection: Payload's own count (`times`). */ const lock = async (collection: string, email: string, times = 50) => { for (let i = 0; i < times; i++) {await restLogin(collection, email, 'a-wrong-password', `198.51.${100 + Math.floor(i / 250)}.${(i % 250) + 1}`)} } return { gqlLogin, lock, restLogin, state } }