|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { createMockRequest, setupCommonApiMocks } from '@sim/testing' |
| 5 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | + |
| 7 | +const handlerMocks = vi.hoisted(() => ({ |
| 8 | + betterAuthGET: vi.fn(), |
| 9 | + betterAuthPOST: vi.fn(), |
| 10 | + ensureAnonymousUserExists: vi.fn(), |
| 11 | + createAnonymousGetSessionResponse: vi.fn(() => ({ |
| 12 | + data: { |
| 13 | + user: { id: 'anon' }, |
| 14 | + session: { id: 'anon-session' }, |
| 15 | + }, |
| 16 | + })), |
| 17 | +})) |
| 18 | + |
| 19 | +vi.mock('better-auth/next-js', () => ({ |
| 20 | + toNextJsHandler: () => ({ |
| 21 | + GET: handlerMocks.betterAuthGET, |
| 22 | + POST: handlerMocks.betterAuthPOST, |
| 23 | + }), |
| 24 | +})) |
| 25 | + |
| 26 | +vi.mock('@/lib/auth', () => ({ |
| 27 | + auth: { handler: {} }, |
| 28 | +})) |
| 29 | + |
| 30 | +vi.mock('@/lib/auth/anonymous', () => ({ |
| 31 | + ensureAnonymousUserExists: handlerMocks.ensureAnonymousUserExists, |
| 32 | + createAnonymousGetSessionResponse: handlerMocks.createAnonymousGetSessionResponse, |
| 33 | +})) |
| 34 | + |
| 35 | +describe('auth catch-all route (DISABLE_AUTH get-session)', () => { |
| 36 | + beforeEach(() => { |
| 37 | + vi.resetModules() |
| 38 | + setupCommonApiMocks() |
| 39 | + handlerMocks.betterAuthGET.mockReset() |
| 40 | + handlerMocks.betterAuthPOST.mockReset() |
| 41 | + handlerMocks.ensureAnonymousUserExists.mockReset() |
| 42 | + handlerMocks.createAnonymousGetSessionResponse.mockClear() |
| 43 | + }) |
| 44 | + |
| 45 | + it('returns anonymous session in better-auth response envelope when auth is disabled', async () => { |
| 46 | + vi.doMock('@/lib/core/config/feature-flags', () => ({ isAuthDisabled: true })) |
| 47 | + |
| 48 | + const req = createMockRequest( |
| 49 | + 'GET', |
| 50 | + undefined, |
| 51 | + {}, |
| 52 | + 'http://localhost:3000/api/auth/get-session' |
| 53 | + ) |
| 54 | + const { GET } = await import('@/app/api/auth/[...all]/route') |
| 55 | + |
| 56 | + const res = await GET(req as any) |
| 57 | + const json = await res.json() |
| 58 | + |
| 59 | + expect(handlerMocks.ensureAnonymousUserExists).toHaveBeenCalledTimes(1) |
| 60 | + expect(handlerMocks.betterAuthGET).not.toHaveBeenCalled() |
| 61 | + expect(json).toEqual({ |
| 62 | + data: { |
| 63 | + user: { id: 'anon' }, |
| 64 | + session: { id: 'anon-session' }, |
| 65 | + }, |
| 66 | + }) |
| 67 | + }) |
| 68 | + |
| 69 | + it('delegates to better-auth handler when auth is enabled', async () => { |
| 70 | + vi.doMock('@/lib/core/config/feature-flags', () => ({ isAuthDisabled: false })) |
| 71 | + |
| 72 | + handlerMocks.betterAuthGET.mockResolvedValueOnce( |
| 73 | + new (await import('next/server')).NextResponse(JSON.stringify({ data: { ok: true } }), { |
| 74 | + headers: { 'content-type': 'application/json' }, |
| 75 | + }) as any |
| 76 | + ) |
| 77 | + |
| 78 | + const req = createMockRequest( |
| 79 | + 'GET', |
| 80 | + undefined, |
| 81 | + {}, |
| 82 | + 'http://localhost:3000/api/auth/get-session' |
| 83 | + ) |
| 84 | + const { GET } = await import('@/app/api/auth/[...all]/route') |
| 85 | + |
| 86 | + const res = await GET(req as any) |
| 87 | + const json = await res.json() |
| 88 | + |
| 89 | + expect(handlerMocks.ensureAnonymousUserExists).not.toHaveBeenCalled() |
| 90 | + expect(handlerMocks.betterAuthGET).toHaveBeenCalledTimes(1) |
| 91 | + expect(json).toEqual({ data: { ok: true } }) |
| 92 | + }) |
| 93 | +}) |
0 commit comments