Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
WojciechGrancow committed Nov 28, 2023
1 parent b1c012c commit 36d505d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
21 changes: 19 additions & 2 deletions apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 = '[email protected]';

registrationPinService.deleteRegistrationPinByEmail.mockResolvedValueOnce(2);
classService.deleteUserDataFromClasses.mockResolvedValueOnce(1);
Expand All @@ -194,6 +196,8 @@ describe(DeletionRequestUc.name, () => {
return {
deletionRequestToExecute,
rocketChatUser,
user,
parentEmail,
};
};

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -364,7 +381,7 @@ describe(DeletionRequestUc.name, () => {

await uc.executeDeletionRequests();

expect(deletionLogService.createDeletionLog).toHaveBeenCalledTimes(10);
expect(deletionLogService.createDeletionLog).toHaveBeenCalledTimes(9);
});
});

Expand Down
15 changes: 4 additions & 11 deletions apps/server/src/modules/deletion/uc/deletion-request.uc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 36d505d

Please sign in to comment.