Skip to content

Commit

Permalink
SPSH-698: Added unit tests for the limit parameter & updated the logi…
Browse files Browse the repository at this point in the history
…c for returned allowedRollen when the limit is set.
  • Loading branch information
phaelcg committed Jul 4, 2024
1 parent e21a368 commit 7a6e0f6
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,48 @@ describe('PersonenkontextWorkflow', () => {

expect(result).toHaveLength(0);
});
it('should limit roles returned allowedRollen if limit is set', async () => {
const organisation: OrganisationDo<true> = DoFactory.createOrganisation(true, {
typ: OrganisationsTyp.LAND,
});
const childOrganisation: OrganisationDo<true> = DoFactory.createOrganisation(true, {
typ: OrganisationsTyp.KLASSE,
});
const rolle1: Rolle<true> = DoFactory.createRolle(true, {
rollenart: RollenArt.ORGADMIN,
name: 'rolle1',
});
const rolle2: Rolle<true> = DoFactory.createRolle(true, {
rollenart: RollenArt.ORGADMIN,
name: 'rolle2',
});
const rolle3: Rolle<true> = DoFactory.createRolle(true, {
rollenart: RollenArt.ORGADMIN,
name: 'rolle3',
});
const rolle4: Rolle<true> = DoFactory.createRolle(true, {
rollenart: RollenArt.ORGADMIN,
name: 'rolle4',
});
const rollen: Rolle<true>[] = [rolle1, rolle2, rolle3, rolle4];
const orgsWithRecht: string[] = [organisation.id, childOrganisation.id];

organisationRepoMock.findById.mockResolvedValue(organisation);
organisationRepoMock.findChildOrgasForIds.mockResolvedValue([childOrganisation]);
organisationRepoMock.findByIds.mockResolvedValue(
new Map(orgsWithRecht.map((id: string) => [id, DoFactory.createOrganisation(true, { id })])),
);
rolleRepoMock.find.mockResolvedValue(rollen);

const permissions: DeepMocked<PersonPermissions> = createMock<PersonPermissions>();
permissions.getOrgIdsWithSystemrecht.mockResolvedValue(orgsWithRecht);

anlage.initialize(organisation.id);

const result: Rolle<true>[] = await anlage.findRollenForOrganisation(permissions, undefined, 2);

expect(result).toHaveLength(2);
});
});
describe('commit', () => {
it('should successfully commit personenkontexte', async () => {
Expand Down Expand Up @@ -1088,5 +1130,44 @@ describe('PersonenkontextWorkflow', () => {
);
expect(result).toEqual([]);
});

it('should return list of limited rollen, if the user is Landesadmin and the limit is set', async () => {
const rolle: Rolle<true> = DoFactory.createRolle(true, { rollenart: RollenArt.SYSADMIN });
const leitRolle: Rolle<true> = DoFactory.createRolle(true, { rollenart: RollenArt.LEIT });
const lehrRolle: Rolle<true> = DoFactory.createRolle(true, { rollenart: RollenArt.LEHR });
const lernRolle: Rolle<true> = DoFactory.createRolle(true, { rollenart: RollenArt.LERN });

const rollen: Rolle<true>[] = [rolle, leitRolle, lehrRolle, lernRolle];
rolleRepoMock.find.mockResolvedValue(rollen);

personpermissionsMock.getOrgIdsWithSystemrecht.mockResolvedValueOnce([
organisationRepoMock.ROOT_ORGANISATION_ID,
]);

const result: Rolle<true>[] = await anlage.findAuthorizedRollen(personpermissionsMock, undefined, 2);
expect(result).toHaveLength(2);
});

it('should return list of limited allowedRollen, if the user is NOT Landesadmin and the limit is set', async () => {
const rolle: Rolle<true> = DoFactory.createRolle(true, { rollenart: RollenArt.SYSADMIN });
const leitRolle: Rolle<true> = DoFactory.createRolle(true, { rollenart: RollenArt.LEIT });
const lehrRolle: Rolle<true> = DoFactory.createRolle(true, { rollenart: RollenArt.LEHR });
const lernRolle: Rolle<true> = DoFactory.createRolle(true, { rollenart: RollenArt.LERN });

const rollen: Rolle<true>[] = [rolle, leitRolle, lehrRolle, lernRolle];
rolleRepoMock.find.mockResolvedValue(rollen);

const organisationDo: OrganisationDo<true> = DoFactory.createOrganisation(true, {
typ: OrganisationsTyp.SCHULE,
});
const organisationMap: Map<string, OrganisationDo<true>> = new Map();
organisationMap.set(organisationDo.id, organisationDo);
organisationRepoMock.findByIds.mockResolvedValueOnce(organisationMap);

personpermissionsMock.getOrgIdsWithSystemrecht.mockResolvedValueOnce([organisationDo.id]);

const result: Rolle<true>[] = await anlage.findAuthorizedRollen(personpermissionsMock, undefined, 2);
expect(result).toHaveLength(2);
});
});
});
12 changes: 5 additions & 7 deletions src/modules/personenkontext/domain/personenkontext-workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,11 @@ export class PersonenkontextWorkflowAggregate {
);

//Landesadmin can view all roles.
if (orgsWithRecht.includes(this.organisationRepo.ROOT_ORGANISATION_ID)) return rollen;
if (orgsWithRecht.includes(this.organisationRepo.ROOT_ORGANISATION_ID)) {
return limit ? rollen.slice(0, limit) : rollen;
}

let allowedRollen: Rolle<true>[] = [];
const allowedRollen: Rolle<true>[] = [];
const organisationMatchesRollenart: OrganisationMatchesRollenart = new OrganisationMatchesRollenart();
(await this.organisationRepo.findByIds(orgsWithRecht)).forEach(function (orga: OrganisationDo<true>) {
rollen.forEach(function (rolle: Rolle<true>) {
Expand All @@ -324,10 +326,6 @@ export class PersonenkontextWorkflowAggregate {
});
});

if (limit) {
allowedRollen = allowedRollen.slice(0, limit);
}

return allowedRollen;
return limit ? allowedRollen.slice(0, limit) : allowedRollen;
}
}

0 comments on commit 7a6e0f6

Please sign in to comment.