Files
ipal-kit/test/plugin.test.ts
T
radoslaw.smolinskiandClaude Opus 5.5 ce0a390351 Security: mandatory Turnstile when configured, admin-only test email, roles on create, safer client IP
- 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]>
2026-10-15 23:54:25 +02:00

52 lines
1.8 KiB
TypeScript

import type { Config } from 'payload'
import { describe, expect, test, vi } from 'vitest'
import { ipalKit } from '../src/plugin.js'
const i18n = {
defaultLocale: 'pl',
locales: [
{ code: 'pl', label: 'Polski' },
{ code: 'en', label: 'English' },
],
}
const base = (): Config =>
({
collections: [{ slug: 'users', auth: true, fields: [] }],
secret: 'test',
}) as unknown as Config
describe('ipalKit — config transform', () => {
test('adds globals, endpoint, localization and roles', async () => {
const config = await ipalKit({ access: { authCollection: 'users' }, i18n, twoFactor: false })(base())
expect(config.globals?.map((g) => g.slug)).toEqual(
expect.arrayContaining(['site-settings', 'site-integrations']),
)
expect(config.endpoints?.some((e) => e.path === '/ipal/test-email')).toBe(true)
expect(config.localization && config.localization.defaultLocale).toBe('pl')
const users = config.collections?.find((c) => c.slug === 'users')
expect(users?.fields.some((f) => 'name' in f && f.name === 'roles')).toBe(true)
})
test('enforces 2FA unless explicitly disabled', async () => {
await expect(ipalKit({ i18n })(base())).rejects.toThrow(/twoFactor\.issuer is required/)
const totp = vi.fn(() => (c: Config) => c)
await ipalKit({ i18n, twoFactor: { issuer: 'Acme', plugin: totp as never } })(base())
expect(totp).toHaveBeenCalledWith(expect.objectContaining({ forceSetup: true, totp: { issuer: 'Acme' } }))
})
test('enabled: false leaves the config untouched', async () => {
const config = base()
expect(await ipalKit({ enabled: false, i18n, twoFactor: false })(config)).toBe(config)
})
test('invalid i18n fails fast', () => {
expect(() => ipalKit({ i18n: { defaultLocale: 'de', locales: i18n.locales }, twoFactor: false })).toThrow()
})
})