|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { createMockRequest } from '@sim/testing' |
| 5 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | + |
| 7 | +const mocks = vi.hoisted(() => ({ |
| 8 | + authenticate: vi.fn(), |
| 9 | + resolveContext: vi.fn(), |
| 10 | + resolveAtlassianCredential: vi.fn(), |
| 11 | +})) |
| 12 | + |
| 13 | +vi.mock('@/lib/selectors/server/resolve-authorized-context', () => ({ |
| 14 | + authenticateSelectorRequest: mocks.authenticate, |
| 15 | + resolveAuthorizedSelectorContext: mocks.resolveContext, |
| 16 | +})) |
| 17 | +vi.mock('@/lib/selectors/application/atlassian-credential', () => ({ |
| 18 | + resolveAtlassianSelectorCredential: mocks.resolveAtlassianCredential, |
| 19 | +})) |
| 20 | + |
| 21 | +import { POST as confluencePages } from '@/app/api/tools/confluence/selector-pages/route' |
| 22 | +import { POST as jiraProject } from '@/app/api/tools/jira/projects/route' |
| 23 | + |
| 24 | +const principal = { |
| 25 | + kind: 'session', |
| 26 | + userId: 'viewer-1', |
| 27 | + sessionId: 'session-1', |
| 28 | +} as const |
| 29 | + |
| 30 | +function request(path: string, body: unknown) { |
| 31 | + return createMockRequest( |
| 32 | + 'POST', |
| 33 | + body, |
| 34 | + { 'content-type': 'application/json' }, |
| 35 | + `http://localhost:3000${path}` |
| 36 | + ) |
| 37 | +} |
| 38 | + |
| 39 | +describe('server-resolved Atlassian selector routes', () => { |
| 40 | + beforeEach(() => { |
| 41 | + vi.clearAllMocks() |
| 42 | + vi.unstubAllGlobals() |
| 43 | + mocks.authenticate.mockResolvedValue({ ok: true, principal }) |
| 44 | + mocks.resolveContext.mockImplementation( |
| 45 | + async (_principal: unknown, input: { context: Record<string, unknown> }) => ({ |
| 46 | + ok: true, |
| 47 | + context: { ...input.context, domain: 'resolved-secret.example.com' }, |
| 48 | + requesterUserId: 'viewer-1', |
| 49 | + workspaceId: 'workspace-1', |
| 50 | + credentialAccess: { credentialOwnerUserId: 'owner-1' }, |
| 51 | + }) |
| 52 | + ) |
| 53 | + mocks.resolveAtlassianCredential.mockResolvedValue({ |
| 54 | + accessToken: 'atlassian-token', |
| 55 | + cloudId: 'cloud-id-1', |
| 56 | + }) |
| 57 | + }) |
| 58 | + |
| 59 | + it('authenticates before parsing a malformed request', async () => { |
| 60 | + mocks.authenticate.mockResolvedValue({ ok: false, status: 401, error: 'Unauthorized' }) |
| 61 | + |
| 62 | + const response = await jiraProject( |
| 63 | + request('/api/tools/jira/projects', { definitely: 'not a Jira selector request' }) |
| 64 | + ) |
| 65 | + |
| 66 | + expect(response.status).toBe(401) |
| 67 | + expect(await response.json()).toEqual({ error: 'Unauthorized' }) |
| 68 | + expect(mocks.resolveContext).not.toHaveBeenCalled() |
| 69 | + }) |
| 70 | + |
| 71 | + it.each([ |
| 72 | + { |
| 73 | + name: 'Jira Project', |
| 74 | + route: jiraProject, |
| 75 | + path: '/api/tools/jira/projects', |
| 76 | + body: { |
| 77 | + credential: 'credential-1', |
| 78 | + workflowId: 'workflow-1', |
| 79 | + domain: '{{INACCESSIBLE_SECRET}}', |
| 80 | + projectId: 'SIM', |
| 81 | + }, |
| 82 | + }, |
| 83 | + { |
| 84 | + name: 'Confluence Page', |
| 85 | + route: confluencePages, |
| 86 | + path: '/api/tools/confluence/selector-pages', |
| 87 | + body: { |
| 88 | + credential: 'credential-1', |
| 89 | + workflowId: 'workflow-1', |
| 90 | + domain: '{{INACCESSIBLE_SECRET}}', |
| 91 | + }, |
| 92 | + }, |
| 93 | + ])('$name stops inaccessible references before provider access', async (testCase) => { |
| 94 | + mocks.resolveContext.mockResolvedValue({ |
| 95 | + ok: false, |
| 96 | + status: 400, |
| 97 | + error: 'Unable to resolve selector configuration', |
| 98 | + }) |
| 99 | + const providerFetch = vi.fn() |
| 100 | + vi.stubGlobal('fetch', providerFetch) |
| 101 | + |
| 102 | + const response = await testCase.route(request(testCase.path, testCase.body)) |
| 103 | + |
| 104 | + expect(response.status).toBe(400) |
| 105 | + expect(await response.json()).toEqual({ error: 'Unable to resolve selector configuration' }) |
| 106 | + expect(mocks.resolveAtlassianCredential).not.toHaveBeenCalled() |
| 107 | + expect(providerFetch).not.toHaveBeenCalled() |
| 108 | + }) |
| 109 | + |
| 110 | + it('maps Jira projects without exposing resolved provider data', async () => { |
| 111 | + vi.stubGlobal( |
| 112 | + 'fetch', |
| 113 | + vi |
| 114 | + .fn() |
| 115 | + .mockResolvedValue( |
| 116 | + Response.json({ id: '10001', name: 'Sim', self: 'https://resolved-secret.example.com' }) |
| 117 | + ) |
| 118 | + ) |
| 119 | + |
| 120 | + const response = await jiraProject( |
| 121 | + request('/api/tools/jira/projects', { |
| 122 | + credential: 'credential-1', |
| 123 | + workflowId: 'workflow-1', |
| 124 | + domain: '{{DOMAIN}}', |
| 125 | + projectId: 'SIM', |
| 126 | + }) |
| 127 | + ) |
| 128 | + |
| 129 | + expect(await response.json()).toEqual({ project: { id: '10001', name: 'Sim' } }) |
| 130 | + }) |
| 131 | + |
| 132 | + it('maps Confluence pages without exposing resolved provider data', async () => { |
| 133 | + vi.stubGlobal( |
| 134 | + 'fetch', |
| 135 | + vi.fn().mockResolvedValue( |
| 136 | + Response.json({ |
| 137 | + results: [ |
| 138 | + { |
| 139 | + id: '20001', |
| 140 | + title: 'Runbook', |
| 141 | + _links: { webui: 'https://resolved-secret.example.com/wiki/runbook' }, |
| 142 | + }, |
| 143 | + ], |
| 144 | + }) |
| 145 | + ) |
| 146 | + ) |
| 147 | + |
| 148 | + const response = await confluencePages( |
| 149 | + request('/api/tools/confluence/selector-pages', { |
| 150 | + credential: 'credential-1', |
| 151 | + workflowId: 'workflow-1', |
| 152 | + domain: '{{DOMAIN}}', |
| 153 | + }) |
| 154 | + ) |
| 155 | + |
| 156 | + expect(await response.json()).toEqual({ files: [{ id: '20001', name: 'Runbook' }] }) |
| 157 | + }) |
| 158 | + |
| 159 | + it('maps provider failures to a stable public response without reading their body', async () => { |
| 160 | + const providerText = vi.fn().mockResolvedValue('provider-body-secret-marker') |
| 161 | + vi.stubGlobal( |
| 162 | + 'fetch', |
| 163 | + vi.fn().mockResolvedValue({ ok: false, status: 418, text: providerText }) |
| 164 | + ) |
| 165 | + |
| 166 | + const response = await jiraProject( |
| 167 | + request('/api/tools/jira/projects', { |
| 168 | + credential: 'credential-1', |
| 169 | + workflowId: 'workflow-1', |
| 170 | + domain: '{{DOMAIN}}', |
| 171 | + projectId: 'SIM', |
| 172 | + }) |
| 173 | + ) |
| 174 | + |
| 175 | + expect(response.status).toBe(502) |
| 176 | + expect(await response.json()).toEqual({ |
| 177 | + error: 'Jira selector discovery failed.', |
| 178 | + status: 502, |
| 179 | + }) |
| 180 | + expect(providerText).not.toHaveBeenCalled() |
| 181 | + }) |
| 182 | +}) |
0 commit comments