|
| 1 | +import { createSessionPrincipal } from '@sim/testing/factories/principal.factory' |
| 2 | +import { blockVisibilityMock } from '@sim/testing/mocks/block-visibility.mock' |
| 3 | +import { workspaceAuthzMock, workspaceAuthzMockFns } from '@sim/testing/mocks/workspace-authz.mock' |
| 4 | +import { |
| 5 | + workspaceContextMock, |
| 6 | + workspaceContextMockFns, |
| 7 | +} from '@sim/testing/mocks/workspace-context.mock' |
| 8 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 9 | + |
| 10 | +const hoisted = vi.hoisted(() => ({ |
| 11 | + allowedIntegrationTypes: vi.fn(), |
| 12 | +})) |
| 13 | + |
| 14 | +vi.mock('@/lib/core/config/block-visibility', () => blockVisibilityMock) |
| 15 | +vi.mock('@/lib/workspaces/application/workspace-context', () => workspaceContextMock) |
| 16 | +vi.mock('@sim/platform-authz/workspace', () => workspaceAuthzMock) |
| 17 | +vi.mock('@/lib/integrations/principal-scope.server', () => ({ |
| 18 | + principalUserId: () => 'user-1', |
| 19 | + allowedIntegrationTypes: hoisted.allowedIntegrationTypes, |
| 20 | + allowedOrganizationIntegrationTypes: hoisted.allowedIntegrationTypes, |
| 21 | +})) |
| 22 | + |
| 23 | +import { v2ListCredentialProvidersContract } from '@/lib/api/contracts/v2/credentials' |
| 24 | +import { listCredentialProviders } from '@/lib/credentials/application/list-credential-providers' |
| 25 | +import { listCredentialProviderCatalog } from '@/lib/credentials/application/provider-catalog' |
| 26 | + |
| 27 | +const CLAUDE_PROVIDER_ID = 'claude-platform-service-account' |
| 28 | +const GITHUB_INSTALLATION_PROVIDER_ID = 'github-app-installation' |
| 29 | +const context = { workspaceId: 'workspace-1', workspaceOrganizationId: null } |
| 30 | +const responseSchema = v2ListCredentialProvidersContract.response.schema |
| 31 | + |
| 32 | +async function listProviders(search?: string) { |
| 33 | + const { providers } = await listCredentialProviders.execute({ |
| 34 | + principal: createSessionPrincipal(), |
| 35 | + input: { workspaceId: 'workspace-1', ...(search ? { search } : {}) }, |
| 36 | + }) |
| 37 | + return providers |
| 38 | +} |
| 39 | + |
| 40 | +describe('v2 credential provider catalog contract', () => { |
| 41 | + beforeEach(() => { |
| 42 | + hoisted.allowedIntegrationTypes.mockResolvedValue(null) |
| 43 | + workspaceContextMockFns.mockLoadActiveWorkspaceApplicationContext.mockResolvedValue({ |
| 44 | + ...context, |
| 45 | + allowPersonalApiKeys: true, |
| 46 | + billedAccountUserId: 'billing-owner-1', |
| 47 | + }) |
| 48 | + workspaceAuthzMockFns.mockResolveEffectiveWorkspacePermission.mockResolvedValue('read') |
| 49 | + }) |
| 50 | + |
| 51 | + it('presents the full unfiltered catalog as a valid response', async () => { |
| 52 | + const providers = await listProviders() |
| 53 | + |
| 54 | + const parsed = responseSchema.safeParse({ data: providers, nextCursor: null }) |
| 55 | + expect(parsed.success ? [] : parsed.error.issues).toEqual([]) |
| 56 | + expect( |
| 57 | + providers.some( |
| 58 | + (provider) => |
| 59 | + provider.type === 'service_account' && |
| 60 | + provider.providerId === GITHUB_INSTALLATION_PROVIDER_ID |
| 61 | + ) |
| 62 | + ).toBe(false) |
| 63 | + }) |
| 64 | + |
| 65 | + it('keeps the field-less GitHub installation in the internal catalog for existing credentials', async () => { |
| 66 | + const catalog = await listCredentialProviderCatalog(createSessionPrincipal(), context) |
| 67 | + const installation = catalog.find( |
| 68 | + (provider) => |
| 69 | + provider.type === 'service_account' && |
| 70 | + provider.providerId === GITHUB_INSTALLATION_PROVIDER_ID |
| 71 | + ) |
| 72 | + |
| 73 | + expect(installation?.fields).toEqual([]) |
| 74 | + expect(responseSchema.safeParse({ data: catalog, nextCursor: null }).success).toBe(false) |
| 75 | + }) |
| 76 | + |
| 77 | + it('lists the native Claude Platform provider when filtered', async () => { |
| 78 | + const providers = await listProviders('claude') |
| 79 | + |
| 80 | + expect(responseSchema.safeParse({ data: providers, nextCursor: null }).success).toBe(true) |
| 81 | + expect(providers).toContainEqual( |
| 82 | + expect.objectContaining({ |
| 83 | + type: 'service_account', |
| 84 | + serviceId: CLAUDE_PROVIDER_ID, |
| 85 | + providerId: CLAUDE_PROVIDER_ID, |
| 86 | + available: true, |
| 87 | + requiresClientGeneratedCredentialId: false, |
| 88 | + fields: [expect.objectContaining({ id: 'apiToken', required: true, secret: true })], |
| 89 | + }) |
| 90 | + ) |
| 91 | + }) |
| 92 | + |
| 93 | + it('reports Claude as unavailable, not missing, when integration policy disables it', async () => { |
| 94 | + hoisted.allowedIntegrationTypes.mockResolvedValue(new Set(['slack'])) |
| 95 | + |
| 96 | + const providers = await listProviders() |
| 97 | + |
| 98 | + expect(responseSchema.safeParse({ data: providers, nextCursor: null }).success).toBe(true) |
| 99 | + expect(providers).toContainEqual( |
| 100 | + expect.objectContaining({ providerId: CLAUDE_PROVIDER_ID, available: false }) |
| 101 | + ) |
| 102 | + }) |
| 103 | +}) |
0 commit comments