Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EW-1053 Add and use Test Factories For TSP #5369

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 73 additions & 63 deletions apps/server/src/infra/sync/tsp/tsp-oauth-data.mapper.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import { faker } from '@faker-js/faker';
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { ProvisioningSystemDto } from '@modules/provisioning';
import {
ExternalClassDto,
ExternalSchoolDto,
ExternalUserDto,
OauthDataDto,
ProvisioningSystemDto,
} from '@modules/provisioning';
externalUserDtoFactory,
oauthDataDtoFactory,
externalClassDtoFactory,
provisioningSystemDtoFactory,
externalSchoolDtoFactory,
} from '@modules/provisioning/testing';
import { Test, TestingModule } from '@nestjs/testing';
import { RoleName } from '@shared/domain/interface';
import { SystemProvisioningStrategy } from '@shared/domain/interface/system-provisioning.strategy';
import {
robjExportSchuelerFactory,
robjExportLehrerFactory,
robjExportKlasseFactory,
} from '@src/infra/tsp-client/testing';
import { Logger } from '@src/core/logger';
import { RobjExportKlasse, RobjExportLehrer, RobjExportSchueler } from '@src/infra/tsp-client';
import { BadDataLoggableException } from '@src/modules/provisioning/loggable';
import { schoolFactory } from '@src/modules/school/testing';
import { systemFactory } from '@src/modules/system/testing';
Expand Down Expand Up @@ -64,71 +69,76 @@ describe(TspOauthDataMapper.name, () => {

const lehrerUid = faker.string.alpha();

const tspTeachers: RobjExportLehrer[] = [
{
lehrerUid,
lehrerNachname: faker.string.alpha(),
lehrerVorname: faker.string.alpha(),
schuleNummer: school.externalId,
},
];
const tspTeacher = robjExportLehrerFactory.build({
lehrerUid,
schuleNummer: school.externalId,
});
const tspTeachers = [tspTeacher];

const klasseId = faker.string.alpha();

const tspClasses: RobjExportKlasse[] = [
{
klasseId,
klasseName: faker.string.alpha(),
lehrerUid,
},
];

const tspStudents: RobjExportSchueler[] = [
{
schuelerUid: faker.string.alpha(),
schuelerNachname: faker.string.alpha(),
schuelerVorname: faker.string.alpha(),
schuleNummer: school.externalId,
klasseId,
},
];
const tspClass = robjExportKlasseFactory.build({
klasseId,
lehrerUid,
});
const tspClasses = [tspClass];

const tspStudent = robjExportSchuelerFactory.build({
schuelerUid: faker.string.alpha(),
schuelerNachname: faker.string.alpha(),
schuelerVorname: faker.string.alpha(),
schuleNummer: school.externalId,
klasseId,
});
const tspStudents = [tspStudent];

const provisioningSystemDto = new ProvisioningSystemDto({
const provisioningSystemDto: ProvisioningSystemDto = provisioningSystemDtoFactory.build({
systemId: system.id,
provisioningStrategy: SystemProvisioningStrategy.TSP,
});

const externalSchool = new ExternalSchoolDto({
externalId: school.externalId ?? '',
const externalClassDto = externalClassDtoFactory.build({
externalId: tspClasses[0].klasseId ?? '',
name: tspClasses[0].klasseName,
});

const externalClass = new ExternalClassDto({
externalId: klasseId,
name: tspClasses[0].klasseName,
const externalTeacherUserDto = externalUserDtoFactory.build({
externalId: tspTeachers[0].lehrerUid ?? '',
firstName: tspTeachers[0].lehrerVorname,
lastName: tspTeachers[0].lehrerNachname,
roles: [RoleName.TEACHER],
email: undefined,
birthday: undefined,
});

const externalStudentUserDto = externalUserDtoFactory.build({
externalId: tspStudents[0].schuelerUid ?? '',
firstName: tspStudents[0].schuelerVorname,
lastName: tspStudents[0].schuelerNachname,
roles: [RoleName.STUDENT],
email: undefined,
birthday: undefined,
});

const externalSchoolDto = externalSchoolDtoFactory.build({
externalId: school.externalId,
name: school.name,
officialSchoolNumber: undefined,
location: undefined,
});

const expected: OauthDataDto[] = [
new OauthDataDto({
const expected = [
oauthDataDtoFactory.build({
system: provisioningSystemDto,
externalUser: new ExternalUserDto({
externalId: tspTeachers[0].lehrerUid ?? '',
firstName: tspTeachers[0].lehrerVorname,
lastName: tspTeachers[0].lehrerNachname,
roles: [RoleName.TEACHER],
}),
externalSchool,
externalClasses: [externalClass],
externalUser: externalTeacherUserDto,
externalClasses: [externalClassDto],
externalSchool: externalSchoolDto,
}),
new OauthDataDto({
oauthDataDtoFactory.build({
system: provisioningSystemDto,
externalUser: new ExternalUserDto({
externalId: tspStudents[0].schuelerUid ?? '',
firstName: tspStudents[0].schuelerVorname,
lastName: tspStudents[0].schuelerNachname,
roles: [RoleName.STUDENT],
}),
externalSchool,
externalClasses: [externalClass],
externalUser: externalStudentUserDto,
externalClasses: [externalClassDto],
externalSchool: externalSchoolDto,
}),
];

Expand Down Expand Up @@ -165,9 +175,9 @@ describe(TspOauthDataMapper.name, () => {
const setup = () => {
const system = systemFactory.build();

const tspClass: RobjExportKlasse = {
const tspClass = robjExportKlasseFactory.build({
klasseId: undefined,
};
});

return { system, tspClass };
};
Expand All @@ -185,9 +195,9 @@ describe(TspOauthDataMapper.name, () => {
const setup = () => {
const system = systemFactory.build();

const tspTeacher: RobjExportLehrer = {
const tspTeacher = robjExportLehrerFactory.build({
lehrerUid: undefined,
};
});

return { system, tspTeacher };
};
Expand All @@ -205,9 +215,9 @@ describe(TspOauthDataMapper.name, () => {
const setup = () => {
const system = systemFactory.build();

const tspStudent: RobjExportSchueler = {
const tspStudent = robjExportSchuelerFactory.build({
schuelerUid: undefined,
};
});

return { system, tspStudent };
};
Expand Down
51 changes: 27 additions & 24 deletions apps/server/src/infra/sync/tsp/tsp-sync.strategy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,21 @@ import { Test, TestingModule } from '@nestjs/testing';
import { UserDO } from '@shared/domain/domainobject';
import { SystemProvisioningStrategy } from '@shared/domain/interface/system-provisioning.strategy';
import { userDoFactory } from '@shared/testing';

import {
externalUserDtoFactory,
oauthDataDtoFactory,
provisioningSystemDtoFactory,
} from '@src/modules/provisioning/testing';
import {
robjExportSchuleFactory,
robjExportLehrerMigrationFactory,
robjExportSchuelerMigrationFactory,
} from '@src/infra/tsp-client/testing';
import { Logger } from '@src/core/logger';
import { Account } from '@src/modules/account';
import { accountDoFactory } from '@src/modules/account/testing';
import { ExternalUserDto, OauthDataDto, ProvisioningService, ProvisioningSystemDto } from '@src/modules/provisioning';
import { OauthDataDto, ProvisioningService } from '@src/modules/provisioning';
import { School } from '@src/modules/school';
import { schoolFactory } from '@src/modules/school/testing';
import { System } from '@src/modules/system';
Expand Down Expand Up @@ -164,12 +175,12 @@ describe(TspSyncStrategy.name, () => {
describe('sync', () => {
describe('when sync is called', () => {
const setup = () => {
const oauthDataDto = new OauthDataDto({
system: new ProvisioningSystemDto({
const oauthDataDto = oauthDataDtoFactory.build({
system: provisioningSystemDtoFactory.build({
systemId: faker.string.alpha(),
provisioningStrategy: SystemProvisioningStrategy.TSP,
}),
externalUser: new ExternalUserDto({
externalUser: externalUserDtoFactory.build({
externalId: faker.string.alpha(),
roles: [],
}),
Expand Down Expand Up @@ -302,10 +313,7 @@ describe(TspSyncStrategy.name, () => {

describe('when school does not exist', () => {
const setup = () => {
const tspSchool: RobjExportSchule = {
schuleNummer: faker.string.alpha(),
schuleName: faker.string.alpha(),
};
const tspSchool = robjExportSchuleFactory.build();
const tspSchools = [tspSchool];

setupMockServices({
Expand All @@ -324,10 +332,7 @@ describe(TspSyncStrategy.name, () => {

describe('when school does exist', () => {
const setup = () => {
const tspSchool: RobjExportSchule = {
schuleNummer: faker.string.alpha(),
schuleName: faker.string.alpha(),
};
const tspSchool = robjExportSchuleFactory.build();
const tspSchools = [tspSchool];
const school = schoolFactory.build();

Expand All @@ -348,10 +353,8 @@ describe(TspSyncStrategy.name, () => {

describe('when tsp school does not have a schulnummer', () => {
const setup = () => {
const tspSchool: RobjExportSchule = {
schuleNummer: undefined,
schuleName: faker.string.alpha(),
};
const tspSchool = robjExportSchuleFactory.build();
tspSchool.schuleNummer = undefined;
const tspSchools = [tspSchool];

setupMockServices({
Expand All @@ -372,14 +375,14 @@ describe(TspSyncStrategy.name, () => {

describe('when UidAlt or UidNeu is missing during migration', () => {
const setup = () => {
const tspTeacher: RobjExportLehrerMigration = {
const tspTeacher = robjExportLehrerMigrationFactory.build({
lehrerUidAlt: undefined,
lehrerUidNeu: faker.string.alpha(),
};
const tspStudent: RobjExportSchuelerMigration = {
});
const tspStudent = robjExportSchuelerMigrationFactory.build({
schuelerUidAlt: faker.string.alpha(),
schuelerUidNeu: undefined,
};
});

setupMockServices({
fetchedStudentMigrations: [tspStudent],
Expand All @@ -398,10 +401,10 @@ describe(TspSyncStrategy.name, () => {

describe('when no user is found during migration', () => {
const setup = () => {
const tspTeacher: RobjExportLehrerMigration = {
const tspTeacher = robjExportLehrerMigrationFactory.build({
lehrerUidAlt: faker.string.alpha(),
lehrerUidNeu: faker.string.alpha(),
};
});

setupMockServices({
fetchedTeacherMigrations: [tspTeacher],
Expand All @@ -422,10 +425,10 @@ describe(TspSyncStrategy.name, () => {

describe('when no account is found during migration', () => {
const setup = () => {
const tspTeacher: RobjExportLehrerMigration = {
const tspTeacher = robjExportLehrerMigrationFactory.build({
lehrerUidAlt: faker.string.alpha(),
lehrerUidNeu: faker.string.alpha(),
};
});

setupMockServices({
fetchedTeacherMigrations: [tspTeacher],
Expand Down
6 changes: 6 additions & 0 deletions apps/server/src/infra/tsp-client/testing/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export { robjExportSchuleFactory } from './robj-export-schule.factory';
export { robjExportKlasseFactory } from './robj-export-klasse.factory';
export { robjExportLehrerFactory } from './robj-export-lehrer.factory';
export { robjExportSchuelerFactory } from './robj-export-schueler.factory';
export { robjExportLehrerMigrationFactory } from './robj-export-lehrer-migration.factory';
export { robjExportSchuelerMigrationFactory } from './robj-export-schueler-migration.factory';
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ObjectId } from '@mikro-orm/mongodb';
import { RobjExportKlasse } from '@src/infra/tsp-client';
import { Factory } from 'fishery';

export const robjExportKlasseFactory = Factory.define<RobjExportKlasse, RobjExportKlasse>(({ sequence }) => {
return {
id: new ObjectId().toHexString(),
version: `version ${sequence}`,
klasseName: `klasseName ${sequence}`,
schuleNummer: `schuleNummer ${sequence}`,
klasseId: `klasseId ${sequence}`,
lehrerUid: `lehrerUid ${sequence}`,
};
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ObjectId } from '@mikro-orm/mongodb';
import { RobjExportLehrerMigration } from '@src/infra/tsp-client';
import { Factory } from 'fishery';

export const robjExportLehrerMigrationFactory = Factory.define<RobjExportLehrerMigration, RobjExportLehrerMigration>(
() => {
return {
lehrerUidAlt: new ObjectId().toHexString(),
lehrerUidNeu: new ObjectId().toHexString(),
};
}
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ObjectId } from '@mikro-orm/mongodb';
import { RobjExportLehrer } from '@src/infra/tsp-client';
import { Factory } from 'fishery';

export const robjExportLehrerFactory = Factory.define<RobjExportLehrer, RobjExportLehrer>(({ sequence }) => {
return {
lehrerUid: new ObjectId().toHexString(),
lehrerTitel: `lehrerTitel ${sequence}`,
lehrerVorname: `lehrerVorname ${sequence}`,
lehrerNachname: `lehrerNachname ${sequence}`,
schuleNummer: `schuleNummer ${sequence}`,
};
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ObjectId } from '@mikro-orm/mongodb';
import { RobjExportSchuelerMigration } from '@src/infra/tsp-client';
import { Factory } from 'fishery';

export const robjExportSchuelerMigrationFactory = Factory.define<
RobjExportSchuelerMigration,
RobjExportSchuelerMigration
>(() => {
return {
schuelerUidAlt: new ObjectId().toHexString(),
schuelerUidNeu: new ObjectId().toHexString(),
};
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ObjectId } from '@mikro-orm/mongodb';
import { RobjExportSchueler } from '@src/infra/tsp-client';
import { Factory } from 'fishery';

export const robjExportSchuelerFactory = Factory.define<RobjExportSchueler, RobjExportSchueler>(({ sequence }) => {
return {
schuelerUid: new ObjectId().toHexString(),
schuelerVorname: `schuelerVorname ${sequence}`,
schuelerNachname: `schuelerNachname ${sequence}`,
schuleNummer: `schuleNummer ${sequence}`,
klasseId: `klasseId ${sequence}`,
};
});
Loading
Loading