Files
accounted/lib/auth/__tests__/turnstile.test.ts
T
Goostaf 6ac9679fb5 feat(auth): base available login methods off GoTrue providers (#1869)
* feat(auth): base login fields on GoTrue providers

Signed-off-by: Goostaf <gasplund2@gmail.com>

# Conflicts:
#	app/(auth)/login/login-client.tsx
#	app/(auth)/register/page.tsx

* fix: address feedback

Signed-off-by: Goostaf <gasplund2@gmail.com>

* chore: remove hardcoded Google enabled checks

Signed-off-by: Goostaf <gasplund2@gmail.com>

# Conflicts:
#	.env.example

* feat: show label when password login is disabled

Signed-off-by: Goostaf <gasplund2@gmail.com>

* feat: use MicrosoftMark, correct comment

Signed-off-by: Goostaf <gasplund2@gmail.com>

* feat: display custom providers

Signed-off-by: Goostaf <gasplund2@gmail.com>

* feat: show when no methods are available

Signed-off-by: Goostaf <gasplund2@gmail.com>

* feat: display custom provider labels

Signed-off-by: Goostaf <gasplund2@gmail.com>

* feat: add SAML login path

Signed-off-by: Goostaf <gasplund2@gmail.com>

* feat: show when no methods are available

Signed-off-by: Goostaf <gasplund2@gmail.com>

* fix: display SAML button when enabled

Signed-off-by: Goostaf <gasplund2@gmail.com>

* fix: preserve nextPath and broken key

Signed-off-by: Goostaf <gasplund2@gmail.com>

* fix: redirect test to client

Signed-off-by: Goostaf <gasplund2@gmail.com>

* fix: expose registerEnabled

Signed-off-by: Goostaf <gasplund2@gmail.com>

* feat: provider allowlist and request timeout

Signed-off-by: Goostaf <gasplund2@gmail.com>

* fix: restore compact labels

Signed-off-by: Goostaf <gasplund2@gmail.com>

* refactor: move withTimeout implementation to utils

Signed-off-by: Goostaf <gasplund2@gmail.com>

* fix: include nextPath

Signed-off-by: Goostaf <gasplund2@gmail.com>

* fix: export function and test case

Signed-off-by: Goostaf <gasplund2@gmail.com>

* feat: only show SAML button if vars configured

Signed-off-by: Goostaf <gasplund2@gmail.com>

* fix(auth): map SAML sign-in error through getErrorMessage

The antipattern ratchet (check:guards, raw-user-error) rejects a raw
error.message reaching a user-visible sink. Route the signInWithSSO
error through getErrorMessage like the other auth error paths.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013BAzJjXQBa9F5L1U42wUMj
Signed-off-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>

* feat(auth): GitHub brand mark on the provider button; decision log

GitHub allows its invertocat in solid black/white, so currentColor is
correct; custom OIDC providers keep the generic key icon.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013BAzJjXQBa9F5L1U42wUMj
Signed-off-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>

---------

Signed-off-by: Goostaf <gasplund2@gmail.com>
Signed-off-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-31 09:13:39 +01:00

136 lines
5.3 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { readFileSync } from 'node:fs'
import path from 'node:path'
import { eventBus } from '@/lib/events/bus'
import {
captchaTokenOptions,
getTurnstileRolloutState,
isTurnstileSubmissionBlocked,
resolveTurnstileSiteKey,
} from '../turnstile'
const readRepoFile = (file: string) =>
readFileSync(path.join(process.cwd(), file), 'utf8')
beforeEach(() => {
vi.clearAllMocks()
eventBus.clear()
})
afterEach(() => {
vi.unstubAllEnvs()
})
describe('Turnstile rollout state', () => {
it('keeps Auth available while the public site key is absent', () => {
expect(resolveTurnstileSiteKey(undefined)).toBeNull()
expect(resolveTurnstileSiteKey('')).toBeNull()
expect(resolveTurnstileSiteKey(' ')).toBeNull()
expect(getTurnstileRolloutState(undefined)).toBe('disabled')
expect(isTurnstileSubmissionBlocked(null, undefined)).toBe(false)
})
it('treats an unsubstituted Docker sentinel as disabled', () => {
const sentinel = '__NEXT_PUBLIC_TURNSTILE_SITE_KEY__'
expect(resolveTurnstileSiteKey(sentinel)).toBeNull()
expect(getTurnstileRolloutState(sentinel)).toBe('disabled')
expect(isTurnstileSubmissionBlocked(null, sentinel)).toBe(false)
})
it('fails closed after the client site key is configured', () => {
const siteKey = ' public-site-key '
expect(resolveTurnstileSiteKey(siteKey)).toBe('public-site-key')
expect(getTurnstileRolloutState(siteKey)).toBe('client-enabled')
expect(isTurnstileSubmissionBlocked(null, siteKey)).toBe(true)
expect(isTurnstileSubmissionBlocked('', siteKey)).toBe(true)
expect(isTurnstileSubmissionBlocked('verified-token', siteKey)).toBe(false)
})
it('reads the runtime-substituted environment value at call time', () => {
vi.stubEnv('NEXT_PUBLIC_TURNSTILE_SITE_KEY', '')
expect(getTurnstileRolloutState()).toBe('disabled')
vi.stubEnv('NEXT_PUBLIC_TURNSTILE_SITE_KEY', 'runtime-site-key')
expect(getTurnstileRolloutState()).toBe('client-enabled')
})
it('forwards only a non-empty token to Supabase Auth', () => {
expect(captchaTokenOptions(null)).toEqual({})
expect(captchaTokenOptions(undefined)).toEqual({})
expect(captchaTokenOptions(' ')).toEqual({})
expect(captchaTokenOptions(' token-value ')).toEqual({
captchaToken: 'token-value',
})
})
})
describe('Turnstile integration contract', () => {
it('protects every public Supabase Auth flow in scope', () => {
const login = readRepoFile('app/(auth)/login/login-client.tsx')
const register = readRepoFile('app/(auth)/register/register-client.tsx')
const sandbox = readRepoFile('app/sandbox/page.tsx')
expect(login).toMatch(
/signInWithPassword\([\s\S]*?options: captchaTokenOptions\(passwordCaptchaToken\)/,
)
expect(login).toMatch(
/resetPasswordForEmail\([\s\S]*?captchaTokenOptions\(resetCaptchaToken\)/,
)
expect(login).toContain('action="accounted_login"')
expect(login).toContain('action="accounted_password_reset"')
// The register page's email flow moved server-side (invite-only brand
// domain gate, 2026-08-27): the captcha token must travel to
// POST /api/auth/signup, and that route must forward it into the GoTrue
// signUp call, so the CAPTCHA still guards the flow end to end.
expect(register).toMatch(
/fetch\('\/api\/auth\/signup'[\s\S]*?captchaTokenOptions\(captchaToken\)/,
)
expect(register).toContain('action="accounted_signup"')
const signupRoute = readRepoFile('app/api/auth/signup/route.ts')
expect(signupRoute).toMatch(/signUp\(\{[\s\S]*?captchaToken/)
expect(sandbox).toMatch(
/signInAnonymously\([\s\S]*?captchaTokenOptions\(captchaToken\)/,
)
expect(sandbox).toContain('action="accounted_sandbox"')
})
it('keeps the public key, CSP, and Docker runtime contract in sync', () => {
const envExample = readRepoFile('.env.example')
const dockerEnvExample = readRepoFile('.env.docker.example')
const dockerfile = readRepoFile('Dockerfile')
const entrypoint = readRepoFile('docker-entrypoint.sh')
const nextConfig = readRepoFile('next.config.ts')
expect(envExample).toContain('NEXT_PUBLIC_TURNSTILE_SITE_KEY=')
expect(dockerEnvExample).toContain('NEXT_PUBLIC_TURNSTILE_SITE_KEY=')
expect(dockerfile).toContain(
'NEXT_PUBLIC_TURNSTILE_SITE_KEY=__NEXT_PUBLIC_TURNSTILE_SITE_KEY__',
)
expect(entrypoint).toContain('__NEXT_PUBLIC_TURNSTILE_SITE_KEY__')
expect(nextConfig).toContain('https://challenges.cloudflare.com')
expect(nextConfig).toMatch(/script-src[\s\S]*?turnstileOrigin/)
expect(nextConfig).toMatch(/frame-src[\s\S]*?turnstileOrigin/)
expect(envExample).not.toContain('TURNSTILE_SECRET_KEY')
expect(dockerEnvExample).not.toContain('TURNSTILE_SECRET_KEY')
})
it('ships matching Swedish and English challenge messages', () => {
const swedish = JSON.parse(readRepoFile('messages/sv.json')).auth
const english = JSON.parse(readRepoFile('messages/en.json')).auth
const keys = [
'turnstile_checking',
'turnstile_required',
'turnstile_error',
]
for (const key of keys) {
expect(swedish[key]).toBeTypeOf('string')
expect(swedish[key]).not.toBe('')
expect(english[key]).toBeTypeOf('string')
expect(english[key]).not.toBe('')
}
})
})