Skip to content

Commit

Permalink
EW-1083 Refactoring tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SimoneRadtke-Cap committed Jan 21, 2025
1 parent a18f5a3 commit f24381e
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 144 deletions.
71 changes: 27 additions & 44 deletions apps/server/src/infra/sync/strategy/tsp/tsp-fetch.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { faker } from '@faker-js/faker';
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { Test, TestingModule } from '@nestjs/testing';
import { AxiosError, AxiosResponse } from 'axios';
import { Logger } from '@src/core/logger';
import { OauthConfigMissingLoggableException } from '@modules/oauth/loggable';
import { systemFactory } from '@modules/system/testing';
import { AxiosErrorLoggable, ErrorLoggable } from '@src/core/error/loggable';
import {
ExportApiInterface,
RobjExportKlasse,
Expand All @@ -16,6 +10,20 @@ import {
RobjExportSchule,
TspClientFactory,
} from '@infra/tsp-client';
import {
robjExportKlasseFactory,
robjExportLehrerFactory,
robjExportLehrerMigrationFactory,
robjExportSchuelerFactory,
robjExportSchuelerMigrationFactory,
robjExportSchuleFactory,
} from '@infra/tsp-client/testing';
import { OauthConfigMissingLoggableException } from '@modules/oauth/loggable';
import { systemFactory } from '@modules/system/testing';
import { Test, TestingModule } from '@nestjs/testing';
import { AxiosErrorLoggable, ErrorLoggable } from '@src/core/error/loggable';
import { Logger } from '@src/core/logger';
import { AxiosError, AxiosResponse } from 'axios';
import { TspFetchService } from './tsp-fetch.service';

describe(TspFetchService.name, () => {
Expand Down Expand Up @@ -45,7 +53,6 @@ describe(TspFetchService.name, () => {
});

afterEach(() => {
jest.clearAllMocks();
jest.resetAllMocks();
});

Expand All @@ -60,8 +67,8 @@ describe(TspFetchService.name, () => {
});

const setupTspClient = () => {
const clientId = faker.string.alpha();
const clientSecret = faker.string.alpha();
const clientId = faker.string.uuid();
const clientSecret = faker.string.alphanumeric(40);
const tokenEndpoint = faker.internet.url();
const system = systemFactory.build({
oauthConfig: {
Expand All @@ -71,61 +78,37 @@ describe(TspFetchService.name, () => {
},
});

const tspSchool: RobjExportSchule = {
schuleName: faker.string.alpha(),
schuleNummer: faker.string.alpha(),
};
const tspSchool = robjExportSchuleFactory.build();
const schools = [tspSchool];
const responseSchools = createMock<AxiosResponse<Array<RobjExportSchule>>>({
data: schools,
});

const tspTeacher: RobjExportLehrer = {
schuleNummer: faker.string.alpha(),
lehrerVorname: faker.string.alpha(),
lehrerNachname: faker.string.alpha(),
lehrerUid: faker.string.alpha(),
};
const tspTeacher = robjExportLehrerFactory.build();
const teachers = [tspTeacher];
const responseTeachers = createMock<AxiosResponse<Array<RobjExportLehrer>>>({
data: teachers,
});

const tspStudent: RobjExportSchueler = {
schuleNummer: faker.string.alpha(),
schuelerVorname: faker.string.alpha(),
schuelerNachname: faker.string.alpha(),
schuelerUid: faker.string.alpha(),
};
const tspStudent = robjExportSchuelerFactory.build();
const students = [tspStudent];
const responseStudents = createMock<AxiosResponse<Array<RobjExportSchueler>>>({
data: students,
});

const tspClass: RobjExportKlasse = {
schuleNummer: faker.string.alpha(),
klasseId: faker.string.alpha(),
klasseName: faker.string.alpha(),
lehrerUid: faker.string.alpha(),
};
const tspClass = robjExportKlasseFactory.build();
const classes = [tspClass];
const responseClasses = createMock<AxiosResponse<Array<RobjExportKlasse>>>({
data: classes,
});

const tspTeacherMigration: RobjExportLehrerMigration = {
lehrerUidAlt: faker.string.alpha(),
lehrerUidNeu: faker.string.alpha(),
};
const tspTeacherMigration = robjExportLehrerMigrationFactory.build();
const teacherMigrations = [tspTeacherMigration];
const responseTeacherMigrations = createMock<AxiosResponse<Array<RobjExportLehrerMigration>>>({
data: teacherMigrations,
});

const tspStudentMigration: RobjExportSchuelerMigration = {
schuelerUidAlt: faker.string.alpha(),
schuelerUidNeu: faker.string.alpha(),
};
const tspStudentMigration = robjExportSchuelerMigrationFactory.build();
const studentMigrations = [tspStudentMigration];
const responseStudentMigrations = createMock<AxiosResponse<Array<RobjExportSchuelerMigration>>>({
data: studentMigrations,
Expand Down Expand Up @@ -359,8 +342,8 @@ describe(TspFetchService.name, () => {
const setup = () => {
const system = systemFactory.build({
oauthConfig: {
clientId: faker.string.alpha(),
clientSecret: faker.string.alpha(),
clientId: faker.string.uuid(),
clientSecret: faker.string.alphanumeric(40),
tokenEndpoint: faker.internet.url(),
},
});
Expand Down Expand Up @@ -391,8 +374,8 @@ describe(TspFetchService.name, () => {
const setup = () => {
const system = systemFactory.build({
oauthConfig: {
clientId: faker.string.alpha(),
clientSecret: faker.string.alpha(),
clientId: faker.string.uuid(),
clientSecret: faker.string.alphanumeric(40),
tokenEndpoint: faker.internet.url(),
},
});
Expand Down Expand Up @@ -431,7 +414,7 @@ describe(TspFetchService.name, () => {
it('should throw an OauthConfigMissingLoggableException', async () => {
const { system } = setup();

await expect(async () => sut.fetchTspSchools(system, 1)).rejects.toThrow(OauthConfigMissingLoggableException);
await expect(() => sut.fetchTspSchools(system, 1)).rejects.toThrow(OauthConfigMissingLoggableException);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ describe(TspOauthDataMapper.name, () => {
});

afterEach(() => {
jest.clearAllMocks();
jest.resetAllMocks();
});

Expand All @@ -60,18 +59,18 @@ describe(TspOauthDataMapper.name, () => {
const system = systemFactory.build();

const school = schoolFactory.build({
externalId: faker.string.alpha(),
externalId: faker.string.uuid(),
});

const lehrerUid = faker.string.alpha();
const lehrerUid = faker.string.uuid();

const tspTeacher = robjExportLehrerFactory.build({
lehrerUid,
schuleNummer: school.externalId,
});
const tspTeachers = [tspTeacher];

const klasseId = faker.string.alpha();
const klasseId = faker.string.uuid();

const tspClass = robjExportKlasseFactory.build({
klasseId,
Expand All @@ -80,9 +79,9 @@ describe(TspOauthDataMapper.name, () => {
const tspClasses = [tspClass];

const tspStudent = robjExportSchuelerFactory.build({
schuelerUid: faker.string.alpha(),
schuelerNachname: faker.string.alpha(),
schuelerVorname: faker.string.alpha(),
schuelerUid: faker.string.uuid(),
schuelerNachname: faker.person.lastName(),
schuelerVorname: faker.person.firstName(),
schuleNummer: school.externalId,
klasseId,
});
Expand Down
19 changes: 9 additions & 10 deletions apps/server/src/infra/sync/strategy/tsp/tsp-sync.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ describe(TspSyncService.name, () => {
});

afterEach(() => {
jest.clearAllMocks();
jest.resetAllMocks();
});

Expand Down Expand Up @@ -102,7 +101,7 @@ describe(TspSyncService.name, () => {
describe('findSchool', () => {
describe('when school is found', () => {
const setup = () => {
const externalId = faker.string.alpha();
const externalId = faker.string.uuid();
const system = systemFactory.build();
const school = schoolFactory.build();

Expand All @@ -122,7 +121,7 @@ describe(TspSyncService.name, () => {

describe('when school is not found', () => {
const setup = () => {
const externalId = faker.string.alpha();
const externalId = faker.string.uuid();
const system = systemFactory.build();

schoolService.getSchools.mockResolvedValueOnce([]);
Expand Down Expand Up @@ -164,8 +163,8 @@ describe(TspSyncService.name, () => {
describe('updateSchool', () => {
describe('when school is updated', () => {
const setup = () => {
const newName = faker.string.alpha();
const oldName = faker.string.alpha();
const newName = faker.person.fullName();
const oldName = faker.person.fullName();
const school = schoolFactory.build({
name: oldName,
});
Expand All @@ -189,7 +188,7 @@ describe(TspSyncService.name, () => {
describe('when school name is undefined', () => {
const setup = () => {
const newName = undefined;
const oldName = faker.string.alpha();
const oldName = faker.person.fullName();
const school = schoolFactory.build({
name: oldName,
});
Expand All @@ -211,8 +210,8 @@ describe(TspSyncService.name, () => {
describe('when school is created', () => {
const setup = () => {
const system = systemFactory.build();
const name = faker.string.alpha();
const externalId = faker.string.alpha();
const name = faker.word.noun();
const externalId = faker.string.uuid();

const schoolYearEntity = schoolYearFactory.build();
const schoolYear = SchoolYearEntityMapper.mapToDo(schoolYearEntity);
Expand Down Expand Up @@ -249,8 +248,8 @@ describe(TspSyncService.name, () => {
describe('when federalState is already cached', () => {
const setup = () => {
const system = systemFactory.build();
const name = faker.string.alpha();
const externalId = faker.string.alpha();
const name = faker.word.noun();
const externalId = faker.string.uuid();

const schoolYearEntity = schoolYearFactory.build();
const schoolYear = SchoolYearEntityMapper.mapToDo(schoolYearEntity);
Expand Down
26 changes: 11 additions & 15 deletions apps/server/src/infra/sync/strategy/tsp/tsp-sync.strategy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import {
RobjExportSchuelerMigration,
RobjExportSchule,
} from '@infra/tsp-client';
import { robjExportSchuleFactory } from '@infra/tsp-client/testing';
import {
robjExportLehrerMigrationFactory,
robjExportSchuelerMigrationFactory,
robjExportSchuleFactory,
} from '@infra/tsp-client/testing';
import { Account } from '@modules/account';
import { OauthDataDto, ProvisioningService } from '@modules/provisioning';
import {
Expand Down Expand Up @@ -95,9 +99,7 @@ describe(TspSyncStrategy.name, () => {
});

afterEach(() => {
jest.clearAllMocks();
jest.resetAllMocks();
jest.restoreAllMocks();
});

afterAll(async () => {
Expand Down Expand Up @@ -147,7 +149,7 @@ describe(TspSyncStrategy.name, () => {
tspFetchService.fetchTspTeacherMigrations.mockResolvedValueOnce(params.fetchedTeacherMigrations ?? []);
tspFetchService.fetchTspStudentMigrations.mockResolvedValueOnce(params.fetchedStudentMigrations ?? []);

tspSyncService.findSchool.mockResolvedValue(params.foundSchool ?? undefined);
tspSyncService.findSchool.mockResolvedValueOnce(params.foundSchool ?? undefined);
tspSyncService.findAllSchoolsForSystem.mockResolvedValueOnce(params.foundSystemSchools ?? []);
tspSyncService.findTspSystemOrFail.mockResolvedValueOnce(params.foundSystem ?? systemFactory.build());

Expand All @@ -169,23 +171,17 @@ describe(TspSyncStrategy.name, () => {
const setup = () => {
const oauthDataDto = oauthDataDtoFactory.build({
system: provisioningSystemDtoFactory.build({
systemId: faker.string.alpha(),
systemId: faker.string.uuid(),
provisioningStrategy: SystemProvisioningStrategy.TSP,
}),
externalUser: externalUserDtoFactory.build({
externalId: faker.string.alpha(),
externalId: faker.string.uuid(),
roles: [],
}),
});
const tspTeacher: RobjExportLehrerMigration = {
lehrerUidAlt: faker.string.alpha(),
lehrerUidNeu: faker.string.alpha(),
};

const tspStudent: RobjExportSchuelerMigration = {
schuelerUidAlt: faker.string.alpha(),
schuelerUidNeu: faker.string.alpha(),
};
const tspTeacher = robjExportLehrerMigrationFactory.build();

const tspStudent = robjExportSchuelerMigrationFactory.build();

setupMockServices({
fetchedStudentMigrations: [tspStudent],
Expand Down
Loading

0 comments on commit f24381e

Please sign in to comment.