|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { authMockFns, createMockRequest } from '@sim/testing' |
| 5 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | + |
| 7 | +const { mockExecute } = vi.hoisted(() => ({ mockExecute: vi.fn() })) |
| 8 | + |
| 9 | +vi.mock('@/lib/credentials/application/list-plaid-options', async () => { |
| 10 | + const { credentialOperations } = await vi.importActual< |
| 11 | + typeof import('@/lib/credentials/application/operations') |
| 12 | + >('@/lib/credentials/application/operations') |
| 13 | + return { |
| 14 | + listPlaidOptions: { |
| 15 | + operation: credentialOperations.read, |
| 16 | + execute: mockExecute, |
| 17 | + }, |
| 18 | + } |
| 19 | +}) |
| 20 | + |
| 21 | +import { OrchestrationError } from '@/lib/core/orchestration/types' |
| 22 | +import { POST } from '@/app/api/tools/plaid/options/route' |
| 23 | + |
| 24 | +const body = { |
| 25 | + kind: 'accounts', |
| 26 | + workspaceId: 'workspace-1', |
| 27 | + credentialId: 'credential-1', |
| 28 | +} as const |
| 29 | + |
| 30 | +function request(requestBody: unknown = body, headers: Record<string, string> = {}) { |
| 31 | + return createMockRequest('POST', requestBody, headers) |
| 32 | +} |
| 33 | + |
| 34 | +describe('POST /api/tools/plaid/options', () => { |
| 35 | + beforeEach(() => { |
| 36 | + vi.clearAllMocks() |
| 37 | + authMockFns.mockGetSession.mockResolvedValue({ |
| 38 | + user: { id: 'user-1' }, |
| 39 | + session: { id: 'session-1' }, |
| 40 | + }) |
| 41 | + mockExecute.mockResolvedValue({ options: [{ id: 'acc-1', label: 'Checking' }] }) |
| 42 | + }) |
| 43 | + |
| 44 | + it('accepts a session and forwards only selector scope plus cancellation', async () => { |
| 45 | + const incoming = request() |
| 46 | + const response = await POST(incoming) |
| 47 | + |
| 48 | + expect(response.status).toBe(200) |
| 49 | + await expect(response.json()).resolves.toEqual({ |
| 50 | + options: [{ id: 'acc-1', label: 'Checking' }], |
| 51 | + }) |
| 52 | + expect(mockExecute).toHaveBeenCalledWith( |
| 53 | + expect.objectContaining({ |
| 54 | + principal: { kind: 'session', userId: 'user-1', sessionId: 'session-1' }, |
| 55 | + input: { body, signal: incoming.signal }, |
| 56 | + request: incoming, |
| 57 | + }) |
| 58 | + ) |
| 59 | + }) |
| 60 | + |
| 61 | + it('rejects unauthenticated and API-key callers', async () => { |
| 62 | + authMockFns.mockGetSession.mockResolvedValue(null) |
| 63 | + expect((await POST(request())).status).toBe(401) |
| 64 | + expect((await POST(request(body, { 'x-api-key': 'key' }))).status).toBe(401) |
| 65 | + expect(mockExecute).not.toHaveBeenCalled() |
| 66 | + }) |
| 67 | + |
| 68 | + it('rejects malformed and overlong selector requests before execution', async () => { |
| 69 | + expect((await POST(request({ ...body, unexpected: true }))).status).toBe(400) |
| 70 | + expect( |
| 71 | + ( |
| 72 | + await POST( |
| 73 | + request({ |
| 74 | + ...body, |
| 75 | + kind: 'institution_search', |
| 76 | + query: 'x'.repeat(257), |
| 77 | + country_codes: ['US'], |
| 78 | + }) |
| 79 | + ) |
| 80 | + ).status |
| 81 | + ).toBe(400) |
| 82 | + expect(mockExecute).not.toHaveBeenCalled() |
| 83 | + }) |
| 84 | + |
| 85 | + it.each([ |
| 86 | + [new OrchestrationError('not_found', 'Credential not found'), 404], |
| 87 | + [new OrchestrationError('forbidden', 'Credential access required'), 403], |
| 88 | + ])('projects credential access failures', async (error, status) => { |
| 89 | + mockExecute.mockRejectedValueOnce(error) |
| 90 | + const response = await POST(request()) |
| 91 | + expect(response.status).toBe(status) |
| 92 | + expect(JSON.stringify(await response.json())).not.toContain('item-token') |
| 93 | + }) |
| 94 | +}) |
0 commit comments