|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { authMockFns } from '@sim/testing' |
| 5 | +import { NextRequest, NextResponse } from 'next/server' |
| 6 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | + |
| 8 | +const mocks = vi.hoisted(() => ({ list: vi.fn(), connect: vi.fn(), rateLimit: vi.fn() })) |
| 9 | + |
| 10 | +vi.mock('@/lib/core/rate-limiter', () => ({ |
| 11 | + enforceUserRateLimit: mocks.rateLimit, |
| 12 | + RateLimiter: class {}, |
| 13 | +})) |
| 14 | +vi.mock('@/lib/knowledge/application/github-installations', () => ({ |
| 15 | + listGitHubSearchInstallations: { |
| 16 | + operation: { id: 'knowledge.github.installations.list' }, |
| 17 | + execute: mocks.list, |
| 18 | + }, |
| 19 | + connectGitHubSearchInstallation: { |
| 20 | + operation: { id: 'knowledge.github.installations.connect' }, |
| 21 | + execute: mocks.connect, |
| 22 | + }, |
| 23 | +})) |
| 24 | +vi.mock('@/lib/oauth/github-installation', () => ({ |
| 25 | + GitHubInstallationError: class extends Error { |
| 26 | + constructor( |
| 27 | + message: string, |
| 28 | + readonly status?: number |
| 29 | + ) { |
| 30 | + super(message) |
| 31 | + } |
| 32 | + }, |
| 33 | +})) |
| 34 | +vi.mock('@/lib/credentials/managed-oauth', () => ({ |
| 35 | + ManagedOAuthCredentialError: class extends Error { |
| 36 | + constructor( |
| 37 | + readonly code: string, |
| 38 | + message: string, |
| 39 | + readonly statusCode: number |
| 40 | + ) { |
| 41 | + super(message) |
| 42 | + } |
| 43 | + }, |
| 44 | +})) |
| 45 | + |
| 46 | +import { OrchestrationError } from '@/lib/core/orchestration/types' |
| 47 | +import { ManagedOAuthCredentialError } from '@/lib/credentials/managed-oauth' |
| 48 | +import { GitHubInstallationError } from '@/lib/oauth/github-installation' |
| 49 | +import { GET, POST } from '@/app/api/knowledge/github/installations/route' |
| 50 | + |
| 51 | +const URL = 'http://localhost/api/knowledge/github/installations' |
| 52 | +const installation = { |
| 53 | + installationId: '123', |
| 54 | + accountId: '456', |
| 55 | + accountLogin: 'acme', |
| 56 | + accountType: 'Organization', |
| 57 | +} |
| 58 | + |
| 59 | +beforeEach(() => { |
| 60 | + vi.clearAllMocks() |
| 61 | + authMockFns.mockGetSession.mockResolvedValue({ |
| 62 | + user: { id: 'admin-1' }, |
| 63 | + session: { id: 'session-1' }, |
| 64 | + }) |
| 65 | + mocks.rateLimit.mockResolvedValue(null) |
| 66 | + mocks.list.mockResolvedValue({ |
| 67 | + available: true, |
| 68 | + installUrl: 'https://github.com/apps/sim-search/installations/new', |
| 69 | + needsUserConnection: false, |
| 70 | + installations: [installation], |
| 71 | + }) |
| 72 | + mocks.connect.mockResolvedValue({ credential: { id: 'cred-1', displayName: 'GitHub · acme' } }) |
| 73 | +}) |
| 74 | + |
| 75 | +describe('GitHub installation route boundary', () => { |
| 76 | + it.each(['GET', 'POST'] as const)( |
| 77 | + 'authenticates %s before parsing or calling the use case', |
| 78 | + async (method) => { |
| 79 | + authMockFns.mockGetSession.mockResolvedValue(null) |
| 80 | + const request = new NextRequest(URL, method === 'POST' ? { method, body: '{' } : undefined) |
| 81 | + const json = vi.spyOn(request, 'json') |
| 82 | + const response = await (method === 'GET' ? GET(request) : POST(request)) |
| 83 | + expect(response.status).toBe(401) |
| 84 | + expect(response.headers.get('Cache-Control')).toBe('private, no-store') |
| 85 | + expect(json).not.toHaveBeenCalled() |
| 86 | + expect(mocks.rateLimit).not.toHaveBeenCalled() |
| 87 | + expect(mocks.list).not.toHaveBeenCalled() |
| 88 | + expect(mocks.connect).not.toHaveBeenCalled() |
| 89 | + } |
| 90 | + ) |
| 91 | + |
| 92 | + it('applies admission before parsing the POST body', async () => { |
| 93 | + mocks.rateLimit.mockResolvedValue( |
| 94 | + NextResponse.json({ error: 'Rate limit exceeded' }, { status: 429 }) |
| 95 | + ) |
| 96 | + const request = new NextRequest(URL, { method: 'POST', body: '{' }) |
| 97 | + const json = vi.spyOn(request, 'json') |
| 98 | + expect((await POST(request)).status).toBe(429) |
| 99 | + expect(json).not.toHaveBeenCalled() |
| 100 | + expect(mocks.connect).not.toHaveBeenCalled() |
| 101 | + expect(mocks.rateLimit).toHaveBeenCalledWith( |
| 102 | + 'github-search-installations', |
| 103 | + 'admin-1', |
| 104 | + undefined |
| 105 | + ) |
| 106 | + }) |
| 107 | + |
| 108 | + it.each(['0', '-1', '1.5', '123/path', ''])( |
| 109 | + 'rejects invalid installation ID %s before the use case', |
| 110 | + async (installationId) => { |
| 111 | + const response = await POST( |
| 112 | + new NextRequest(URL, { |
| 113 | + method: 'POST', |
| 114 | + headers: { 'Content-Type': 'application/json' }, |
| 115 | + body: JSON.stringify({ organizationId: 'org-1', installationId }), |
| 116 | + }) |
| 117 | + ) |
| 118 | + expect(response.status).toBe(400) |
| 119 | + expect(mocks.connect).not.toHaveBeenCalled() |
| 120 | + } |
| 121 | + ) |
| 122 | + |
| 123 | + it('requires organization scope for GET', async () => { |
| 124 | + expect((await GET(new NextRequest(URL))).status).toBe(400) |
| 125 | + expect(mocks.list).not.toHaveBeenCalled() |
| 126 | + }) |
| 127 | + |
| 128 | + it('forwards GET identity and cancellation and projects a private installation list', async () => { |
| 129 | + const controller = new AbortController() |
| 130 | + const request = new NextRequest(`${URL}?organizationId=org-1`, { signal: controller.signal }) |
| 131 | + mocks.list.mockResolvedValue({ |
| 132 | + available: true, |
| 133 | + installUrl: 'https://github.com/apps/sim-search/installations/new', |
| 134 | + needsUserConnection: false, |
| 135 | + installations: [{ ...installation, accessToken: 'private' }], |
| 136 | + privateKey: 'private', |
| 137 | + }) |
| 138 | + const response = await GET(request) |
| 139 | + expect(response.status).toBe(200) |
| 140 | + expect(response.headers.get('Cache-Control')).toBe('private, no-store') |
| 141 | + expect(mocks.list).toHaveBeenCalledWith( |
| 142 | + expect.objectContaining({ |
| 143 | + principal: { kind: 'session', userId: 'admin-1', sessionId: 'session-1' }, |
| 144 | + input: { organizationId: 'org-1', signal: request.signal }, |
| 145 | + }) |
| 146 | + ) |
| 147 | + expect(await response.json()).toEqual({ |
| 148 | + success: true, |
| 149 | + available: true, |
| 150 | + installUrl: 'https://github.com/apps/sim-search/installations/new', |
| 151 | + needsUserConnection: false, |
| 152 | + installations: [installation], |
| 153 | + }) |
| 154 | + }) |
| 155 | + |
| 156 | + it('forwards POST cancellation and only returns the safe credential projection', async () => { |
| 157 | + const request = new NextRequest(URL, { |
| 158 | + method: 'POST', |
| 159 | + headers: { 'Content-Type': 'application/json' }, |
| 160 | + body: JSON.stringify({ organizationId: 'org-1', installationId: '123' }), |
| 161 | + }) |
| 162 | + mocks.connect.mockResolvedValue({ |
| 163 | + credential: { |
| 164 | + id: 'cred-1', |
| 165 | + displayName: 'GitHub · acme', |
| 166 | + encryptedServiceAccountKey: 'private', |
| 167 | + }, |
| 168 | + created: true, |
| 169 | + }) |
| 170 | + const response = await POST(request) |
| 171 | + expect(response.status).toBe(200) |
| 172 | + expect(response.headers.get('Cache-Control')).toBe('private, no-store') |
| 173 | + expect(mocks.connect).toHaveBeenCalledWith( |
| 174 | + expect.objectContaining({ |
| 175 | + input: { organizationId: 'org-1', installationId: '123', signal: request.signal }, |
| 176 | + }) |
| 177 | + ) |
| 178 | + expect(await response.json()).toEqual({ |
| 179 | + success: true, |
| 180 | + credential: { id: 'cred-1', displayName: 'GitHub · acme' }, |
| 181 | + }) |
| 182 | + }) |
| 183 | + |
| 184 | + it.each([ |
| 185 | + [ |
| 186 | + new OrchestrationError('forbidden', 'Organization administrator access is required'), |
| 187 | + 403, |
| 188 | + 'Organization administrator access is required', |
| 189 | + ], |
| 190 | + [ |
| 191 | + new GitHubInstallationError('Installation permission denied', 403), |
| 192 | + 403, |
| 193 | + 'Installation permission denied', |
| 194 | + ], |
| 195 | + [ |
| 196 | + new GitHubInstallationError('GitHub is temporarily unavailable', 503), |
| 197 | + 502, |
| 198 | + 'GitHub is temporarily unavailable', |
| 199 | + ], |
| 200 | + [ |
| 201 | + new ManagedOAuthCredentialError( |
| 202 | + 'MANAGED_CREDENTIAL_NEEDS_REAUTH', |
| 203 | + 'private refresh details', |
| 204 | + 401 |
| 205 | + ), |
| 206 | + 401, |
| 207 | + 'Reconnect your GitHub account to continue installation setup', |
| 208 | + ], |
| 209 | + [new Error('private database details'), 500, 'Internal server error'], |
| 210 | + ] as const)( |
| 211 | + 'projects %s without successful installation data', |
| 212 | + async (error, status, message) => { |
| 213 | + mocks.list.mockRejectedValue(error) |
| 214 | + const response = await GET(new NextRequest(`${URL}?organizationId=org-1`)) |
| 215 | + expect(response.status).toBe(status) |
| 216 | + expect(response.headers.get('Cache-Control')).toBe('private, no-store') |
| 217 | + const body = await response.json() |
| 218 | + expect(body.error).toBe(message) |
| 219 | + expect(body).not.toHaveProperty('installations') |
| 220 | + expect(body).not.toHaveProperty('credential') |
| 221 | + } |
| 222 | + ) |
| 223 | +}) |
0 commit comments