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

SPSH-1275 #777

Open
wants to merge 21 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
16 changes: 8 additions & 8 deletions src/modules/cron/cron.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export class CronController {
`System hat Benutzer ${person?.referrer} (${person?.id}) gesperrt, da nach Ablauf der Frist keine KoPers.-Nr. eingetragen war.`,
);
} else {
this.logger.info(
this.logger.error(
`System konnte Benutzer ${person?.referrer} (${person?.id}) nach Ablauf der Frist ohne KoPers.-Nr. nicht sperren. Fehler: ${updateResult.error.message}`,
);
}
Expand All @@ -119,7 +119,7 @@ export class CronController {
`System hat alle Benutzer mit einer fehlenden KoPers.-Nr nach Ablauf der Frist gesperrt.`,
);
} else {
this.logger.info(
this.logger.error(
`System konnte nicht alle Benutzer mit einer fehlenden KoPers.-Nr nach Ablauf der Frist sperren.`,
);
}
Expand Down Expand Up @@ -181,7 +181,7 @@ export class CronController {
.createNew()
.commit(personId, new Date(), count, personenKontexteToKeep, permissions);
if (result instanceof PersonenkontexteUpdateError) {
this.logger.info(
this.logger.error(
`System konnte die befristete Schulzuordnung des Benutzers ${person?.referrer} (${person?.id}) nicht aufheben. Fehler: ${result.message}`,
);
} else {
Expand All @@ -207,7 +207,7 @@ export class CronController {
if (allSuccessful) {
this.logger.info(`System hat alle abgelaufenen Schulzuordnungen entfernt.`);
} else {
this.logger.info(`System konnte nicht alle abgelaufenen Schulzuordnungen entfernen.`);
this.logger.error(`System konnte nicht alle abgelaufenen Schulzuordnungen entfernen.`);
}

return allSuccessful;
Expand Down Expand Up @@ -270,7 +270,7 @@ export class CronController {
`System hat ${person?.referrer} (${person?.id}) nach 84 Tagen ohne Schulzuordnung gelöscht.`,
);
} else {
this.logger.info(
this.logger.error(
`System konnte Benutzer ${person?.referrer} (${person?.id}) nach 84 Tagen ohne Schulzuordnung nicht löschen. Fehler: ${deleteResult.error.message}`,
);
}
Expand All @@ -286,7 +286,7 @@ export class CronController {
if (allSuccessful) {
this.logger.info(`System hat alle Benutzer mit einer fehlenden Schulzuordnung nach 84 Tagen gelöscht.`);
} else {
this.logger.info(
this.logger.error(
`System konnte nicht alle Benutzer mit einer fehlenden Schulzuordnung nach 84 Tagen löschen.`,
);
}
Expand Down Expand Up @@ -345,7 +345,7 @@ export class CronController {
`System hat die befristete Sperre von Benutzer ${person.value.referrer} (${person.value.id}) aufgehoben.`,
);
} else {
this.logger.info(
this.logger.error(
`System konnte befristete Sperre von Benutzer ${person.value.referrer} (${person.value.id}) nicht aufheben. Fehler: ${updateResult.error.message}`,
);
}
Expand All @@ -363,7 +363,7 @@ export class CronController {
`System hat die befristete Sperre von allen gesperrten Benutzern mit abgelaufener Befristung aufgehoben.`,
);
} else {
this.logger.info(
this.logger.error(
`System hat die befristete Sperre nicht von allen gesperrten Benutzern mit abgelaufener Befristung aufgehoben.`,
);
}
Expand Down
121 changes: 69 additions & 52 deletions src/modules/person/api/person.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ describe('PersonController', () => {
faker.lorem.word(),
faker.lorem.word(),
faker.string.uuid(),
undefined, // referrer
faker.person.fullName(), // referrer
undefined, // stammorganisation
undefined, // initialenFamilienname
undefined, // initialenVorname
Expand Down Expand Up @@ -736,57 +736,68 @@ describe('PersonController', () => {

describe('when locking a user is successful', () => {
const person: Person<true> = getPerson();
const lockUserBodyParams: LockUserBodyParams = {
lock: true,
locked_by: 'Theo Tester',
locked_until: new Date(),
};
it('should return a success message', async () => {
personRepositoryMock.getPersonIfAllowed.mockResolvedValueOnce({ ok: true, value: person });
keycloakUserService.updateKeycloakUserStatus.mockResolvedValueOnce({ ok: true, value: undefined });

const response: { message: string } = await personController.lockPerson(
params.personId,
lockUserBodyParams,
personPermissionsMock,
);

expect(response).toEqual({ message: 'User has been successfully locked.' });
expect(personRepositoryMock.getPersonIfAllowed).toHaveBeenCalledTimes(1);
expect(keycloakUserService.updateKeycloakUserStatus).toHaveBeenCalledTimes(1);
});
it.each([[new Date()], [undefined]])(
'should return a success message when locked_until is %p',
async (lockedUntil: Date | undefined) => {
const lockUserBodyParams: LockUserBodyParams = {
lock: true,
locked_by: 'Theo Tester',
locked_until: lockedUntil,
};
personRepositoryMock.getPersonIfAllowed.mockResolvedValueOnce({ ok: true, value: person });
keycloakUserService.updateKeycloakUserStatus.mockResolvedValueOnce({ ok: true, value: undefined });

const response: { message: string } = await personController.lockPerson(
params.personId,
lockUserBodyParams,
personPermissionsMock,
);

expect(response).toEqual({ message: 'User has been successfully locked.' });
expect(personRepositoryMock.getPersonIfAllowed).toHaveBeenCalledTimes(1);
expect(keycloakUserService.updateKeycloakUserStatus).toHaveBeenCalledTimes(1);
},
);
});

describe('when unlocking a user is successful', () => {
const person: Person<true> = getPerson();
const lockUserBodyParams: LockUserBodyParams = {
lock: false,
locked_by: 'Theo Tester',
locked_until: new Date(),
};
it('should return a success message', async () => {
personRepositoryMock.getPersonIfAllowed.mockResolvedValueOnce({ ok: true, value: person });
keycloakUserService.updateKeycloakUserStatus.mockResolvedValueOnce({ ok: true, value: undefined });

const response: { message: string } = await personController.lockPerson(
params.personId,
lockUserBodyParams,
personPermissionsMock,
);

expect(response).toEqual({ message: 'User has been successfully unlocked.' });
expect(personRepositoryMock.getPersonIfAllowed).toHaveBeenCalledTimes(1);
expect(keycloakUserService.updateKeycloakUserStatus).toHaveBeenCalledTimes(1);
});
it.each([[new Date()], [undefined]])(
'should return a success message when locked_until is %p',
async (lockedUntil: Date | undefined) => {
const lockUserBodyParams: LockUserBodyParams = {
lock: false,
locked_by: 'Theo Tester',
locked_until: lockedUntil,
};
personRepositoryMock.getPersonIfAllowed.mockResolvedValueOnce({ ok: true, value: person });
keycloakUserService.updateKeycloakUserStatus.mockResolvedValueOnce({ ok: true, value: undefined });

const response: { message: string } = await personController.lockPerson(
params.personId,
lockUserBodyParams,
personPermissionsMock,
);

expect(response).toEqual({ message: 'User has been successfully unlocked.' });
expect(personRepositoryMock.getPersonIfAllowed).toHaveBeenCalledTimes(1);
expect(keycloakUserService.updateKeycloakUserStatus).toHaveBeenCalledTimes(1);
},
);
});

describe('when person does not exist or no permissions', () => {
const lockUserBodyParams: LockUserBodyParams = {
lock: false,
locked_by: '2024-01-01T00:00:00Z',
locked_until: new Date(),
};
it('should throw an error', async () => {
it.each([
{ lock: false, description: 'lock is false' },
{ lock: true, description: 'lock is true' },
])('should throw an error when $description', async ({ lock }: { lock: boolean }) => {
const lockUserBodyParams: LockUserBodyParams = {
lock,
locked_by: '2024-01-01T00:00:00Z',
locked_until: new Date(),
};
personRepositoryMock.getPersonIfAllowed.mockResolvedValueOnce({
ok: false,
error: new EntityNotFoundError('Person'),
Expand All @@ -798,15 +809,18 @@ describe('PersonController', () => {
});

describe('when keycloakUserId is missing', () => {
const lockUserBodyParams: LockUserBodyParams = {
lock: false,
locked_by: '2024-01-01T00:00:00Z',
locked_until: new Date(),
};
const person: Person<true> = getPerson();
person.keycloakUserId = undefined;

it('should throw an error', async () => {
it.each([
{ lock: false, description: 'lock is false' },
{ lock: true, description: 'lock is true' },
])('should throw an error when $description', async ({ lock }: { lock: boolean }) => {
const lockUserBodyParams: LockUserBodyParams = {
lock,
locked_by: '2024-01-01T00:00:00Z',
locked_until: new Date(),
};
personRepositoryMock.getPersonIfAllowed.mockResolvedValueOnce({
ok: true,
value: person,
Expand All @@ -821,9 +835,12 @@ describe('PersonController', () => {
const person: Person<true> = getPerson();
person.keycloakUserId = 'keycloak-12345';

it('should throw an error', async () => {
it.each([
{ lock: false, description: 'lock is false' },
{ lock: true, description: 'lock is true' },
])('should throw an error when $description', async ({ lock }: { lock: boolean }) => {
const lockUserBodyParams: LockUserBodyParams = {
lock: false,
lock,
locked_by: '2024-01-01T00:00:00Z',
locked_until: new Date(),
};
Expand Down Expand Up @@ -910,7 +927,7 @@ describe('PersonController', () => {
);
});

it('should throw PersonalnummerRequiredError when personalnummer was not provided and faminlienname or vorname did not change', async () => {
it('should throw PersonalnummerRequiredError when personalnummer was not provided and familienname or vorname did not change', async () => {
const person: Person<true> = getPerson();
const bodyWithInvalidPersonalnummer: PersonMetadataBodyParams = {
familienname: person.familienname,
Expand Down
Loading
Loading