disability.integration.test.ts 25 KB

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