From 173317c02953067bce0d943e2544b75fff9a6ff4 Mon Sep 17 00:00:00 2001 From: Timo K Date: Fri, 29 Nov 2024 15:34:31 +0100 Subject: [PATCH] add tests --- ...hentication.controller.integration-spec.ts | 11 ++- .../person-time-limit-info.service.spec.ts | 81 +++++++++++++++++++ .../dbiam-personenkontext.service.spec.ts | 56 +++++++++++++ 3 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 src/modules/authentication/services/person-time-limit-info.service.spec.ts diff --git a/src/modules/authentication/api/authentication.controller.integration-spec.ts b/src/modules/authentication/api/authentication.controller.integration-spec.ts index da7fa0cd4..b2d7918f6 100644 --- a/src/modules/authentication/api/authentication.controller.integration-spec.ts +++ b/src/modules/authentication/api/authentication.controller.integration-spec.ts @@ -27,6 +27,7 @@ import { OrganisationRepository } from '../../organisation/persistence/organisat import { KeycloakConfig } from '../../../shared/config/keycloak.config.js'; import { KeycloakUserService } from '../../keycloak-administration/index.js'; import PersonTimeLimitService from '../services/person-time-limit-info.service.js'; +import { TimeLimitOccasion } from '../domain/time-limit-occasion.enums.js'; describe('AuthenticationController', () => { let module: TestingModule; @@ -39,7 +40,7 @@ describe('AuthenticationController', () => { let rolleRepoMock: DeepMocked; const keycloakUserServiceMock: DeepMocked = createMock(); let keyCloakConfig: KeycloakConfig; - + let personTimeLimitServiceMock: DeepMocked; beforeAll(async () => { module = await Test.createTestingModule({ imports: [ @@ -94,6 +95,7 @@ describe('AuthenticationController', () => { dbiamPersonenkontextRepoMock = module.get(DBiamPersonenkontextRepo); organisationRepoMock = module.get(OrganisationRepository); rolleRepoMock = module.get(RolleRepo); + personTimeLimitServiceMock = module.get(PersonTimeLimitService); }); afterEach(() => { @@ -280,6 +282,13 @@ describe('AuthenticationController', () => { value: person.updatedAt, }); + personTimeLimitServiceMock.getPersonTimeLimitInfo.mockResolvedValueOnce([ + { + occasion: TimeLimitOccasion.KOPERS, + deadline: faker.date.future(), + }, + ]); + const requestMock: Request = setupRequest(); const result: UserinfoResponse = await authController.info(permissions, requestMock); diff --git a/src/modules/authentication/services/person-time-limit-info.service.spec.ts b/src/modules/authentication/services/person-time-limit-info.service.spec.ts new file mode 100644 index 000000000..656baca4e --- /dev/null +++ b/src/modules/authentication/services/person-time-limit-info.service.spec.ts @@ -0,0 +1,81 @@ +import { DeepMocked, createMock } from '@golevelup/ts-jest'; +import { TestingModule, Test } from '@nestjs/testing'; +import { PersonRepository } from '../../person/persistence/person.repository.js'; +import PersonTimeLimitService from './person-time-limit-info.service.js'; +import { DBiamPersonenkontextService } from '../../personenkontext/domain/dbiam-personenkontext.service.js'; +import { Person } from '../../person/domain/person.js'; +import { DoFactory } from '../../../../test/utils/do-factory.js'; +import { PersonTimeLimitInfo } from '../domain/person-time-limit-info.js'; +import { TimeLimitOccasion } from '../domain/time-limit-occasion.enums.js'; +import { Personenkontext } from '../../personenkontext/domain/personenkontext.js'; + +describe('PersonTimeLimitService', () => { + let module: TestingModule; + let sut: PersonTimeLimitService; + let personRepoMock: DeepMocked; + let dBiamPersonenkontextServiceMock: DeepMocked; + + beforeAll(async () => { + module = await Test.createTestingModule({ + providers: [ + PersonTimeLimitService, + { + provide: PersonRepository, + useValue: createMock(), + }, + { + provide: DBiamPersonenkontextService, + useValue: createMock(), + }, + ], + }).compile(); + sut = module.get(PersonTimeLimitService); + personRepoMock = module.get(PersonRepository); + dBiamPersonenkontextServiceMock = module.get(DBiamPersonenkontextService); + }); + + afterAll(async () => { + await module.close(); + }); + + beforeEach(() => { + jest.resetAllMocks(); + }); + + it('should be defined', () => { + expect(sut).toBeDefined(); + }); + + describe('getPersonTimeLimitInfo', () => { + it('should return PersonTimeLimitInfo array', async () => { + const person: Person = DoFactory.createPerson(true); + person.personalnummer = undefined; + personRepoMock.findById.mockResolvedValue(person); + + dBiamPersonenkontextServiceMock.isPersonalnummerRequiredForAnyPersonenkontextForPerson.mockResolvedValue( + true, + ); + const pesonenkontext: Personenkontext = DoFactory.createPersonenkontext(true); + dBiamPersonenkontextServiceMock.getKopersPersonenkontext.mockResolvedValue(pesonenkontext); + + const result: PersonTimeLimitInfo[] = await sut.getPersonTimeLimitInfo(person.id); + + const expectedDeadline: Date = new Date(pesonenkontext.createdAt); + expectedDeadline.setDate(expectedDeadline.getDate() + 56); + + expect(result).toEqual([ + { + occasion: TimeLimitOccasion.KOPERS, + deadline: expectedDeadline, + }, + ]); + }); + + it('should return empty array when person isnt found ', async () => { + personRepoMock.findById.mockResolvedValue(null); + const result: PersonTimeLimitInfo[] = await sut.getPersonTimeLimitInfo(''); + + expect(result).toEqual([]); + }); + }); +}); diff --git a/src/modules/personenkontext/domain/dbiam-personenkontext.service.spec.ts b/src/modules/personenkontext/domain/dbiam-personenkontext.service.spec.ts index 5f22acb14..d42bbaed9 100644 --- a/src/modules/personenkontext/domain/dbiam-personenkontext.service.spec.ts +++ b/src/modules/personenkontext/domain/dbiam-personenkontext.service.spec.ts @@ -114,4 +114,60 @@ describe('DBiamPersonenkontextService', () => { }); }); }); + + describe('getKopersPersonenkontext', () => { + describe('when a person has a personenkontext with a rolle with koperspflichtig merkmale', () => { + it('should return the personenkontext', async () => { + const personenkontexte: Personenkontext[] = [ + personenkontextFactory.construct('1', faker.date.past(), faker.date.recent(), '', '1', '1', '1'), + personenkontextFactory.construct('2', faker.date.past(), faker.date.recent(), '', '1', '2', '1'), + personenkontextFactory.construct('3', faker.date.past(), faker.date.recent(), '', '1', '1', '2'), + ]; + dbiamPersonenKontextRepoMock.findByPerson.mockResolvedValue(personenkontexte); + + const mapRollen: Map> = new Map(); + mapRollen.set( + '1', + DoFactory.createRolle(true, { + rollenart: RollenArt.LEHR, + merkmale: [RollenMerkmal.KOPERS_PFLICHT], + id: '1', + }), + ); + mapRollen.set('2', DoFactory.createRolle(true, { rollenart: RollenArt.LEIT, merkmale: [], id: '2' })); + rolleRepoMock.findByIds.mockResolvedValueOnce(mapRollen); + + const result: Personenkontext | null = await sut.getKopersPersonenkontext('1'); + expect(result).toEqual(personenkontexte[0]); + }); + }); + + describe('when a person has no personenkontext with a rolle with koperspflichtig merkmale', () => { + it('should return null', async () => { + const personenkontexte: Personenkontext[] = [ + personenkontextFactory.construct('1', faker.date.past(), faker.date.recent(), '', '1', '1', '1'), + personenkontextFactory.construct('2', faker.date.past(), faker.date.recent(), '', '1', '2', '1'), + personenkontextFactory.construct('3', faker.date.past(), faker.date.recent(), '', '1', '1', '2'), + ]; + dbiamPersonenKontextRepoMock.findByPerson.mockResolvedValue(personenkontexte); + + const mapRollen: Map> = new Map(); + + mapRollen.set( + '1', + DoFactory.createRolle(true, { + rollenart: RollenArt.LERN, + merkmale: [], + id: '1', + }), + ); + + mapRollen.set('2', DoFactory.createRolle(true, { rollenart: RollenArt.LEIT, merkmale: [], id: '2' })); + rolleRepoMock.findByIds.mockResolvedValueOnce(mapRollen); + + const result: Personenkontext | null = await sut.getKopersPersonenkontext('1'); + expect(result).toBeNull(); + }); + }); + }); });