d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
140 lines
3.8 KiB
TypeScript
140 lines
3.8 KiB
TypeScript
/**
|
|
* @vitest-environment node
|
|
*/
|
|
import { createMockRequest } from '@sim/testing'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
const handlerMocks = vi.hoisted(() => ({
|
|
betterAuthGET: vi.fn(),
|
|
betterAuthPOST: vi.fn(),
|
|
ensureAnonymousUserExists: vi.fn(),
|
|
createAnonymousSession: vi.fn(() => ({
|
|
user: { id: 'anon' },
|
|
session: { id: 'anon-session' },
|
|
})),
|
|
isAuthDisabled: false,
|
|
}))
|
|
|
|
vi.mock('better-auth/next-js', () => ({
|
|
toNextJsHandler: () => ({
|
|
GET: handlerMocks.betterAuthGET,
|
|
POST: handlerMocks.betterAuthPOST,
|
|
}),
|
|
}))
|
|
|
|
vi.mock('@/lib/auth', () => ({
|
|
auth: { handler: {} },
|
|
}))
|
|
|
|
vi.mock('@/lib/auth/anonymous', () => ({
|
|
ensureAnonymousUserExists: handlerMocks.ensureAnonymousUserExists,
|
|
createAnonymousSession: handlerMocks.createAnonymousSession,
|
|
}))
|
|
|
|
vi.mock('@/lib/core/config/env-flags', () => ({
|
|
get isAuthDisabled() {
|
|
return handlerMocks.isAuthDisabled
|
|
},
|
|
}))
|
|
|
|
import { GET, POST } from '@/app/api/auth/[...all]/route'
|
|
|
|
describe('auth catch-all route (DISABLE_AUTH get-session)', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
handlerMocks.isAuthDisabled = false
|
|
})
|
|
|
|
it('returns anonymous session in better-auth response envelope when auth is disabled', async () => {
|
|
handlerMocks.isAuthDisabled = true
|
|
|
|
const req = createMockRequest(
|
|
'GET',
|
|
undefined,
|
|
{},
|
|
'http://localhost:3000/api/auth/get-session'
|
|
)
|
|
|
|
const res = await GET(req as any)
|
|
const json = await res.json()
|
|
|
|
expect(handlerMocks.ensureAnonymousUserExists).toHaveBeenCalledTimes(1)
|
|
expect(handlerMocks.betterAuthGET).not.toHaveBeenCalled()
|
|
expect(json).toEqual({
|
|
user: { id: 'anon' },
|
|
session: { id: 'anon-session' },
|
|
})
|
|
})
|
|
|
|
it('delegates to better-auth handler when auth is enabled', async () => {
|
|
handlerMocks.isAuthDisabled = false
|
|
|
|
const { NextResponse } = await import('next/server')
|
|
handlerMocks.betterAuthGET.mockResolvedValueOnce(
|
|
new NextResponse(JSON.stringify({ data: { ok: true } }), {
|
|
headers: { 'content-type': 'application/json' },
|
|
}) as any
|
|
)
|
|
|
|
const req = createMockRequest(
|
|
'GET',
|
|
undefined,
|
|
{},
|
|
'http://localhost:3000/api/auth/get-session'
|
|
)
|
|
|
|
const res = await GET(req as any)
|
|
const json = await res.json()
|
|
|
|
expect(handlerMocks.ensureAnonymousUserExists).not.toHaveBeenCalled()
|
|
expect(handlerMocks.betterAuthGET).toHaveBeenCalledTimes(1)
|
|
expect(json).toEqual({ data: { ok: true } })
|
|
})
|
|
})
|
|
|
|
describe('auth catch-all route organization mutations', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
it('blocks Better Auth organization mutation endpoints that bypass app lifecycle rules', async () => {
|
|
const req = createMockRequest(
|
|
'POST',
|
|
undefined,
|
|
{},
|
|
'http://localhost:3000/api/auth/organization/create'
|
|
)
|
|
|
|
const res = await POST(req as any)
|
|
const json = await res.json()
|
|
|
|
expect(res.status).toBe(404)
|
|
expect(handlerMocks.betterAuthPOST).not.toHaveBeenCalled()
|
|
expect(json).toEqual({
|
|
error: 'Organization mutations are handled by application API routes.',
|
|
})
|
|
})
|
|
|
|
it('allows safe Better Auth organization session endpoints', async () => {
|
|
const { NextResponse } = await import('next/server')
|
|
handlerMocks.betterAuthPOST.mockResolvedValueOnce(
|
|
new NextResponse(JSON.stringify({ data: { ok: true } }), {
|
|
headers: { 'content-type': 'application/json' },
|
|
}) as any
|
|
)
|
|
|
|
const req = createMockRequest(
|
|
'POST',
|
|
undefined,
|
|
{},
|
|
'http://localhost:3000/api/auth/organization/set-active'
|
|
)
|
|
|
|
const res = await POST(req as any)
|
|
const json = await res.json()
|
|
|
|
expect(handlerMocks.betterAuthPOST).toHaveBeenCalledTimes(1)
|
|
expect(json).toEqual({ data: { ok: true } })
|
|
})
|
|
})
|