Files
ipal-kit/test/i18n.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

75 lines
3.0 KiB
TypeScript

import { describe, expect, test } from 'vitest'
import { buildLocalizedPath, switchLocalePath } from '../src/modules/i18n/localizedPath.js'
import { matchAcceptLanguage, negotiateLocale } from '../src/modules/i18n/negotiateLocale.js'
import { isHoneypotTripped } from '../src/modules/forms/honeypot.js'
const config = {
defaultLocale: 'pl',
locales: [
{ code: 'pl', label: 'Polski' },
{ code: 'en', label: 'English' },
],
}
const single = { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'Polski' }] }
describe('negotiateLocale', () => {
test('cookie → Accept-Language → default', () => {
expect(negotiateLocale({ acceptLanguage: 'en', config, cookieLocale: 'pl' })).toBe('pl')
expect(negotiateLocale({ acceptLanguage: 'de-DE,en-US;q=0.8', config })).toBe('en')
expect(negotiateLocale({ acceptLanguage: 'fr', config, cookieLocale: 'xx' })).toBe('pl')
})
test('respects q-values', () => {
expect(matchAcceptLanguage('en;q=0.5,pl;q=0.9', config)).toBe('pl')
expect(matchAcceptLanguage(null, config)).toBeUndefined()
})
})
describe('buildLocalizedPath', () => {
const slugs = { en: 'about', pl: 'o-nas' }
test('prefixes the locale', () => {
expect(buildLocalizedPath({ config, locale: 'en', slugs })).toBe('/en/about')
})
test('home collapses to the locale root, per-locale home slugs', () => {
const home = { en: 'home', pl: 'strona-glowna' }
expect(buildLocalizedPath({ config, homeSlug: home, locale: 'pl', slugs: home })).toBe('/pl')
expect(buildLocalizedPath({ config, homeSlug: home, locale: 'en', slugs: home })).toBe('/en')
})
test('single-locale sites drop the prefix', () => {
expect(buildLocalizedPath({ config: single, locale: 'pl', slugs })).toBe('/o-nas')
expect(buildLocalizedPath({ config: single, locale: 'pl', slugs: { pl: 'home' } })).toBe('/')
})
test('archive entries need a prefix in that locale', () => {
const prefix = { pl: 'artykuly' }
expect(buildLocalizedPath({ config, locale: 'pl', prefix, slugs })).toBe('/pl/artykuly/o-nas')
expect(buildLocalizedPath({ config, locale: 'en', prefix, slugs })).toBeUndefined()
})
test('unknown locale or missing slug → undefined', () => {
expect(buildLocalizedPath({ config, locale: 'de', slugs })).toBeUndefined()
expect(buildLocalizedPath({ config, locale: 'en', slugs: { pl: 'x' } })).toBeUndefined()
})
})
describe('switchLocalePath', () => {
test('never dead-ends: falls back to archive, then locale root', () => {
expect(switchLocalePath({ config, slugs: { pl: 'post' }, targetLocale: 'en', prefix: { en: 'articles', pl: 'artykuly' } })).toBe(
'/en/articles',
)
expect(switchLocalePath({ config, slugs: { pl: 'o-nas' }, targetLocale: 'en' })).toBe('/en')
})
})
describe('honeypot', () => {
test('filled → tripped', () => {
expect(isHoneypotTripped({ company_url: 'http://spam' })).toBe(true)
expect(isHoneypotTripped({ company_url: ' ' })).toBe(false)
expect(isHoneypotTripped({})).toBe(false)
})
})