Files
accounted/lib/sandbox/__tests__/guard.test.ts
T
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

60 lines
2.2 KiB
TypeScript

import { describe, it, expect, vi } from 'vitest'
import { isSandboxCompany, sandboxBlockedResponse, guardSandbox } from '../guard'
function mockSupabase(isSandboxValue: boolean | null) {
const maybeSingle = vi.fn().mockResolvedValue({
data: isSandboxValue === null ? null : { is_sandbox: isSandboxValue },
})
const eq = vi.fn(() => ({ maybeSingle }))
const select = vi.fn(() => ({ eq }))
const from = vi.fn(() => ({ select }))
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return { from } as any
}
describe('guardSandbox', () => {
it('returns null for non-sandbox companies so the route proceeds', async () => {
const supabase = mockSupabase(false)
const result = await guardSandbox(supabase, '00000000-0000-0000-0000-000000000001')
expect(result).toBeNull()
})
it('returns null when no company_settings row exists', async () => {
const supabase = mockSupabase(null)
const result = await guardSandbox(supabase, '00000000-0000-0000-0000-000000000001')
expect(result).toBeNull()
})
it('returns the 403 NextResponse for sandbox companies', async () => {
const supabase = mockSupabase(true)
const result = await guardSandbox(supabase, '00000000-0000-0000-0000-000000000001')
expect(result).not.toBeNull()
expect(result!.status).toBe(403)
const body = await result!.json()
expect(body.sandbox_blocked).toBe(true)
expect(body.error).toMatch(/sandlådan/i)
expect(body.error_en).toMatch(/sandbox/i)
})
})
describe('isSandboxCompany', () => {
it('returns false when is_sandbox is missing', async () => {
const supabase = mockSupabase(null)
expect(await isSandboxCompany(supabase, 'cid')).toBe(false)
})
it('returns true only when is_sandbox is exactly true', async () => {
expect(await isSandboxCompany(mockSupabase(true), 'cid')).toBe(true)
expect(await isSandboxCompany(mockSupabase(false), 'cid')).toBe(false)
})
})
describe('sandboxBlockedResponse', () => {
it('returns a 403 with sandbox_blocked envelope', async () => {
const res = sandboxBlockedResponse()
expect(res.status).toBe(403)
const body = await res.json()
expect(body.sandbox_blocked).toBe(true)
})
})