|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { createMockRequest, setEnvFlags } from '@sim/testing' |
| 5 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | + |
| 7 | +const { mockGetSession, mockSignInSSO, mockIsAllowed, mockEnforceIpRateLimit } = vi.hoisted(() => ({ |
| 8 | + mockGetSession: vi.fn(), |
| 9 | + mockSignInSSO: vi.fn(), |
| 10 | + mockIsAllowed: vi.fn(), |
| 11 | + mockEnforceIpRateLimit: vi.fn(), |
| 12 | +})) |
| 13 | + |
| 14 | +vi.mock('@/lib/auth', () => ({ |
| 15 | + getSession: mockGetSession, |
| 16 | + auth: { api: { signInSSO: mockSignInSSO } }, |
| 17 | +})) |
| 18 | +vi.mock('@/lib/auth/sso/idp-initiated-login', () => ({ isIdpInitiatedLoginAllowed: mockIsAllowed })) |
| 19 | +vi.mock('@/lib/core/rate-limiter', () => ({ enforceIpRateLimit: mockEnforceIpRateLimit })) |
| 20 | + |
| 21 | +import { GET } from '@/app/(auth)/sso/launch/[providerId]/route' |
| 22 | + |
| 23 | +const context = { params: Promise.resolve({ providerId: 'acme-okta' }) } |
| 24 | +const ISSUER = 'https://acme.okta.test' |
| 25 | +const SIGN_IN_LINK = 'https://test.sim.ai/sso?provider=acme-okta' |
| 26 | + |
| 27 | +function open(search = `?iss=${encodeURIComponent(ISSUER)}`) { |
| 28 | + return GET( |
| 29 | + createMockRequest('GET', undefined, {}, `https://test.sim.ai/sso/launch/acme-okta${search}`), |
| 30 | + context |
| 31 | + ) |
| 32 | +} |
| 33 | + |
| 34 | +/** Better Auth answers with the authorization URL and the signed `state` cookie for it. */ |
| 35 | +function authorizationResponse() { |
| 36 | + return new Response(JSON.stringify({ url: 'https://acme.okta.test/oauth2/v1/authorize?x=1' }), { |
| 37 | + status: 200, |
| 38 | + headers: { 'content-type': 'application/json', 'set-cookie': 'sso_state=abc; Path=/' }, |
| 39 | + }) |
| 40 | +} |
| 41 | + |
| 42 | +describe('GET /sso/launch/[providerId]', () => { |
| 43 | + beforeEach(() => { |
| 44 | + vi.clearAllMocks() |
| 45 | + setEnvFlags({ isSsoEnabled: true }) |
| 46 | + mockGetSession.mockResolvedValue(null) |
| 47 | + mockIsAllowed.mockResolvedValue(true) |
| 48 | + mockEnforceIpRateLimit.mockResolvedValue(null) |
| 49 | + mockSignInSSO.mockResolvedValue(authorizationResponse()) |
| 50 | + }) |
| 51 | + |
| 52 | + it("redirects to the identity provider and carries Better Auth's state cookie", async () => { |
| 53 | + const response = await open() |
| 54 | + |
| 55 | + expect(response.status).toBe(307) |
| 56 | + expect(response.headers.get('location')).toBe('https://acme.okta.test/oauth2/v1/authorize?x=1') |
| 57 | + expect(response.headers.get('set-cookie')).toContain('sso_state=abc') |
| 58 | + expect(mockIsAllowed).toHaveBeenCalledWith('acme-okta', ISSUER) |
| 59 | + const [{ body }] = mockSignInSSO.mock.calls[0] |
| 60 | + expect(body.providerId).toBe('acme-okta') |
| 61 | + expect(body).not.toHaveProperty('email') |
| 62 | + /** The plugin appends `?error=…`, which must not corrupt the provider on the way back. */ |
| 63 | + const retry = new URL(`${body.errorCallbackURL}?error=invalid_provider`) |
| 64 | + expect(retry.pathname).toBe('/sso') |
| 65 | + expect(retry.searchParams.get('provider')).toBe('acme-okta') |
| 66 | + }) |
| 67 | + |
| 68 | + it('sends someone already signed in to the app without signing in again', async () => { |
| 69 | + mockGetSession.mockResolvedValue({ user: { id: 'user-1' } }) |
| 70 | + |
| 71 | + const response = await open() |
| 72 | + |
| 73 | + expect(response.headers.get('location')).toBe('https://test.sim.ai/home') |
| 74 | + expect(mockIsAllowed).not.toHaveBeenCalled() |
| 75 | + expect(mockSignInSSO).not.toHaveBeenCalled() |
| 76 | + }) |
| 77 | + |
| 78 | + it.each([ |
| 79 | + ['no issuer', '', () => undefined], |
| 80 | + [ |
| 81 | + 'an issuer the provider does not use', |
| 82 | + `?iss=${encodeURIComponent('https://other.test')}`, |
| 83 | + () => mockIsAllowed.mockResolvedValue(false), |
| 84 | + ], |
| 85 | + ])("sends a visitor with %s to the provider's sign-in link", async (_label, search, arrange) => { |
| 86 | + arrange() |
| 87 | + |
| 88 | + const response = await open(search) |
| 89 | + |
| 90 | + expect(response.headers.get('location')).toBe(SIGN_IN_LINK) |
| 91 | + expect(mockSignInSSO).not.toHaveBeenCalled() |
| 92 | + }) |
| 93 | + |
| 94 | + it("falls back to the provider's sign-in link when sign-in cannot start", async () => { |
| 95 | + mockSignInSSO.mockResolvedValue(new Response('{}', { status: 400 })) |
| 96 | + |
| 97 | + const response = await open() |
| 98 | + |
| 99 | + expect(response.headers.get('location')).toBe(SIGN_IN_LINK) |
| 100 | + }) |
| 101 | + |
| 102 | + it('sends a rate-limited visitor to the sign-in link before any lookup', async () => { |
| 103 | + mockEnforceIpRateLimit.mockResolvedValue(new Response(null, { status: 429 })) |
| 104 | + |
| 105 | + const response = await open() |
| 106 | + |
| 107 | + expect(response.headers.get('location')).toBe(SIGN_IN_LINK) |
| 108 | + expect(mockGetSession).not.toHaveBeenCalled() |
| 109 | + expect(mockIsAllowed).not.toHaveBeenCalled() |
| 110 | + }) |
| 111 | + |
| 112 | + it('leaves SSO off when the deployment has not enabled it', async () => { |
| 113 | + setEnvFlags({ isSsoEnabled: false }) |
| 114 | + |
| 115 | + const response = await open() |
| 116 | + |
| 117 | + expect(response.headers.get('location')).toBe('https://test.sim.ai/login') |
| 118 | + expect(mockEnforceIpRateLimit).not.toHaveBeenCalled() |
| 119 | + }) |
| 120 | +}) |
0 commit comments