files.integration.test.ts 13 KB

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