Files
ipal-kit/reference/e2e/late-staff.spec.ts
T
Radosław SmolińskiandClaude Fable 5.2 be2e6d52d9 A staff collection whose accounts can hold editor or admin gets the admin rules (H-2, J-3)
In the released 1.16.7. The plugin's checks go by the value on the account, but a project's own staff collection
without rules got the admin rules only when ALL its role values were the plugin's; otherwise 'an account of this
collection' could create, change and delete accounts. Measured on bc0fa65 (SQLite; the first shape also on Postgres
and MongoDB, 2FA off and on): with roles user/editor/admin/boss, with a text and with a json roles field, config-time
and added by a later plugin, an account with the role user creates an admin (201) who reads Site integrations, changes
another staff collection's roles and lists the panel's accounts (200 / 200 / 200), takes the admin over and deletes it;
with viewer/editor a viewer creates an editor who gets past the access rule of Site settings.
canHoldStaffRole(collection): a roles select with editor or admin among its values, or a roles field whose values
cannot be listed. Then: create / delete / unlock an admin, update an admin or the account itself, roles only by an
admin. A project's own rules win. A warning at start when no staff account is an admin and such a collection exists.
The built-site fixture gets a config-time and a late collection of that kind; nothing here touches copied collections.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
2026-11-25 02:59:44 +01:00

106 lines
7.5 KiB
TypeScript

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 ipalKit 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<APIRequestContext> } }, 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()
}
// 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()}
})
})