|
1 | | -import { credential, credentialMember } from '@sim/db/schema' |
| 1 | +import { account, credential, credentialMember } from '@sim/db/schema' |
2 | 2 | import { queueTableRows, resetDbChainMock } from '@sim/testing' |
3 | 3 | import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest' |
4 | 4 |
|
5 | | -const { mockCheckWorkspaceAccess } = vi.hoisted(() => ({ |
| 5 | +const { mockCheckWorkspaceAccess, mockGetUserEntityPermissions } = vi.hoisted(() => ({ |
6 | 6 | mockCheckWorkspaceAccess: vi.fn(), |
| 7 | + mockGetUserEntityPermissions: vi.fn(), |
7 | 8 | })) |
8 | 9 |
|
9 | 10 | vi.mock('@/lib/workspaces/permissions/utils', () => ({ |
10 | 11 | checkWorkspaceAccess: mockCheckWorkspaceAccess, |
| 12 | + getUserEntityPermissions: mockGetUserEntityPermissions, |
11 | 13 | resolveWorkspaceAccess: vi.fn(async (workspaceId: string, userId: string, provided?: any) => |
12 | 14 | provided && provided.workspace?.id === workspaceId |
13 | 15 | ? provided |
14 | 16 | : mockCheckWorkspaceAccess(workspaceId, userId) |
15 | 17 | ), |
16 | 18 | })) |
17 | 19 |
|
18 | | -import { getCredentialActorContext } from '@/lib/credentials/access' |
| 20 | +import { getCredentialActorContext, resolveCredentialTokenIdentity } from '@/lib/credentials/access' |
19 | 21 |
|
20 | 22 | afterAll(resetDbChainMock) |
21 | 23 |
|
@@ -88,3 +90,74 @@ describe('getCredentialActorContext', () => { |
88 | 90 | expect(ctx.isAdmin).toBe(false) |
89 | 91 | }) |
90 | 92 | }) |
| 93 | + |
| 94 | +describe('resolveCredentialTokenIdentity', () => { |
| 95 | + beforeEach(() => { |
| 96 | + vi.clearAllMocks() |
| 97 | + resetDbChainMock() |
| 98 | + }) |
| 99 | + |
| 100 | + it('resolves the account owner even when it is not the caller', async () => { |
| 101 | + queueTableRows(credential, [{ workspaceId: 'ws', type: 'oauth', accountId: 'acct1' }]) |
| 102 | + queueTableRows(account, [{ userId: 'authorizer' }]) |
| 103 | + mockGetUserEntityPermissions.mockResolvedValue('write') |
| 104 | + |
| 105 | + await expect(resolveCredentialTokenIdentity('c1', 'ws')).resolves.toEqual({ |
| 106 | + kind: 'oauth', |
| 107 | + userId: 'authorizer', |
| 108 | + }) |
| 109 | + }) |
| 110 | + |
| 111 | + it('rejects a credential belonging to another workspace', async () => { |
| 112 | + queueTableRows(credential, [{ workspaceId: 'other-ws', type: 'oauth', accountId: 'acct1' }]) |
| 113 | + |
| 114 | + await expect(resolveCredentialTokenIdentity('c1', 'ws')).resolves.toBeNull() |
| 115 | + }) |
| 116 | + |
| 117 | + it('reports service accounts as needing no user id', async () => { |
| 118 | + queueTableRows(credential, [{ workspaceId: 'ws', type: 'service_account', accountId: null }]) |
| 119 | + |
| 120 | + await expect(resolveCredentialTokenIdentity('c1', 'ws')).resolves.toEqual({ |
| 121 | + kind: 'service_account', |
| 122 | + }) |
| 123 | + }) |
| 124 | + |
| 125 | + it('rejects a service account from another workspace', async () => { |
| 126 | + queueTableRows(credential, [ |
| 127 | + { workspaceId: 'other-ws', type: 'service_account', accountId: null }, |
| 128 | + ]) |
| 129 | + |
| 130 | + await expect(resolveCredentialTokenIdentity('c1', 'ws')).resolves.toBeNull() |
| 131 | + }) |
| 132 | + |
| 133 | + it('rejects a credential type that is neither oauth nor a service account', async () => { |
| 134 | + queueTableRows(credential, [{ workspaceId: 'ws', type: 'env_personal', accountId: null }]) |
| 135 | + |
| 136 | + await expect(resolveCredentialTokenIdentity('c1', 'ws')).resolves.toBeNull() |
| 137 | + }) |
| 138 | + |
| 139 | + it('rejects when the owner no longer has workspace access', async () => { |
| 140 | + queueTableRows(credential, [{ workspaceId: 'ws', type: 'oauth', accountId: 'acct1' }]) |
| 141 | + queueTableRows(account, [{ userId: 'departed' }]) |
| 142 | + mockGetUserEntityPermissions.mockResolvedValue(null) |
| 143 | + |
| 144 | + await expect(resolveCredentialTokenIdentity('c1', 'ws')).resolves.toBeNull() |
| 145 | + }) |
| 146 | + |
| 147 | + it('falls back to a legacy raw account id when no credential row exists', async () => { |
| 148 | + queueTableRows(account, [{ userId: 'legacy-owner' }]) |
| 149 | + mockGetUserEntityPermissions.mockResolvedValue('admin') |
| 150 | + |
| 151 | + await expect(resolveCredentialTokenIdentity('acct-legacy', 'ws')).resolves.toEqual({ |
| 152 | + kind: 'oauth', |
| 153 | + userId: 'legacy-owner', |
| 154 | + }) |
| 155 | + }) |
| 156 | + |
| 157 | + it('returns null when the account row is missing', async () => { |
| 158 | + queueTableRows(credential, [{ workspaceId: 'ws', type: 'oauth', accountId: 'acct1' }]) |
| 159 | + |
| 160 | + await expect(resolveCredentialTokenIdentity('c1', 'ws')).resolves.toBeNull() |
| 161 | + expect(mockGetUserEntityPermissions).not.toHaveBeenCalled() |
| 162 | + }) |
| 163 | +}) |
0 commit comments