import { type APIRequestContext, expect, test } from '@playwright/test' import { randomUUID } from 'node:crypto' import { totp } from './totp' /** * On the BUILT site (pnpm e2e:built, webpack and Turbopack), what 1.17.2 changed at the sign-in and in the panel: * - a locked staff account answers its login exactly like a wrong password (accounts.lockedAnswer) — the hook * recognises Payload's error by the name Payload gives it, which a minified bundle must keep; * - the plugin's panel actions refuse a request from another origin (adminSameOrigin), the site's own still works. * Needs the E2E_* test admin (e2e/create-test-admin.ts). The locked account is a fresh one made here, so the test * admins are never locked. */ const { E2E_ADMIN_EMAIL: adminEmail, E2E_ADMIN_PASSWORD: password, E2E_TOTP_SECRET: secret } = process.env let admin: APIRequestContext test.beforeAll(async ({ baseURL, playwright }) => { test.skip(!adminEmail || !password || !secret, 'E2E_ADMIN_* values are not set') admin = await playwright.request.newContext({ baseURL, extraHTTPHeaders: { origin: baseURL! } }) expect((await admin.post('/api/users/login', { data: { email: adminEmail, password } })).status()).toBe(200) expect((await (await admin.post('/api/verify-totp', { data: { token: totp(secret!) } })).json()).ok).toBe(true) }) test.afterAll(async () => { await admin?.dispose() }) test('a locked staff account gets the wrong-password answer, REST and GraphQL', async ({ baseURL, playwright }) => { const email = `locked-${randomUUID().slice(0, 8)}@example.com` const own = `L-${randomUUID()}` expect((await admin.post('/api/users', { data: { email, password: own, roles: ['editor'] } })).status()).toBe(201) const anon = await playwright.request.newContext({ baseURL }) const login = async (address: string, pass: string) => { const res = await anon.post('/api/users/login', { data: { email: address, password: pass } }) return { body: await res.text(), status: res.status() } } for (let i = 0; i < 5; i++) {await login(email, 'a-wrong-password-1')} const invented = await login(`nobody-${randomUUID().slice(0, 8)}@example.com`, 'a-wrong-password-1') expect(invented.status).toBe(401) expect(await login(email, own)).toEqual(invented) const gql = async (address: string, pass: string) => { const res = await anon.post('/api/graphql', { data: { query: `mutation { loginUser(email: ${JSON.stringify(address)}, password: ${JSON.stringify(pass)}) { token } }` } }) const error = ((await res.json()) as { errors?: { extensions?: { name?: string }; message: string }[] }).errors?.[0] return { message: error?.message, name: error?.extensions?.name } } expect(await gql(email, own)).toEqual(await gql(`nobody-${randomUUID().slice(0, 8)}@example.com`, 'x-wrong-password-1')) await anon.dispose() }) test('panel actions: another origin gets 403 with the admin cookie, the site itself goes through', async ({ baseURL }) => { const foreign = await admin.fetch('/api/ipal/cookie-approval', { data: '{"ids":[]}', headers: { 'content-type': 'text/plain', origin: 'https://blog.example.test', 'sec-fetch-site': 'same-site' }, method: 'POST', }) expect(foreign.status()).toBe(403) const own = await admin.fetch('/api/ipal/cookie-approval', { data: '{"ids":[]}', headers: { 'content-type': 'text/plain', origin: baseURL! }, method: 'POST' }) expect(own.status()).toBe(200) })