/** * The plugin's panel actions only from the site's own origin (adminSameOrigin, 1.17.2). Shared by * adminSameOrigin.test.ts (default) and adminSameOriginOff.test.ts (`adminSameOrigin: false`): one Payload per file. * A signed-in administrator's cookie rides on a `text/plain` POST (no CORS preflight) from a page on a sibling * subdomain. 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 { cookiesOf, startPayloadServer, type TestServer } from './http/server.js' import { totp } from './http/totp.js' const SECRET = 'JBSWY3DPEHPK3PXP' const PASSWORD = 'a-long-test-password-1' export const SIBLING = { origin: 'https://blog.site.example', 'sec-fetch-site': 'same-site' } /** Every panel action the rule covers (path under /api). */ export const ACTIONS = ['/iapl/mail-relay/connect', '/iapl/mail-relay/disconnect', '/ipal/test-email', '/ipal/cookie-scan', '/ipal/cookie-approval', '/ipal/cookie-crawl'] export function adminSite(adminSameOrigin?: false) { const state = {} as { cookie: string; payload: Payload; server: TestServer; warn: string[] } beforeAll(async () => { process.env.IAPL_COOKIE_SCAN_URL ??= 'http://127.0.0.1:9' process.env.IAPL_COOKIE_SCAN_TOKEN ??= 'test-token-0123456789abcdef0123456789' const config = buildTestConfig({ admin: { user: 'users' }, collections: [{ slug: 'media', fields: [], upload: { staticDir: '/tmp/iapl-admin-origin-media' } }], db: testDatabase('adminorigin'), editor: lexicalEditor(), plugins: [ iapl({ access: { authCollection: 'users' }, ...(adminSameOrigin === false ? { adminSameOrigin } : {}), cookieScanner: { crawl: true }, i18n: { defaultLocale: 'en', locales: [{ code: 'en', label: 'English' }] }, twoFactor: { issuer: 'Example', plugin: payloadTotp }, }), ], secret: 'integration-test-secret-0123456789', telemetry: false, }) state.payload = await getPayload({ config }) state.warn = [] const warn = state.payload.logger.warn.bind(state.payload.logger) state.payload.logger.warn = ((msg: unknown, ...rest: unknown[]) => { state.warn.push(String(msg)) return (warn as (...a: unknown[]) => void)(msg, ...rest) }) as never state.server = await startPayloadServer(config) await state.payload.create({ collection: 'users' as never, data: { email: 'admin@example.com', password: PASSWORD, roles: ['admin'], totpSecret: SECRET } as never }) const login = await fetch(`${state.server.url}/api/users/login`, { body: JSON.stringify({ email: 'admin@example.com', password: PASSWORD }), headers: { 'content-type': 'application/json' }, method: 'POST' }) const first = cookiesOf(login) const verify = await fetch(`${state.server.url}/api/verify-totp`, { body: JSON.stringify({ token: totp(SECRET) }), headers: { 'content-type': 'application/json', cookie: first.join('; ') }, method: 'POST' }) state.cookie = [...first, ...cookiesOf(verify)].join('; ') }, 120_000) afterAll(async () => { await state.server?.close() await state.payload?.destroy?.() }) /** A POST as a foreign page can send it: text/plain, the admin's cookie. */ const action = (path: string, headers: Record, body = '{"to":"someone@example.com"}') => fetch(`${state.server.url}/api${path}`, { body, headers: { 'content-type': 'text/plain', cookie: state.cookie, ...headers }, method: 'POST', signal: AbortSignal.timeout(20_000) }) return { action, state } }