- Turnstile is on only when both keys are set (panel or env); then submitForm requires a valid token, so a bot can no longer skip the check by omitting it. Not configured -> no widget, no check. New getTurnstileConfig() for layouts. - /api/ipal/test-email now requires the admin role (403 for other users). - roles field: create access is admin-only, so open sign-up can't self-assign admin. - clientIp reads X-Forwarded-For from the right, accepts Headers, can be pinned via option or IPAL_IP_HEADER; exposed on search/MCP routes. - Pluggable shared rate-limit store (setRateLimitStore) for multi-instance setups. - Replace the template int.spec with real unit tests in test/ (44 tests). Co-Authored-By: Claude Opus 5.5 <[email protected]>
76 lines
2.6 KiB
TypeScript
76 lines
2.6 KiB
TypeScript
import type { FieldAccess, PayloadRequest } from 'payload'
|
|
|
|
import { describe, expect, test, vi } from 'vitest'
|
|
|
|
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, roles: ['admin'] }
|
|
const editor = { id: 2, roles: ['editor'] }
|
|
const user = { id: 3, roles: ['user'] }
|
|
|
|
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)
|
|
})
|
|
})
|
|
|
|
describe('roles field', () => {
|
|
const field = buildRolesField() as { access: { create: FieldAccess; update: FieldAccess } }
|
|
const call = (fn: FieldAccess, u: unknown) => fn({ req: { 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)
|
|
})
|
|
|
|
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: { 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()
|
|
})
|
|
})
|