Files
ipal-kit/test/access.test.ts
T
Radosław SmolińskiandClaude Fable 5.2 0a4f001797 IAPL rename, scripted: preferred names in src, bin, scripts, services, tests; bin files and skill folders moved
Replaced by an explicit list of forms only (ten exported names, runtime env names, project file
names and marks, commands, skill names, the log prefix, the brand in prose). Package name, import
paths, routes, CSS tokens, data attributes, custom keys, context flags, the cookie and kv keys are
not touched. package.json keeps the five old bin names beside the five new ones.
The old names stop working in this commit and come back as aliases in the next; docs follow.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
2026-11-25 17:24:21 +01:00

137 lines
6.4 KiB
TypeScript

import type { FieldAccess, PayloadRequest } from 'payload'
import { describe, expect, test, vi } from 'vitest'
import { adminOnly, adminOnlyField, adminOrEditor, adminOrEditorField, adminOrSelf, adminOrSelfIn, requireRole, requireRoleField } from '../src/modules/access/access.js'
import { hasMinimumRole, isAdmin, isEditor, registerStaffCollections, staffRegistrySize } from '../src/modules/access/predicates.js'
import { buildRolesField } from '../src/modules/access/rolesField.js'
import { testEmailEndpoint } from '../src/modules/email/test/testEmailEndpoint.js'
vi.mock('payload', () => ({ addDataAndFileToRequest: vi.fn(async () => {}) }))
const admin = { id: 1, collection: 'users', roles: ['admin'] }
const editor = { id: 2, collection: 'users', roles: ['editor'] }
const user = { id: 3, collection: 'users', roles: ['user'] }
/** `req.payload` as far as the role checks read it: the panel logs in with `users`. */
const payload = { config: { admin: { user: 'users' } } } as never
describe('role predicates', () => {
test('hierarchy admin > editor > user', () => {
expect(isAdmin(admin, payload)).toBe(true)
expect(isAdmin(editor, payload)).toBe(false)
expect(isEditor(admin, payload)).toBe(true)
expect(isEditor(user, payload)).toBe(false)
expect(hasMinimumRole(user, 'user', payload)).toBe(true)
})
test('unknown roles and odd shapes grant nothing', () => {
expect(isAdmin({ roles: ['root'] })).toBe(false)
expect(isAdmin({ roles: 'admin' })).toBe(false)
expect(isAdmin(null)).toBe(false)
})
test('a role counts only on an account of a staff collection', () => {
const member = { id: 1, collection: 'members', roles: ['admin'] }
expect(isAdmin(member, payload)).toBe(false)
expect(isEditor(member, payload)).toBe(false)
expect(hasMinimumRole(member, 'user', payload)).toBe(false)
expect(isAdmin({ id: 1, roles: ['admin'] }, payload)).toBe(false) // no collection: not an authenticated staff account
expect(isAdmin(admin, payload)).toBe(true)
// named in access.staffCollections → its roles count
const withMembers = { config: { admin: { user: 'users' }, custom: { ipal: { staffCollections: ['members'] } } } } as never
expect(isAdmin(member, withMembers)).toBe(true)
})
test('where the staff collections are not known, nobody is staff (browser bundle, middleware)', () => {
// no iapl config in this process (test/setup.ts clears the registry) and no source given
expect(isAdmin(admin)).toBe(false)
expect(isAdmin({ collection: 'customers', roles: ['admin'] })).toBe(false)
expect(hasMinimumRole(user, 'user')).toBe(false)
// …until the caller says where staff come from: the config (also the client config), or the list itself
expect(isAdmin(admin, { admin: { user: 'users' } })).toBe(true)
expect(isAdmin(admin, { staffCollections: ['users'] })).toBe(true)
expect(isAdmin({ collection: 'customers', roles: ['admin'] }, { staffCollections: ['users'] })).toBe(false)
})
test('two configs in one process: a call without a source gets only what both call staff', () => {
const a = {}
const b = {}
registerStaffCollections(a, ['users'])
expect(isAdmin(admin)).toBe(true)
registerStaffCollections(b, ['staff', 'customers'])
expect(isAdmin(admin)).toBe(false) // not staff in B
expect(isAdmin({ collection: 'customers', roles: ['admin'] })).toBe(false) // not staff in A
registerStaffCollections(b, ['users', 'customers'])
expect(isAdmin(admin)).toBe(true)
expect(isAdmin({ collection: 'customers', roles: ['admin'] })).toBe(false)
// with the source, each config gets its own answer
expect(isAdmin({ collection: 'customers', roles: ['admin'] }, { staffCollections: ['users', 'customers'] })).toBe(true)
// an instance taking over its config's token replaces the entry instead of adding one
const instance = {}
registerStaffCollections(instance, ['users'], a)
expect(staffRegistrySize()).toBe(2)
})
test('every ready access function refuses that account', () => {
const member = { id: 1, collection: 'members', roles: ['admin'] }
const req = { payload, user: member }
for (const fn of [adminOnly, adminOrEditor, requireRole('user'), adminOrSelfIn('users')]) {
expect(fn({ req } as never)).toBe(false)
}
for (const fn of [adminOnlyField, adminOrEditorField, requireRoleField('user')]) {
expect(fn({ req } as never)).toBe(false)
}
// adminOrSelf cannot know the collection it guards: no admin shortcut, only the id rule (use adminOrSelfIn)
expect(adminOrSelf({ req } as never)).toEqual({ id: { equals: 1 } })
expect(adminOnly({ req: { payload, user: admin } } as never)).toBe(true)
})
})
describe('roles field', () => {
const field = buildRolesField() as { access: { create: FieldAccess; update: FieldAccess } }
const call = (fn: FieldAccess, u: unknown) => fn({ req: { payload, user: u } } as never)
test('only admins can set roles on create — no self-promotion via sign-up', () => {
expect(call(field.access.create, null)).toBe(false)
expect(call(field.access.create, user)).toBe(false)
expect(call(field.access.create, admin)).toBe(true)
// an "admin" of another auth collection is nobody here
expect(call(field.access.create, { id: 1, collection: 'members', roles: ['admin'] })).toBe(false)
})
test('only admins can change roles', () => {
expect(call(field.access.update, editor)).toBe(false)
expect(call(field.access.update, admin)).toBe(true)
})
})
describe('test-email endpoint', () => {
const run = (u: unknown) => {
const sendEmail = vi.fn(async () => ({ sent: true }))
const req = {
data: { to: '[email protected]' },
payload: { config: { admin: { user: 'users' } }, logger: { error: vi.fn() }, sendEmail },
user: u,
} as unknown as PayloadRequest
return { response: testEmailEndpoint.handler(req), sendEmail }
}
test('anonymous → 401', async () => {
const { response, sendEmail } = run(null)
expect((await response).status).toBe(401)
expect(sendEmail).not.toHaveBeenCalled()
})
test('logged-in non-admin → 403', async () => {
const { response, sendEmail } = run(editor)
expect((await response).status).toBe(403)
expect(sendEmail).not.toHaveBeenCalled()
})
test('admin → sends', async () => {
const { response, sendEmail } = run(admin)
expect((await response).status).toBe(200)
expect(sendEmail).toHaveBeenCalledOnce()
})
})