files.integration.test.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
  2. import { testClient } from 'hono/testing';
  3. import { DataSource } from 'typeorm';
  4. import { File } from '@/server/modules/files/file.entity';
  5. import { FileService } from '@/server/modules/files/file.service';
  6. import { MinioService } from '@/server/modules/files/minio.service';
  7. import { authMiddleware } from '@/server/middleware/auth.middleware';
  8. import { fileApiRoutes } from '@/server/api';
  9. // Mock dependencies
  10. vi.mock('@/server/modules/files/file.service', () => ({
  11. FileService: vi.fn().mockImplementation(() => ({
  12. createFile: vi.fn()
  13. }))
  14. }));
  15. vi.mock('@/server/modules/files/minio.service');
  16. vi.mock('@/server/middleware/auth.middleware');
  17. describe('File API Integration Tests', () => {
  18. let client: ReturnType<typeof testClient<typeof fileApiRoutes>>['api']['v1'];
  19. let mockFileService: FileService;
  20. let mockMinioService: MinioService;
  21. let mockDataSource: DataSource;
  22. beforeEach(async () => {
  23. vi.clearAllMocks();
  24. mockDataSource = {} as DataSource;
  25. mockMinioService = new MinioService();
  26. // Mock auth middleware to bypass authentication
  27. vi.mocked(authMiddleware).mockImplementation((_, next) => next());
  28. // Get the mocked FileService instance
  29. mockFileService = new FileService(mockDataSource);
  30. client = testClient(fileApiRoutes).api.v1;
  31. });
  32. afterEach(() => {
  33. vi.clearAllMocks();
  34. });
  35. describe('POST /api/v1/files/upload-policy', () => {
  36. it('should generate upload policy successfully', async () => {
  37. const mockFileData = {
  38. name: 'test.txt',
  39. type: 'text/plain',
  40. size: 1024,
  41. path: '/uploads/test.txt',
  42. description: 'Test file',
  43. uploadUserId: 1
  44. };
  45. const mockResponse = {
  46. file: {
  47. id: 1,
  48. ...mockFileData,
  49. path: '1/test-uuid-123-test.txt',
  50. uploadTime: new Date(),
  51. createdAt: new Date(),
  52. updatedAt: new Date()
  53. },
  54. uploadPolicy: {
  55. host: 'https://minio.example.com',
  56. key: '1/test-uuid-123-test.txt',
  57. bucket: 'd8dai'
  58. }
  59. };
  60. mockFileService.createFile = vi.fn().mockResolvedValue(mockResponse);
  61. const response = await client.files['upload-policy'].$post({
  62. json: mockFileData,
  63. header: {
  64. 'Authorization': 'Bearer test-token'
  65. }
  66. });
  67. if (response.status !== 200) {
  68. const error = await response.json();
  69. console.debug('Error response:', JSON.stringify(error, null, 2));
  70. console.debug('Response status:', response.status);
  71. }
  72. expect(response.status).toBe(200);
  73. const result = await response.json();
  74. expect(result).toEqual(mockResponse);
  75. expect(mockFileService.createFile).toHaveBeenCalledWith(mockFileData);
  76. });
  77. it('should return 400 for invalid request data', async () => {
  78. const invalidData = {
  79. name: '', // Empty name
  80. type: 'text/plain'
  81. };
  82. const response = await app.request('/api/v1/files/upload-policy', {
  83. method: 'POST',
  84. headers: {
  85. 'Content-Type': 'application/json',
  86. 'Authorization': 'Bearer test-token'
  87. },
  88. body: JSON.stringify(invalidData)
  89. });
  90. expect(response.status).toBe(400);
  91. });
  92. it('should handle service errors gracefully', async () => {
  93. const mockFileData = {
  94. name: 'test.txt',
  95. type: 'text/plain',
  96. uploadUserId: 1
  97. };
  98. mockFileService.createFile = vi.fn().mockRejectedValue(new Error('Service error'));
  99. const response = await app.request('/api/v1/files/upload-policy', {
  100. method: 'POST',
  101. headers: {
  102. 'Content-Type': 'application/json',
  103. 'Authorization': 'Bearer test-token'
  104. },
  105. body: JSON.stringify(mockFileData)
  106. });
  107. expect(response.status).toBe(500);
  108. });
  109. });
  110. describe('GET /api/v1/files/{id}/url', () => {
  111. it('should generate file access URL successfully', async () => {
  112. const mockUrl = 'https://minio.example.com/presigned-url';
  113. vi.mocked(mockFileService.getFileUrl).mockResolvedValue(mockUrl);
  114. const response = await app.request('/api/v1/files/1/url', {
  115. method: 'GET',
  116. headers: {
  117. 'Authorization': 'Bearer test-token'
  118. }
  119. });
  120. expect(response.status).toBe(200);
  121. const result = await response.json();
  122. expect(result).toEqual({ url: mockUrl });
  123. expect(mockFileService.getFileUrl).toHaveBeenCalledWith(1);
  124. });
  125. it('should return 404 when file not found', async () => {
  126. vi.mocked(mockFileService.getFileUrl).mockRejectedValue(new Error('文件不存在'));
  127. const response = await app.request('/api/v1/files/999/url', {
  128. method: 'GET',
  129. headers: {
  130. 'Authorization': 'Bearer test-token'
  131. }
  132. });
  133. expect(response.status).toBe(404);
  134. });
  135. });
  136. describe('GET /api/v1/files/{id}/download', () => {
  137. it('should generate file download URL successfully', async () => {
  138. const mockDownloadInfo = {
  139. url: 'https://minio.example.com/download-url',
  140. filename: 'test.txt'
  141. };
  142. vi.mocked(mockFileService.getFileDownloadUrl).mockResolvedValue(mockDownloadInfo);
  143. const response = await app.request('/api/v1/files/1/download', {
  144. method: 'GET',
  145. headers: {
  146. 'Authorization': 'Bearer test-token'
  147. }
  148. });
  149. expect(response.status).toBe(200);
  150. const result = await response.json();
  151. expect(result).toEqual(mockDownloadInfo);
  152. expect(mockFileService.getFileDownloadUrl).toHaveBeenCalledWith(1);
  153. });
  154. it('should return 404 when file not found for download', async () => {
  155. vi.mocked(mockFileService.getFileDownloadUrl).mockRejectedValue(new Error('文件不存在'));
  156. const response = await app.request('/api/v1/files/999/download', {
  157. method: 'GET',
  158. headers: {
  159. 'Authorization': 'Bearer test-token'
  160. }
  161. });
  162. expect(response.status).toBe(404);
  163. });
  164. });
  165. describe('DELETE /api/v1/files/{id}', () => {
  166. it('should delete file successfully', async () => {
  167. vi.mocked(mockFileService.deleteFile).mockResolvedValue(true);
  168. const response = await app.request('/api/v1/files/1', {
  169. method: 'DELETE',
  170. headers: {
  171. 'Authorization': 'Bearer test-token'
  172. }
  173. });
  174. expect(response.status).toBe(200);
  175. const result = await response.json();
  176. expect(result).toEqual({ success: true });
  177. expect(mockFileService.deleteFile).toHaveBeenCalledWith(1);
  178. });
  179. it('should return 404 when file not found for deletion', async () => {
  180. vi.mocked(mockFileService.deleteFile).mockRejectedValue(new Error('文件不存在'));
  181. const response = await app.request('/api/v1/files/999', {
  182. method: 'DELETE',
  183. headers: {
  184. 'Authorization': 'Bearer test-token'
  185. }
  186. });
  187. expect(response.status).toBe(404);
  188. });
  189. it('should handle deletion errors', async () => {
  190. vi.mocked(mockFileService.deleteFile).mockRejectedValue(new Error('删除失败'));
  191. const response = await app.request('/api/v1/files/1', {
  192. method: 'DELETE',
  193. headers: {
  194. 'Authorization': 'Bearer test-token'
  195. }
  196. });
  197. expect(response.status).toBe(500);
  198. });
  199. });
  200. describe('POST /api/v1/files/multipart-policy', () => {
  201. it('should generate multipart upload policy successfully', async () => {
  202. const mockRequestData = {
  203. name: 'large-file.zip',
  204. type: 'application/zip',
  205. size: 1024 * 1024 * 100, // 100MB
  206. uploadUserId: 1,
  207. partCount: 5
  208. };
  209. const mockResponse = {
  210. file: {
  211. id: 1,
  212. ...mockRequestData,
  213. path: '1/test-uuid-123-large-file.zip',
  214. uploadTime: new Date(),
  215. createdAt: new Date(),
  216. updatedAt: new Date()
  217. },
  218. uploadId: 'upload-123',
  219. uploadUrls: ['url1', 'url2', 'url3', 'url4', 'url5'],
  220. bucket: 'd8dai',
  221. key: '1/test-uuid-123-large-file.zip'
  222. };
  223. vi.mocked(mockFileService.createMultipartUploadPolicy).mockResolvedValue(mockResponse);
  224. const response = await app.request('/api/v1/files/multipart-policy', {
  225. method: 'POST',
  226. headers: {
  227. 'Content-Type': 'application/json',
  228. 'Authorization': 'Bearer test-token'
  229. },
  230. body: JSON.stringify(mockRequestData)
  231. });
  232. expect(response.status).toBe(200);
  233. const result = await response.json();
  234. expect(result).toEqual(mockResponse);
  235. expect(mockFileService.createMultipartUploadPolicy).toHaveBeenCalledWith(
  236. {
  237. name: 'large-file.zip',
  238. type: 'application/zip',
  239. size: 104857600,
  240. uploadUserId: 1
  241. },
  242. 5
  243. );
  244. });
  245. it('should validate multipart policy request data', async () => {
  246. const invalidData = {
  247. name: 'test.zip',
  248. // Missing required fields
  249. };
  250. const response = await app.request('/api/v1/files/multipart-policy', {
  251. method: 'POST',
  252. headers: {
  253. 'Content-Type': 'application/json',
  254. 'Authorization': 'Bearer test-token'
  255. },
  256. body: JSON.stringify(invalidData)
  257. });
  258. expect(response.status).toBe(400);
  259. });
  260. });
  261. describe('POST /api/v1/files/multipart-complete', () => {
  262. it('should complete multipart upload successfully', async () => {
  263. const mockCompleteData = {
  264. uploadId: 'upload-123',
  265. bucket: 'd8dai',
  266. key: '1/test-file.zip',
  267. parts: [
  268. { partNumber: 1, etag: 'etag1' },
  269. { partNumber: 2, etag: 'etag2' }
  270. ]
  271. };
  272. const mockResponse = {
  273. fileId: 1,
  274. url: 'https://minio.example.com/file.zip',
  275. key: '1/test-file.zip',
  276. size: 2048
  277. };
  278. vi.mocked(mockFileService.completeMultipartUpload).mockResolvedValue(mockResponse);
  279. const response = await app.request('/api/v1/files/multipart-complete', {
  280. method: 'POST',
  281. headers: {
  282. 'Content-Type': 'application/json',
  283. 'Authorization': 'Bearer test-token'
  284. },
  285. body: JSON.stringify(mockCompleteData)
  286. });
  287. expect(response.status).toBe(200);
  288. const result = await response.json();
  289. expect(result).toEqual(mockResponse);
  290. expect(mockFileService.completeMultipartUpload).toHaveBeenCalledWith(mockCompleteData);
  291. });
  292. it('should validate complete multipart request data', async () => {
  293. const invalidData = {
  294. uploadId: 'upload-123',
  295. // Missing required fields
  296. };
  297. const response = await app.request('/api/v1/files/multipart-complete', {
  298. method: 'POST',
  299. headers: {
  300. 'Content-Type': 'application/json',
  301. 'Authorization': 'Bearer test-token'
  302. },
  303. body: JSON.stringify(invalidData)
  304. });
  305. expect(response.status).toBe(400);
  306. });
  307. it('should handle completion errors', async () => {
  308. const completeData = {
  309. uploadId: 'upload-123',
  310. bucket: 'd8dai',
  311. key: '1/test-file.zip',
  312. parts: [{ partNumber: 1, etag: 'etag1' }]
  313. };
  314. vi.mocked(mockFileService.completeMultipartUpload).mockRejectedValue(new Error('Completion failed'));
  315. const response = await app.request('/api/v1/files/multipart-complete', {
  316. method: 'POST',
  317. headers: {
  318. 'Content-Type': 'application/json',
  319. 'Authorization': 'Bearer test-token'
  320. },
  321. body: JSON.stringify(completeData)
  322. });
  323. expect(response.status).toBe(500);
  324. });
  325. });
  326. describe('CRUD Operations', () => {
  327. it('should list files successfully', async () => {
  328. const mockFiles = [
  329. {
  330. id: 1,
  331. name: 'file1.txt',
  332. type: 'text/plain',
  333. size: 1024,
  334. uploadUserId: 1
  335. },
  336. {
  337. id: 2,
  338. name: 'file2.txt',
  339. type: 'text/plain',
  340. size: 2048,
  341. uploadUserId: 1
  342. }
  343. ];
  344. vi.spyOn(mockFileService, 'getAll').mockResolvedValue(mockFiles as File[]);
  345. const response = await app.request('/api/v1/files', {
  346. method: 'GET',
  347. headers: {
  348. 'Authorization': 'Bearer test-token'
  349. }
  350. });
  351. expect(response.status).toBe(200);
  352. const result = await response.json();
  353. expect(result).toEqual(mockFiles);
  354. });
  355. it('should get file by ID successfully', async () => {
  356. const mockFile = {
  357. id: 1,
  358. name: 'file.txt',
  359. type: 'text/plain',
  360. size: 1024,
  361. uploadUserId: 1
  362. };
  363. vi.spyOn(mockFileService, 'getById').mockResolvedValue(mockFile as File);
  364. const response = await app.request('/api/v1/files/1', {
  365. method: 'GET',
  366. headers: {
  367. 'Authorization': 'Bearer test-token'
  368. }
  369. });
  370. expect(response.status).toBe(200);
  371. const result = await response.json();
  372. expect(result).toEqual(mockFile);
  373. });
  374. it('should search files successfully', async () => {
  375. const mockFiles = [
  376. {
  377. id: 1,
  378. name: 'document.pdf',
  379. type: 'application/pdf',
  380. size: 1024,
  381. uploadUserId: 1
  382. }
  383. ];
  384. vi.spyOn(mockFileService, 'search').mockResolvedValue(mockFiles as File[]);
  385. const response = await app.request('/api/v1/files?search=document', {
  386. method: 'GET',
  387. headers: {
  388. 'Authorization': 'Bearer test-token'
  389. }
  390. });
  391. expect(response.status).toBe(200);
  392. const result = await response.json();
  393. expect(result).toEqual(mockFiles);
  394. expect(mockFileService.search).toHaveBeenCalledWith('document', ['name', 'type', 'description']);
  395. });
  396. });
  397. });