Co-Authored-By: Claude Fable 5.2 <[email protected]>
79 lines
5.1 KiB
TypeScript
79 lines
5.1 KiB
TypeScript
import { type APIRequestContext, expect, test } from '@playwright/test'
|
|
import { randomUUID } from 'node:crypto'
|
|
|
|
import { 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!)
|
|
expect((await admin.post('/api/users/login', { data: { 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)
|
|
|
|
// 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()}
|
|
})
|
|
})
|