|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { |
| 5 | + auditMock, |
| 6 | + auditMockFns, |
| 7 | + authMockFns, |
| 8 | + encryptionMock, |
| 9 | + encryptionMockFns, |
| 10 | + workflowsApiUtilsMock, |
| 11 | + workflowsApiUtilsMockFns, |
| 12 | +} from '@sim/testing' |
| 13 | +import { NextRequest } from 'next/server' |
| 14 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 15 | + |
| 16 | +const { mockCheckChatAccess } = vi.hoisted(() => ({ |
| 17 | + mockCheckChatAccess: vi.fn(), |
| 18 | +})) |
| 19 | + |
| 20 | +const mockCreateErrorResponse = workflowsApiUtilsMockFns.mockCreateErrorResponse |
| 21 | +const mockDecryptSecret = encryptionMockFns.mockDecryptSecret |
| 22 | +const mockRecordAudit = auditMockFns.mockRecordAudit |
| 23 | + |
| 24 | +vi.mock('@sim/audit', () => auditMock) |
| 25 | +vi.mock('@/app/api/workflows/utils', () => workflowsApiUtilsMock) |
| 26 | +vi.mock('@/lib/core/security/encryption', () => encryptionMock) |
| 27 | +vi.mock('@/app/api/chat/utils', () => ({ |
| 28 | + checkChatAccess: mockCheckChatAccess, |
| 29 | +})) |
| 30 | + |
| 31 | +import { GET } from '@/app/api/chat/manage/[id]/password/route' |
| 32 | + |
| 33 | +const passwordChat = { |
| 34 | + id: 'chat-123', |
| 35 | + workflowId: 'workflow-123', |
| 36 | + identifier: 'test-chat', |
| 37 | + title: 'Test Chat', |
| 38 | + authType: 'password', |
| 39 | + password: 'encrypted-password', |
| 40 | +} |
| 41 | + |
| 42 | +function makeRequest() { |
| 43 | + return new NextRequest('http://localhost:3000/api/chat/manage/chat-123/password') |
| 44 | +} |
| 45 | + |
| 46 | +function callGet() { |
| 47 | + return GET(makeRequest(), { params: Promise.resolve({ id: 'chat-123' }) }) |
| 48 | +} |
| 49 | + |
| 50 | +describe('Chat Password Reveal API Route', () => { |
| 51 | + beforeEach(() => { |
| 52 | + vi.clearAllMocks() |
| 53 | + |
| 54 | + authMockFns.mockGetSession.mockResolvedValue({ |
| 55 | + user: { id: 'user-id', name: 'Test User', email: 'user@example.com' }, |
| 56 | + }) |
| 57 | + |
| 58 | + mockCreateErrorResponse.mockImplementation((message, status = 500) => { |
| 59 | + return new Response(JSON.stringify({ error: message }), { |
| 60 | + status, |
| 61 | + headers: { 'Content-Type': 'application/json' }, |
| 62 | + }) |
| 63 | + }) |
| 64 | + |
| 65 | + mockDecryptSecret.mockResolvedValue({ decrypted: 'super-secret' }) |
| 66 | + mockCheckChatAccess.mockResolvedValue({ |
| 67 | + hasAccess: true, |
| 68 | + chat: passwordChat, |
| 69 | + workspaceId: 'workspace-123', |
| 70 | + }) |
| 71 | + }) |
| 72 | + |
| 73 | + it('should return 401 when user is not authenticated', async () => { |
| 74 | + authMockFns.mockGetSession.mockResolvedValue(null) |
| 75 | + |
| 76 | + const response = await callGet() |
| 77 | + |
| 78 | + expect(response.status).toBe(401) |
| 79 | + const data = await response.json() |
| 80 | + expect(data.error).toBe('Unauthorized') |
| 81 | + expect(mockDecryptSecret).not.toHaveBeenCalled() |
| 82 | + }) |
| 83 | + |
| 84 | + it('should return 404 when chat not found or access denied', async () => { |
| 85 | + mockCheckChatAccess.mockResolvedValue({ hasAccess: false }) |
| 86 | + |
| 87 | + const response = await callGet() |
| 88 | + |
| 89 | + expect(response.status).toBe(404) |
| 90 | + const data = await response.json() |
| 91 | + expect(data.error).toBe('Chat not found or access denied') |
| 92 | + expect(mockCheckChatAccess).toHaveBeenCalledWith('chat-123', 'user-id') |
| 93 | + expect(mockDecryptSecret).not.toHaveBeenCalled() |
| 94 | + }) |
| 95 | + |
| 96 | + it('should return 404 when the chat has no password set', async () => { |
| 97 | + mockCheckChatAccess.mockResolvedValue({ |
| 98 | + hasAccess: true, |
| 99 | + chat: { ...passwordChat, authType: 'public', password: null }, |
| 100 | + workspaceId: 'workspace-123', |
| 101 | + }) |
| 102 | + |
| 103 | + const response = await callGet() |
| 104 | + |
| 105 | + expect(response.status).toBe(404) |
| 106 | + const data = await response.json() |
| 107 | + expect(data.error).toBe('This chat does not have a password set') |
| 108 | + expect(mockDecryptSecret).not.toHaveBeenCalled() |
| 109 | + }) |
| 110 | + |
| 111 | + it('should return the decrypted password and record an audit event', async () => { |
| 112 | + const response = await callGet() |
| 113 | + |
| 114 | + expect(response.status).toBe(200) |
| 115 | + const data = await response.json() |
| 116 | + expect(data.password).toBe('super-secret') |
| 117 | + expect(mockDecryptSecret).toHaveBeenCalledWith('encrypted-password') |
| 118 | + expect(mockRecordAudit).toHaveBeenCalledWith( |
| 119 | + expect.objectContaining({ |
| 120 | + workspaceId: 'workspace-123', |
| 121 | + actorId: 'user-id', |
| 122 | + action: 'chat.password_viewed', |
| 123 | + resourceId: 'chat-123', |
| 124 | + }) |
| 125 | + ) |
| 126 | + expect(response.headers.get('Cache-Control')).toBe('private, no-store') |
| 127 | + }) |
| 128 | + |
| 129 | + it('should return 500 without echoing the decryption error', async () => { |
| 130 | + mockDecryptSecret.mockRejectedValue( |
| 131 | + new Error('Invalid encrypted value format. Expected "iv:encrypted:authTag"') |
| 132 | + ) |
| 133 | + |
| 134 | + const response = await callGet() |
| 135 | + |
| 136 | + expect(response.status).toBe(500) |
| 137 | + const data = await response.json() |
| 138 | + expect(data.error).toBe('Failed to reveal chat password') |
| 139 | + expect(mockRecordAudit).not.toHaveBeenCalled() |
| 140 | + }) |
| 141 | +}) |
0 commit comments