files.integration.test.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import { describe, it, expect, beforeEach, vi } from 'vitest';
  2. import { testClient } from 'hono/testing';
  3. import {
  4. IntegrationTestDatabase,
  5. setupIntegrationDatabaseHooks,
  6. TestDataFactory
  7. } from '../utils/integration-test-db';
  8. import { fileApiRoutes } from '../../src/api';
  9. import { AuthService } from '@d8d/auth-module';
  10. import { UserService as UserServiceMt } from '@d8d/user-module';
  11. import { MinioService } from '@d8d/file-module';
  12. // Mock MinIO service to avoid real connections in tests
  13. vi.mock('@d8d/file-module', async (importOriginal) => {
  14. const actual = await importOriginal<typeof import('@d8d/file-module')>();
  15. return {
  16. ...actual,
  17. fileRoutes: actual.fileRoutes, // 确保导出fileRoutesMt
  18. MinioService: vi.fn(() => ({
  19. bucketName: 'd8dai',
  20. ensureBucketExists: vi.fn().mockResolvedValue(true),
  21. objectExists: vi.fn().mockResolvedValue(false),
  22. deleteObject: vi.fn().mockResolvedValue(true),
  23. generateUploadPolicy: vi.fn().mockResolvedValue({
  24. 'x-amz-algorithm': 'AWS4-HMAC-SHA256',
  25. 'x-amz-credential': 'test-credential',
  26. 'x-amz-date': '20250101T120000Z',
  27. policy: 'test-policy',
  28. 'x-amz-signature': 'test-signature',
  29. host: 'https://minio.example.com',
  30. key: 'test-key',
  31. bucket: 'd8dai'
  32. }),
  33. getPresignedFileUrl: vi.fn().mockResolvedValue('https://minio.example.com/presigned-url'),
  34. getPresignedFileDownloadUrl: vi.fn().mockResolvedValue('https://minio.example.com/download-url'),
  35. createMultipartUpload: vi.fn().mockResolvedValue('test-upload-id'),
  36. generateMultipartUploadUrls: vi.fn().mockResolvedValue(['https://minio.example.com/part1', 'https://minio.example.com/part2']),
  37. completeMultipartUpload: vi.fn().mockResolvedValue({
  38. size: 104857600
  39. }),
  40. createObject: vi.fn().mockResolvedValue('https://minio.example.com/d8dai/test-file'),
  41. getFileUrl: vi.fn().mockReturnValue('https://minio.example.com/d8dai/test-file')
  42. }))
  43. };
  44. });
  45. // 设置集成测试钩子
  46. setupIntegrationDatabaseHooks()
  47. describe.skip('文件API连通性测试', () => {
  48. let client: ReturnType<typeof testClient<typeof fileApiRoutes>>['api']['v1'];
  49. let testToken: string;
  50. beforeEach(async () => {
  51. // 创建测试客户端
  52. client = testClient(fileApiRoutes).api.v1;
  53. // 创建测试用户并生成token
  54. const dataSource = await IntegrationTestDatabase.getDataSource();
  55. const userService = new UserServiceMt(dataSource);
  56. const authService = new AuthService(userService);
  57. // 确保admin用户存在
  58. const user = await authService.ensureAdminExists();
  59. // 生成admin用户的token
  60. testToken = authService.generateToken(user);
  61. });
  62. describe('文件上传策略API连通性', () => {
  63. it('应该成功响应文件上传策略请求', async () => {
  64. const fileData = {
  65. name: 'test.txt',
  66. type: 'text/plain',
  67. size: 1024,
  68. path: '/uploads/test.txt',
  69. description: 'Test file'
  70. };
  71. const response = await client.files['upload-policy'].$post({
  72. json: fileData
  73. },
  74. {
  75. headers: {
  76. 'Authorization': `Bearer ${testToken}`
  77. }
  78. });
  79. expect(response.status).toBe(200);
  80. });
  81. it('应该拒绝无认证令牌的请求', async () => {
  82. const fileData = {
  83. name: 'test.txt',
  84. type: 'text/plain',
  85. size: 1024,
  86. path: '/uploads/test.txt'
  87. };
  88. const response = await client.files['upload-policy'].$post({
  89. json: fileData
  90. });
  91. expect(response.status).toBe(401);
  92. });
  93. });
  94. describe('文件URL生成API连通性', () => {
  95. it('应该成功响应文件URL生成请求', async () => {
  96. const dataSource = await IntegrationTestDatabase.getDataSource();
  97. if (!dataSource) throw new Error('Database not initialized');
  98. const testFile = await TestDataFactory.createTestFile(dataSource, {
  99. name: 'testfile_url.txt',
  100. type: 'text/plain',
  101. size: 1024,
  102. path: 'testfile_url.txt'
  103. });
  104. const response = await client.files[':id']['url'].$get({
  105. param: { id: testFile.id }
  106. },
  107. {
  108. headers: {
  109. 'Authorization': `Bearer ${testToken}`
  110. }
  111. });
  112. expect(response.status).toBe(200);
  113. });
  114. it('应该返回404当文件不存在时', async () => {
  115. const response = await client.files[':id']['url'].$get({
  116. param: { id: 999999 }
  117. },
  118. {
  119. headers: {
  120. 'Authorization': `Bearer ${testToken}`
  121. }
  122. });
  123. expect(response.status).toBe(404);
  124. });
  125. });
  126. describe('文件下载URL生成API连通性', () => {
  127. it('应该成功响应文件下载URL生成请求', async () => {
  128. const dataSource = await IntegrationTestDatabase.getDataSource();
  129. if (!dataSource) throw new Error('Database not initialized');
  130. const testFile = await TestDataFactory.createTestFile(dataSource, {
  131. name: 'testfile_download.txt',
  132. type: 'text/plain',
  133. size: 1024,
  134. path: 'testfile_download.txt'
  135. });
  136. const response = await client.files[':id']['download'].$get({
  137. param: { id: testFile.id }
  138. },
  139. {
  140. headers: {
  141. 'Authorization': `Bearer ${testToken}`
  142. }
  143. });
  144. expect(response.status).toBe(200);
  145. });
  146. it('应该返回404当文件不存在时', async () => {
  147. const response = await client.files[':id']['download'].$get({
  148. param: { id: 999999 }
  149. },
  150. {
  151. headers: {
  152. 'Authorization': `Bearer ${testToken}`
  153. }
  154. });
  155. expect(response.status).toBe(404);
  156. });
  157. });
  158. describe('文件CRUD API连通性', () => {
  159. it('应该成功响应文件列表请求', async () => {
  160. const dataSource = await IntegrationTestDatabase.getDataSource();
  161. if (!dataSource) throw new Error('Database not initialized');
  162. await TestDataFactory.createTestFile(dataSource, {
  163. name: 'file1.txt',
  164. type: 'text/plain',
  165. size: 1024,
  166. path: 'file1.txt'
  167. });
  168. const response = await client.files.$get({
  169. query: {}
  170. },
  171. {
  172. headers: {
  173. 'Authorization': `Bearer ${testToken}`
  174. }
  175. });
  176. expect(response.status).toBe(200);
  177. });
  178. it('应该成功响应单个文件详情请求', async () => {
  179. const dataSource = await IntegrationTestDatabase.getDataSource();
  180. if (!dataSource) throw new Error('Database not initialized');
  181. const testFile = await TestDataFactory.createTestFile(dataSource, {
  182. name: 'testfile_detail.txt',
  183. type: 'text/plain',
  184. size: 1024,
  185. path: 'testfile_detail.txt'
  186. });
  187. const response = await client.files[':id'].$get({
  188. param: { id: testFile.id }
  189. },
  190. {
  191. headers: {
  192. 'Authorization': `Bearer ${testToken}`
  193. }
  194. });
  195. expect(response.status).toBe(200);
  196. });
  197. });
  198. describe('多部分上传API连通性', () => {
  199. it('应该成功响应多部分上传策略请求', async () => {
  200. const multipartData = {
  201. fileKey: 'large-file.zip',
  202. totalSize: 1024 * 1024 * 100, // 100MB
  203. partSize: 1024 * 1024 * 20, // 20MB
  204. name: 'large-file.zip',
  205. type: 'application/zip'
  206. };
  207. const response = await client.files['multipart-policy'].$post({
  208. json: multipartData
  209. },
  210. {
  211. headers: {
  212. 'Authorization': `Bearer ${testToken}`
  213. }
  214. });
  215. expect(response.status).toBe(200);
  216. });
  217. });
  218. });