|
| 1 | +/** |
| 2 | + * Drives the Assistant's GitHub token resolution through the real token resolver |
| 3 | + * (`lib/oauth/token-resolution`), so the tool policy it binds against is the one the |
| 4 | + * resolver actually reads, not a mock's. |
| 5 | + */ |
| 6 | +import { auditMock } from '@sim/testing/mocks/audit.mock' |
| 7 | +import { authOAuthUtilsMock, authOAuthUtilsMockFns } from '@sim/testing/mocks/auth-oauth-utils.mock' |
| 8 | +import { posthogServerMock } from '@sim/testing/mocks/posthog-server.mock' |
| 9 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 10 | + |
| 11 | +const { mockAuthorizeCredentialUseForAuth, mockPersonalCredentialUseCase } = vi.hoisted(() => ({ |
| 12 | + mockAuthorizeCredentialUseForAuth: vi.fn(), |
| 13 | + mockPersonalCredentialUseCase: vi.fn(), |
| 14 | +})) |
| 15 | + |
| 16 | +vi.mock('@sim/audit', () => auditMock) |
| 17 | +vi.mock('@/lib/posthog/server', () => posthogServerMock) |
| 18 | +vi.mock('@/lib/oauth/credential-service', () => authOAuthUtilsMock) |
| 19 | +vi.mock('@/lib/auth/credential-access', () => ({ |
| 20 | + authorizeCredentialUseForAuth: mockAuthorizeCredentialUseForAuth, |
| 21 | +})) |
| 22 | +vi.mock('@/lib/mothership/application/execute-credential-use-case', () => ({ |
| 23 | + executeCopilotCredentialUseCase: mockPersonalCredentialUseCase, |
| 24 | +})) |
| 25 | +vi.unmock('@/lib/oauth/utils') |
| 26 | + |
| 27 | +import { resolveExecutorCredentialToken } from '@/executor/utils/credential-token' |
| 28 | +import { getToolMetadata } from '@/tools/metadata' |
| 29 | + |
| 30 | +const { mockGetCredential, mockRefreshTokenIfNeeded, mockResolveOAuthAccountId } = |
| 31 | + authOAuthUtilsMockFns |
| 32 | + |
| 33 | +const ASSISTANT = { |
| 34 | + userId: 'person', |
| 35 | + workspaceId: 'ws-1', |
| 36 | + chatId: 'chat-1', |
| 37 | + toolCallId: 'call-1', |
| 38 | + copilotToolExecution: true, |
| 39 | + requestMode: 'assistant' as const, |
| 40 | +} |
| 41 | + |
| 42 | +function resolve(overrides: Partial<Parameters<typeof resolveExecutorCredentialToken>[0]> = {}) { |
| 43 | + return resolveExecutorCredentialToken({ |
| 44 | + requestId: 'req-1', |
| 45 | + credentialId: 'own-account', |
| 46 | + userId: 'person', |
| 47 | + toolId: 'github_search_users_v2', |
| 48 | + copilotExecutionContext: ASSISTANT, |
| 49 | + ...overrides, |
| 50 | + }) |
| 51 | +} |
| 52 | + |
| 53 | +describe('Assistant GitHub calls through the real token resolver', () => { |
| 54 | + beforeEach(async () => { |
| 55 | + const actual = await vi.importActual<typeof import('@/tools/metadata')>('@/tools/metadata') |
| 56 | + vi.mocked(getToolMetadata).mockImplementation(actual.getToolMetadata) |
| 57 | + mockPersonalCredentialUseCase.mockResolvedValue({ id: 'own-account' }) |
| 58 | + mockAuthorizeCredentialUseForAuth.mockResolvedValue({ |
| 59 | + ok: true, |
| 60 | + requesterUserId: 'person', |
| 61 | + credentialOwnerUserId: 'person', |
| 62 | + workspaceId: 'ws-1', |
| 63 | + resolvedCredentialId: 'own-account', |
| 64 | + }) |
| 65 | + mockRefreshTokenIfNeeded.mockResolvedValue({ accessToken: 'personal-github-oauth' }) |
| 66 | + }) |
| 67 | + |
| 68 | + it("resolves the person's own GitHub account for a GitHub tool", async () => { |
| 69 | + mockResolveOAuthAccountId.mockResolvedValue({ |
| 70 | + credentialType: 'oauth', |
| 71 | + credentialId: 'own-account', |
| 72 | + providerId: 'github-repositories', |
| 73 | + accountId: 'account-1', |
| 74 | + workspaceId: 'ws-1', |
| 75 | + usedCredentialTable: true, |
| 76 | + }) |
| 77 | + mockGetCredential.mockResolvedValue({ providerId: 'github-repositories', scope: 'repo' }) |
| 78 | + |
| 79 | + await expect(resolve()).resolves.toMatchObject({ accessToken: 'personal-github-oauth' }) |
| 80 | + }) |
| 81 | + |
| 82 | + it('never hands the Assistant a saved workspace GitHub token', async () => { |
| 83 | + mockResolveOAuthAccountId.mockResolvedValue({ |
| 84 | + credentialType: 'service_account', |
| 85 | + credentialId: 'workspace-token', |
| 86 | + providerId: 'github-service-account', |
| 87 | + accountId: '', |
| 88 | + workspaceId: 'ws-1', |
| 89 | + usedCredentialTable: true, |
| 90 | + }) |
| 91 | + |
| 92 | + await expect(resolve({ credentialId: 'workspace-token' })).rejects.toThrow( |
| 93 | + 'The selected credential type is not supported by this tool' |
| 94 | + ) |
| 95 | + expect(authOAuthUtilsMockFns.mockResolveServiceAccountToken).not.toHaveBeenCalled() |
| 96 | + }) |
| 97 | + |
| 98 | + it("does not let a workflow run bind GitHub tools to a person's GitHub account", async () => { |
| 99 | + mockResolveOAuthAccountId.mockResolvedValue({ |
| 100 | + credentialType: 'oauth', |
| 101 | + credentialId: 'own-account', |
| 102 | + providerId: 'github-repositories', |
| 103 | + accountId: 'account-1', |
| 104 | + workspaceId: 'ws-1', |
| 105 | + usedCredentialTable: true, |
| 106 | + }) |
| 107 | + mockGetCredential.mockResolvedValue({ providerId: 'github-repositories', scope: 'repo' }) |
| 108 | + |
| 109 | + await expect(resolve({ copilotExecutionContext: undefined })).rejects.toThrow( |
| 110 | + 'The selected credential type is not supported by this tool' |
| 111 | + ) |
| 112 | + expect(mockRefreshTokenIfNeeded).not.toHaveBeenCalled() |
| 113 | + }) |
| 114 | +}) |
0 commit comments