|
| 1 | +/** |
| 2 | + * Tests for the batch presigned upload API route |
| 3 | + * |
| 4 | + * @vitest-environment node |
| 5 | + */ |
| 6 | + |
| 7 | +import { authMockFns, storageServiceMock, storageServiceMockFns } from '@sim/testing' |
| 8 | +import { NextRequest } from 'next/server' |
| 9 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 10 | + |
| 11 | +const { |
| 12 | + mockValidateFileType, |
| 13 | + mockGetUserEntityPermissions, |
| 14 | + mockRecordKnowledgeBaseFileOwnershipMany, |
| 15 | +} = vi.hoisted(() => ({ |
| 16 | + mockValidateFileType: vi.fn().mockReturnValue(null), |
| 17 | + mockGetUserEntityPermissions: vi.fn().mockResolvedValue('write'), |
| 18 | + mockRecordKnowledgeBaseFileOwnershipMany: vi.fn().mockResolvedValue(undefined), |
| 19 | +})) |
| 20 | + |
| 21 | +vi.mock('@/lib/uploads/config', () => ({ |
| 22 | + getServeStoragePrefix: () => 's3', |
| 23 | +})) |
| 24 | + |
| 25 | +vi.mock('@/lib/uploads/core/storage-service', () => storageServiceMock) |
| 26 | + |
| 27 | +vi.mock('@/lib/uploads/utils/validation', () => ({ |
| 28 | + validateFileType: mockValidateFileType, |
| 29 | + SUPPORTED_ARCHIVE_EXTENSIONS: ['zip'] as const, |
| 30 | +})) |
| 31 | + |
| 32 | +vi.mock('@/lib/workspaces/permissions/utils', () => ({ |
| 33 | + getUserEntityPermissions: mockGetUserEntityPermissions, |
| 34 | +})) |
| 35 | + |
| 36 | +vi.mock('@/lib/uploads/server/metadata', () => ({ |
| 37 | + recordKnowledgeBaseFileOwnershipMany: mockRecordKnowledgeBaseFileOwnershipMany, |
| 38 | +})) |
| 39 | + |
| 40 | +import { POST } from '@/app/api/files/presigned/batch/route' |
| 41 | + |
| 42 | +const KB_QUERY = 'type=knowledge-base&workspaceId=ws-1' |
| 43 | + |
| 44 | +const buildRequest = (query: string, files?: unknown) => |
| 45 | + new NextRequest(`http://localhost:3000/api/files/presigned/batch?${query}`, { |
| 46 | + method: 'POST', |
| 47 | + body: JSON.stringify({ |
| 48 | + files: files ?? [{ fileName: 'doc.pdf', contentType: 'application/pdf', fileSize: 1024 }], |
| 49 | + }), |
| 50 | + }) |
| 51 | + |
| 52 | +describe('/api/files/presigned/batch', () => { |
| 53 | + beforeEach(() => { |
| 54 | + vi.clearAllMocks() |
| 55 | + authMockFns.mockGetSession.mockResolvedValue({ user: { id: 'user-1' } }) |
| 56 | + mockValidateFileType.mockReturnValue(null) |
| 57 | + mockGetUserEntityPermissions.mockResolvedValue('write') |
| 58 | + mockRecordKnowledgeBaseFileOwnershipMany.mockResolvedValue(undefined) |
| 59 | + storageServiceMockFns.mockHasCloudStorage.mockReturnValue(true) |
| 60 | + storageServiceMockFns.mockGenerateBatchPresignedUploadUrls.mockImplementation( |
| 61 | + async (files: Array<{ fileName: string }>, context: string) => |
| 62 | + files.map((file) => ({ |
| 63 | + url: `https://example.com/${context}/${file.fileName}`, |
| 64 | + key: `${context}/${file.fileName}`, |
| 65 | + })) |
| 66 | + ) |
| 67 | + }) |
| 68 | + |
| 69 | + it('returns 401 when the caller has no session', async () => { |
| 70 | + authMockFns.mockGetSession.mockResolvedValue(null) |
| 71 | + |
| 72 | + const response = await POST(buildRequest(KB_QUERY)) |
| 73 | + |
| 74 | + expect(response.status).toBe(401) |
| 75 | + expect(storageServiceMockFns.mockGenerateBatchPresignedUploadUrls).not.toHaveBeenCalled() |
| 76 | + }) |
| 77 | + |
| 78 | + it.each([ |
| 79 | + 'workspace-logos', |
| 80 | + 'profile-pictures', |
| 81 | + 'execution', |
| 82 | + 'mothership', |
| 83 | + 'chat', |
| 84 | + 'copilot', |
| 85 | + 'workspace', |
| 86 | + ])('refuses to presign the %s context', async (type) => { |
| 87 | + const response = await POST(buildRequest(`type=${type}&workspaceId=ws-1`)) |
| 88 | + const data = await response.json() |
| 89 | + |
| 90 | + expect(response.status).toBe(400) |
| 91 | + expect(data.error).toContain('Invalid type parameter') |
| 92 | + expect(storageServiceMockFns.mockGenerateBatchPresignedUploadUrls).not.toHaveBeenCalled() |
| 93 | + }) |
| 94 | + |
| 95 | + it('returns 400 when type is missing', async () => { |
| 96 | + const response = await POST(buildRequest('workspaceId=ws-1')) |
| 97 | + |
| 98 | + expect(response.status).toBe(400) |
| 99 | + expect(storageServiceMockFns.mockGenerateBatchPresignedUploadUrls).not.toHaveBeenCalled() |
| 100 | + }) |
| 101 | + |
| 102 | + it('returns 400 when workspaceId is missing', async () => { |
| 103 | + const response = await POST(buildRequest('type=knowledge-base')) |
| 104 | + const data = await response.json() |
| 105 | + |
| 106 | + expect(response.status).toBe(400) |
| 107 | + expect(data.error).toContain('workspaceId') |
| 108 | + expect(mockGetUserEntityPermissions).not.toHaveBeenCalled() |
| 109 | + expect(storageServiceMockFns.mockGenerateBatchPresignedUploadUrls).not.toHaveBeenCalled() |
| 110 | + }) |
| 111 | + |
| 112 | + it.each([['read'], [null]])( |
| 113 | + 'returns 403 when the caller has %s access to the workspace', |
| 114 | + async (permission) => { |
| 115 | + mockGetUserEntityPermissions.mockResolvedValue(permission) |
| 116 | + |
| 117 | + const response = await POST(buildRequest(KB_QUERY)) |
| 118 | + |
| 119 | + expect(response.status).toBe(403) |
| 120 | + expect(storageServiceMockFns.mockGenerateBatchPresignedUploadUrls).not.toHaveBeenCalled() |
| 121 | + } |
| 122 | + ) |
| 123 | + |
| 124 | + it('authorizes the workspace before returning the local-storage fallback', async () => { |
| 125 | + storageServiceMockFns.mockHasCloudStorage.mockReturnValue(false) |
| 126 | + mockGetUserEntityPermissions.mockResolvedValue('read') |
| 127 | + |
| 128 | + const response = await POST(buildRequest(KB_QUERY)) |
| 129 | + |
| 130 | + expect(response.status).toBe(403) |
| 131 | + }) |
| 132 | + |
| 133 | + it('rejects unsupported file types before minting any URL', async () => { |
| 134 | + mockValidateFileType.mockReturnValue({ |
| 135 | + code: 'UNSUPPORTED_FILE_TYPE', |
| 136 | + message: 'Unsupported file type: html.', |
| 137 | + supportedTypes: ['pdf'], |
| 138 | + }) |
| 139 | + |
| 140 | + const response = await POST( |
| 141 | + buildRequest(KB_QUERY, [{ fileName: 'poc.html', contentType: 'text/html', fileSize: 41 }]) |
| 142 | + ) |
| 143 | + const data = await response.json() |
| 144 | + |
| 145 | + expect(response.status).toBe(400) |
| 146 | + expect(data.code).toBe('UNSUPPORTED_FILE_TYPE') |
| 147 | + expect(storageServiceMockFns.mockGenerateBatchPresignedUploadUrls).not.toHaveBeenCalled() |
| 148 | + }) |
| 149 | + |
| 150 | + it('mints knowledge-base URLs and records workspace ownership for a permitted caller', async () => { |
| 151 | + const response = await POST(buildRequest(KB_QUERY)) |
| 152 | + const data = await response.json() |
| 153 | + |
| 154 | + expect(response.status).toBe(200) |
| 155 | + expect(mockGetUserEntityPermissions).toHaveBeenCalledWith('user-1', 'workspace', 'ws-1') |
| 156 | + expect(storageServiceMockFns.mockGenerateBatchPresignedUploadUrls).toHaveBeenCalledWith( |
| 157 | + [{ fileName: 'doc.pdf', contentType: 'application/pdf', fileSize: 1024 }], |
| 158 | + 'knowledge-base', |
| 159 | + 'user-1', |
| 160 | + 3600 |
| 161 | + ) |
| 162 | + expect(data.files).toHaveLength(1) |
| 163 | + expect(data.files[0].fileInfo.key).toBe('knowledge-base/doc.pdf') |
| 164 | + expect(data.files[0].fileInfo.path).toContain('?context=knowledge-base') |
| 165 | + expect(data.directUploadSupported).toBe(true) |
| 166 | + expect(mockRecordKnowledgeBaseFileOwnershipMany).toHaveBeenCalledWith([ |
| 167 | + { |
| 168 | + key: 'knowledge-base/doc.pdf', |
| 169 | + userId: 'user-1', |
| 170 | + workspaceId: 'ws-1', |
| 171 | + originalName: 'doc.pdf', |
| 172 | + contentType: 'application/pdf', |
| 173 | + size: 1024, |
| 174 | + }, |
| 175 | + ]) |
| 176 | + }) |
| 177 | + |
| 178 | + it('returns the fallback response when cloud storage is not configured', async () => { |
| 179 | + storageServiceMockFns.mockHasCloudStorage.mockReturnValue(false) |
| 180 | + |
| 181 | + const response = await POST(buildRequest(KB_QUERY)) |
| 182 | + const data = await response.json() |
| 183 | + |
| 184 | + expect(response.status).toBe(200) |
| 185 | + expect(data.directUploadSupported).toBe(false) |
| 186 | + expect(data.files[0].presignedUrl).toBe('') |
| 187 | + expect(storageServiceMockFns.mockGenerateBatchPresignedUploadUrls).not.toHaveBeenCalled() |
| 188 | + }) |
| 189 | +}) |
0 commit comments