person-extension.integration.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. import { describe, it, expect, beforeEach } from 'vitest';
  2. import { testClient } from 'hono/testing';
  3. import { IntegrationTestDatabase, setupIntegrationDatabaseHooksWithEntities } from '@d8d/shared-test-util';
  4. import { JWTUtil } from '@d8d/shared-utils';
  5. import { UserEntity, Role } from '@d8d/user-module';
  6. import { File } from '@d8d/file-module';
  7. import { Platform } from '@d8d/allin-platform-module/entities';
  8. import { EmploymentOrder, OrderPerson, OrderPersonAsset } from '@d8d/allin-order-module/entities';
  9. import { DisabledPerson, DisabledBankCard } from '../../src/entities';
  10. import personExtensionRoutes from '../../src/routes/person-extension.route';
  11. import { Company } from '@d8d/allin-company-module/entities';
  12. // 设置集成测试钩子 - 需要包含所有相关实体
  13. setupIntegrationDatabaseHooksWithEntities([
  14. UserEntity, File, Role, Platform, Company,
  15. EmploymentOrder, OrderPerson, OrderPersonAsset,
  16. DisabledPerson, DisabledBankCard
  17. ])
  18. describe('人才扩展API集成测试', () => {
  19. let client: ReturnType<typeof testClient<typeof personExtensionRoutes>>;
  20. let testToken: string;
  21. let testUser: UserEntity;
  22. let testCompany: Company;
  23. let testPlatform: Platform;
  24. let testDisabledPerson: DisabledPerson;
  25. let testOrder: EmploymentOrder;
  26. beforeEach(async () => {
  27. // 创建测试客户端
  28. client = testClient(personExtensionRoutes);
  29. // 获取数据源
  30. const dataSource = await IntegrationTestDatabase.getDataSource();
  31. // 创建测试平台
  32. const platformRepository = dataSource.getRepository(Platform);
  33. testPlatform = platformRepository.create({
  34. platformName: `测试平台_${Date.now()}`,
  35. contactPerson: '平台管理员',
  36. contactPhone: '13800138000',
  37. contactEmail: 'admin@example.com',
  38. address: '测试地址',
  39. status: 1
  40. });
  41. await platformRepository.save(testPlatform);
  42. // 创建测试公司
  43. const companyRepository = dataSource.getRepository(Company);
  44. testCompany = companyRepository.create({
  45. companyName: `测试公司_${Date.now()}`,
  46. contactPerson: '公司联系人',
  47. contactPhone: '13900139000',
  48. contactEmail: 'company@example.com',
  49. address: '公司地址',
  50. platformId: testPlatform.id,
  51. status: 1
  52. });
  53. await companyRepository.save(testCompany);
  54. // 创建测试企业用户
  55. const userRepository = dataSource.getRepository(UserEntity);
  56. testUser = userRepository.create({
  57. username: `enterprise_user_${Date.now()}`,
  58. password: 'test_password',
  59. nickname: '企业测试用户',
  60. registrationSource: 'web',
  61. companyId: testCompany.id
  62. });
  63. await userRepository.save(testUser);
  64. // 生成测试用户的token
  65. testToken = JWTUtil.generateToken({
  66. id: testUser.id,
  67. username: testUser.username,
  68. roles: [{ name: 'enterprise_user' }],
  69. companyId: testCompany.id
  70. });
  71. // 创建测试残疾人
  72. const disabledPersonRepo = dataSource.getRepository(DisabledPerson);
  73. testDisabledPerson = disabledPersonRepo.create({
  74. name: '测试残疾人',
  75. idCard: `110101${Date.now() % 100000000}`,
  76. gender: 1,
  77. birthDate: new Date('1990-01-01'),
  78. disabilityType: '视力残疾',
  79. disabilityLevel: '一级',
  80. address: '测试地址'
  81. });
  82. await disabledPersonRepo.save(testDisabledPerson);
  83. // 创建测试订单
  84. const orderRepo = dataSource.getRepository(EmploymentOrder);
  85. testOrder = orderRepo.create({
  86. orderName: '测试订单',
  87. platformId: testPlatform.id,
  88. companyId: testCompany.id,
  89. orderStatus: 'confirmed',
  90. workStatus: 'working'
  91. });
  92. await orderRepo.save(testOrder);
  93. // 创建订单人员关联,使人员属于该企业
  94. const orderPersonRepo = dataSource.getRepository(OrderPerson);
  95. const orderPerson = orderPersonRepo.create({
  96. orderId: testOrder.id,
  97. personId: testDisabledPerson.id,
  98. joinDate: new Date('2024-01-01'),
  99. workStatus: 'working',
  100. salaryDetail: 5000.00
  101. });
  102. await orderPersonRepo.save(orderPerson);
  103. });
  104. describe('GET /api/v1/yongren/disability-person/{id}/work-history', () => {
  105. it('应该返回人员工作历史', async () => {
  106. // 准备测试数据:多个订单关联
  107. const dataSource = await IntegrationTestDatabase.getDataSource();
  108. const orderPersonRepo = dataSource.getRepository(OrderPerson);
  109. // 创建另一个订单关联
  110. const orderRepo = dataSource.getRepository(EmploymentOrder);
  111. const anotherOrder = orderRepo.create({
  112. orderName: '另一个测试订单',
  113. platformId: testPlatform.id,
  114. companyId: testCompany.id,
  115. orderStatus: 'completed',
  116. workStatus: 'working'
  117. });
  118. await orderRepo.save(anotherOrder);
  119. const anotherOrderPerson = orderPersonRepo.create({
  120. orderId: anotherOrder.id,
  121. personId: testDisabledPerson.id,
  122. joinDate: new Date('2024-06-01'),
  123. actualStartDate: new Date('2024-06-02'),
  124. leaveDate: new Date('2024-12-31'),
  125. workStatus: 'resigned',
  126. salaryDetail: 6000.00
  127. });
  128. await orderPersonRepo.save(anotherOrderPerson);
  129. // 调用API
  130. const response = await client.api.v1.yongren.disabilityPerson[':id'].workHistory.$get({
  131. param: { id: testDisabledPerson.id },
  132. header: {
  133. Authorization: `Bearer ${testToken}`
  134. }
  135. });
  136. expect(response.status).toBe(200);
  137. const data = await response.json();
  138. // 验证响应结构
  139. expect(data).toHaveProperty('工作历史');
  140. expect(Array.isArray(data.工作历史)).toBe(true);
  141. // 验证数据
  142. expect(data.工作历史.length).toBeGreaterThanOrEqual(1);
  143. const workHistory = data.工作历史[0];
  144. expect(workHistory).toHaveProperty('订单ID');
  145. expect(workHistory).toHaveProperty('订单名称');
  146. expect(workHistory).toHaveProperty('入职日期');
  147. expect(workHistory).toHaveProperty('工作状态');
  148. expect(workHistory).toHaveProperty('个人薪资');
  149. });
  150. it('访问其他企业人员应该返回403', async () => {
  151. // 创建另一个公司的残疾人
  152. const dataSource = await IntegrationTestDatabase.getDataSource();
  153. // 创建另一个公司
  154. const companyRepository = dataSource.getRepository(Company);
  155. const otherCompany = companyRepository.create({
  156. companyName: `其他公司_${Date.now()}`,
  157. contactPerson: '其他联系人',
  158. contactPhone: '13900139001',
  159. contactEmail: 'other@example.com',
  160. address: '其他地址',
  161. platformId: testPlatform.id,
  162. status: 1
  163. });
  164. await companyRepository.save(otherCompany);
  165. // 创建另一个公司的残疾人
  166. const disabledPersonRepo = dataSource.getRepository(DisabledPerson);
  167. const otherDisabledPerson = disabledPersonRepo.create({
  168. name: '其他公司残疾人',
  169. idCard: `110101${Date.now() % 100000000 + 1000}`,
  170. gender: 2,
  171. birthDate: new Date('1995-01-01'),
  172. disabilityType: '听力残疾',
  173. disabilityLevel: '二级',
  174. address: '其他地址'
  175. });
  176. await disabledPersonRepo.save(otherDisabledPerson);
  177. // 尝试访问其他公司人员数据
  178. const response = await client.api.v1.yongren.disabilityPerson[':id'].workHistory.$get({
  179. param: { id: otherDisabledPerson.id },
  180. header: {
  181. Authorization: `Bearer ${testToken}`
  182. }
  183. });
  184. expect(response.status).toBe(403);
  185. });
  186. });
  187. describe('GET /api/v1/yongren/disability-person/{id}/salary-history', () => {
  188. it('应该返回人员薪资历史', async () => {
  189. // 注意:薪资历史可能需要从薪资模块获取,这里暂时返回空数组
  190. const response = await client.api.v1.yongren.disabilityPerson[':id'].salaryHistory.$get({
  191. param: { id: testDisabledPerson.id },
  192. header: {
  193. Authorization: `Bearer ${testToken}`
  194. }
  195. });
  196. expect(response.status).toBe(200);
  197. const data = await response.json();
  198. // 验证响应结构
  199. expect(data).toHaveProperty('薪资历史');
  200. expect(Array.isArray(data.薪资历史)).toBe(true);
  201. });
  202. });
  203. describe('GET /api/v1/yongren/disability-person/{id}/credit-info', () => {
  204. it('应该返回人员征信信息', async () => {
  205. // 准备测试数据:银行卡信息
  206. const dataSource = await IntegrationTestDatabase.getDataSource();
  207. // 创建文件
  208. const fileRepository = dataSource.getRepository(File);
  209. const testFile = fileRepository.create({
  210. name: '银行卡照片.jpg',
  211. type: 'image/jpeg',
  212. size: 1024,
  213. path: 'uploads/bank-card.jpg',
  214. description: '银行卡照片',
  215. uploadUserId: testUser.id,
  216. uploadTime: new Date()
  217. });
  218. await fileRepository.save(testFile);
  219. // 创建银行卡记录
  220. const bankCardRepo = dataSource.getRepository(DisabledBankCard);
  221. const bankCard = bankCardRepo.create({
  222. personId: testDisabledPerson.id,
  223. subBankName: '测试支行',
  224. bankNameId: 1, // 假设银行ID为1
  225. cardNumber: '6228481234567890123',
  226. cardholderName: '测试持卡人',
  227. cardType: '一类卡',
  228. fileId: testFile.id,
  229. isDefault: 1
  230. });
  231. await bankCardRepo.save(bankCard);
  232. // 调用API
  233. const response = await client.api.v1.yongren.disabilityPerson[':id'].creditInfo.$get({
  234. param: { id: testDisabledPerson.id },
  235. header: {
  236. Authorization: `Bearer ${testToken}`
  237. }
  238. });
  239. expect(response.status).toBe(200);
  240. const data = await response.json();
  241. // 验证响应结构
  242. expect(data).toHaveProperty('征信信息');
  243. expect(Array.isArray(data.征信信息)).toBe(true);
  244. if (data.征信信息.length > 0) {
  245. const creditInfo = data.征信信息[0];
  246. expect(creditInfo).toHaveProperty('文件ID');
  247. expect(creditInfo).toHaveProperty('银行卡号');
  248. expect(creditInfo).toHaveProperty('持卡人姓名');
  249. }
  250. });
  251. });
  252. describe('GET /api/v1/yongren/disability-person/{id}/videos', () => {
  253. it('应该返回人员视频关联', async () => {
  254. // 准备测试数据:订单人员资产(视频)
  255. const dataSource = await IntegrationTestDatabase.getDataSource();
  256. // 创建文件
  257. const fileRepository = dataSource.getRepository(File);
  258. const testFile = fileRepository.create({
  259. name: '工作视频.mp4',
  260. type: 'video/mp4',
  261. size: 1024000,
  262. path: 'uploads/work-video.mp4',
  263. description: '工作视频',
  264. uploadUserId: testUser.id,
  265. uploadTime: new Date()
  266. });
  267. await fileRepository.save(testFile);
  268. // 创建订单人员资产记录
  269. const assetRepo = dataSource.getRepository(OrderPersonAsset);
  270. const asset = assetRepo.create({
  271. orderId: testOrder.id,
  272. personId: testDisabledPerson.id,
  273. assetType: 'work_video',
  274. assetFileType: 'video',
  275. fileId: testFile.id,
  276. relatedTime: new Date()
  277. });
  278. await assetRepo.save(asset);
  279. // 调用API
  280. const response = await client.api.v1.yongren.disabilityPerson[':id'].videos.$get({
  281. param: { id: testDisabledPerson.id },
  282. header: {
  283. Authorization: `Bearer ${testToken}`
  284. }
  285. });
  286. expect(response.status).toBe(200);
  287. const data = await response.json();
  288. // 验证响应结构
  289. expect(data).toHaveProperty('视频列表');
  290. expect(Array.isArray(data.视频列表)).toBe(true);
  291. if (data.视频列表.length > 0) {
  292. const video = data.视频列表[0];
  293. expect(video).toHaveProperty('视频类型');
  294. expect(video).toHaveProperty('文件ID');
  295. expect(video).toHaveProperty('关联订单ID');
  296. }
  297. });
  298. });
  299. });