disability.integration.test.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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 disabledPersonRoutes from '../../src/routes/disabled-person.routes';
  8. import { DisabledPerson } from '../../src/entities/disabled-person.entity';
  9. import { DisabledBankCard } from '../../src/entities/disabled-bank-card.entity';
  10. import { DisabledPhoto } from '../../src/entities/disabled-photo.entity';
  11. import { DisabledRemark } from '../../src/entities/disabled-remark.entity';
  12. import { DisabledVisit } from '../../src/entities/disabled-visit.entity';
  13. // 设置集成测试钩子 - 包含所有相关实体
  14. setupIntegrationDatabaseHooksWithEntities([
  15. UserEntity,
  16. Role,
  17. File,
  18. DisabledPerson,
  19. DisabledBankCard,
  20. DisabledPhoto,
  21. DisabledRemark,
  22. DisabledVisit
  23. ])
  24. describe('残疾人管理API集成测试', () => {
  25. let client: ReturnType<typeof testClient<typeof disabledPersonRoutes>>;
  26. let testToken: string;
  27. let testUser: UserEntity;
  28. let testFile: File;
  29. beforeEach(async () => {
  30. // 创建测试客户端
  31. client = testClient(disabledPersonRoutes);
  32. // 获取数据源
  33. const dataSource = await IntegrationTestDatabase.getDataSource();
  34. // 创建测试用户
  35. const userRepository = dataSource.getRepository(UserEntity);
  36. testUser = userRepository.create({
  37. username: `test_user_${Date.now()}`,
  38. password: 'test_password',
  39. nickname: '测试用户',
  40. registrationSource: 'web'
  41. });
  42. await userRepository.save(testUser);
  43. // 创建测试文件(用于照片集成测试)
  44. const fileRepository = dataSource.getRepository(File);
  45. testFile = fileRepository.create({
  46. name: 'test_photo.jpg',
  47. path: 'test_photo.jpg',
  48. type: 'image/jpeg',
  49. size: 1024,
  50. uploadUserId: testUser.id,
  51. uploadTime: new Date()
  52. });
  53. await fileRepository.save(testFile);
  54. // 生成测试用户的token
  55. testToken = JWTUtil.generateToken({
  56. id: testUser.id,
  57. username: testUser.username,
  58. roles: [{name:'user'}]
  59. });
  60. });
  61. describe('POST /createDisabledPerson', () => {
  62. it('应该成功创建残疾人基本信息', async () => {
  63. const createData = {
  64. name: '张三',
  65. gender: '男',
  66. idCard: '110101199001011234',
  67. disabilityId: 'D123456789',
  68. disabilityType: '肢体残疾',
  69. disabilityLevel: '一级',
  70. idAddress: '北京市东城区',
  71. phone: '13800138000',
  72. province: '北京市',
  73. city: '北京市'
  74. };
  75. const response = await client.createDisabledPerson.$post({
  76. json: createData
  77. }, {
  78. headers: {
  79. 'Authorization': `Bearer ${testToken}`
  80. }
  81. });
  82. expect(response.status).toBe(200);
  83. if (response.status === 200) {
  84. const data = await response.json();
  85. expect(data.name).toBe(createData.name);
  86. expect(data.idCard).toBe(createData.idCard);
  87. expect(data.disabilityId).toBe(createData.disabilityId);
  88. }
  89. });
  90. it('应该验证身份证号唯一性', async () => {
  91. // 先创建一个残疾人
  92. const dataSource = await IntegrationTestDatabase.getDataSource();
  93. const disabledPersonRepository = dataSource.getRepository(DisabledPerson);
  94. const existingPerson = disabledPersonRepository.create({
  95. name: '李四',
  96. gender: '女',
  97. idCard: '110101199001011235',
  98. disabilityId: 'D123456788',
  99. disabilityType: '视力残疾',
  100. disabilityLevel: '二级',
  101. idAddress: '北京市西城区',
  102. phone: '13900139000',
  103. province: '北京市',
  104. city: '北京市'
  105. });
  106. await disabledPersonRepository.save(existingPerson);
  107. // 尝试创建相同身份证号的残疾人
  108. const createData = {
  109. name: '王五',
  110. gender: '男',
  111. idCard: '110101199001011235', // 重复的身份证号
  112. disabilityId: 'D123456777',
  113. disabilityType: '听力残疾',
  114. disabilityLevel: '三级',
  115. idAddress: '北京市朝阳区',
  116. phone: '13700137000',
  117. province: '北京市',
  118. city: '北京市'
  119. };
  120. const response = await client.createDisabledPerson.$post({
  121. json: createData
  122. }, {
  123. headers: {
  124. 'Authorization': `Bearer ${testToken}`
  125. }
  126. });
  127. expect(response.status).toBe(400);
  128. });
  129. it('应该验证必填字段', async () => {
  130. const createData = {
  131. name: '', // 空字符串,应该验证失败
  132. gender: '男',
  133. idCard: '110101199001011236',
  134. disabilityId: 'D123456776',
  135. disabilityType: '肢体残疾',
  136. disabilityLevel: '一级',
  137. idAddress: '北京市海淀区',
  138. phone: '13600136000',
  139. province: '北京市',
  140. city: '北京市'
  141. };
  142. const response = await client.createDisabledPerson.$post({
  143. json: createData
  144. }, {
  145. headers: {
  146. 'Authorization': `Bearer ${testToken}`
  147. }
  148. });
  149. expect(response.status).toBe(400);
  150. });
  151. });
  152. describe('POST /deleteDisabledPerson', () => {
  153. it('应该成功删除残疾人', async () => {
  154. // 先创建一个残疾人
  155. const dataSource = await IntegrationTestDatabase.getDataSource();
  156. const disabledPersonRepository = dataSource.getRepository(DisabledPerson);
  157. const person = disabledPersonRepository.create({
  158. name: '测试删除人员',
  159. gender: '男',
  160. idCard: '110101199001011237',
  161. disabilityId: 'D123456775',
  162. disabilityType: '肢体残疾',
  163. disabilityLevel: '一级',
  164. idAddress: '北京市石景山区',
  165. phone: '13500135000',
  166. province: '北京市',
  167. city: '北京市'
  168. });
  169. await disabledPersonRepository.save(person);
  170. const deleteData = {
  171. id: person.id
  172. };
  173. const response = await client.deleteDisabledPerson.$post({
  174. json: deleteData
  175. }, {
  176. headers: {
  177. 'Authorization': `Bearer ${testToken}`
  178. }
  179. });
  180. expect(response.status).toBe(200);
  181. if (response.status === 200) {
  182. const data = await response.json();
  183. expect(data.success).toBe(true);
  184. // 验证残疾人已被删除
  185. const deletedPerson = await disabledPersonRepository.findOne({ where: { id: person.id } });
  186. expect(deletedPerson).toBeNull();
  187. }
  188. });
  189. it('应该处理不存在的残疾人ID', async () => {
  190. const deleteData = {
  191. id: 99999 // 不存在的ID
  192. };
  193. const response = await client.deleteDisabledPerson.$post({
  194. json: deleteData
  195. }, {
  196. headers: {
  197. 'Authorization': `Bearer ${testToken}`
  198. }
  199. });
  200. expect(response.status).toBe(404);
  201. });
  202. });
  203. describe('POST /updateDisabledPerson', () => {
  204. it('应该成功更新残疾人信息', async () => {
  205. // 先创建一个残疾人
  206. const dataSource = await IntegrationTestDatabase.getDataSource();
  207. const disabledPersonRepository = dataSource.getRepository(DisabledPerson);
  208. const person = disabledPersonRepository.create({
  209. name: '原始姓名',
  210. gender: '男',
  211. idCard: '110101199001011238',
  212. disabilityId: 'D123456774',
  213. disabilityType: '肢体残疾',
  214. disabilityLevel: '一级',
  215. idAddress: '北京市通州区',
  216. phone: '13400134000',
  217. province: '北京市',
  218. city: '北京市'
  219. });
  220. await disabledPersonRepository.save(person);
  221. const updateData = {
  222. id: person.id,
  223. name: '更新后的姓名',
  224. gender: '女',
  225. phone: '13300133000'
  226. };
  227. const response = await client.updateDisabledPerson.$post({
  228. json: updateData
  229. }, {
  230. headers: {
  231. 'Authorization': `Bearer ${testToken}`
  232. }
  233. });
  234. expect(response.status).toBe(200);
  235. if (response.status === 200) {
  236. const data = await response.json();
  237. expect(data.name).toBe(updateData.name);
  238. expect(data.gender).toBe(updateData.gender);
  239. expect(data.phone).toBe(updateData.phone);
  240. }
  241. });
  242. it('应该验证身份证号唯一性(更新时)', async () => {
  243. // 创建两个残疾人
  244. const dataSource = await IntegrationTestDatabase.getDataSource();
  245. const disabledPersonRepository = dataSource.getRepository(DisabledPerson);
  246. const person1 = disabledPersonRepository.create({
  247. name: '人员A',
  248. gender: '男',
  249. idCard: '110101199001011239',
  250. disabilityId: 'D123456773',
  251. disabilityType: '肢体残疾',
  252. disabilityLevel: '一级',
  253. idAddress: '北京市顺义区',
  254. phone: '13200132000',
  255. province: '北京市',
  256. city: '北京市'
  257. });
  258. await disabledPersonRepository.save(person1);
  259. const person2 = disabledPersonRepository.create({
  260. name: '人员B',
  261. gender: '女',
  262. idCard: '110101199001011240',
  263. disabilityId: 'D123456772',
  264. disabilityType: '视力残疾',
  265. disabilityLevel: '二级',
  266. idAddress: '北京市大兴区',
  267. phone: '13100131000',
  268. province: '北京市',
  269. city: '北京市'
  270. });
  271. await disabledPersonRepository.save(person2);
  272. // 尝试将人员2的身份证号改为人员1的身份证号
  273. const updateData = {
  274. id: person2.id,
  275. idCard: '110101199001011239' // 重复的身份证号
  276. };
  277. const response = await client.updateDisabledPerson.$post({
  278. json: updateData
  279. }, {
  280. headers: {
  281. 'Authorization': `Bearer ${testToken}`
  282. }
  283. });
  284. expect(response.status).toBe(400);
  285. });
  286. it('应该处理不存在的残疾人', async () => {
  287. const updateData = {
  288. id: 99999, // 不存在的ID
  289. name: '新姓名'
  290. };
  291. const response = await client.updateDisabledPerson.$post({
  292. json: updateData
  293. }, {
  294. headers: {
  295. 'Authorization': `Bearer ${testToken}`
  296. }
  297. });
  298. expect(response.status).toBe(404);
  299. });
  300. });
  301. describe('GET /getAllDisabledPersons', () => {
  302. it('应该成功获取残疾人列表(分页)', async () => {
  303. // 创建一些测试数据
  304. const dataSource = await IntegrationTestDatabase.getDataSource();
  305. const disabledPersonRepository = dataSource.getRepository(DisabledPerson);
  306. for (let i = 1; i <= 5; i++) {
  307. const person = disabledPersonRepository.create({
  308. name: `残疾人${i}`,
  309. gender: i % 2 === 0 ? '女' : '男',
  310. idCard: `1101011990010112${40 + i}`,
  311. disabilityId: `D1234567${70 + i}`,
  312. disabilityType: '肢体残疾',
  313. disabilityLevel: '一级',
  314. idAddress: `北京市测试区${i}`,
  315. phone: `138001380${i}`,
  316. province: '北京市',
  317. city: '北京市'
  318. });
  319. await disabledPersonRepository.save(person);
  320. }
  321. const response = await client.getAllDisabledPersons.$get({
  322. query: {
  323. skip: 0,
  324. take: 10
  325. }
  326. }, {
  327. headers: {
  328. 'Authorization': `Bearer ${testToken}`
  329. }
  330. });
  331. expect(response.status).toBe(200);
  332. if (response.status === 200) {
  333. const data = await response.json();
  334. expect(data.data).toHaveLength(5);
  335. expect(data.total).toBe(5);
  336. expect(data.data[0].name).toBe('残疾人5'); // 按ID降序排列
  337. }
  338. });
  339. it('应该处理分页参数', async () => {
  340. // 创建更多测试数据
  341. const dataSource = await IntegrationTestDatabase.getDataSource();
  342. const disabledPersonRepository = dataSource.getRepository(DisabledPerson);
  343. for (let i = 1; i <= 15; i++) {
  344. const person = disabledPersonRepository.create({
  345. name: `分页人员${i}`,
  346. gender: i % 2 === 0 ? '女' : '男',
  347. idCard: `1101011990010113${i}`,
  348. disabilityId: `D1234568${i}`,
  349. disabilityType: '肢体残疾',
  350. disabilityLevel: '一级',
  351. idAddress: `北京市分页区${i}`,
  352. phone: `138001381${i}`,
  353. province: '北京市',
  354. city: '北京市'
  355. });
  356. await disabledPersonRepository.save(person);
  357. }
  358. const response = await client.getAllDisabledPersons.$get({
  359. query: {
  360. skip: 5,
  361. take: 5
  362. }
  363. }, {
  364. headers: {
  365. 'Authorization': `Bearer ${testToken}`
  366. }
  367. });
  368. expect(response.status).toBe(200);
  369. if (response.status === 200) {
  370. const data = await response.json();
  371. expect(data.data).toHaveLength(5);
  372. expect(data.total).toBe(15);
  373. }
  374. });
  375. });
  376. describe('GET /searchDisabledPersons', () => {
  377. it('应该成功按姓名搜索残疾人', async () => {
  378. // 创建测试数据
  379. const dataSource = await IntegrationTestDatabase.getDataSource();
  380. const disabledPersonRepository = dataSource.getRepository(DisabledPerson);
  381. const person1 = disabledPersonRepository.create({
  382. name: '张三',
  383. gender: '男',
  384. idCard: '110101199001011241',
  385. disabilityId: 'D123456771',
  386. disabilityType: '肢体残疾',
  387. disabilityLevel: '一级',
  388. idAddress: '北京市昌平区',
  389. phone: '13000130001',
  390. province: '北京市',
  391. city: '北京市'
  392. });
  393. await disabledPersonRepository.save(person1);
  394. const person2 = disabledPersonRepository.create({
  395. name: '李四',
  396. gender: '女',
  397. idCard: '110101199001011242',
  398. disabilityId: 'D123456770',
  399. disabilityType: '视力残疾',
  400. disabilityLevel: '二级',
  401. idAddress: '北京市平谷区',
  402. phone: '13000130002',
  403. province: '北京市',
  404. city: '北京市'
  405. });
  406. await disabledPersonRepository.save(person2);
  407. const response = await client.searchDisabledPersons.$get({
  408. query: {
  409. keyword: '张三',
  410. skip: 0,
  411. take: 10
  412. }
  413. }, {
  414. headers: {
  415. 'Authorization': `Bearer ${testToken}`
  416. }
  417. });
  418. expect(response.status).toBe(200);
  419. if (response.status === 200) {
  420. const data = await response.json();
  421. expect(data.data).toHaveLength(1);
  422. expect(data.data[0].name).toBe('张三');
  423. }
  424. });
  425. it('应该验证搜索关键词不能为空', async () => {
  426. const response = await client.searchDisabledPersons.$get({
  427. query: {
  428. keyword: '', // 空关键词
  429. skip: 0,
  430. take: 10
  431. }
  432. }, {
  433. headers: {
  434. 'Authorization': `Bearer ${testToken}`
  435. }
  436. });
  437. expect(response.status).toBe(400);
  438. });
  439. });
  440. describe('GET /getDisabledPerson/{id}', () => {
  441. it('应该成功获取单个残疾人详情', async () => {
  442. // 先创建一个残疾人
  443. const dataSource = await IntegrationTestDatabase.getDataSource();
  444. const disabledPersonRepository = dataSource.getRepository(DisabledPerson);
  445. const person = disabledPersonRepository.create({
  446. name: '测试人员详情',
  447. gender: '男',
  448. idCard: '110101199001011243',
  449. disabilityId: 'D123456769',
  450. disabilityType: '肢体残疾',
  451. disabilityLevel: '一级',
  452. idAddress: '北京市怀柔区',
  453. phone: '13000130003',
  454. province: '北京市',
  455. city: '北京市',
  456. canDirectContact: 1,
  457. isMarried: 1
  458. });
  459. await disabledPersonRepository.save(person);
  460. const response = await client.getDisabledPerson[':id'].$get({
  461. param: {
  462. id: person.id
  463. }
  464. }, {
  465. headers: {
  466. 'Authorization': `Bearer ${testToken}`
  467. }
  468. });
  469. expect(response.status).toBe(200);
  470. if (response.status === 200) {
  471. const data = await response.json();
  472. expect(data?.name).toBe('测试人员详情');
  473. expect(data?.canDirectContact).toBe(1);
  474. }
  475. });
  476. it('应该处理不存在的残疾人ID', async () => {
  477. const response = await client.getDisabledPerson[':id'].$get({
  478. param: {
  479. id: 99999 // 不存在的ID
  480. }
  481. }, {
  482. headers: {
  483. 'Authorization': `Bearer ${testToken}`
  484. }
  485. });
  486. expect(response.status).toBe(200); // 返回200,但数据为null
  487. if (response.status === 200) {
  488. const data = await response.json();
  489. expect(data).toBeNull();
  490. }
  491. });
  492. });
  493. describe('GET /getDisabledPersonByIdCard', () => {
  494. it('应该成功根据身份证号查询残疾人', async () => {
  495. // 先创建一个残疾人
  496. const dataSource = await IntegrationTestDatabase.getDataSource();
  497. const disabledPersonRepository = dataSource.getRepository(DisabledPerson);
  498. const person = disabledPersonRepository.create({
  499. name: '身份证查询测试',
  500. gender: '女',
  501. idCard: '110101199001011244',
  502. disabilityId: 'D123456768',
  503. disabilityType: '听力残疾',
  504. disabilityLevel: '三级',
  505. idAddress: '北京市密云区',
  506. phone: '13000130004',
  507. province: '北京市',
  508. city: '北京市'
  509. });
  510. await disabledPersonRepository.save(person);
  511. const response = await client.findByIdCard[':idCard'].$get({
  512. param: {
  513. idCard: '110101199001011244'
  514. }
  515. }, {
  516. headers: {
  517. 'Authorization': `Bearer ${testToken}`
  518. }
  519. });
  520. expect(response.status).toBe(200);
  521. if (response.status === 200) {
  522. const data = await response.json();
  523. expect(data?.name).toBe('身份证查询测试');
  524. expect(data?.idCard).toBe('110101199001011244');
  525. }
  526. });
  527. it('应该处理不存在的身份证号', async () => {
  528. const response = await client.findByIdCard[':idCard'].$get({
  529. param: {
  530. idCard: '999999999999999999' // 不存在的身份证号
  531. }
  532. }, {
  533. headers: {
  534. 'Authorization': `Bearer ${testToken}`
  535. }
  536. });
  537. expect(response.status).toBe(200); // 返回200,但数据为null
  538. if (response.status === 200) {
  539. const data = await response.json();
  540. expect(data).toBeNull();
  541. }
  542. });
  543. });
  544. describe('POST /createAggregatedDisabledPerson', () => {
  545. it('应该成功创建聚合残疾人信息(包含所有关联数据)', async () => {
  546. const createData = {
  547. personInfo: {
  548. name: '聚合创建测试',
  549. gender: '男',
  550. idCard: '110101199001011245',
  551. disabilityId: 'D123456767',
  552. disabilityType: '肢体残疾',
  553. disabilityLevel: '一级',
  554. idAddress: '北京市延庆区',
  555. phone: '13000130005',
  556. province: '北京市',
  557. city: '北京市'
  558. },
  559. bankCards: [
  560. {
  561. subBankName: '北京分行',
  562. bankName: '中国工商银行',
  563. cardNumber: '6222021234567890123',
  564. cardholderName: '聚合创建测试',
  565. fileId: testFile.id,
  566. isDefault: 0
  567. }
  568. ],
  569. photos: [
  570. {
  571. photoType: '身份证照片',
  572. fileId: testFile.id // 使用测试文件ID
  573. }
  574. ],
  575. remarks: [
  576. {
  577. remarkContent: '家庭经济困难,需要帮助',
  578. operatorId: 1
  579. }
  580. ],
  581. visits: [
  582. {
  583. visitDate: '2025-12-02T10:00:00Z',
  584. visitType: '电话回访',
  585. visitContent: '初次回访,了解基本情况',
  586. visitorId: 1
  587. }
  588. ]
  589. };
  590. const response = await client.createAggregatedDisabledPerson.$post({
  591. json: createData
  592. }, {
  593. headers: {
  594. 'Authorization': `Bearer ${testToken}`
  595. }
  596. });
  597. expect(response.status).toBe(200);
  598. if (response.status === 200) {
  599. const data = await response.json();
  600. expect(data.personInfo.name).toBe('聚合创建测试');
  601. expect(data.bankCards).toHaveLength(1);
  602. expect(data.photos).toHaveLength(1);
  603. expect(data.remarks).toHaveLength(1);
  604. expect(data.visits).toHaveLength(1);
  605. // 验证文件集成
  606. expect(data.photos[0].fileId).toBe(testFile.id);
  607. }
  608. });
  609. it('应该验证文件ID的有效性', async () => {
  610. const createData = {
  611. personInfo: {
  612. name: '文件验证测试',
  613. gender: '女',
  614. idCard: '110101199001011246',
  615. disabilityId: 'D123456766',
  616. disabilityType: '视力残疾',
  617. disabilityLevel: '二级',
  618. idAddress: '北京市房山区',
  619. phone: '13000130006',
  620. province: '北京市',
  621. city: '北京市'
  622. },
  623. photos: [
  624. {
  625. photoType: '身份证照片',
  626. fileId: 99999 // 不存在的文件ID
  627. }
  628. ]
  629. };
  630. const response = await client.createAggregatedDisabledPerson.$post({
  631. json: createData
  632. }, {
  633. headers: {
  634. 'Authorization': `Bearer ${testToken}`
  635. }
  636. });
  637. expect(response.status).toBe(400); // 应该返回400,因为文件ID无效
  638. });
  639. });
  640. describe('GET /getAggregatedDisabledPerson/{personId}', () => {
  641. it('应该成功获取聚合残疾人信息', async () => {
  642. // 先创建一个完整的残疾人数据(包含所有关联数据)
  643. const dataSource = await IntegrationTestDatabase.getDataSource();
  644. // 创建残疾人
  645. const disabledPersonRepository = dataSource.getRepository(DisabledPerson);
  646. const person = disabledPersonRepository.create({
  647. name: '聚合查询测试',
  648. gender: '男',
  649. idCard: '110101199001011247',
  650. disabilityId: 'D123456765',
  651. disabilityType: '肢体残疾',
  652. disabilityLevel: '一级',
  653. idAddress: '北京市门头沟区',
  654. phone: '13000130007',
  655. province: '北京市',
  656. city: '北京市'
  657. });
  658. await disabledPersonRepository.save(person);
  659. // 创建银行卡
  660. const bankCardRepository = dataSource.getRepository(DisabledBankCard);
  661. const bankCard = bankCardRepository.create({
  662. personId: person.id,
  663. subBankName: '北京分行',
  664. bankName: '中国建设银行',
  665. cardNumber: '6227001234567890123',
  666. cardholderName: '聚合查询测试',
  667. fileId: testFile.id,
  668. isDefault: 0
  669. });
  670. await bankCardRepository.save(bankCard);
  671. // 创建照片(使用测试文件)
  672. const photoRepository = dataSource.getRepository(DisabledPhoto);
  673. const photo = photoRepository.create({
  674. personId: person.id,
  675. photoType: '身份证照片',
  676. fileId: testFile.id
  677. });
  678. await photoRepository.save(photo);
  679. // 创建备注
  680. const remarkRepository = dataSource.getRepository(DisabledRemark);
  681. const remark = remarkRepository.create({
  682. personId: person.id,
  683. remarkContent: '目前无工作,需要就业帮助',
  684. operatorId: 1
  685. });
  686. await remarkRepository.save(remark);
  687. // 创建回访记录
  688. const visitRepository = dataSource.getRepository(DisabledVisit);
  689. const visit = visitRepository.create({
  690. personId: person.id,
  691. visitDate: new Date('2025-12-01T14:30:00Z'),
  692. visitType: '上门回访',
  693. visitContent: '了解就业需求',
  694. visitorId: 2
  695. });
  696. await visitRepository.save(visit);
  697. const response = await client.getAggregatedDisabledPerson[':id'].$get({
  698. param: {
  699. id: person.id
  700. }
  701. }, {
  702. headers: {
  703. 'Authorization': `Bearer ${testToken}`
  704. }
  705. });
  706. expect(response.status).toBe(200);
  707. if (response.status === 200) {
  708. const data = await response.json();
  709. expect(data).not.toBeNull();
  710. expect(data!.personInfo.name).toBe('聚合查询测试');
  711. expect(data!.bankCards).toHaveLength(1);
  712. expect(data!.photos).toHaveLength(1);
  713. expect(data!.remarks).toHaveLength(1);
  714. expect(data!.visits).toHaveLength(1);
  715. // 验证文件数据完整性
  716. expect(data!.photos[0].fileId).toBe(testFile.id);
  717. }
  718. });
  719. it('应该处理不存在的残疾人ID', async () => {
  720. const response = await client.getAggregatedDisabledPerson[':id'].$get({
  721. param: {
  722. id: 99999 // 不存在的ID
  723. }
  724. }, {
  725. headers: {
  726. 'Authorization': `Bearer ${testToken}`
  727. }
  728. });
  729. expect(response.status).toBe(404);
  730. });
  731. });
  732. describe('认证测试', () => {
  733. it('应该验证所有端点需要认证', async () => {
  734. // 测试没有token的情况
  735. const response = await client.createDisabledPerson.$post({
  736. json: {
  737. name: '测试人员',
  738. gender: '男',
  739. idCard: '110101199001011235',
  740. disabilityId: 'D123456789',
  741. disabilityType: '视力残疾',
  742. disabilityLevel: '一级',
  743. idAddress: '北京市东城区',
  744. phone: '13800138000',
  745. province: '北京市',
  746. city: '北京市'
  747. }
  748. });
  749. expect(response.status).toBe(401);
  750. });
  751. it('应该验证无效token', async () => {
  752. const response = await client.createDisabledPerson.$post({
  753. json: {
  754. name: '测试人员',
  755. gender: '男',
  756. idCard: '110101199001011236',
  757. disabilityId: 'D123456790',
  758. disabilityType: '视力残疾',
  759. disabilityLevel: '一级',
  760. idAddress: '北京市东城区',
  761. phone: '13800138001',
  762. province: '北京市',
  763. city: '北京市'
  764. }
  765. }, {
  766. headers: {
  767. 'Authorization': 'Bearer invalid_token'
  768. }
  769. });
  770. expect(response.status).toBe(401);
  771. });
  772. });
  773. });