/** * The documented non-interactive paths keep working with 2FA on (real HTTP, see securityHttp.test.ts): * an API key of a staff account, and the bearer token of GET /api/ipal/doctor. */ import type { Payload } from 'payload' import { payloadTotp } from '@clocklimited/payload-2fa' import { lexicalEditor } from '@payloadcms/richtext-lexical' import os from 'os' import path from 'path' import { getPayload } from 'payload' import { afterAll, beforeAll, expect, test, vi } 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' vi.mock('server-only', () => ({})) vi.mock('next/headers.js', async () => (await import('./http/server.js')).nextHeadersMock) const API_KEY = 'b7f0c3a2-5d1e-4f6a-9c8b-0e1d2f3a4b5c' const DOCTOR_TOKEN = 'doctor-token-0123456789-0123456789-abcdef' let server: TestServer let payload: Payload beforeAll(async () => { process.env.IAPL_DOCTOR_TOKEN = DOCTOR_TOKEN const config = buildTestConfig({ collections: [ // The project's own staff collection, with API keys. { slug: 'users', auth: { useAPIKey: true }, fields: [] }, { slug: 'media', fields: [], upload: { staticDir: path.join(os.tmpdir(), 'ipal-test-media') } }, ], db: testDatabase('sechttp_tok'), editor: lexicalEditor(), plugins: [ iapl({ access: { authCollection: 'users' }, forms: {}, i18n: { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'Polski' }] }, twoFactor: { issuer: 'Example', plugin: payloadTotp }, }), ], secret: 'integration-test-secret-0123456789', telemetry: false, }) payload = await getPayload({ config }) server = await startPayloadServer(config) await payload.create({ collection: 'users' as never, data: { apiKey: API_KEY, email: 'robot@example.com', enableAPIKey: true, password: 'a-long-test-password-1', roles: ['admin'], totpSecret: 'JBSWY3DPEHPK3PXP' } as never, }) }, 120_000) afterAll(async () => { delete process.env.IAPL_DOCTOR_TOKEN await server?.close() await payload?.destroy?.() }) const get = (pathname: string, authorization?: string) => fetch(`${server.url}/api${pathname}`, { headers: authorization ? { authorization } : {} }) test('an API key of an admin account reads Site integrations and form submissions without a TOTP code', async () => { const key = `users API-Key ${API_KEY}` expect((await get('/globals/site-integrations', key)).status).toBe(200) expect((await get('/form-submissions', key)).status).toBe(200) expect((await get('/users', key)).status).toBe(200) expect((await get('/globals/site-integrations', 'users API-Key wrong-key')).status).toBe(403) }) test('GET /api/ipal/doctor answers its bearer token, and nobody else', async () => { const ok = await get('/ipal/doctor', `Bearer ${DOCTOR_TOKEN}`) expect(ok.status).toBe(200) expect(await ok.json()).toMatchObject({ usersWithout2fa: 0 }) expect((await get('/ipal/doctor')).status).toBe(401) expect((await get('/ipal/doctor', 'Bearer not-the-token-0123456789-0123456789')).status).toBe(401) })