F1: roles count only for accounts of a staff collection, in every exported predicate and access builder (isAdmin, isEditor, hasMinimumRole, adminOnly, adminOrEditor, requireRole, adminOrSelf, adminOrSelfIn, the Field variants, the roles field) and first in userMeetsRole. An account of another auth collection with its own roles: ['admin'] gets nothing. Staff collections: admin.user, access.authCollection, access.staffCollections. Predicates take req.payload as an optional last argument and otherwise use the staff collections of the config ipalKit built in this process. F2: every endpoint @clocklimited/payload-2fa registers answers accounts of the 2FA collection only (setup-totp wrote the secret into the 2FA collection under the caller's id, whatever collection the caller came from); remove and the admin reset/remove endpoints also need the second factor. F3: per-entity opt-out honoured, operation by operation: the package's custom.totp.disableAccessWrapper and ipal-kit's custom.ipal.secondFactor (passed on to the package); the package's global disableAccessWrapper too. F4: a no-user auth strategy on the 2FA collection guards the live config at the start of each request, so entities from plugins listed after ipalKit stay guarded after a dev hot reload and with disableOnInit. Tests through HTTP for all four; the 2FA tests now run on payload-2fa 3.0.0-beta.7 (what client sites use), a second file on beta.1 (alias), and the HTTP files can run against Postgres (IPAL_TEST_POSTGRES_URI). Docs, agent guide, skills and changelog updated; the not-yet-enrolled limitation is stated plainly. Co-Authored-By: Claude Fable 5.2 <[email protected]>
107 lines
4.6 KiB
TypeScript
107 lines
4.6 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 } 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)).toBe(true)
|
|
expect(isAdmin(editor)).toBe(false)
|
|
expect(isEditor(admin)).toBe(true)
|
|
expect(isEditor(user)).toBe(false)
|
|
expect(hasMinimumRole(user, 'user')).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('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()
|
|
})
|
|
})
|