From 89bd3b90011104cd38dc2fc0ad0d50a7fc83eb8c Mon Sep 17 00:00:00 2001 From: Thomas Feldtkeller Date: Fri, 13 Dec 2024 10:36:24 +0100 Subject: [PATCH] refactor tests --- .../service/room-membership.service.spec.ts | 139 ++++++++++-------- 1 file changed, 80 insertions(+), 59 deletions(-) diff --git a/apps/server/src/modules/room-membership/service/room-membership.service.spec.ts b/apps/server/src/modules/room-membership/service/room-membership.service.spec.ts index bfc45cf99d..c6249661dc 100644 --- a/apps/server/src/modules/room-membership/service/room-membership.service.spec.ts +++ b/apps/server/src/modules/room-membership/service/room-membership.service.spec.ts @@ -178,6 +178,9 @@ describe('RoomMembershipService', () => { schoolId, }); + groupService.findById.mockResolvedValue(group); + roomMembershipRepo.findByRoomId.mockResolvedValue(roomMembership); + return { group, room, roomMembership }; }; @@ -185,6 +188,14 @@ describe('RoomMembershipService', () => { groupService.findGroups.mockResolvedValue({ total: groups.length, data: groups }); }; + const setupRoomRoles = () => { + const roomOwnerRole = roleFactory.buildWithId({ name: RoleName.ROOMOWNER }); + const roomEditorRole = roleFactory.buildWithId({ name: RoleName.ROOMEDITOR }); + roleService.findByName.mockResolvedValue(roomOwnerRole); + + return { roomOwnerRole, roomEditorRole }; + }; + const setupUserWithSecondarySchool = () => { const secondarySchool = schoolFactory.build(); const otherSchool = schoolFactory.build(); @@ -194,99 +205,109 @@ describe('RoomMembershipService', () => { roles: [role], secondarySchools: [{ schoolId: secondarySchool.id, role: new RoleDto(guestTeacher) }], }); + const externalUserId = externalUser.id as string; - return { secondarySchool, externalUser, otherSchool }; + return { secondarySchool, externalUser, externalUserId, otherSchool }; }; - it('should pass the schoolId of the room', async () => { - const { secondarySchool, externalUser } = setupUserWithSecondarySchool(); - - const roomEditorRole = roleFactory.buildWithId({ name: RoleName.ROOMEDITOR }); + describe('when removing user from a different school, with no further groups on host school', () => { + const setup = () => { + const { secondarySchool, externalUserId } = setupUserWithSecondarySchool(); + const { roomEditorRole } = setupRoomRoles(); - const { room, group, roomMembership } = setupGroupAndRoom(secondarySchool.id); - group.addUser({ userId: externalUser.id as string, roleId: roomEditorRole.id }); + const { room, group } = setupGroupAndRoom(secondarySchool.id); + group.addUser({ userId: externalUserId, roleId: roomEditorRole.id }); - roomMembershipRepo.findByRoomId.mockResolvedValue(roomMembership); - groupService.findById.mockResolvedValue(group); - groupService.removeUsersFromGroup.mockResolvedValue(group); - mockGroupsAtSchoolAfterRemoval([]); + mockGroupsAtSchoolAfterRemoval([]); - await service.removeMembersFromRoom(room.id, [externalUser.id as string]); + return { secondarySchool, externalUserId, room, group }; + }; - expect(groupService.findGroups).toHaveBeenCalledWith(expect.objectContaining({ schoolId: secondarySchool.id })); - }); + it('should pass the schoolId of the room', async () => { + const { secondarySchool, externalUserId, room } = setup(); - it('should remove roomMembership', async () => { - const user = userFactory.buildWithId(); - const { room, group, roomMembership } = setupGroupAndRoom(user.school.id); - const roomOwnerRole = roleFactory.buildWithId({ name: RoleName.ROOMOWNER }); - groupService.findById.mockResolvedValue(group); - roomMembershipRepo.findByRoomId.mockResolvedValue(roomMembership); - roleService.findByName.mockResolvedValue(roomOwnerRole); - mockGroupsAtSchoolAfterRemoval([group]); + await service.removeMembersFromRoom(room.id, [externalUserId]); - await service.removeMembersFromRoom(room.id, [user.id]); + expect(groupService.findGroups).toHaveBeenCalledWith( + expect.objectContaining({ schoolId: secondarySchool.id }) + ); + }); - expect(groupService.removeUsersFromGroup).toHaveBeenCalledWith(group.id, [user.id]); - }); + it('should remove user from room', async () => { + const { group, externalUserId, room } = setup(); - describe('when after removal: user is not in any room of that secondary school', () => { - it('should remove user from secondary school', async () => { - const { secondarySchool, externalUser } = setupUserWithSecondarySchool(); + await service.removeMembersFromRoom(room.id, [externalUserId]); - const { room, group, roomMembership } = setupGroupAndRoom(secondarySchool.id); - const roomOwnerRole = roleFactory.buildWithId({ name: RoleName.ROOMOWNER }); - const roomEditorRole = roleFactory.buildWithId({ name: RoleName.ROOMEDITOR }); - group.addUser({ userId: externalUser.id as string, roleId: roomEditorRole.id }); + expect(groupService.removeUsersFromGroup).toHaveBeenCalledWith(group.id, [externalUserId]); + }); - roleService.findByName.mockResolvedValue(roomOwnerRole); - roomMembershipRepo.findByRoomId.mockResolvedValue(roomMembership); - groupService.findById.mockResolvedValue(group); - groupService.removeUsersFromGroup.mockResolvedValue(group); - mockGroupsAtSchoolAfterRemoval([]); + it('should remove user from secondary school', async () => { + const { secondarySchool, externalUserId, room } = setup(); - await service.removeMembersFromRoom(room.id, [externalUser.id as string]); + await service.removeMembersFromRoom(room.id, [externalUserId]); - expect(userService.removeSecondarySchoolFromUsers).toHaveBeenCalledWith( - [externalUser.id], - secondarySchool.id - ); + expect(userService.removeSecondarySchoolFromUsers).toHaveBeenCalledWith([externalUserId], secondarySchool.id); }); }); - describe('when after removal: user is still in a room of that secondary school', () => { - it('should not remove user from secondary school', async () => { + describe('when removing user from a different school, with further groups on host school', () => { + const setup = () => { const { secondarySchool, externalUser } = setupUserWithSecondarySchool(); + const { roomEditorRole } = setupRoomRoles(); - const roomOwnerRole = roleFactory.buildWithId({ name: RoleName.ROOMOWNER }); - const roomEditorRole = roleFactory.buildWithId({ name: RoleName.ROOMEDITOR }); - - const { room, group, roomMembership } = setupGroupAndRoom(secondarySchool.id); + const { room, group } = setupGroupAndRoom(secondarySchool.id); group.addUser({ userId: externalUser.id as string, roleId: roomEditorRole.id }); const { group: group2 } = setupGroupAndRoom(secondarySchool.id); group2.addUser({ userId: externalUser.id as string, roleId: roomEditorRole.id }); - roleService.findByName.mockResolvedValue(roomOwnerRole); - roomMembershipRepo.findByRoomId.mockResolvedValue(roomMembership); - groupService.findById.mockResolvedValue(group); - groupService.removeUsersFromGroup.mockResolvedValue(group); mockGroupsAtSchoolAfterRemoval([group2]); + return { externalUser, room }; + }; + + it('should not remove user from secondary school', async () => { + const { externalUser, room } = setup(); + await service.removeMembersFromRoom(room.id, [externalUser.id as string]); expect(userService.removeSecondarySchoolFromUsers).not.toHaveBeenCalled(); }); }); - describe('when owner should be removed', () => { - it('should throw a badrequest exception', async () => { + describe('when removing user from the same school', () => { + const setup = () => { const user = userFactory.buildWithId(); - const { room, group, roomMembership } = setupGroupAndRoom(user.school.id); - roomMembershipRepo.findByRoomId.mockResolvedValue(roomMembership); - const roomOwnerRole = roleFactory.buildWithId({ name: RoleName.ROOMOWNER }); + const { roomEditorRole } = setupRoomRoles(); + const { room, group } = setupGroupAndRoom(user.school.id); + group.addUser({ userId: user.id, roleId: roomEditorRole.id }); + + mockGroupsAtSchoolAfterRemoval([group]); + + return { user, room, group }; + }; + + it('should remove user from room', async () => { + const { user, group, room } = setup(); + + await service.removeMembersFromRoom(room.id, [user.id]); + + expect(groupService.removeUsersFromGroup).toHaveBeenCalledWith(group.id, [user.id]); + }); + }); + + describe('when removing the owner of the room', () => { + const setup = () => { + const user = userFactory.buildWithId(); + const { room, group } = setupGroupAndRoom(user.school.id); + const { roomOwnerRole } = setupRoomRoles(); + group.addUser({ userId: user.id, roleId: roomOwnerRole.id }); - groupService.findById.mockResolvedValue(group); - roleService.findByName.mockResolvedValue(roomOwnerRole); + + return { user, room }; + }; + + it('should throw a badrequest exception', async () => { + const { user, room } = setup(); await expect(service.removeMembersFromRoom(room.id, [user.id])).rejects.toThrowError(BadRequestException); });