| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987 |
- import { describe, it, expect, beforeEach } from 'vitest';
- import { testClient } from 'hono/testing';
- import { IntegrationTestDatabase, setupIntegrationDatabaseHooksWithEntities } from '@d8d/shared-test-util';
- import { JWTUtil } from '@d8d/shared-utils';
- import { UserEntity, Role } from '@d8d/user-module';
- import { File } from '@d8d/file-module';
- import disabledPersonRoutes from '../../src/routes/disabled-person.routes';
- import { DisabledPerson } from '../../src/entities/disabled-person.entity';
- import { DisabledBankCard } from '../../src/entities/disabled-bank-card.entity';
- import { DisabledPhoto } from '../../src/entities/disabled-photo.entity';
- import { DisabledRemark } from '../../src/entities/disabled-remark.entity';
- import { DisabledVisit } from '../../src/entities/disabled-visit.entity';
- // 设置集成测试钩子 - 包含所有相关实体
- setupIntegrationDatabaseHooksWithEntities([
- UserEntity,
- Role,
- File,
- DisabledPerson,
- DisabledBankCard,
- DisabledPhoto,
- DisabledRemark,
- DisabledVisit
- ])
- describe('残疾人管理API集成测试', () => {
- let client: ReturnType<typeof testClient<typeof disabledPersonRoutes>>;
- let testToken: string;
- let testUser: UserEntity;
- let testFile: File;
- beforeEach(async () => {
- // 创建测试客户端
- client = testClient(disabledPersonRoutes);
- // 获取数据源
- const dataSource = await IntegrationTestDatabase.getDataSource();
- // 创建测试用户
- const userRepository = dataSource.getRepository(UserEntity);
- testUser = userRepository.create({
- username: `test_user_${Date.now()}`,
- password: 'test_password',
- nickname: '测试用户',
- registrationSource: 'web'
- });
- await userRepository.save(testUser);
- // 创建测试文件(用于照片集成测试)
- const fileRepository = dataSource.getRepository(File);
- testFile = fileRepository.create({
- name: 'test_photo.jpg',
- path: 'test_photo.jpg',
- type: 'image/jpeg',
- size: 1024,
- uploadUserId: testUser.id,
- uploadTime: new Date()
- });
- await fileRepository.save(testFile);
- // 生成测试用户的token
- testToken = JWTUtil.generateToken({
- id: testUser.id,
- username: testUser.username,
- roles: [{name:'user'}]
- });
- });
- describe('POST /createDisabledPerson', () => {
- it('应该成功创建残疾人基本信息', async () => {
- const createData = {
- name: '张三',
- gender: '男',
- idCard: '110101199001011234',
- disabilityId: 'D123456789',
- disabilityType: '肢体残疾',
- disabilityLevel: '一级',
- idAddress: '北京市东城区',
- phone: '13800138000',
- province: '北京市',
- city: '北京市',
- // 新增字段
- canDirectContact: 1,
- isInBlackList: 0,
- jobStatus: 1,
- // 日期字段
- idValidDate: new Date('2030-12-31'),
- disabilityValidDate: new Date('2030-12-31')
- };
- const response = await client.createDisabledPerson.$post({
- json: createData
- }, {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- expect(response.status).toBe(200);
- if (response.status === 200) {
- const data = await response.json();
- expect(data.name).toBe(createData.name);
- expect(data.idCard).toBe(createData.idCard);
- expect(data.disabilityId).toBe(createData.disabilityId);
- // 验证新增字段
- expect(data.canDirectContact).toBe(createData.canDirectContact);
- expect(data.isInBlackList).toBe(createData.isInBlackList);
- expect(data.jobStatus).toBe(createData.jobStatus);
- // 验证日期字段
- expect(data.idValidDate).toBeDefined();
- expect(data.disabilityValidDate).toBeDefined();
- if (data.idValidDate) {
- expect(new Date(data.idValidDate).toISOString().split('T')[0]).toBe('2030-12-31');
- }
- if (data.disabilityValidDate) {
- expect(new Date(data.disabilityValidDate).toISOString().split('T')[0]).toBe('2030-12-31');
- }
- }
- });
- it('应该验证身份证号唯一性', async () => {
- // 先创建一个残疾人
- const dataSource = await IntegrationTestDatabase.getDataSource();
- const disabledPersonRepository = dataSource.getRepository(DisabledPerson);
- const existingPerson = disabledPersonRepository.create({
- name: '李四',
- gender: '女',
- idCard: '110101199001011235',
- disabilityId: 'D123456788',
- disabilityType: '视力残疾',
- disabilityLevel: '二级',
- idAddress: '北京市西城区',
- phone: '13900139000',
- province: '北京市',
- city: '北京市',
- // 新增字段
- canDirectContact: 0,
- isInBlackList: 0,
- jobStatus: 2,
- // 日期字段
- idValidDate: new Date('2035-05-15'),
- disabilityValidDate: new Date('2035-05-15')
- });
- await disabledPersonRepository.save(existingPerson);
- // 尝试创建相同身份证号的残疾人
- const createData = {
- name: '王五',
- gender: '男',
- idCard: '110101199001011235', // 重复的身份证号
- disabilityId: 'D123456777',
- disabilityType: '听力残疾',
- disabilityLevel: '三级',
- idAddress: '北京市朝阳区',
- phone: '13700137000',
- province: '北京市',
- city: '北京市',
- // 新增字段
- canDirectContact: 1,
- isInBlackList: 1,
- jobStatus: 3,
- // 日期字段
- idValidDate: new Date('2043-08-20'),
- disabilityValidDate: new Date('2043-08-20')
- };
- const response = await client.createDisabledPerson.$post({
- json: createData
- }, {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- expect(response.status).toBe(400);
- });
- it('应该验证必填字段', async () => {
- const createData = {
- name: '', // 空字符串,应该验证失败
- gender: '男',
- idCard: '110101199001011236',
- disabilityId: 'D123456776',
- disabilityType: '肢体残疾',
- disabilityLevel: '一级',
- idAddress: '北京市海淀区',
- phone: '13600136000',
- province: '北京市',
- city: '北京市',
- // 新增字段(可选字段,不影响必填验证)
- canDirectContact: 1,
- isInBlackList: 0,
- jobStatus: 1,
- // 日期字段(可选字段,不影响必填验证)
- idValidDate: new Date('2028-03-10'),
- disabilityValidDate: new Date('2028-03-10')
- };
- const response = await client.createDisabledPerson.$post({
- json: createData
- }, {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- expect(response.status).toBe(400);
- });
- });
- describe('POST /deleteDisabledPerson', () => {
- it('应该成功删除残疾人', async () => {
- // 先创建一个残疾人
- const dataSource = await IntegrationTestDatabase.getDataSource();
- const disabledPersonRepository = dataSource.getRepository(DisabledPerson);
- const person = disabledPersonRepository.create({
- name: '测试删除人员',
- gender: '男',
- idCard: '110101199001011237',
- disabilityId: 'D123456775',
- disabilityType: '肢体残疾',
- disabilityLevel: '一级',
- idAddress: '北京市石景山区',
- phone: '13500135000',
- province: '北京市',
- city: '北京市'
- });
- await disabledPersonRepository.save(person);
- const deleteData = {
- id: person.id
- };
- const response = await client.deleteDisabledPerson.$post({
- json: deleteData
- }, {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- expect(response.status).toBe(200);
- if (response.status === 200) {
- const data = await response.json();
- expect(data.success).toBe(true);
- // 验证残疾人已被删除
- const deletedPerson = await disabledPersonRepository.findOne({ where: { id: person.id } });
- expect(deletedPerson).toBeNull();
- }
- });
- it('应该处理不存在的残疾人ID', async () => {
- const deleteData = {
- id: 99999 // 不存在的ID
- };
- const response = await client.deleteDisabledPerson.$post({
- json: deleteData
- }, {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- expect(response.status).toBe(404);
- });
- });
- describe('POST /updateDisabledPerson', () => {
- it('应该成功更新残疾人信息', async () => {
- // 先创建一个残疾人
- const dataSource = await IntegrationTestDatabase.getDataSource();
- const disabledPersonRepository = dataSource.getRepository(DisabledPerson);
- const person = disabledPersonRepository.create({
- name: '原始姓名',
- gender: '男',
- idCard: '110101199001011238',
- disabilityId: 'D123456774',
- disabilityType: '肢体残疾',
- disabilityLevel: '一级',
- idAddress: '北京市通州区',
- phone: '13400134000',
- province: '北京市',
- city: '北京市',
- // 新增字段初始值
- canDirectContact: 1,
- isInBlackList: 0,
- jobStatus: 1,
- // 日期字段初始值
- idValidDate: new Date('2023-01-01'),
- disabilityValidDate: new Date('2023-01-01')
- });
- await disabledPersonRepository.save(person);
- const updateData = {
- id: person.id,
- name: '更新后的姓名',
- gender: '女',
- phone: '13300133000',
- // 更新新增字段
- canDirectContact: 0,
- isInBlackList: 1,
- jobStatus: 1,
- // 更新日期字段
- idValidDate: new Date('2033-01-01'), // 更新有效期
- disabilityValidDate: new Date('2033-01-01') // 更新有效期
- };
- const response = await client.updateDisabledPerson.$post({
- json: updateData
- }, {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- expect(response.status).toBe(200);
- if (response.status === 200) {
- const data = await response.json();
- expect(data.name).toBe(updateData.name);
- expect(data.gender).toBe(updateData.gender);
- expect(data.phone).toBe(updateData.phone);
- // 验证新增字段已更新
- expect(data.canDirectContact).toBe(updateData.canDirectContact);
- expect(data.isInBlackList).toBe(updateData.isInBlackList);
- expect(data.jobStatus).toBe(updateData.jobStatus);
- // 验证日期字段
- expect(data.idValidDate).toBeDefined();
- expect(data.disabilityValidDate).toBeDefined();
- if (data.idValidDate) {
- expect(new Date(data.idValidDate).toISOString().split('T')[0]).toBe('2033-01-01');
- }
- if (data.disabilityValidDate) {
- expect(new Date(data.disabilityValidDate).toISOString().split('T')[0]).toBe('2033-01-01');
- }
- }
- });
- it('应该验证身份证号唯一性(更新时)', async () => {
- // 创建两个残疾人
- const dataSource = await IntegrationTestDatabase.getDataSource();
- const disabledPersonRepository = dataSource.getRepository(DisabledPerson);
- const person1 = disabledPersonRepository.create({
- name: '人员A',
- gender: '男',
- idCard: '110101199001011239',
- disabilityId: 'D123456773',
- disabilityType: '肢体残疾',
- disabilityLevel: '一级',
- idAddress: '北京市顺义区',
- phone: '13200132000',
- province: '北京市',
- city: '北京市',
- // 新增字段
- canDirectContact: 1,
- isInBlackList: 0,
- jobStatus: 1,
- // 日期字段
- idValidDate: new Date('2028-06-15'),
- disabilityValidDate: new Date('2028-06-15')
- });
- await disabledPersonRepository.save(person1);
- const person2 = disabledPersonRepository.create({
- name: '人员B',
- gender: '女',
- idCard: '110101199001011240',
- disabilityId: 'D123456772',
- disabilityType: '视力残疾',
- disabilityLevel: '二级',
- idAddress: '北京市大兴区',
- phone: '13100131000',
- province: '北京市',
- city: '北京市',
- // 新增字段
- canDirectContact: 0,
- isInBlackList: 1,
- jobStatus: 2,
- // 日期字段
- idValidDate: new Date('2034-09-20'),
- disabilityValidDate: new Date('2034-09-20')
- });
- await disabledPersonRepository.save(person2);
- // 尝试将人员2的身份证号改为人员1的身份证号
- const updateData = {
- id: person2.id,
- idCard: '110101199001011239' // 重复的身份证号
- };
- const response = await client.updateDisabledPerson.$post({
- json: updateData
- }, {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- expect(response.status).toBe(400);
- });
- it('应该处理不存在的残疾人', async () => {
- const updateData = {
- id: 99999, // 不存在的ID
- name: '新姓名'
- };
- const response = await client.updateDisabledPerson.$post({
- json: updateData
- }, {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- expect(response.status).toBe(404);
- });
- });
- describe('GET /getAllDisabledPersons', () => {
- it('应该成功获取残疾人列表(分页)', async () => {
- // 创建一些测试数据
- const dataSource = await IntegrationTestDatabase.getDataSource();
- const disabledPersonRepository = dataSource.getRepository(DisabledPerson);
- for (let i = 1; i <= 5; i++) {
- const person = disabledPersonRepository.create({
- name: `残疾人${i}`,
- gender: i % 2 === 0 ? '女' : '男',
- idCard: `1101011990010112${40 + i}`,
- disabilityId: `D1234567${70 + i}`,
- disabilityType: '肢体残疾',
- disabilityLevel: '一级',
- idAddress: `北京市测试区${i}`,
- phone: `138001380${i}`,
- province: '北京市',
- city: '北京市'
- });
- await disabledPersonRepository.save(person);
- }
- const response = await client.getAllDisabledPersons.$get({
- query: {
- skip: 0,
- take: 10
- }
- }, {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- expect(response.status).toBe(200);
- if (response.status === 200) {
- const data = await response.json();
- expect(data.data).toHaveLength(5);
- expect(data.total).toBe(5);
- expect(data.data[0].name).toBe('残疾人5'); // 按ID降序排列
- }
- });
- it('应该处理分页参数', async () => {
- // 创建更多测试数据
- const dataSource = await IntegrationTestDatabase.getDataSource();
- const disabledPersonRepository = dataSource.getRepository(DisabledPerson);
- for (let i = 1; i <= 15; i++) {
- const person = disabledPersonRepository.create({
- name: `分页人员${i}`,
- gender: i % 2 === 0 ? '女' : '男',
- idCard: `1101011990010113${i}`,
- disabilityId: `D1234568${i}`,
- disabilityType: '肢体残疾',
- disabilityLevel: '一级',
- idAddress: `北京市分页区${i}`,
- phone: `138001381${i}`,
- province: '北京市',
- city: '北京市'
- });
- await disabledPersonRepository.save(person);
- }
- const response = await client.getAllDisabledPersons.$get({
- query: {
- skip: 5,
- take: 5
- }
- }, {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- expect(response.status).toBe(200);
- if (response.status === 200) {
- const data = await response.json();
- expect(data.data).toHaveLength(5);
- expect(data.total).toBe(15);
- }
- });
- });
- describe('GET /searchDisabledPersons', () => {
- it('应该成功按姓名搜索残疾人', async () => {
- // 创建测试数据
- const dataSource = await IntegrationTestDatabase.getDataSource();
- const disabledPersonRepository = dataSource.getRepository(DisabledPerson);
- const person1 = disabledPersonRepository.create({
- name: '张三',
- gender: '男',
- idCard: '110101199001011241',
- disabilityId: 'D123456771',
- disabilityType: '肢体残疾',
- disabilityLevel: '一级',
- idAddress: '北京市昌平区',
- phone: '13000130001',
- province: '北京市',
- city: '北京市'
- });
- await disabledPersonRepository.save(person1);
- const person2 = disabledPersonRepository.create({
- name: '李四',
- gender: '女',
- idCard: '110101199001011242',
- disabilityId: 'D123456770',
- disabilityType: '视力残疾',
- disabilityLevel: '二级',
- idAddress: '北京市平谷区',
- phone: '13000130002',
- province: '北京市',
- city: '北京市'
- });
- await disabledPersonRepository.save(person2);
- const response = await client.searchDisabledPersons.$get({
- query: {
- keyword: '张三',
- skip: 0,
- take: 10
- }
- }, {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- expect(response.status).toBe(200);
- if (response.status === 200) {
- const data = await response.json();
- expect(data.data).toHaveLength(1);
- expect(data.data[0].name).toBe('张三');
- }
- });
- it('应该验证搜索关键词不能为空', async () => {
- const response = await client.searchDisabledPersons.$get({
- query: {
- keyword: '', // 空关键词
- skip: 0,
- take: 10
- }
- }, {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- expect(response.status).toBe(400);
- });
- });
- describe('GET /getDisabledPerson/{id}', () => {
- it('应该成功获取单个残疾人详情', async () => {
- // 先创建一个残疾人
- const dataSource = await IntegrationTestDatabase.getDataSource();
- const disabledPersonRepository = dataSource.getRepository(DisabledPerson);
- const person = disabledPersonRepository.create({
- name: '测试人员详情',
- gender: '男',
- idCard: '110101199001011243',
- disabilityId: 'D123456769',
- disabilityType: '肢体残疾',
- disabilityLevel: '一级',
- idAddress: '北京市怀柔区',
- phone: '13000130003',
- province: '北京市',
- city: '北京市',
- canDirectContact: 1,
- isMarried: 1
- });
- await disabledPersonRepository.save(person);
- const response = await client.getDisabledPerson[':id'].$get({
- param: {
- id: person.id
- }
- }, {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- expect(response.status).toBe(200);
- if (response.status === 200) {
- const data = await response.json();
- expect(data?.name).toBe('测试人员详情');
- expect(data?.canDirectContact).toBe(1);
- }
- });
- it('应该处理不存在的残疾人ID', async () => {
- const response = await client.getDisabledPerson[':id'].$get({
- param: {
- id: 99999 // 不存在的ID
- }
- }, {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- expect(response.status).toBe(200); // 返回200,但数据为null
- if (response.status === 200) {
- const data = await response.json();
- expect(data).toBeNull();
- }
- });
- });
- describe('GET /getDisabledPersonByIdCard', () => {
- it('应该成功根据身份证号查询残疾人', async () => {
- // 先创建一个残疾人
- const dataSource = await IntegrationTestDatabase.getDataSource();
- const disabledPersonRepository = dataSource.getRepository(DisabledPerson);
- const person = disabledPersonRepository.create({
- name: '身份证查询测试',
- gender: '女',
- idCard: '110101199001011244',
- disabilityId: 'D123456768',
- disabilityType: '听力残疾',
- disabilityLevel: '三级',
- idAddress: '北京市密云区',
- phone: '13000130004',
- province: '北京市',
- city: '北京市'
- });
- await disabledPersonRepository.save(person);
- const response = await client.findByIdCard[':idCard'].$get({
- param: {
- idCard: '110101199001011244'
- }
- }, {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- expect(response.status).toBe(200);
- if (response.status === 200) {
- const data = await response.json();
- expect(data?.name).toBe('身份证查询测试');
- expect(data?.idCard).toBe('110101199001011244');
- }
- });
- it('应该处理不存在的身份证号', async () => {
- const response = await client.findByIdCard[':idCard'].$get({
- param: {
- idCard: '999999999999999999' // 不存在的身份证号
- }
- }, {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- expect(response.status).toBe(200); // 返回200,但数据为null
- if (response.status === 200) {
- const data = await response.json();
- expect(data).toBeNull();
- }
- });
- });
- describe('POST /createAggregatedDisabledPerson', () => {
- it('应该成功创建聚合残疾人信息(包含所有关联数据)', async () => {
- const createData = {
- personInfo: {
- name: '聚合创建测试',
- gender: '男',
- idCard: '110101199001011245',
- disabilityId: 'D123456767',
- disabilityType: '肢体残疾',
- disabilityLevel: '一级',
- idAddress: '北京市延庆区',
- phone: '13000130005',
- province: '北京市',
- city: '北京市',
- // 新增字段
- canDirectContact: 1,
- isInBlackList: 0,
- jobStatus: 1,
- // 日期字段
- idValidDate: new Date('2045-11-30'),
- disabilityValidDate: new Date('2045-11-30')
- },
- bankCards: [
- {
- subBankName: '北京分行',
- bankName: '中国工商银行',
- cardNumber: '6222021234567890123',
- cardholderName: '聚合创建测试',
- fileId: testFile.id,
- isDefault: 0
- }
- ],
- photos: [
- {
- photoType: '身份证照片',
- fileId: testFile.id // 使用测试文件ID
- }
- ],
- remarks: [
- {
- remarkContent: '家庭经济困难,需要帮助',
- operatorId: 1
- }
- ],
- visits: [
- {
- visitDate: '2025-12-02T10:00:00Z',
- visitType: '电话回访',
- visitContent: '初次回访,了解基本情况',
- visitorId: 1
- }
- ]
- };
- const response = await client.createAggregatedDisabledPerson.$post({
- json: createData
- }, {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- expect(response.status).toBe(200);
- if (response.status === 200) {
- const data = await response.json();
- expect(data.personInfo.name).toBe('聚合创建测试');
- // 验证新增字段
- expect(data.personInfo.canDirectContact).toBe(1);
- expect(data.personInfo.isInBlackList).toBe(0);
- expect(data.personInfo.jobStatus).toBe(1);
- // 验证日期字段
- expect(data.personInfo.idValidDate).toBeDefined();
- expect(data.personInfo.disabilityValidDate).toBeDefined();
- if (data.personInfo.idValidDate) {
- expect(new Date(data.personInfo.idValidDate).toISOString().split('T')[0]).toBe('2045-11-30');
- }
- if (data.personInfo.disabilityValidDate) {
- expect(new Date(data.personInfo.disabilityValidDate).toISOString().split('T')[0]).toBe('2045-11-30');
- }
- expect(data.bankCards).toHaveLength(1);
- expect(data.photos).toHaveLength(1);
- expect(data.remarks).toHaveLength(1);
- expect(data.visits).toHaveLength(1);
- // 验证文件集成
- expect(data.photos[0].fileId).toBe(testFile.id);
- }
- });
- it('应该验证文件ID的有效性', async () => {
- const createData = {
- personInfo: {
- name: '文件验证测试',
- gender: '女',
- idCard: '110101199001011246',
- disabilityId: 'D123456766',
- disabilityType: '视力残疾',
- disabilityLevel: '二级',
- idAddress: '北京市房山区',
- phone: '13000130006',
- province: '北京市',
- city: '北京市',
- // 新增字段
- canDirectContact: 0,
- isInBlackList: 1,
- jobStatus: 2,
- // 日期字段
- idValidDate: new Date('2036-07-25'),
- disabilityValidDate: new Date('2036-07-25')
- },
- photos: [
- {
- photoType: '身份证照片',
- fileId: 99999 // 不存在的文件ID
- }
- ]
- };
- const response = await client.createAggregatedDisabledPerson.$post({
- json: createData
- }, {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- expect(response.status).toBe(400); // 应该返回400,因为文件ID无效
- });
- });
- describe('GET /getAggregatedDisabledPerson/{personId}', () => {
- it('应该成功获取聚合残疾人信息', async () => {
- // 先创建一个完整的残疾人数据(包含所有关联数据)
- const dataSource = await IntegrationTestDatabase.getDataSource();
- // 创建残疾人
- const disabledPersonRepository = dataSource.getRepository(DisabledPerson);
- const person = disabledPersonRepository.create({
- name: '聚合查询测试',
- gender: '男',
- idCard: '110101199001011247',
- disabilityId: 'D123456765',
- disabilityType: '肢体残疾',
- disabilityLevel: '一级',
- idAddress: '北京市门头沟区',
- phone: '13000130007',
- province: '北京市',
- city: '北京市'
- });
- await disabledPersonRepository.save(person);
- // 创建银行卡
- const bankCardRepository = dataSource.getRepository(DisabledBankCard);
- const bankCard = bankCardRepository.create({
- personId: person.id,
- subBankName: '北京分行',
- bankName: '中国建设银行',
- cardNumber: '6227001234567890123',
- cardholderName: '聚合查询测试',
- fileId: testFile.id,
- isDefault: 0
- });
- await bankCardRepository.save(bankCard);
- // 创建照片(使用测试文件)
- const photoRepository = dataSource.getRepository(DisabledPhoto);
- const photo = photoRepository.create({
- personId: person.id,
- photoType: '身份证照片',
- fileId: testFile.id
- });
- await photoRepository.save(photo);
- // 创建备注
- const remarkRepository = dataSource.getRepository(DisabledRemark);
- const remark = remarkRepository.create({
- personId: person.id,
- remarkContent: '目前无工作,需要就业帮助',
- operatorId: 1
- });
- await remarkRepository.save(remark);
- // 创建回访记录
- const visitRepository = dataSource.getRepository(DisabledVisit);
- const visit = visitRepository.create({
- personId: person.id,
- visitDate: new Date('2025-12-01T14:30:00Z'),
- visitType: '上门回访',
- visitContent: '了解就业需求',
- visitorId: 2
- });
- await visitRepository.save(visit);
- const response = await client.getAggregatedDisabledPerson[':id'].$get({
- param: {
- id: person.id
- }
- }, {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- // 调试:打印响应状态和错误信息
- console.debug('响应状态:', response.status);
- if (response.status !== 200) {
- const errorText = await response.text();
- console.debug('错误响应:', errorText);
- }
- expect(response.status).toBe(200);
- if (response.status === 200) {
- const data = await response.json();
- expect(data).not.toBeNull();
- expect(data!.personInfo.name).toBe('聚合查询测试');
- expect(data!.bankCards).toHaveLength(1);
- expect(data!.photos).toHaveLength(1);
- expect(data!.remarks).toHaveLength(1);
- expect(data!.visits).toHaveLength(1);
- // 验证文件数据完整性
- expect(data!.photos[0].fileId).toBe(testFile.id);
- }
- });
- it('应该处理不存在的残疾人ID', async () => {
- const response = await client.getAggregatedDisabledPerson[':id'].$get({
- param: {
- id: 99999 // 不存在的ID
- }
- }, {
- headers: {
- 'Authorization': `Bearer ${testToken}`
- }
- });
- expect(response.status).toBe(404);
- });
- });
- describe('认证测试', () => {
- it('应该验证所有端点需要认证', async () => {
- // 测试没有token的情况
- const response = await client.createDisabledPerson.$post({
- json: {
- name: '测试人员',
- gender: '男',
- idCard: '110101199001011235',
- disabilityId: 'D123456789',
- disabilityType: '视力残疾',
- disabilityLevel: '一级',
- idAddress: '北京市东城区',
- phone: '13800138000',
- province: '北京市',
- city: '北京市'
- }
- });
- expect(response.status).toBe(401);
- });
- it('应该验证无效token', async () => {
- const response = await client.createDisabledPerson.$post({
- json: {
- name: '测试人员',
- gender: '男',
- idCard: '110101199001011236',
- disabilityId: 'D123456790',
- disabilityType: '视力残疾',
- disabilityLevel: '一级',
- idAddress: '北京市东城区',
- phone: '13800138001',
- province: '北京市',
- city: '北京市'
- }
- }, {
- headers: {
- 'Authorization': 'Bearer invalid_token'
- }
- });
- expect(response.status).toBe(401);
- });
- });
- });
|