|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { createEnvMock, createMockRequest } from '@sim/testing' |
| 5 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | + |
| 7 | +const { |
| 8 | + mockGetSession, |
| 9 | + mockRegisterSSOProvider, |
| 10 | + mockHasSSOAccess, |
| 11 | + mockValidateUrlWithDNS, |
| 12 | + dbState, |
| 13 | + memberTable, |
| 14 | + ssoProviderTable, |
| 15 | +} = vi.hoisted(() => ({ |
| 16 | + mockGetSession: vi.fn(), |
| 17 | + mockRegisterSSOProvider: vi.fn(), |
| 18 | + mockHasSSOAccess: vi.fn(), |
| 19 | + mockValidateUrlWithDNS: vi.fn(), |
| 20 | + dbState: { members: [] as any[], providers: [] as any[] }, |
| 21 | + memberTable: { |
| 22 | + userId: 'member.userId', |
| 23 | + organizationId: 'member.organizationId', |
| 24 | + role: 'member.role', |
| 25 | + }, |
| 26 | + ssoProviderTable: { |
| 27 | + id: 'sso.id', |
| 28 | + providerId: 'sso.providerId', |
| 29 | + domain: 'sso.domain', |
| 30 | + issuer: 'sso.issuer', |
| 31 | + userId: 'sso.userId', |
| 32 | + organizationId: 'sso.organizationId', |
| 33 | + oidcConfig: 'sso.oidcConfig', |
| 34 | + samlConfig: 'sso.samlConfig', |
| 35 | + }, |
| 36 | +})) |
| 37 | + |
| 38 | +function makeBuilder(rows: any[]): any { |
| 39 | + const thenable: any = Promise.resolve(rows) |
| 40 | + thenable.where = () => makeBuilder(rows) |
| 41 | + thenable.limit = () => Promise.resolve(rows) |
| 42 | + thenable.orderBy = () => Promise.resolve(rows) |
| 43 | + return thenable |
| 44 | +} |
| 45 | + |
| 46 | +vi.mock('@sim/db', () => ({ |
| 47 | + db: { |
| 48 | + select: () => ({ |
| 49 | + from: (table: unknown) => |
| 50 | + makeBuilder(table === memberTable ? dbState.members : dbState.providers), |
| 51 | + }), |
| 52 | + }, |
| 53 | + member: memberTable, |
| 54 | + ssoProvider: ssoProviderTable, |
| 55 | +})) |
| 56 | + |
| 57 | +vi.mock('@/lib/auth', () => ({ |
| 58 | + getSession: mockGetSession, |
| 59 | + auth: { api: { registerSSOProvider: mockRegisterSSOProvider } }, |
| 60 | +})) |
| 61 | + |
| 62 | +vi.mock('@/lib/billing', () => ({ |
| 63 | + hasSSOAccess: mockHasSSOAccess, |
| 64 | +})) |
| 65 | + |
| 66 | +vi.mock('@/lib/auth/sso/domain', () => ({ |
| 67 | + normalizeSSODomain: (input: unknown): string | null => { |
| 68 | + if (typeof input !== 'string') return null |
| 69 | + const value = input.trim().toLowerCase() |
| 70 | + return /^[a-z0-9-]+(\.[a-z0-9-]+)+$/.test(value) ? value : null |
| 71 | + }, |
| 72 | +})) |
| 73 | + |
| 74 | +vi.mock('@/lib/core/security/input-validation.server', () => ({ |
| 75 | + validateUrlWithDNS: mockValidateUrlWithDNS, |
| 76 | + secureFetchWithPinnedIP: vi.fn(), |
| 77 | +})) |
| 78 | + |
| 79 | +vi.mock('@/lib/core/config/env', () => createEnvMock({ SSO_ENABLED: 'true' })) |
| 80 | + |
| 81 | +import { POST } from '@/app/api/auth/sso/register/route' |
| 82 | + |
| 83 | +const OIDC_BODY = { |
| 84 | + providerType: 'oidc' as const, |
| 85 | + providerId: 'acme-oidc', |
| 86 | + issuer: 'https://idp.acme.com', |
| 87 | + domain: 'acme.com', |
| 88 | + clientId: 'client-id', |
| 89 | + clientSecret: 'client-secret', |
| 90 | + authorizationEndpoint: 'https://idp.acme.com/authorize', |
| 91 | + tokenEndpoint: 'https://idp.acme.com/token', |
| 92 | + userInfoEndpoint: 'https://idp.acme.com/userinfo', |
| 93 | + jwksEndpoint: 'https://idp.acme.com/jwks', |
| 94 | +} |
| 95 | + |
| 96 | +function request(body: Record<string, unknown>) { |
| 97 | + return createMockRequest('POST', body) |
| 98 | +} |
| 99 | + |
| 100 | +describe('POST /api/auth/sso/register', () => { |
| 101 | + beforeEach(() => { |
| 102 | + vi.clearAllMocks() |
| 103 | + dbState.members = [] |
| 104 | + dbState.providers = [] |
| 105 | + mockGetSession.mockResolvedValue({ user: { id: 'u1' } }) |
| 106 | + mockHasSSOAccess.mockResolvedValue(true) |
| 107 | + mockValidateUrlWithDNS.mockResolvedValue({ isValid: true, resolvedIP: '1.2.3.4' }) |
| 108 | + mockRegisterSSOProvider.mockResolvedValue({ providerId: 'acme-oidc' }) |
| 109 | + }) |
| 110 | + |
| 111 | + it('rejects callers without an Enterprise plan', async () => { |
| 112 | + mockHasSSOAccess.mockResolvedValue(false) |
| 113 | + const res = await POST(request({ ...OIDC_BODY, orgId: 'org1' })) |
| 114 | + expect(res.status).toBe(403) |
| 115 | + expect(mockRegisterSSOProvider).not.toHaveBeenCalled() |
| 116 | + }) |
| 117 | + |
| 118 | + it('rejects callers who are not an admin/owner of the target org', async () => { |
| 119 | + dbState.members = [{ organizationId: 'org1', role: 'member' }] |
| 120 | + const res = await POST(request({ ...OIDC_BODY, orgId: 'org1' })) |
| 121 | + expect(res.status).toBe(403) |
| 122 | + expect(mockRegisterSSOProvider).not.toHaveBeenCalled() |
| 123 | + }) |
| 124 | + |
| 125 | + it('rejects an invalid domain', async () => { |
| 126 | + dbState.members = [{ organizationId: 'org1', role: 'owner' }] |
| 127 | + const res = await POST(request({ ...OIDC_BODY, domain: 'not-a-domain', orgId: 'org1' })) |
| 128 | + expect(res.status).toBe(400) |
| 129 | + expect(mockRegisterSSOProvider).not.toHaveBeenCalled() |
| 130 | + }) |
| 131 | + |
| 132 | + it('rejects a domain already registered by another organization', async () => { |
| 133 | + dbState.members = [{ organizationId: 'org-attacker', role: 'owner' }] |
| 134 | + dbState.providers = [{ domain: 'acme.com', userId: 'u-victim', organizationId: 'org-victim' }] |
| 135 | + const res = await POST(request({ ...OIDC_BODY, orgId: 'org-attacker' })) |
| 136 | + const json = await res.json() |
| 137 | + expect(res.status).toBe(409) |
| 138 | + expect(json.code).toBe('SSO_DOMAIN_ALREADY_REGISTERED') |
| 139 | + expect(mockRegisterSSOProvider).not.toHaveBeenCalled() |
| 140 | + }) |
| 141 | + |
| 142 | + it('matches conflicts across casing variants', async () => { |
| 143 | + dbState.members = [{ organizationId: 'org-attacker', role: 'owner' }] |
| 144 | + dbState.providers = [{ domain: 'ACME.com', userId: 'u-victim', organizationId: 'org-victim' }] |
| 145 | + const res = await POST(request({ ...OIDC_BODY, orgId: 'org-attacker' })) |
| 146 | + expect(res.status).toBe(409) |
| 147 | + expect(mockRegisterSSOProvider).not.toHaveBeenCalled() |
| 148 | + }) |
| 149 | + |
| 150 | + it('registers when the domain is unclaimed', async () => { |
| 151 | + dbState.members = [{ organizationId: 'org1', role: 'owner' }] |
| 152 | + const res = await POST(request({ ...OIDC_BODY, orgId: 'org1' })) |
| 153 | + expect(res.status).toBe(200) |
| 154 | + expect(mockRegisterSSOProvider).toHaveBeenCalledTimes(1) |
| 155 | + }) |
| 156 | + |
| 157 | + it('allows the owning tenant to update its own provider for the same domain', async () => { |
| 158 | + dbState.members = [{ organizationId: 'org1', role: 'owner' }] |
| 159 | + dbState.providers = [{ domain: 'acme.com', userId: 'u1', organizationId: 'org1' }] |
| 160 | + const res = await POST(request({ ...OIDC_BODY, orgId: 'org1' })) |
| 161 | + expect(res.status).toBe(200) |
| 162 | + expect(mockRegisterSSOProvider).toHaveBeenCalledTimes(1) |
| 163 | + }) |
| 164 | + |
| 165 | + it('normalizes the domain before persisting it', async () => { |
| 166 | + dbState.members = [{ organizationId: 'org1', role: 'owner' }] |
| 167 | + const res = await POST(request({ ...OIDC_BODY, domain: 'ACME.com', orgId: 'org1' })) |
| 168 | + expect(res.status).toBe(200) |
| 169 | + expect(mockRegisterSSOProvider).toHaveBeenCalledTimes(1) |
| 170 | + const config = mockRegisterSSOProvider.mock.calls[0][0].body |
| 171 | + expect(config.domain).toBe('acme.com') |
| 172 | + }) |
| 173 | +}) |
0 commit comments