tenant-isolation.integration.test.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. import { describe, it, expect, beforeEach, afterEach } from 'vitest';
  2. import { AppDataSource } from '@d8d/shared-utils';
  3. import { setupIntegrationDatabaseHooksWithEntities } from '@d8d/shared-test-util';
  4. import { FileMt } from '../../src/entities/file.entity';
  5. import { FileServiceMt } from '../../src/services/file.service.mt';
  6. import { UserEntityMt } from '@d8d/user-module-mt/entities';
  7. describe('文件模块多租户数据隔离测试', () => {
  8. let fileService: FileServiceMt;
  9. const tenant1UserId = 1;
  10. const tenant2UserId = 2;
  11. // 设置数据库钩子
  12. setupIntegrationDatabaseHooksWithEntities([FileMt, UserEntityMt]);
  13. beforeEach(async () => {
  14. // 创建文件服务实例
  15. fileService = new FileServiceMt(AppDataSource);
  16. // 清理文件数据
  17. const fileRepository = AppDataSource.getRepository(FileMt);
  18. await fileRepository.delete({});
  19. });
  20. describe('文件创建租户隔离', () => {
  21. it('应该为租户1创建文件并设置正确的租户ID', async () => {
  22. const fileData = {
  23. name: 'tenant1_file.pdf',
  24. type: 'application/pdf',
  25. size: 1024,
  26. path: 'test/path',
  27. uploadUserId: tenant1UserId,
  28. description: '租户1的文件'
  29. };
  30. const result = await fileService.createFile(fileData, 1);
  31. expect(result.file.tenantId).toBe(1);
  32. expect(result.file.name).toBe('tenant1_file.pdf');
  33. expect(result.file.uploadUserId).toBe(tenant1UserId);
  34. });
  35. it('应该为租户2创建文件并设置正确的租户ID', async () => {
  36. const fileData = {
  37. name: 'tenant2_file.pdf',
  38. type: 'application/pdf',
  39. size: 2048,
  40. path: 'test/path',
  41. uploadUserId: tenant2UserId,
  42. description: '租户2的文件'
  43. };
  44. const result = await fileService.createFile(fileData, 2);
  45. expect(result.file.tenantId).toBe(2);
  46. expect(result.file.name).toBe('tenant2_file.pdf');
  47. expect(result.file.uploadUserId).toBe(tenant2UserId);
  48. });
  49. });
  50. describe('文件查询租户隔离', () => {
  51. beforeEach(async () => {
  52. // 创建测试文件
  53. const fileRepository = AppDataSource.getRepository(FileMt);
  54. // 租户1的文件
  55. await fileRepository.save([
  56. fileRepository.create({
  57. name: 'tenant1_file1.pdf',
  58. type: 'application/pdf',
  59. size: 1024,
  60. path: 'tenant1/path1',
  61. uploadUserId: tenant1UserId,
  62. tenantId: 1,
  63. uploadTime: new Date()
  64. }),
  65. fileRepository.create({
  66. name: 'tenant1_file2.jpg',
  67. type: 'image/jpeg',
  68. size: 2048,
  69. path: 'tenant1/path2',
  70. uploadUserId: tenant1UserId,
  71. tenantId: 1,
  72. uploadTime: new Date()
  73. })
  74. ]);
  75. // 租户2的文件
  76. await fileRepository.save([
  77. fileRepository.create({
  78. name: 'tenant2_file1.pdf',
  79. type: 'application/pdf',
  80. size: 3072,
  81. path: 'tenant2/path1',
  82. uploadUserId: tenant2UserId,
  83. tenantId: 2,
  84. uploadTime: new Date()
  85. })
  86. ]);
  87. });
  88. it('应该只返回租户1的文件列表', async () => {
  89. const files = await fileService.findAll({ tenantId: 1 });
  90. expect(files).toHaveLength(2);
  91. expect(files.every(file => file.tenantId === 1)).toBe(true);
  92. expect(files.some(file => file.name === 'tenant1_file1.pdf')).toBe(true);
  93. expect(files.some(file => file.name === 'tenant1_file2.jpg')).toBe(true);
  94. });
  95. it('应该只返回租户2的文件列表', async () => {
  96. const files = await fileService.findAll({ tenantId: 2 });
  97. expect(files).toHaveLength(1);
  98. expect(files[0].tenantId).toBe(2);
  99. expect(files[0].name).toBe('tenant2_file1.pdf');
  100. });
  101. it('应该正确获取租户1的特定文件', async () => {
  102. const fileRepository = AppDataSource.getRepository(FileMt);
  103. const tenant1File = await fileRepository.findOneBy({ tenantId: 1, name: 'tenant1_file1.pdf' });
  104. if (tenant1File) {
  105. const file = await fileService.getById(tenant1File.id, { tenantId: 1 });
  106. expect(file).toBeDefined();
  107. expect(file?.tenantId).toBe(1);
  108. expect(file?.name).toBe('tenant1_file1.pdf');
  109. }
  110. });
  111. it('租户1不应该访问租户2的文件', async () => {
  112. const fileRepository = AppDataSource.getRepository(FileMt);
  113. const tenant2File = await fileRepository.findOneBy({ tenantId: 2, name: 'tenant2_file1.pdf' });
  114. if (tenant2File) {
  115. const file = await fileService.getById(tenant2File.id, { tenantId: 1 });
  116. expect(file).toBeNull();
  117. }
  118. });
  119. });
  120. describe('文件更新租户隔离', () => {
  121. let tenant1File: FileMt;
  122. beforeEach(async () => {
  123. // 创建租户1的测试文件
  124. const fileRepository = AppDataSource.getRepository(FileMt);
  125. tenant1File = fileRepository.create({
  126. name: 'original_name.pdf',
  127. type: 'application/pdf',
  128. size: 1024,
  129. path: 'tenant1/original',
  130. uploadUserId: tenant1UserId,
  131. tenantId: 1,
  132. uploadTime: new Date()
  133. });
  134. await fileRepository.save(tenant1File);
  135. });
  136. it('应该允许租户1更新自己的文件', async () => {
  137. const updateData = {
  138. name: 'updated_name.pdf',
  139. description: '更新后的描述'
  140. };
  141. const updatedFile = await fileService.update(tenant1File.id, updateData, { tenantId: 1 });
  142. expect(updatedFile.name).toBe('updated_name.pdf');
  143. expect(updatedFile.description).toBe('更新后的描述');
  144. expect(updatedFile.tenantId).toBe(1);
  145. });
  146. it('不应该允许租户2更新租户1的文件', async () => {
  147. const updateData = {
  148. name: 'hacked_name.pdf'
  149. };
  150. await expect(fileService.update(tenant1File.id, updateData, { tenantId: 2 }))
  151. .rejects.toThrow();
  152. });
  153. });
  154. describe('文件删除租户隔离', () => {
  155. let tenant1File: FileMt;
  156. let tenant2File: FileMt;
  157. beforeEach(async () => {
  158. // 创建测试文件
  159. const fileRepository = AppDataSource.getRepository(FileMt);
  160. tenant1File = fileRepository.create({
  161. name: 'tenant1_delete_test.pdf',
  162. type: 'application/pdf',
  163. size: 1024,
  164. path: 'tenant1/delete_test',
  165. uploadUserId: tenant1UserId,
  166. tenantId: 1,
  167. uploadTime: new Date()
  168. });
  169. tenant2File = fileRepository.create({
  170. name: 'tenant2_delete_test.pdf',
  171. type: 'application/pdf',
  172. size: 2048,
  173. path: 'tenant2/delete_test',
  174. uploadUserId: tenant2UserId,
  175. tenantId: 2,
  176. uploadTime: new Date()
  177. });
  178. await fileRepository.save([tenant1File, tenant2File]);
  179. });
  180. it('应该允许租户1删除自己的文件', async () => {
  181. const result = await fileService.delete(tenant1File.id, { tenantId: 1 });
  182. expect(result).toBe(true);
  183. // 验证文件已被删除
  184. const fileRepository = AppDataSource.getRepository(FileMt);
  185. const deletedFile = await fileRepository.findOneBy({ id: tenant1File.id });
  186. expect(deletedFile).toBeNull();
  187. });
  188. it('不应该允许租户2删除租户1的文件', async () => {
  189. await expect(fileService.delete(tenant1File.id, { tenantId: 2 }))
  190. .rejects.toThrow();
  191. // 验证文件仍然存在
  192. const fileRepository = AppDataSource.getRepository(FileMt);
  193. const existingFile = await fileRepository.findOneBy({ id: tenant1File.id });
  194. expect(existingFile).toBeDefined();
  195. });
  196. it('应该允许租户2删除自己的文件', async () => {
  197. const result = await fileService.delete(tenant2File.id, { tenantId: 2 });
  198. expect(result).toBe(true);
  199. // 验证文件已被删除
  200. const fileRepository = AppDataSource.getRepository(FileMt);
  201. const deletedFile = await fileRepository.findOneBy({ id: tenant2File.id });
  202. expect(deletedFile).toBeNull();
  203. });
  204. });
  205. describe('文件存储路径租户隔离', () => {
  206. it('应该为租户1的文件生成包含租户ID的存储路径', async () => {
  207. const fileData = {
  208. name: 'tenant1_path_test.pdf',
  209. type: 'application/pdf',
  210. size: 1024,
  211. uploadUserId: tenant1UserId,
  212. description: '租户1路径测试文件'
  213. };
  214. const result = await fileService.createFile(fileData, 1);
  215. expect(result.file.path).toContain('tenants/1/');
  216. expect(result.file.path).toContain(tenant1UserId.toString());
  217. expect(result.file.path).toContain('tenant1_path_test.pdf');
  218. });
  219. it('应该为租户2的文件生成包含租户ID的存储路径', async () => {
  220. const fileData = {
  221. name: 'tenant2_path_test.pdf',
  222. type: 'application/pdf',
  223. size: 1024,
  224. uploadUserId: tenant2UserId,
  225. description: '租户2路径测试文件'
  226. };
  227. const result = await fileService.createFile(fileData, 2);
  228. expect(result.file.path).toContain('tenants/2/');
  229. expect(result.file.path).toContain(tenant2UserId.toString());
  230. expect(result.file.path).toContain('tenant2_path_test.pdf');
  231. });
  232. });
  233. });