/** * The documented way to exempt a service account from 2FA setup (security.md "Decide `exempt` from a field only an * administrator can write"), checked through real HTTP requests: an editor can rename itself to any e-mail, so an * e-mail list is no boundary; an admin-only checkbox is — the editor's own save leaves it as it is (REST and GraphQL). * 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, describe, expect, test, vi } from 'vitest' import { adminOnlyField } from '../src/index.js' import { iapl } from '../src/plugin.js' import { buildTestConfig } from './helpers.js' import { testDatabase } from './http/db.js' import { cookiesOf, startPayloadServer, type TestServer } from './http/server.js' import { totp } from './http/totp.js' vi.mock('server-only', () => ({})) vi.mock('next/headers.js', async () => (await import('./http/server.js')).nextHeadersMock) const SECRET = 'JBSWY3DPEHPK3PXP' const PASSWORD = 'a-long-test-password-1' let server: TestServer let payload: Payload let editorId: number | string let cookie: string const call = (p: string, init: RequestInit = {}) => fetch(`${server.url}/api${p}`, { ...init, headers: { 'content-type': 'application/json', cookie, ...(init.headers as Record) } }) beforeAll(async () => { const config = buildTestConfig({ admin: { user: 'users' }, collections: [ { slug: 'users', auth: true, fields: [{ name: 'serviceAccount', type: 'checkbox', access: { create: adminOnlyField, update: adminOnlyField }, defaultValue: false }] }, { slug: 'media', fields: [], upload: { staticDir: '/tmp/iapl-exempt-media' } }, ], db: testDatabase('exemptfield'), editor: lexicalEditor(), plugins: [ iapl({ access: { authCollection: 'users' }, i18n: { defaultLocale: 'en', locales: [{ code: 'en', label: 'English' }] }, twoFactor: { issuer: 'Example', plugin: payloadTotp, requireSetup: { exempt: ({ user }) => user.serviceAccount === true } }, }), ], secret: 'integration-test-secret-0123456789', telemetry: false, }) payload = await getPayload({ config }) server = await startPayloadServer(config) const editor = await payload.create({ collection: 'users' as never, data: { email: 'editor@example.com', password: PASSWORD, roles: ['editor'], totpSecret: SECRET } as never }) editorId = (editor as { id: number | string }).id const login = await fetch(`${server.url}/api/users/login`, { body: JSON.stringify({ email: 'editor@example.com', password: PASSWORD }), headers: { 'content-type': 'application/json' }, method: 'POST' }) const first = cookiesOf(login) cookie = first.join('; ') const verify = await call('/verify-totp', { body: JSON.stringify({ token: totp(SECRET) }), method: 'POST' }) expect(((await verify.json()) as { ok?: boolean }).ok).toBe(true) cookie = [...first, ...cookiesOf(verify)].join('; ') }, 120_000) afterAll(async () => { await server?.close() await payload?.destroy?.() }) describe('exempt from 2FA setup: by an admin-only field, not by e-mail', () => { test('an editor cannot set the field on itself (REST and GraphQL); the value stays false', async () => { const rest = await call(`/users/${editorId}`, { body: JSON.stringify({ serviceAccount: true }), method: 'PATCH' }) expect(rest.status).toBe(200) await call('/graphql', { body: JSON.stringify({ query: `mutation { updateUser(id: ${JSON.stringify(editorId)}, data: { serviceAccount: true }) { id } }` }), method: 'POST' }) const stored = (await payload.findByID({ id: editorId, collection: 'users' as never, overrideAccess: true })) as unknown as { serviceAccount?: boolean } expect(stored.serviceAccount).toBe(false) }) test('…while the same editor can change its own e-mail to anything: an e-mail list is no boundary', async () => { const res = await call(`/users/${editorId}`, { body: JSON.stringify({ email: 'it@example.com' }), method: 'PATCH' }) expect(res.status).toBe(200) const stored = (await payload.findByID({ id: editorId, collection: 'users' as never, overrideAccess: true })) as unknown as { email: string } expect(stored.email).toBe('it@example.com') }) })