From 36d505df29feaa91e1c79ff31e7422b1b34bae5b Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Tue, 28 Nov 2023 09:09:56 +0100 Subject: [PATCH] add tests --- .../deletion/uc/deletion-request.uc.spec.ts | 21 +++++++++++++++++-- .../deletion/uc/deletion-request.uc.ts | 15 ++++--------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts b/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts index f0f66ffe0b9..69ec72a0db5 100644 --- a/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts +++ b/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts @@ -1,6 +1,6 @@ import { Test, TestingModule } from '@nestjs/testing'; import { DeepMocked, createMock } from '@golevelup/ts-jest'; -import { setupEntities } from '@shared/testing'; +import { setupEntities, userDoFactory } from '@shared/testing'; import { AccountService } from '@modules/account/services'; import { ClassService } from '@modules/class'; import { CourseGroupService, CourseService } from '@modules/learnroom/service'; @@ -175,9 +175,11 @@ describe(DeletionRequestUc.name, () => { const setup = () => { jest.clearAllMocks(); const deletionRequestToExecute = deletionRequestFactory.build({ deleteAfter: new Date('2023-01-01') }); + const user = userDoFactory.buildWithId(); const rocketChatUser: RocketChatUser = rocketChatUserFactory.build({ userId: deletionRequestToExecute.targetRefId, }); + const parentEmail = 'parent@parent.eu'; registrationPinService.deleteRegistrationPinByEmail.mockResolvedValueOnce(2); classService.deleteUserDataFromClasses.mockResolvedValueOnce(1); @@ -194,6 +196,8 @@ describe(DeletionRequestUc.name, () => { return { deletionRequestToExecute, rocketChatUser, + user, + parentEmail, }; }; @@ -233,6 +237,19 @@ describe(DeletionRequestUc.name, () => { expect(registrationPinService.deleteRegistrationPinByEmail).toHaveBeenCalled(); }); + it('should call userService.getParentEmailsFromUser to get parentEmails', async () => { + const { deletionRequestToExecute, user, parentEmail } = setup(); + + deletionRequestService.findAllItemsToExecute.mockResolvedValueOnce([deletionRequestToExecute]); + userService.findById.mockResolvedValueOnce(user); + userService.getParentEmailsFromUser.mockRejectedValue([parentEmail]); + registrationPinService.deleteRegistrationPinByEmail.mockRejectedValueOnce(2); + + await uc.executeDeletionRequests(); + + expect(userService.getParentEmailsFromUser).toHaveBeenCalledWith(deletionRequestToExecute.targetRefId); + }); + it('should call classService.deleteUserDataFromClasses to delete user data in class module', async () => { const { deletionRequestToExecute } = setup(); @@ -364,7 +381,7 @@ describe(DeletionRequestUc.name, () => { await uc.executeDeletionRequests(); - expect(deletionLogService.createDeletionLog).toHaveBeenCalledTimes(10); + expect(deletionLogService.createDeletionLog).toHaveBeenCalledTimes(9); }); }); diff --git a/apps/server/src/modules/deletion/uc/deletion-request.uc.ts b/apps/server/src/modules/deletion/uc/deletion-request.uc.ts index 945af8b9b96..7bacc428310 100644 --- a/apps/server/src/modules/deletion/uc/deletion-request.uc.ts +++ b/apps/server/src/modules/deletion/uc/deletion-request.uc.ts @@ -137,20 +137,13 @@ export class DeletionRequestUc { private async removeUserRegistrationPin(deletionRequest: DeletionRequest) { const userToDeletion = await this.userService.findById(deletionRequest.targetRefId); const parentEmails = await this.userService.getParentEmailsFromUser(deletionRequest.targetRefId); - let deletedParentRegistrationPin = 0; + const emailsToDeletion: string[] = [userToDeletion.email, ...parentEmails]; - if (parentEmails) { - const result = await Promise.all( - parentEmails.map((email) => this.registrationPinService.deleteRegistrationPinByEmail(email)) - ); - deletedParentRegistrationPin = result.filter((res) => res !== 0).length; - } - - const deletedUserRegistrationPins: number = await this.registrationPinService.deleteRegistrationPinByEmail( - userToDeletion.email + const result = await Promise.all( + emailsToDeletion.map((email) => this.registrationPinService.deleteRegistrationPinByEmail(email)) ); + const deletedRegistrationPin = result.filter((res) => res !== 0).length; - const deletedRegistrationPin = deletedUserRegistrationPins + deletedParentRegistrationPin; await this.logDeletion( deletionRequest, DeletionDomainModel.REGISTRATIONPIN,