|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const { mockGetCredentialActorContext, mockGetServiceAccountToken, mockRefreshTokenIfNeeded } = |
| 7 | + vi.hoisted(() => ({ |
| 8 | + mockGetCredentialActorContext: vi.fn(), |
| 9 | + mockGetServiceAccountToken: vi.fn(), |
| 10 | + mockRefreshTokenIfNeeded: vi.fn(), |
| 11 | + })) |
| 12 | + |
| 13 | +vi.mock('@/lib/credentials/access', () => ({ |
| 14 | + getCredentialActorContext: mockGetCredentialActorContext, |
| 15 | +})) |
| 16 | +vi.mock('@/app/api/auth/oauth/utils', () => ({ |
| 17 | + getServiceAccountToken: mockGetServiceAccountToken, |
| 18 | + refreshTokenIfNeeded: mockRefreshTokenIfNeeded, |
| 19 | +})) |
| 20 | + |
| 21 | +import { resolveVertexCredential } from '@/executor/utils/vertex-credential' |
| 22 | + |
| 23 | +function actorContext(workspaceId: string) { |
| 24 | + return { |
| 25 | + credential: { |
| 26 | + id: 'cred-b', |
| 27 | + workspaceId, |
| 28 | + type: 'service_account', |
| 29 | + accountId: null, |
| 30 | + }, |
| 31 | + member: { id: 'member-1' }, |
| 32 | + hasWorkspaceAccess: true, |
| 33 | + canWriteWorkspace: true, |
| 34 | + isAdmin: false, |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +describe('resolveVertexCredential workspace binding', () => { |
| 39 | + beforeEach(() => { |
| 40 | + vi.clearAllMocks() |
| 41 | + mockGetServiceAccountToken.mockResolvedValue('gcp-access-token') |
| 42 | + }) |
| 43 | + |
| 44 | + it('rejects a credential owned by a different workspace than the executing workflow', async () => { |
| 45 | + mockGetCredentialActorContext.mockResolvedValue(actorContext('workspace-b')) |
| 46 | + |
| 47 | + await expect( |
| 48 | + resolveVertexCredential({ |
| 49 | + credentialId: 'cred-b', |
| 50 | + actingUserId: 'user-1', |
| 51 | + workspaceId: 'workspace-a', |
| 52 | + }) |
| 53 | + ).rejects.toThrow('Credential is not accessible from this workflow workspace') |
| 54 | + |
| 55 | + expect(mockGetServiceAccountToken).not.toHaveBeenCalled() |
| 56 | + }) |
| 57 | + |
| 58 | + it('resolves a credential owned by the executing workspace', async () => { |
| 59 | + mockGetCredentialActorContext.mockResolvedValue(actorContext('workspace-a')) |
| 60 | + |
| 61 | + await expect( |
| 62 | + resolveVertexCredential({ |
| 63 | + credentialId: 'cred-b', |
| 64 | + actingUserId: 'user-1', |
| 65 | + workspaceId: 'workspace-a', |
| 66 | + }) |
| 67 | + ).resolves.toBe('gcp-access-token') |
| 68 | + }) |
| 69 | + |
| 70 | + it('still enforces the user-to-credential check within the same workspace', async () => { |
| 71 | + mockGetCredentialActorContext.mockResolvedValue({ |
| 72 | + ...actorContext('workspace-a'), |
| 73 | + member: null, |
| 74 | + isAdmin: false, |
| 75 | + }) |
| 76 | + |
| 77 | + await expect( |
| 78 | + resolveVertexCredential({ |
| 79 | + credentialId: 'cred-b', |
| 80 | + actingUserId: 'user-1', |
| 81 | + workspaceId: 'workspace-a', |
| 82 | + }) |
| 83 | + ).rejects.toThrow('Not authorized to use this Vertex AI credential') |
| 84 | + }) |
| 85 | + |
| 86 | + it('requires an authenticated acting user', async () => { |
| 87 | + await expect( |
| 88 | + resolveVertexCredential({ |
| 89 | + credentialId: 'cred-b', |
| 90 | + actingUserId: undefined, |
| 91 | + workspaceId: 'workspace-a', |
| 92 | + }) |
| 93 | + ).rejects.toThrow('requires an authenticated user') |
| 94 | + }) |
| 95 | +}) |
0 commit comments