From 48083ae17fea4422b3e2f1c4db52d947ff0c25bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marvin=20=C3=96hlerking?= Date: Fri, 14 Jun 2024 16:02:43 +0200 Subject: [PATCH] filter empty mediumId --- .../src/infra/schulconnex-client/index.ts | 2 +- .../infra/schulconnex-client/testing/index.ts | 1 + ...chulconnex-lizenz-info-response-factory.ts | 8 +- .../sanis/schulconnex-response-mapper.spec.ts | 124 +++++++++++++----- .../sanis/schulconnex-response-mapper.ts | 26 ++-- 5 files changed, 112 insertions(+), 49 deletions(-) diff --git a/apps/server/src/infra/schulconnex-client/index.ts b/apps/server/src/infra/schulconnex-client/index.ts index 9249683024a..8d08d2fa56f 100644 --- a/apps/server/src/infra/schulconnex-client/index.ts +++ b/apps/server/src/infra/schulconnex-client/index.ts @@ -17,5 +17,5 @@ export { SchulconnexPersonenkontextResponse, SchulconnexSonstigeGruppenzugehoerigeResponse, } from './response'; -export { schulconnexResponseFactory } from './testing/schulconnex-response-factory'; +export { schulconnexResponseFactory, schulconnexLizenzInfoResponseFactory } from './testing'; export { SchulconnexClientConfig } from './schulconnex-client-config'; diff --git a/apps/server/src/infra/schulconnex-client/testing/index.ts b/apps/server/src/infra/schulconnex-client/testing/index.ts index 673fc651a51..23b700123f1 100644 --- a/apps/server/src/infra/schulconnex-client/testing/index.ts +++ b/apps/server/src/infra/schulconnex-client/testing/index.ts @@ -1 +1,2 @@ export { schulconnexResponseFactory } from './schulconnex-response-factory'; +export { schulconnexLizenzInfoResponseFactory } from './schulconnex-lizenz-info-response-factory'; diff --git a/apps/server/src/infra/schulconnex-client/testing/schulconnex-lizenz-info-response-factory.ts b/apps/server/src/infra/schulconnex-client/testing/schulconnex-lizenz-info-response-factory.ts index 16e9d355428..c89989a180e 100644 --- a/apps/server/src/infra/schulconnex-client/testing/schulconnex-lizenz-info-response-factory.ts +++ b/apps/server/src/infra/schulconnex-client/testing/schulconnex-lizenz-info-response-factory.ts @@ -1,8 +1,8 @@ import { Factory } from 'fishery'; import { SchulconnexLizenzInfoActionType, SchulconnexLizenzInfoResponse } from '../response'; -export const schulconnexLizenzInfoResponseFactory = Factory.define(() => [ - { +export const schulconnexLizenzInfoResponseFactory = Factory.define(() => { + return { target: { uid: 'bildungscloud', partOf: '', @@ -12,5 +12,5 @@ export const schulconnexLizenzInfoResponseFactory = Factory.define { provisioningFeatures = module.get(ProvisioningFeatures); }); - const setupSchulconnexResponse = () => { - const externalUserId = 'aef1f4fd-c323-466e-962b-a84354c0e713'; - const externalSchoolId = 'df66c8e6-cfac-40f7-b35b-0da5d8ee680e'; + describe('mapToExternalSchoolDto', () => { + describe('when a schulconnex response is provided', () => { + const setup = () => { + const externalSchoolId = 'df66c8e6-cfac-40f7-b35b-0da5d8ee680e'; - const schulconnexResponse: SchulconnexResponse = schulconnexResponseFactory.build(); - const licenseResponse: SchulconnexLizenzInfoResponse[] = schulconnexLizenzInfoResponseFactory.build(); + const schulconnexResponse: SchulconnexResponse = schulconnexResponseFactory.build(); - return { - externalUserId, - externalSchoolId, - schulconnexResponse, - licenseResponse, - }; - }; + return { + externalSchoolId, + schulconnexResponse, + }; + }; - describe('mapToExternalSchoolDto', () => { - describe('when a schulconnex response is provided', () => { it('should map the response to an ExternalSchoolDto', () => { - const { schulconnexResponse, externalSchoolId } = setupSchulconnexResponse(); + const { schulconnexResponse, externalSchoolId } = setup(); const result: ExternalSchoolDto = mapper.mapToExternalSchoolDto(schulconnexResponse); @@ -77,8 +73,19 @@ describe(SchulconnexResponseMapper.name, () => { describe('mapToExternalUserDto', () => { describe('when a schulconnex response is provided', () => { + const setup = () => { + const externalUserId = 'aef1f4fd-c323-466e-962b-a84354c0e713'; + + const schulconnexResponse: SchulconnexResponse = schulconnexResponseFactory.build(); + + return { + externalUserId, + schulconnexResponse, + }; + }; + it('should map the response to an ExternalUserDto', () => { - const { schulconnexResponse, externalUserId } = setupSchulconnexResponse(); + const { schulconnexResponse, externalUserId } = setup(); const result: ExternalUserDto = mapper.mapToExternalUserDto(schulconnexResponse); @@ -97,7 +104,7 @@ describe(SchulconnexResponseMapper.name, () => { describe('mapToExternalGroupDtos', () => { describe('when no group is given', () => { const setup = () => { - const { schulconnexResponse } = setupSchulconnexResponse(); + const schulconnexResponse: SchulconnexResponse = schulconnexResponseFactory.build(); schulconnexResponse.personenkontexte[0].gruppen = undefined; return { @@ -116,10 +123,8 @@ describe(SchulconnexResponseMapper.name, () => { describe('when unknown group type is given', () => { const setup = () => { - const { schulconnexResponse } = setupSchulconnexResponse(); - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - schulconnexResponse.personenkontexte[0].gruppen?.[0].gruppe.typ = 'unknown'; + const schulconnexResponse: SchulconnexResponse = schulconnexResponseFactory.build(); + schulconnexResponse.personenkontexte[0].gruppen![0].gruppe.typ = 'unknown' as SchulconnexGroupType; return { schulconnexResponse, @@ -141,7 +146,7 @@ describe(SchulconnexResponseMapper.name, () => { schulconnexOtherGroupusersEnabled: true, }); - const { schulconnexResponse } = setupSchulconnexResponse(); + const schulconnexResponse: SchulconnexResponse = schulconnexResponseFactory.build(); const personenkontext: SchulconnexPersonenkontextResponse = schulconnexResponse.personenkontexte[0]; const group: SchulconnexGruppenResponse = personenkontext.gruppen![0]; const otherParticipant: SchulconnexSonstigeGruppenzugehoerigeResponse = group.sonstige_gruppenzugehoerige![0]; @@ -179,7 +184,7 @@ describe(SchulconnexResponseMapper.name, () => { describe('when group type other is provided', () => { const setup = () => { - const { schulconnexResponse } = setupSchulconnexResponse(); + const schulconnexResponse: SchulconnexResponse = schulconnexResponseFactory.build(); schulconnexResponse.personenkontexte[0].gruppen![0]!.gruppe.typ = SchulconnexGroupType.OTHER; return { @@ -202,7 +207,7 @@ describe(SchulconnexResponseMapper.name, () => { describe('when group type course is provided', () => { const setup = () => { - const { schulconnexResponse } = setupSchulconnexResponse(); + const schulconnexResponse: SchulconnexResponse = schulconnexResponseFactory.build(); schulconnexResponse.personenkontexte[0].gruppen![0]!.gruppe.typ = SchulconnexGroupType.COURSE; return { @@ -225,7 +230,7 @@ describe(SchulconnexResponseMapper.name, () => { describe('when the group role mapping for the user is missing', () => { const setup = () => { - const { schulconnexResponse } = setupSchulconnexResponse(); + const schulconnexResponse: SchulconnexResponse = schulconnexResponseFactory.build(); schulconnexResponse.personenkontexte[0].gruppen![0]!.gruppenzugehoerigkeit.rollen = [ SchulconnexGroupRole.SCHOOL_SUPPORT, ]; @@ -246,7 +251,7 @@ describe(SchulconnexResponseMapper.name, () => { describe('when the user has no role in the group', () => { const setup = () => { - const { schulconnexResponse } = setupSchulconnexResponse(); + const schulconnexResponse: SchulconnexResponse = schulconnexResponseFactory.build(); schulconnexResponse.personenkontexte[0].gruppen![0]!.gruppenzugehoerigkeit.rollen = []; return { @@ -268,7 +273,7 @@ describe(SchulconnexResponseMapper.name, () => { Object.assign>(provisioningFeatures, { schulconnexOtherGroupusersEnabled: false, }); - const { schulconnexResponse } = setupSchulconnexResponse(); + const schulconnexResponse: SchulconnexResponse = schulconnexResponseFactory.build(); schulconnexResponse.personenkontexte[0].gruppen![0].sonstige_gruppenzugehoerige = undefined; return { @@ -291,7 +296,7 @@ describe(SchulconnexResponseMapper.name, () => { schulconnexOtherGroupusersEnabled: true, }); - const { schulconnexResponse } = setupSchulconnexResponse(); + const schulconnexResponse: SchulconnexResponse = schulconnexResponseFactory.build(); schulconnexResponse.personenkontexte[0].gruppen![0].sonstige_gruppenzugehoerige = undefined; return { @@ -310,7 +315,7 @@ describe(SchulconnexResponseMapper.name, () => { describe('when other participants have unknown roles', () => { const setup = () => { - const { schulconnexResponse } = setupSchulconnexResponse(); + const schulconnexResponse: SchulconnexResponse = schulconnexResponseFactory.build(); schulconnexResponse.personenkontexte[0].gruppen![0]!.sonstige_gruppenzugehoerige = [ { ktid: 'ktid', @@ -334,9 +339,19 @@ describe(SchulconnexResponseMapper.name, () => { }); describe('mapToExternalLicenses', () => { - describe('when a sanis response with license is provided', () => { + describe('when a license response has a medium id and no media source', () => { + const setup = () => { + const licenseResponse: SchulconnexLizenzInfoResponse[] = schulconnexLizenzInfoResponseFactory.buildList(1, { + target: { uid: 'bildungscloud', partOf: '' }, + }); + + return { + licenseResponse, + }; + }; + it('should map the response to an ExternalLicenseDto', () => { - const { licenseResponse } = setupSchulconnexResponse(); + const { licenseResponse } = setup(); const result: ExternalLicenseDto[] = SchulconnexResponseMapper.mapToExternalLicenses(licenseResponse); @@ -348,5 +363,50 @@ describe(SchulconnexResponseMapper.name, () => { ]); }); }); + + describe('when a license response has a medium id and a media source', () => { + const setup = () => { + const licenseResponse: SchulconnexLizenzInfoResponse[] = schulconnexLizenzInfoResponseFactory.buildList(1, { + target: { uid: 'bildungscloud', partOf: 'bildungscloud-source' }, + }); + + return { + licenseResponse, + }; + }; + + it('should map the response to an ExternalLicenseDto', () => { + const { licenseResponse } = setup(); + + const result: ExternalLicenseDto[] = SchulconnexResponseMapper.mapToExternalLicenses(licenseResponse); + + expect(result).toEqual([ + { + mediumId: 'bildungscloud', + mediaSourceId: 'bildungscloud-source', + }, + ]); + }); + }); + + describe('when a license response has no medium id', () => { + const setup = () => { + const licenseResponse: SchulconnexLizenzInfoResponse[] = schulconnexLizenzInfoResponseFactory.buildList(1, { + target: { uid: '', partOf: 'bildungscloud-source' }, + }); + + return { + licenseResponse, + }; + }; + + it('should should be filtered out', () => { + const { licenseResponse } = setup(); + + const result: ExternalLicenseDto[] = SchulconnexResponseMapper.mapToExternalLicenses(licenseResponse); + + expect(result).toEqual([]); + }); + }); }); }); diff --git a/apps/server/src/modules/provisioning/strategy/sanis/schulconnex-response-mapper.ts b/apps/server/src/modules/provisioning/strategy/sanis/schulconnex-response-mapper.ts index ebfb786a18b..aef7f09366d 100644 --- a/apps/server/src/modules/provisioning/strategy/sanis/schulconnex-response-mapper.ts +++ b/apps/server/src/modules/provisioning/strategy/sanis/schulconnex-response-mapper.ts @@ -173,18 +173,20 @@ export class SchulconnexResponseMapper { } public static mapToExternalLicenses(licenseInfos: SchulconnexLizenzInfoResponse[]): ExternalLicenseDto[] { - const externalLicenseDtos: ExternalLicenseDto[] = licenseInfos.map((license: SchulconnexLizenzInfoResponse) => { - if (license.target.partOf === '') { - license.target.partOf = undefined; - } - - const externalLicenseDto: ExternalLicenseDto = new ExternalLicenseDto({ - mediumId: license.target.uid, - mediaSourceId: license.target.partOf, - }); - - return externalLicenseDto; - }); + const externalLicenseDtos: ExternalLicenseDto[] = licenseInfos + .map((license: SchulconnexLizenzInfoResponse) => { + if (license.target.partOf === '') { + license.target.partOf = undefined; + } + + const externalLicenseDto: ExternalLicenseDto = new ExternalLicenseDto({ + mediumId: license.target.uid, + mediaSourceId: license.target.partOf, + }); + + return externalLicenseDto; + }) + .filter((license: ExternalLicenseDto) => license.mediumId !== ''); return externalLicenseDtos; }