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 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 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
132 changes: 68 additions & 64 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,18 @@
import { faker } from '@faker-js/faker';
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import {
ExternalClassDto,
ExternalSchoolDto,
ExternalUserDto,
OauthDataDto,
ProvisioningSystemDto,
} from '@modules/provisioning';
import { ProvisioningSystemDto } from '@modules/provisioning';
import { Test, TestingModule } from '@nestjs/testing';
import { RoleName } from '@shared/domain/interface';
import { SystemProvisioningStrategy } from '@shared/domain/interface/system-provisioning.strategy';
import { externalSchoolDtoFactory } from '@shared/testing';
import { externalClassDtoFactory } from '@shared/testing/factory/external-class-dto.factory';
import { externalUserDtoFactory } from '@shared/testing/factory/external-user-dto.factory';
import { oauthDataDtoFactory } from '@shared/testing/factory/oauth-data-dto.factory';
import { provisioningSystemDtoFactory } from '@shared/testing/factory/provisioning-system-dto.factory';
import { robjExportKlasseFactory } from '@shared/testing/factory/robj-export-klasse.factory';
import { robjExportLehrerFactory } from '@shared/testing/factory/robj-export-lehrer.factory';
import { robjExportSchuelerFactory } from '@shared/testing/factory/robj-export-schueler.factory';
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 +65,74 @@ 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 externalTeacherUserDto = externalUserDtoFactory.build({
externalId: tspTeachers[0].lehrerUid ?? '',
firstName: tspTeachers[0].lehrerVorname,
lastName: tspTeachers[0].lehrerNachname,
roles: [RoleName.TEACHER],
email: undefined,
});

const externalClass = new ExternalClassDto({
externalId: klasseId,
name: tspClasses[0].klasseName,
const externalStudentUserDto = externalUserDtoFactory.build({
externalId: tspStudents[0].schuelerUid ?? '',
firstName: tspStudents[0].schuelerVorname,
lastName: tspStudents[0].schuelerNachname,
roles: [RoleName.STUDENT],
email: 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 +169,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 +189,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 +209,9 @@ describe(TspOauthDataMapper.name, () => {
const setup = () => {
const system = systemFactory.build();

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

return { system, tspStudent };
};
Expand Down
48 changes: 23 additions & 25 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,17 +13,23 @@ 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 } from '@shared/testing/factory/external-user-dto.factory';
import { oauthDataDtoFactory } from '@shared/testing/factory/oauth-data-dto.factory';
import { provisioningSystemDtoFactory } from '@shared/testing/factory/provisioning-system-dto.factory';
import { robjExportLehrerMigrationFactory } from '@shared/testing/factory/robj-export-lehrer-migration.factory';
import { robjExportSchuelerMigrationFactory } from '@shared/testing/factory/robj-export-schueler-migration.factory';
import { robjExportSchuleFactory } from '@shared/testing/factory/robj-export-schule.factory';
import { Logger } from '@src/core/logger';
import { Account } from '@src/modules/account';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid '@src'

Suggested change
import { Account } from '@src/modules/account';
import { robjExportSchuleFactory } from '@infra/tsp-client/testing';

import { accountDoFactory } from '@src/modules/account/testing';
import { ExternalUserDto, OauthDataDto, ProvisioningService, ProvisioningSystemDto } from '@src/modules/provisioning';
import { OauthDataDto, ProvisioningService } from '@src/modules/provisioning';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove @src

import { School } from '@src/modules/school';
import { schoolFactory } from '@src/modules/school/testing';
import { System } from '@src/modules/system';
import { systemFactory } from '@src/modules/system/testing';
import { SyncStrategyTarget } from '../sync-strategy.types';
import { TspLegacyMigrationService } from './tsp-legacy-migration.service';
import { TspFetchService } from './tsp-fetch.service';
import { TspLegacyMigrationService } from './tsp-legacy-migration.service';
import { TspOauthDataMapper } from './tsp-oauth-data.mapper';
import { TspSyncConfig } from './tsp-sync.config';
import { TspSyncService } from './tsp-sync.service';
Expand Down Expand Up @@ -164,12 +170,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(),
}),
});
Expand Down Expand Up @@ -301,10 +307,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 @@ -323,10 +326,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 @@ -347,10 +347,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 @@ -371,14 +369,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 @@ -397,10 +395,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 @@ -421,10 +419,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
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { System, SystemService } from '@modules/system';
import { systemFactory } from '@modules/system/testing';
import { InternalServerErrorException } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { SystemProvisioningStrategy } from '@shared/domain/interface/system-provisioning.strategy';
import { oauthDataDtoFactory } from '@shared/testing/factory/oauth-data-dto.factory';
import { provisioningDtoFactory } from '@shared/testing/factory/provisioning-dto.factory';
import { provisioningSystemDtoFactory } from '@shared/testing/factory/provisioning-system-dto.factory';
import { systemFactory } from '@src/modules/system/testing';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove @src

import {
ExternalUserDto,
OauthDataDto,
Expand All @@ -12,8 +15,8 @@ import {
ProvisioningSystemDto,
} from '../dto';
import { IservProvisioningStrategy, OidcMockProvisioningStrategy, SanisProvisioningStrategy } from '../strategy';
import { ProvisioningService } from './provisioning.service';
import { TspProvisioningStrategy } from '../strategy/tsp/tsp.strategy';
import { ProvisioningService } from './provisioning.service';

describe('ProvisioningService', () => {
let module: TestingModule;
Expand Down Expand Up @@ -83,18 +86,18 @@ describe('ProvisioningService', () => {
provisioningUrl: 'https://api.moin.schule/',
provisioningStrategy: SystemProvisioningStrategy.SANIS,
});
const provisioningSystemDto: ProvisioningSystemDto = new ProvisioningSystemDto({
const provisioningSystemDto: ProvisioningSystemDto = provisioningSystemDtoFactory.build({
systemId: system.id,
provisioningUrl: 'https://api.moin.schule/',
provisioningStrategy: SystemProvisioningStrategy.SANIS,
});
const oauthDataDto: OauthDataDto = new OauthDataDto({
const oauthDataDto: OauthDataDto = oauthDataDtoFactory.build({
system: provisioningSystemDto,
externalUser: new ExternalUserDto({
externalId: 'externalUserId',
}),
});
const provisioningDto: ProvisioningDto = new ProvisioningDto({
const provisioningDto: ProvisioningDto = provisioningDtoFactory.build({
externalUserId: 'externalUserId',
});

Expand Down
Loading
Loading