|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | + |
| 5 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | + |
| 7 | +const { mockAuthenticateApiKey, mockGetWorkspaceBillingSettings, mockGetUserEntityPermissions } = |
| 8 | + vi.hoisted(() => ({ |
| 9 | + mockAuthenticateApiKey: vi.fn(), |
| 10 | + mockGetWorkspaceBillingSettings: vi.fn(), |
| 11 | + mockGetUserEntityPermissions: vi.fn(), |
| 12 | + })) |
| 13 | + |
| 14 | +vi.mock('@/lib/api-key/auth', () => ({ |
| 15 | + authenticateApiKey: (...args: unknown[]) => mockAuthenticateApiKey(...args), |
| 16 | +})) |
| 17 | + |
| 18 | +vi.mock('@/lib/workspaces/utils', () => ({ |
| 19 | + getWorkspaceBillingSettings: (...args: unknown[]) => mockGetWorkspaceBillingSettings(...args), |
| 20 | +})) |
| 21 | + |
| 22 | +vi.mock('@/lib/workspaces/permissions/utils', () => ({ |
| 23 | + getUserEntityPermissions: (...args: unknown[]) => mockGetUserEntityPermissions(...args), |
| 24 | +})) |
| 25 | + |
| 26 | +import { databaseMock } from '@sim/testing' |
| 27 | +import { authenticateApiKeyFromHeader } from '@/lib/api-key/service' |
| 28 | + |
| 29 | +const mockDb = databaseMock.db |
| 30 | + |
| 31 | +describe('authenticateApiKeyFromHeader', () => { |
| 32 | + beforeEach(() => { |
| 33 | + vi.clearAllMocks() |
| 34 | + mockGetWorkspaceBillingSettings.mockResolvedValue({ |
| 35 | + billedAccountUserId: 'billing-user', |
| 36 | + allowPersonalApiKeys: true, |
| 37 | + }) |
| 38 | + mockGetUserEntityPermissions.mockResolvedValue({ permissionType: 'admin' }) |
| 39 | + }) |
| 40 | + |
| 41 | + function createAwaitableQuery<T>(rows: T[]) { |
| 42 | + return { |
| 43 | + where: vi.fn().mockResolvedValue(rows), |
| 44 | + then: (onFulfilled: (value: T[]) => unknown, onRejected?: (reason: unknown) => unknown) => |
| 45 | + Promise.resolve(rows).then(onFulfilled, onRejected), |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + it('authenticates a valid key when the joined user row is missing', async () => { |
| 50 | + const rows = [ |
| 51 | + { |
| 52 | + id: 'key-1', |
| 53 | + userId: 'user-1', |
| 54 | + userName: null, |
| 55 | + userEmail: null, |
| 56 | + workspaceId: 'ws-1', |
| 57 | + type: 'workspace', |
| 58 | + key: 'stored-key', |
| 59 | + expiresAt: null, |
| 60 | + }, |
| 61 | + ] |
| 62 | + |
| 63 | + vi.mocked(mockDb.select).mockReturnValue({ |
| 64 | + from: vi.fn().mockReturnValue({ |
| 65 | + leftJoin: vi.fn().mockReturnValue(createAwaitableQuery(rows)), |
| 66 | + }), |
| 67 | + } as any) |
| 68 | + mockAuthenticateApiKey.mockResolvedValue(true) |
| 69 | + |
| 70 | + const result = await authenticateApiKeyFromHeader('raw-key', { |
| 71 | + workspaceId: 'ws-1', |
| 72 | + keyTypes: ['workspace', 'personal'], |
| 73 | + }) |
| 74 | + |
| 75 | + expect(result).toEqual({ |
| 76 | + success: true, |
| 77 | + userId: 'user-1', |
| 78 | + userName: null, |
| 79 | + userEmail: null, |
| 80 | + keyId: 'key-1', |
| 81 | + keyType: 'workspace', |
| 82 | + workspaceId: 'ws-1', |
| 83 | + }) |
| 84 | + }) |
| 85 | + |
| 86 | + it('still scopes the authentication result by workspace', async () => { |
| 87 | + const rows = [ |
| 88 | + { |
| 89 | + id: 'key-1', |
| 90 | + userId: 'user-1', |
| 91 | + userName: null, |
| 92 | + userEmail: null, |
| 93 | + workspaceId: 'ws-2', |
| 94 | + type: 'workspace', |
| 95 | + key: 'stored-key', |
| 96 | + expiresAt: null, |
| 97 | + }, |
| 98 | + ] |
| 99 | + |
| 100 | + vi.mocked(mockDb.select).mockReturnValue({ |
| 101 | + from: vi.fn().mockReturnValue({ |
| 102 | + leftJoin: vi.fn().mockReturnValue(createAwaitableQuery(rows)), |
| 103 | + }), |
| 104 | + } as any) |
| 105 | + mockAuthenticateApiKey.mockResolvedValue(true) |
| 106 | + |
| 107 | + const result = await authenticateApiKeyFromHeader('raw-key', { |
| 108 | + workspaceId: 'ws-1', |
| 109 | + keyTypes: ['workspace'], |
| 110 | + }) |
| 111 | + |
| 112 | + expect(result).toEqual({ success: false, error: 'Invalid API key' }) |
| 113 | + }) |
| 114 | +}) |
0 commit comments