Files
ipal-kit/test/m1TurnstileEnv.test.ts
Radosław SmolińskiandClaude Fable 5.2 42fdad8c2d M1 (2, 3): the Turnstile env secret is deprecated; doctor rules access-missing, turnstile-degraded, turnstile-env-key
(2) Stage one of the deprecation: TURNSTILE_SECRET_KEY from the environment still
works when the panel field is empty, logs once that it is deprecated, and the
doctor's deep status reports it (rule turnstile-env-key, warning). Nothing is
removed.
(3) access-missing (static, warning): ipalKit without `access` means every panel
account is an admin. turnstile-degraded (deep): Turnstile is not checking forms —
warning, error after a day. The deep status carries the recorded state and where
the secret comes from, never a key.

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

61 lines
3.1 KiB
TypeScript

/**
* M1: the Turnstile secret key from the environment is deprecated — it still works, and says so once.
* Cloudflare is the mocked `fetch`.
*/
import { afterEach, describe, expect, test, vi } from 'vitest'
import { passTurnstile } from '../src/modules/forms/submitForm.js'
import { getTurnstileConfig, turnstileSecretFromEnv } from '../src/modules/turnstile/config.js'
vi.mock('server-only', () => ({}))
type Option = 'allow' | 'reject' | { files?: 'allow' | 'reject'; text?: 'allow' | 'reject' } | undefined
const payloadWith = (integrations: Record<string, unknown>, option?: Option) => ({
config: { custom: { ipal: { forms: option ? { turnstileOnConfigError: option } : {} } }, globals: [{ slug: 'site-integrations' }] },
findGlobal: async () => integrations,
logger: { error: () => undefined, info: () => undefined, warn: () => undefined },
updateGlobal: async () => ({}),
})
const KEYS = { turnstileSecretKey: 'secret', turnstileSiteKey: 'site' }
const cloudflare = (body: unknown, status = 200) => vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify(body), { status })))
afterEach(() => {
vi.unstubAllGlobals()
vi.unstubAllEnvs()
vi.restoreAllMocks()
})
describe('the Turnstile secret from the environment (deprecated)', () => {
test('turnstileSecretFromEnv: only when the panel field is empty and the variable is set', () => {
vi.stubEnv('TURNSTILE_SECRET_KEY', 'from-env')
expect(turnstileSecretFromEnv({})).toBe(true)
expect(turnstileSecretFromEnv({ turnstileSecretKey: ' ' })).toBe(true)
expect(turnstileSecretFromEnv({ turnstileSecretKey: 'from-panel' })).toBe(false)
vi.stubEnv('TURNSTILE_SECRET_KEY', '')
expect(turnstileSecretFromEnv({})).toBe(false)
})
test('it still works, and says once that it is deprecated', async () => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined)
vi.stubEnv('TURNSTILE_SECRET_KEY', 'from-env')
const payload = payloadWith({ turnstileSiteKey: 'site' })
expect(await getTurnstileConfig(payload as never)).toEqual({ enabled: true, siteKey: 'site' })
expect(await getTurnstileConfig(payload as never)).toEqual({ enabled: true, siteKey: 'site' })
const lines = warn.mock.calls.map((c) => String(c[0])).filter((l) => l.includes('Deprecated'))
expect(lines).toHaveLength(1)
expect(lines[0]).toMatch(/TURNSTILE_SECRET_KEY.*Site integrations.*keeps working for now/)
// the check itself runs with the env secret
cloudflare({ success: true })
expect(await passTurnstile({ payload: payload as never, turnstileToken: 'token' })).toBeNull()
const sent = String((vi.mocked(fetch).mock.calls[0] as unknown as [string, { body: URLSearchParams }])[1].body)
expect(sent).toContain('secret=from-env')
})
test('with the key in the panel there is nothing to say', async () => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined)
vi.stubEnv('TURNSTILE_SECRET_KEY', 'from-env')
await getTurnstileConfig(payloadWith(KEYS) as never)
expect(warn.mock.calls.map((c) => String(c[0])).filter((l) => l.includes('Deprecated'))).toEqual([])
})
})