/** * 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, 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([]) }) })