|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { authMockFns, createMockRequest } from '@sim/testing' |
| 5 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | +import { CredentialAccessRequiredError } from '@/lib/credentials/application/authorized-credential-use-case' |
| 7 | + |
| 8 | +const mocks = vi.hoisted(() => ({ |
| 9 | + read: vi.fn(), |
| 10 | + update: vi.fn(), |
| 11 | + remove: vi.fn(), |
| 12 | +})) |
| 13 | + |
| 14 | +vi.mock('@/lib/credentials/application/credential-crud', () => ({ |
| 15 | + CredentialProviderOperationError: class CredentialProviderOperationError extends Error {}, |
| 16 | + getWorkspaceCredentialUseCase: { |
| 17 | + operation: { id: 'credentials.read' }, |
| 18 | + execute: mocks.read, |
| 19 | + }, |
| 20 | + updateWorkspaceCredentialUseCase: { |
| 21 | + operation: { id: 'credentials.update' }, |
| 22 | + execute: mocks.update, |
| 23 | + }, |
| 24 | +})) |
| 25 | + |
| 26 | +vi.mock('@/lib/credentials/application/service-account', () => ({ |
| 27 | + deleteCredentialUseCase: { |
| 28 | + operation: { id: 'credentials.delete' }, |
| 29 | + execute: mocks.remove, |
| 30 | + }, |
| 31 | +})) |
| 32 | + |
| 33 | +import { GET } from '@/app/api/credentials/[id]/route' |
| 34 | + |
| 35 | +const CREDENTIAL_ID = 'credential-1' |
| 36 | +const routeContext = { params: Promise.resolve({ id: CREDENTIAL_ID }) } |
| 37 | + |
| 38 | +describe('GET /api/credentials/[id]', () => { |
| 39 | + beforeEach(() => { |
| 40 | + vi.clearAllMocks() |
| 41 | + authMockFns.mockGetSession.mockResolvedValue({ |
| 42 | + user: { id: 'writer-1' }, |
| 43 | + session: { id: 'session-1' }, |
| 44 | + }) |
| 45 | + }) |
| 46 | + |
| 47 | + it('preserves the generic denial for a workspace writer without credential access', async () => { |
| 48 | + mocks.read.mockRejectedValue(new CredentialAccessRequiredError()) |
| 49 | + |
| 50 | + const response = await GET( |
| 51 | + createMockRequest('GET', undefined, {}, `http://localhost/api/credentials/${CREDENTIAL_ID}`), |
| 52 | + routeContext |
| 53 | + ) |
| 54 | + |
| 55 | + expect(response.status).toBe(403) |
| 56 | + expect(await response.json()).toEqual({ error: 'Forbidden' }) |
| 57 | + }) |
| 58 | +}) |
0 commit comments