|
| 1 | +import { dbChainMockFns, resetDbChainMock } from '@sim/testing' |
| 2 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 3 | + |
| 4 | +const { createWorkspaceKey } = vi.hoisted(() => ({ createWorkspaceKey: vi.fn() })) |
| 5 | +vi.mock('@/lib/api-key/auth', () => ({ |
| 6 | + createApiKey: vi.fn(), |
| 7 | + createWorkspaceApiKey: createWorkspaceKey, |
| 8 | +})) |
| 9 | + |
| 10 | +import { |
| 11 | + performCreatePersonalApiKey, |
| 12 | + performCreateWorkspaceApiKey, |
| 13 | +} from '@/lib/api-key/orchestration' |
| 14 | + |
| 15 | +const INTERNAL_DETAIL = 'connect ECONNREFUSED 10.0.0.5:5432 for role april' |
| 16 | + |
| 17 | +describe('API key creation failures', () => { |
| 18 | + beforeEach(() => { |
| 19 | + resetDbChainMock() |
| 20 | + createWorkspaceKey.mockReset() |
| 21 | + }) |
| 22 | + |
| 23 | + it('never echoes an internal personal-key failure to the caller', async () => { |
| 24 | + dbChainMockFns.limit.mockRejectedValueOnce(new Error(INTERNAL_DETAIL)) |
| 25 | + |
| 26 | + const result = await performCreatePersonalApiKey({ userId: 'user-1', name: 'CLI' }) |
| 27 | + |
| 28 | + expect(result).toMatchObject({ success: false, errorCode: 'internal' }) |
| 29 | + expect(result.error).not.toContain('ECONNREFUSED') |
| 30 | + }) |
| 31 | + |
| 32 | + it('never echoes an internal workspace-key failure, but still names a conflict', async () => { |
| 33 | + createWorkspaceKey.mockRejectedValueOnce(new Error(INTERNAL_DETAIL)) |
| 34 | + const internal = await performCreateWorkspaceApiKey({ |
| 35 | + workspaceId: 'ws-1', |
| 36 | + userId: 'user-1', |
| 37 | + name: 'CLI', |
| 38 | + }) |
| 39 | + expect(internal).toMatchObject({ success: false, errorCode: 'internal' }) |
| 40 | + expect(internal.error).not.toContain('ECONNREFUSED') |
| 41 | + |
| 42 | + createWorkspaceKey.mockRejectedValueOnce(new Error('A key named "CLI" already exists')) |
| 43 | + const conflict = await performCreateWorkspaceApiKey({ |
| 44 | + workspaceId: 'ws-1', |
| 45 | + userId: 'user-1', |
| 46 | + name: 'CLI', |
| 47 | + }) |
| 48 | + expect(conflict).toEqual({ |
| 49 | + success: false, |
| 50 | + errorCode: 'conflict', |
| 51 | + error: 'A key named "CLI" already exists', |
| 52 | + }) |
| 53 | + }) |
| 54 | +}) |
0 commit comments