import { type APIRequestContext, expect, test } from '@playwright/test' import { randomUUID } from 'node:crypto' import { secondTestAdmin, totp } from './totp' /** * The account rules on a BUILT site, for a staff collection that a plugin listed after iapl adds * (e2e/late-staff-fixture.ts). Unit tests run unminified code; a production bundle renames every function, and a * rule that told Payload's default access function by its name was silently skipped there. Run it against both * bundlers (README "Production check"): * E2E_LATE_STAFF=1 pnpm build && … pnpm exec playwright test late-staff (webpack) * E2E_LATE_STAFF=1 pnpm next build --turbopack && … the same (Turbopack) */ const { E2E_ADMIN_EMAIL: email, E2E_ADMIN_PASSWORD: password, E2E_LATE_STAFF: on, E2E_TOTP_SECRET: secret } = process.env const PASSWORD = `T-${randomUUID()}` test.describe('a late staff collection on a built site', () => { test.skip(on !== '1', 'E2E_LATE_STAFF=1 not set (build and start the site with it)') test.skip(!email || !password || !secret, 'E2E_* test admin not configured') const context = (playwright: { request: { newContext: (o: object) => Promise } }, baseURL: string) => playwright.request.newContext({ baseURL, extraHTTPHeaders: { origin: baseURL } }) test('a visitor account and an anonymous request get nothing; nobody raises its own role', async ({ baseURL, playwright }) => { const tag = randomUUID().slice(0, 8) // a fully signed-in admin of the panel's collection const admin = await context(playwright, baseURL!) // (the second test admin: the smoke and back-link specs spend the first one's 2FA attempts) expect((await admin.post('/api/users/login', { data: { email: secondTestAdmin(email!), password } })).status()).toBe(200) expect((await (await admin.post('/api/verify-totp', { data: { token: totp(secret!) } })).json()).ok).toBe(true) // anonymous: the first-user route of the (possibly empty) late collection, in two spellings const nobody = await context(playwright, baseURL!) for (const spelling of ['first-register', 'First-Register']) { const res = await nobody.post(`/api/late-staff/${spelling}`, { data: { email: `first-${tag}@example.com`, password: PASSWORD, roles: ['admin'] } }) expect(res.status(), spelling).toBe(403) } expect((await nobody.get('/api/late-staff')).status()).toBe(403) // the admin makes an account there (the rules let an admin through) const made = await admin.post('/api/late-staff', { data: { email: `low-${tag}@example.com`, password: PASSWORD, roles: ['user'] } }) expect(made.status()).toBe(201) const id = (await made.json()).doc.id as string // a visitor account, signed up by itself const visitor = await context(playwright, baseURL!) expect((await visitor.post('/api/e2e-visitors', { data: { email: `v-${tag}@example.com`, password: PASSWORD } })).status()).toBe(201) expect((await visitor.post('/api/e2e-visitors/login', { data: { email: `v-${tag}@example.com`, password: PASSWORD } })).status()).toBe(200) const got = { create: (await visitor.post('/api/late-staff', { data: { email: `x-${tag}@example.com`, password: PASSWORD, roles: ['admin'] } })).status(), patch: (await visitor.patch(`/api/late-staff/${id}`, { data: { password: `taken-${PASSWORD}` } })).status(), read: (await visitor.get('/api/late-staff')).status(), unlock: (await visitor.post('/api/late-staff/unlock', { data: { email: `low-${tag}@example.com` } })).status(), versions: (await visitor.get('/api/late-staff/versions')).status(), // last: it would remove the account the other checks use delete: (await visitor.delete(`/api/late-staff/${id}`)).status(), } expect(got).toEqual({ create: 403, delete: 403, patch: 403, read: 403, unlock: 403, versions: 403 }) // nothing an admin opens, either expect((await visitor.get('/api/globals/site-integrations')).status()).toBe(403) // the account itself (2FA covers the panel's collection only): its own password yes, its own roles no const low = await context(playwright, baseURL!) expect((await low.post('/api/late-staff/login', { data: { email: `low-${tag}@example.com`, password: PASSWORD } })).status()).toBe(200) await low.patch(`/api/late-staff/${id}`, { data: { roles: ['admin'] } }) const after = await (await admin.get(`/api/late-staff/${id}`)).json() expect(after.roles).toEqual(['user']) expect((await low.get('/api/globals/site-integrations')).status()).toBe(403) expect((await low.post('/api/late-staff/unlock', { data: { email: `low-${tag}@example.com` } })).status()).toBe(403) // a short password is refused there too (the password rule) expect((await admin.post('/api/late-staff', { data: { email: `s-${tag}@example.com`, password: 'tooshort', roles: ['user'] } })).status()).toBe(400) // Staff collections whose accounts can hold `admin` although the roles are not just the plugin's — a select with // one more value (in the config), a text field (added by a later plugin): the lowest account does not make an // admin, take the collection's admin over, or delete it, and reaches nothing an admin reaches. for (const slug of ['e2e-crew', 'e2e-late-text']) { const itsAdmin = await admin.post(`/api/${slug}`, { data: { email: `a-${tag}@${slug}.example.com`, password: PASSWORD, roles: ['admin'] } }) const itsUser = await admin.post(`/api/${slug}`, { data: { email: `u-${tag}@${slug}.example.com`, password: PASSWORD, roles: ['user'] } }) expect([slug, itsAdmin.status(), itsUser.status()]).toEqual([slug, 201, 201]) const itsAdminId = (await itsAdmin.json()).doc.id as string const itsUserId = (await itsUser.json()).doc.id as string const api = await context(playwright, baseURL!) expect((await api.post(`/api/${slug}/login`, { data: { email: `u-${tag}@${slug}.example.com`, password: PASSWORD } })).status()).toBe(200) expect([slug, { create: (await api.post(`/api/${slug}`, { data: { email: `made-${tag}@${slug}.example.com`, password: PASSWORD, roles: ['admin'] } })).status(), integrations: (await api.get('/api/globals/site-integrations')).status(), patch: (await api.patch(`/api/${slug}/${itsAdminId}`, { data: { password: `taken-${PASSWORD}` } })).status(), users: (await api.get('/api/users')).status(), // last: it would remove the account the other calls are about zDelete: (await api.delete(`/api/${slug}/${itsAdminId}`)).status(), }]).toEqual([slug, { create: 403, integrations: 403, patch: 403, users: 403, zDelete: 403 }]) await api.patch(`/api/${slug}/${itsUserId}`, { data: { roles: ['admin'] } }) expect((await (await admin.get(`/api/${slug}/${itsUserId}`)).json()).roles, `${slug}: its own roles`).toEqual(['user']) expect((await api.patch(`/api/${slug}/${itsUserId}`, { data: { password: `own-${PASSWORD}` } })).status(), `${slug}: its own password`).toBe(200) for (const id of [itsAdminId, itsUserId]) {expect((await admin.delete(`/api/${slug}/${id}`)).status()).toBe(200)} await api.dispose() } // Copies of staff collections (e2e/late-staff-fixture.ts → copies): the lowest account of each copy, and an account // of the ORIGINAL with the role `user`, get nothing that a plugin rule or the copy's own rule refuses — whatever // role values the copy has and wherever it uses a copied rule. (Each of these shapes was open in some version that // REWROTE copied rules; the plugin now only ever puts its rule in front.) const original = await admin.post('/api/e2e-staff', { data: { email: `o-${tag}@staff.example.com`, password: PASSWORD, roles: ['user'] } }) expect(original.status()).toBe(201) const fromOriginal = await context(playwright, baseURL!) expect((await fromOriginal.post('/api/e2e-staff/login', { data: { email: `o-${tag}@staff.example.com`, password: PASSWORD } })).status()).toBe(200) for (const slug of ['e2e-copy', 'e2e-copy-cross-op', 'e2e-copy-values', 'e2e-copy-team-admin', 'e2e-copy-opt-out', 'e2e-copy-not-staff']) { const top = await admin.post(`/api/${slug}`, { data: { email: `a-${tag}@${slug}.example.com`, password: PASSWORD, roles: ['admin'] } }) const lowest = await admin.post(`/api/${slug}`, { data: { email: `u-${tag}@${slug}.example.com`, password: PASSWORD, roles: ['user'] } }) expect([slug, top.status(), lowest.status()]).toEqual([slug, 201, 201]) const topId = (await top.json()).doc.id as string const lowestId = (await lowest.json()).doc.id as string const own = await context(playwright, baseURL!) expect((await own.post(`/api/${slug}/login`, { data: { email: `u-${tag}@${slug}.example.com`, password: PASSWORD } })).status()).toBe(200) for (const [who, api] of [['its own lowest account', own], ['an account of the original', fromOriginal], ['a visitor', visitor]] as [string, APIRequestContext][]) { expect([slug, who, { create: (await api.post(`/api/${slug}`, { data: { email: `made-${tag}-${who.length}@${slug}.example.com`, password: PASSWORD, roles: ['admin'] } })).status(), integrations: (await api.get('/api/globals/site-integrations')).status(), patchLowest: who === 'its own lowest account' ? 'itself' : (await api.patch(`/api/${slug}/${lowestId}`, { data: { password: `taken-${PASSWORD}` } })).status(), patchTop: (await api.patch(`/api/${slug}/${topId}`, { data: { password: `taken-${PASSWORD}` } })).status(), zDelete: (await api.delete(`/api/${slug}/${topId}`)).status(), }]).toEqual([slug, who, { create: 403, integrations: 403, patchLowest: who === 'its own lowest account' ? 'itself' : 403, patchTop: 403, zDelete: 403 }]) } await own.patch(`/api/${slug}/${lowestId}`, { data: { roles: ['admin'] } }) expect((await (await admin.get(`/api/${slug}/${lowestId}`)).json()).roles, `${slug}: its own roles`).toEqual(['user']) for (const id of [topId, lowestId]) {expect((await admin.delete(`/api/${slug}/${id}`)).status(), `${slug}: an admin cleans up`).toBe(200)} await own.dispose() } // a copy with rules of its own ("nobody"): an admin is refused too — the plugin's rule went in front, it replaced nothing expect((await admin.post('/api/e2e-copy-own-rules', { data: { email: `x-${tag}@own.example.com`, password: PASSWORD, roles: ['admin'] } })).status(), 'own rules: create').toBe(403) expect((await admin.get('/api/e2e-copy-own-rules')).status(), 'own rules: read').toBe(403) // the cross-op copy: read is "an admin" there (the create rule), also for the copy's own accounts — checked above through // create / patch; and the list itself: expect((await fromOriginal.get('/api/e2e-copy-cross-op')).status()).toBe(403) expect((await admin.get('/api/e2e-copy-cross-op')).status()).toBe(200) await fromOriginal.dispose() // an admin: everything, and it cleans up expect((await admin.get('/api/late-staff/versions')).status()).toBe(200) expect((await admin.post('/api/late-staff/unlock', { data: { email: `low-${tag}@example.com` } })).status()).toBe(200) expect((await admin.delete(`/api/late-staff/${id}`)).status()).toBe(200) for (const api of [admin, nobody, visitor, low]) {await api.dispose()} }) })