diff --git a/test/authorization/service/group.service.test.ts b/test/authorization/service/group.service.test.ts index de95a80..aa956a0 100644 --- a/test/authorization/service/group.service.test.ts +++ b/test/authorization/service/group.service.test.ts @@ -59,7 +59,7 @@ describe('test Group Service', () => { let groupService: GroupServiceInterface; let groupRepository: GroupRepository; let userRepository: UserRepository; - let userGroupRepository: UserGroupRepository; + // let userGroupRepository: UserGroupRepository; let dataSource: DataSource; @@ -87,41 +87,44 @@ describe('test Group Service', () => { let updateGroupByIdMock: jest.Mock; let getUsersByIdsMock: jest.Mock; let getUsersByGroupIdMock: jest.Mock; + let getUserCountForGroupIdMock: jest.Mock; let userGroupCreateMock: jest.Mock; let userGroupRemoveMock: jest.Mock; let userGroupSaveMock: jest.Mock; let getRepositoryMock: jest.Mock; + let entityManager: any; + + const userGroupRepository: UserGroupRepository = ({ + create: (userGroupCreateMock = jest.fn()), + remove: (userGroupRemoveMock = jest.fn()), + save: (userGroupSaveMock = jest.fn()), + getUserGroupsByUserId: jest.fn(), + } as unknown) as UserGroupRepository; + + const repositoryMap = new Map([ + [UserGroupRepository, userGroupRepository], + // [GroupRepository, groupRepository], + // [GroupRoleRepository, groupRoleRepository], + // [GroupPermissionRepository, groupPermissionRepository], + ]); + + getRepositoryMock = jest + .fn() + .mockImplementation((type) => repositoryMap.get(type)); + + entityManager = { getRepository: getRepositoryMock }; const mockDataSource = { createEntityManager: jest.fn(), transaction: jest.fn(), - // manager: { - // transaction: jest.fn().mockImplementation(() => { - // entityManager: { - // getRepository: jest.fn().mockImplementation(() => { - // remove: userGroupRemoveMock = jest.fn(); - // save: userGroupSaveMock = jest.fn(); - // }); - // } - // }), - // }, + manager: { + transaction: jest.fn().mockImplementation((cb) => { + return cb(entityManager); + }), + }, }; - // let dataSourceMockFactory: jest.Mock; - // dataSourceMockFactory = jest.fn(() => ({ - // manager: { - // transaction: jest.fn().mockImplementation((entityManager) => { - // entityManager: jest.Mock = { - // getRepository: getRepositoryMock = jest.fn().mockImplementation(() => ({ - // remove: userGroupRemoveMock = jest.fn(), - // save: userGroupSaveMock = jest.fn(), - // })), - // } - // }), - // } - // })); - beforeEach(async () => { const moduleRef = await Test.createTestingModule({ imports: [], @@ -167,8 +170,8 @@ describe('test Group Service', () => { groupService = moduleRef.get(GroupService); groupRepository = moduleRef.get(GroupRepository); userRepository = moduleRef.get(UserRepository); - userGroupRepository = moduleRef.get(UserGroupRepository); - dataSource = moduleRef.get(DataSource); + // userGroupRepository = moduleRef.get(UserGroupRepository); + // dataSource = moduleRef.get(DataSource); createQueryBuilderMock = groupRepository.createQueryBuilder = jest .fn() @@ -184,9 +187,10 @@ describe('test Group Service', () => { updateGroupByIdMock = groupRepository.updateGroupById = jest.fn(); getUsersByIdsMock = userRepository.getUsersByIds = jest.fn(); getUsersByGroupIdMock = userRepository.getUsersByGroupId = jest.fn(); - userGroupCreateMock = userGroupRepository.create = jest.fn(); - userGroupRemoveMock = userGroupRepository.remove = jest.fn(); - userGroupSaveMock = userGroupRepository.save = jest.fn(); + // userGroupCreateMock = userGroupRepository.create = jest.fn(); + // userGroupRemoveMock = userGroupRepository.remove = jest.fn(); + // userGroupSaveMock = userGroupRepository.save = jest.fn(); + getUserCountForGroupIdMock = userRepository.getUserCountForGroupId = jest.fn(); // const repositoryMap = new Map([ // [UserGroupRepository, userGroupRepository], @@ -286,70 +290,70 @@ describe('test Group Service', () => { expect(result).toEqual(groups[0]); }); - // it('should update users in a group', async () => { - // const input: UpdateGroupInput = { - // users: ['ccecef4f-58d3-477b-87ee-847ee22efe4d'], - // }; + it('should update users in a group', async () => { + const input: UpdateGroupInput = { + users: ['ccecef4f-58d3-477b-87ee-847ee22efe4d'], + }; - // updateGroupByIdMock.mockResolvedValue(true); - // getGroupByIdMock.mockResolvedValue(groups[0]); - // getUsersByIdsMock.mockResolvedValue([ - // { - // id: 'ccecef4f-58d3-477b-87ee-847ee22efe4d', - // firstName: 'New1', - // lastName: 'New2', - // origin: 'simple', - // status: Status.ACTIVE, - // }, - // ]); - // getUsersByGroupIdMock.mockResolvedValue(users); - // userGroupCreateMock.mockResolvedValue({ - // userId: 'ccecef4f-58d3-477b-87ee-847ee22efe4d', - // groupId: '3282163d-fd5a-4026-b912-1a9cc5eefc98', - // }); - // // getRepositoryMock.mockResolvedValue(UserGroupRepository); - // userGroupRemoveMock.mockResolvedValue({ - // userId: users[0].id, - // groupId: '3282163d-fd5a-4026-b912-1a9cc5eefc98', - // }); - // userGroupSaveMock.mockResolvedValue({ - // userId: 'ccecef4f-58d3-477b-87ee-847ee22efe4d', - // groupId: '3282163d-fd5a-4026-b912-1a9cc5eefc98', - // }); - - // // dataSource.manager.transaction = jest.fn().mockImplementation(); - - // const result = await groupService.updateGroup( - // '3282163d-fd5a-4026-b912-1a9cc5eefc98', - // input, - // ); + updateGroupByIdMock.mockResolvedValue(true); + getGroupByIdMock.mockResolvedValue(groups[0]); + getUsersByIdsMock.mockResolvedValue([ + { + id: 'ccecef4f-58d3-477b-87ee-847ee22efe4d', + firstName: 'New1', + lastName: 'New2', + origin: 'simple', + status: Status.ACTIVE, + }, + ]); + getUsersByGroupIdMock.mockResolvedValue(users); + userGroupCreateMock.mockResolvedValue({ + userId: 'ccecef4f-58d3-477b-87ee-847ee22efe4d', + groupId: '3282163d-fd5a-4026-b912-1a9cc5eefc98', + }); + // getRepositoryMock.mockResolvedValue(UserGroupRepository); + userGroupRemoveMock.mockResolvedValue({ + userId: users[0].id, + groupId: '3282163d-fd5a-4026-b912-1a9cc5eefc98', + }); + userGroupSaveMock.mockResolvedValue({ + userId: 'ccecef4f-58d3-477b-87ee-847ee22efe4d', + groupId: '3282163d-fd5a-4026-b912-1a9cc5eefc98', + }); - // expect(updateGroupByIdMock).toBeCalledWith(VALID_GROUP_ID, input); - // expect(getGroupByIdMock).toBeCalledWith(VALID_GROUP_ID); - // expect(getUsersByIdsMock).toBeCalledWith([ - // 'ccecef4f-58d3-477b-87ee-847ee22efe4d', - // ]); - // expect(getUsersByGroupIdMock).toBeCalledWith(VALID_GROUP_ID); - // expect(userGroupCreateMock).toBeCalledWith([ - // { - // userId: 'ccecef4f-58d3-477b-87ee-847ee22efe4d', - // groupId: VALID_GROUP_ID, - // }, - // ]); - // // expect(userGroupRemoveMock).toBeCalledWith([ - // // { - // // userId: users[0].id, - // // groupId: '3282163d-fd5a-4026-b912-1a9cc5eefc98', - // // }, - // // ]); - // // expect(userGroupSaveMock).toBeCalledWith([ - // // { - // // userId: 'ccecef4f-58d3-477b-87ee-847ee22efe4d', - // // groupId: '3282163d-fd5a-4026-b912-1a9cc5eefc98', - // // }, - // // ]); - // expect(result).toEqual(groups[0]); - // }); + // dataSource.manager.transaction = jest.fn().mockImplementation(); + + const result = await groupService.updateGroup( + '3282163d-fd5a-4026-b912-1a9cc5eefc98', + input, + ); + + expect(updateGroupByIdMock).toBeCalledWith(VALID_GROUP_ID, input); + expect(getGroupByIdMock).toBeCalledWith(VALID_GROUP_ID); + expect(getUsersByIdsMock).toBeCalledWith([ + 'ccecef4f-58d3-477b-87ee-847ee22efe4d', + ]); + expect(getUsersByGroupIdMock).toBeCalledWith(VALID_GROUP_ID); + expect(userGroupCreateMock).toBeCalledWith([ + { + userId: 'ccecef4f-58d3-477b-87ee-847ee22efe4d', + groupId: VALID_GROUP_ID, + }, + ]); + expect(userGroupRemoveMock).toBeCalledWith([ + { + userId: users[0].id, + groupId: '3282163d-fd5a-4026-b912-1a9cc5eefc98', + }, + ]); + expect(userGroupSaveMock).toBeCalledWith([ + { + userId: 'ccecef4f-58d3-477b-87ee-847ee22efe4d', + groupId: '3282163d-fd5a-4026-b912-1a9cc5eefc98', + }, + ]); + expect(result).toEqual(groups[0]); + }); it('should throw exception for invalid id', async () => { const input: UpdateGroupInput = { @@ -375,94 +379,78 @@ describe('test Group Service', () => { }); }); - // describe('deleteGroup', () => { - // it('should delete a group', async () => { - // // groupRepository - // // .softDelete('ae032b1b-cc3c-4e44-9197-276ca877a7f8') - // // .resolves(Arg.any()); - // // userRepository.createQueryBuilder().returns(userQueryBuilder); - // userQueryBuilder - // .innerJoinAndSelect(UserGroup, 'userGroup', 'userGroup.userId=User.id') - // .returns(userQueryBuilder); - // userQueryBuilder - // .where('userGroup.groupId = :id', { - // id: 'ae032b1b-cc3c-4e44-9197-276ca877a7f8', - // }) - // .returns(userQueryBuilder); - // userQueryBuilder.getCount().resolves(0); - // const result = await groupService.deleteGroup( - // 'ae032b1b-cc3c-4e44-9197-276ca877a7f8', - // ); - // expect(result).toEqual(groups[0]); - // }); - // }); - - // describe('updateGroupPermissions', () => { - // it('should add permissions to a group', async () => { - // const request = [ - // { - // groupId: 'ae032b1b-cc3c-4e44-9197-276ca877a7f8', - // permissionId: '2b33268a-7ff5-4cac-a87a-6bfc4430d34c', - // }, - // ]; - // const input: UpdateGroupPermissionInput = { - // permissions: ['2b33268a-7ff5-4cac-a87a-6bfc4430d34c'], - // }; - - // permissionRepository - // .findByIds(['2b33268a-7ff5-4cac-a87a-6bfc4430d34c']) - // .resolves(permissions); - - // groupPermissionRepository.create(request).returns(request); - - // permissionRepository - // .createQueryBuilder('permission') - // .returns(permissionQueryBuilder); - // permissionQueryBuilder - // .leftJoinAndSelect( - // GroupPermission, - // 'groupPermission', - // 'permission.id = groupPermission.permissionId', - // ) - // .returns(permissionQueryBuilder); - - // permissionQueryBuilder - // .where('groupPermission.groupId = :groupId', { - // groupId: 'ae032b1b-cc3c-4e44-9197-276ca877a7f8', - // }) - // .returns(permissionQueryBuilder); - - // permissionQueryBuilder.getMany().resolves(permissions); - - // // mockDataSource.manager.transaction(Arg.any()).resolves(request); + describe('deleteGroup', () => { + it('should delete a group', async () => { + getGroupByIdMock.mockResolvedValue(groups[0]); + getUserCountForGroupIdMock.mockResolvedValue(0); - // const result = await groupService.updateGroupPermissions( - // 'ae032b1b-cc3c-4e44-9197-276ca877a7f8', - // input, - // ); - // expect(result).toEqual(permissions); - // }); + const result = await groupService.deleteGroup( + '3282163d-fd5a-4026-b912-1a9cc5eefc98', + ); - // it('should throw exception when adding invalid permissions to a group', async () => { - // const input: UpdateGroupPermissionInput = { - // permissions: ['3e9e78c9-3fcd-4eed-b027-62f794680b03'], - // }; + expect(getGroupByIdMock).toBeCalledWith(VALID_GROUP_ID); + expect(getUserCountForGroupIdMock).toBeCalledWith(VALID_GROUP_ID); - // permissionRepository - // .findByIds(['3e9e78c9-3fcd-4eed-b027-62f794680b03']) - // .resolves([]); + expect(result).toEqual(groups[0]); + }); + }); - // const result = groupService.updateGroupPermissions( - // 'ae032b1b-cc3c-4e44-9197-276ca877a7f8', - // input, - // ); - // await expect(result).rejects.toThrowError( - // new PermissionNotFoundException( - // ['3e9e78c9-3fcd-4eed-b027-62f794680b03'].toString(), - // ), - // ); - // }); - // }); + describe('updateGroupPermissions', () => { + // it('should add permissions to a group', async () => { + // const request = [ + // { + // groupId: 'ae032b1b-cc3c-4e44-9197-276ca877a7f8', + // permissionId: '2b33268a-7ff5-4cac-a87a-6bfc4430d34c', + // }, + // ]; + // const input: UpdateGroupPermissionInput = { + // permissions: ['2b33268a-7ff5-4cac-a87a-6bfc4430d34c'], + // }; + // permissionRepository + // .findByIds(['2b33268a-7ff5-4cac-a87a-6bfc4430d34c']) + // .resolves(permissions); + // groupPermissionRepository.create(request).returns(request); + // permissionRepository + // .createQueryBuilder('permission') + // .returns(permissionQueryBuilder); + // permissionQueryBuilder + // .leftJoinAndSelect( + // GroupPermission, + // 'groupPermission', + // 'permission.id = groupPermission.permissionId', + // ) + // .returns(permissionQueryBuilder); + // permissionQueryBuilder + // .where('groupPermission.groupId = :groupId', { + // groupId: 'ae032b1b-cc3c-4e44-9197-276ca877a7f8', + // }) + // .returns(permissionQueryBuilder); + // permissionQueryBuilder.getMany().resolves(permissions); + // // mockDataSource.manager.transaction(Arg.any()).resolves(request); + // const result = await groupService.updateGroupPermissions( + // 'ae032b1b-cc3c-4e44-9197-276ca877a7f8', + // input, + // ); + // expect(result).toEqual(permissions); + // }); + // it('should throw exception when adding invalid permissions to a group', async () => { + // const input: UpdateGroupPermissionInput = { + // permissions: ['3e9e78c9-3fcd-4eed-b027-62f794680b03'], + // }; + // permissionRepository + // .findByIds(['3e9e78c9-3fcd-4eed-b027-62f794680b03']) + // .resolves([]); + // const result = groupService.updateGroupPermissions( + // 'ae032b1b-cc3c-4e44-9197-276ca877a7f8', + // input, + // ); + // await expect(result).rejects.toThrowError( + // new PermissionNotFoundException( + // ['3e9e78c9-3fcd-4eed-b027-62f794680b03'].toString(), + // ), + // ); + // }); + }); // describe('getGroupRoles', () => { // it('should get group roles', async () => { diff --git a/test/authorization/service/role.service.test.ts b/test/authorization/service/role.service.test.ts index 2c63953..16cd2f6 100644 --- a/test/authorization/service/role.service.test.ts +++ b/test/authorization/service/role.service.test.ts @@ -1,28 +1,21 @@ -import { Substitute } from '@fluffy-spoon/substitute'; -import { ConfigService } from '@nestjs/config'; import { Test } from '@nestjs/testing'; -import { getRepositoryToken } from '@nestjs/typeorm'; import Role from 'src/authorization/entity/role.entity'; -import { - DataSource, - Repository, - SelectQueryBuilder, - UpdateResult, -} from 'typeorm'; -import { AuthenticationHelper } from '../../../src/authentication/authentication.helper'; -import GroupRole from '../../../src/authorization/entity/groupRole.entity'; +import RolePermission from 'src/authorization/entity/rolePermission.entity'; +import { DataSource, UpdateResult } from 'typeorm'; import Permission from '../../../src/authorization/entity/permission.entity'; -import RolePermission from '../../../src/authorization/entity/rolePermission.entity'; +import { PermissionNotFoundException } from '../../../src/authorization/exception/permission.exception'; import { RoleNotFoundException } from '../../../src/authorization/exception/role.exception'; +import { GroupRoleRepository } from '../../../src/authorization/repository/groupRole.repository'; import { PermissionRepository } from '../../../src/authorization/repository/permission.repository'; import { RoleRepository } from '../../../src/authorization/repository/role.repository'; +import { RolePermissionRepository } from '../../../src/authorization/repository/rolePermission.repository'; import { RoleService } from '../../../src/authorization/service/role.service'; import { RoleCacheServiceInterface } from '../../../src/authorization/service/rolecache.service.interface'; import SearchService from '../../../src/authorization/service/search.service'; -import { RedisCacheService } from '../../../src/cache/redis-cache/redis-cache.service'; import { NewRoleInput, UpdateRoleInput, + UpdateRolePermissionInput, } from '../../../src/schema/graphql.schema'; const VALID_ROLE_ID = 'ae032b1b-cc3c-4e44-9197-276ca877a7f8'; @@ -40,6 +33,12 @@ const permissions: Permission[] = [ label: 'Customers', }, ]; +const rolePermissions: RolePermission[] = [ + { + roleId: VALID_ROLE_ID, + permissionId: '2b33268a-7ff5-4cac-a87a-6bfc4430d34c', + }, +]; const updateResult: UpdateResult = { raw: '', affected: 1, @@ -48,49 +47,65 @@ const updateResult: UpdateResult = { describe('test Role Service', () => { let roleService: RoleService; - let roleRepository: RoleRepository; - let permissionRepository: PermissionRepository; - const groupRoleRepository = Substitute.for>(); - const rolePermissionRepository = Substitute.for>(); - const roleCacheService = Substitute.for(); - const redisCacheService = Substitute.for(); - const searchService = Substitute.for(); - const groupRoleQueryBuilder = Substitute.for>(); - const permissionQueryBuilder = Substitute.for< - SelectQueryBuilder - >(); - let createQueryBuilderMock: jest.Mock; let getManyAndCountMock: jest.Mock; - let findOneByMock: jest.Mock; let saveMock: jest.Mock; - let updateMock: jest.Mock; - let getManyMock: jest.Mock; + let getRoleByIdMock: jest.Mock; + let updateRoleByIdMock: jest.Mock; + let getGroupCountForRoleIdMock: jest.Mock; + let roleSoftDeleteMock: jest.Mock; + let rolePermissionSoftDeleteMock: jest.Mock; + let getPermissionsByIdsMock: jest.Mock; + let getPermissionsByRoleIdMock: jest.Mock; + + let withRepositoryMock: jest.Mock; + let entityManager: any; + + const roleRepository: RoleRepository = ({ + getRoleById: (getRoleByIdMock = jest.fn()), + save: (saveMock = jest.fn()), + updateRoleById: (updateRoleByIdMock = jest.fn()), + softDelete: (roleSoftDeleteMock = jest.fn()), + } as unknown) as RoleRepository; + const permissionRepository: PermissionRepository = ({ + getPermissionsByIds: (getPermissionsByIdsMock = jest.fn()), + getPermissionsByRoleId: (getPermissionsByRoleIdMock = jest.fn()), + } as unknown) as PermissionRepository; + const groupRoleRepository: GroupRoleRepository = ({ + getGroupCountForRoleId: (getGroupCountForRoleIdMock = jest.fn()), + softDelete: jest.fn(), + } as unknown) as GroupRoleRepository; + const rolePermissionRepository: RolePermissionRepository = ({ + create: jest.fn(), + softDelete: (rolePermissionSoftDeleteMock = jest.fn()), + } as unknown) as RolePermissionRepository; + const roleCacheService: RoleCacheServiceInterface = ({} as unknown) as RoleCacheServiceInterface; + const searchService: SearchService = ({} as unknown) as SearchService; + + const repositoryMap = new Map([ + [RoleRepository, roleRepository], + [RolePermissionRepository, rolePermissionRepository], + ]); + + withRepositoryMock = jest + .fn() + .mockImplementation((type) => repositoryMap.get(type)); + + entityManager = { withRepository: withRepositoryMock }; const mockDataSource = { createEntityManager: jest.fn(), transaction: jest.fn(), + manager: { + transaction: jest.fn().mockImplementation((cb) => { + return cb(entityManager); + }), + }, }; beforeEach(async () => { const moduleRef = await Test.createTestingModule({ providers: [ - RoleService, - ConfigService, - AuthenticationHelper, - RoleRepository, - PermissionRepository, - { - provide: getRepositoryToken(GroupRole), - useValue: groupRoleRepository, - }, - { - provide: getRepositoryToken(RolePermission), - useValue: rolePermissionRepository, - }, - { provide: RoleCacheServiceInterface, useValue: roleCacheService }, - { provide: RedisCacheService, useValue: redisCacheService }, - { provide: SearchService, useValue: searchService }, { provide: DataSource, useValue: mockDataSource, @@ -98,9 +113,15 @@ describe('test Role Service', () => { ], }).compile(); - roleService = moduleRef.get(RoleService); - roleRepository = moduleRef.get(RoleRepository); - permissionRepository = moduleRef.get(PermissionRepository); + roleService = new RoleService( + roleCacheService, + roleRepository, + groupRoleRepository, + rolePermissionRepository, + permissionRepository, + moduleRef.get(DataSource), + searchService, + ); createQueryBuilderMock = roleRepository.createQueryBuilder = jest .fn() @@ -110,10 +131,6 @@ describe('test Role Service', () => { orderBy: jest.fn().mockReturnThis(), getManyAndCount: (getManyAndCountMock = jest.fn()), }); - - findOneByMock = roleRepository.findOneBy = jest.fn(); - saveMock = roleRepository.save = jest.fn(); - updateMock = roleRepository.update = jest.fn(); }); describe('getAllRoles', () => { @@ -134,27 +151,22 @@ describe('test Role Service', () => { describe('getRoleById', () => { it('should get a role for valid id', async () => { - findOneByMock.mockResolvedValue(roles[0]); + getRoleByIdMock.mockResolvedValue(roles[0]); const result = await roleService.getRoleById(VALID_ROLE_ID); - expect(findOneByMock).toBeCalledWith({ - id: VALID_ROLE_ID, - }); + expect(getRoleByIdMock).toBeCalledWith(VALID_ROLE_ID); expect(result).toEqual(roles[0]); }); it('should throw exception for invalid id', async () => { - let result; - findOneByMock.mockResolvedValue(null); + getRoleByIdMock.mockResolvedValue(null); try { - result = await roleService.getRoleById(INVALID_ROLE_ID); + await roleService.getRoleById(INVALID_ROLE_ID); } catch (error) { - expect(findOneByMock).toBeCalledWith({ - id: INVALID_ROLE_ID, - }); + expect(getRoleByIdMock).toBeCalledWith(INVALID_ROLE_ID); expect(error).toBeInstanceOf(RoleNotFoundException); } @@ -183,18 +195,16 @@ describe('test Role Service', () => { name: 'Test1', }; - updateMock.mockResolvedValue(updateResult); - findOneByMock.mockResolvedValue(roles[0]); + updateRoleByIdMock.mockResolvedValue(updateResult); + getRoleByIdMock.mockResolvedValue(roles[0]); const result = await roleService.updateRole( 'ae032b1b-cc3c-4e44-9197-276ca877a7f8', input, ); - expect(updateMock).toBeCalledWith(VALID_ROLE_ID, input); - expect(findOneByMock).toBeCalledWith({ - id: VALID_ROLE_ID, - }); + expect(updateRoleByIdMock).toBeCalledWith(VALID_ROLE_ID, input); + expect(getRoleByIdMock).toBeCalledWith(VALID_ROLE_ID); expect(result).toEqual({ id: VALID_ROLE_ID, @@ -203,123 +213,109 @@ describe('test Role Service', () => { }); }); - // describe('deleteRole', () => { - // it('should delete a role', async () => { - // // roleRepository - // // .softDelete('ae032b1b-cc3c-4e44-9197-276ca877a7f8') - // // .resolves(Arg.any()); - // groupRoleRepository.createQueryBuilder().returns(groupRoleQueryBuilder); - // groupRoleQueryBuilder - // .innerJoinAndSelect(Group, 'group', 'group.id = GroupRole.groupId') - // .returns(groupRoleQueryBuilder); - // groupRoleQueryBuilder - // .where('GroupRole.roleId= :id', { - // id: 'ae032b1b-cc3c-4e44-9197-276ca877a7f8', - // }) - // .returns(groupRoleQueryBuilder); - // groupRoleQueryBuilder.getCount().resolves(0); - // const resp = await roleService.deleteRole( - // 'ae032b1b-cc3c-4e44-9197-276ca877a7f8', - // ); - // expect(resp).toEqual(roles[0]); - // }); - // }); - - // describe('updateRolePermissions', () => { - // it('should add permissions to a role', async () => { - // const request = [ - // { - // roleId: 'ae032b1b-cc3c-4e44-9197-276ca877a7f8', - // permissionId: '2b33268a-7ff5-4cac-a87a-6bfc4430d34c', - // }, - // ]; - // const input: UpdateRolePermissionInput = { - // permissions: ['2b33268a-7ff5-4cac-a87a-6bfc4430d34c'], - // }; - - // // permissionRepository - // // .findByIds(['2b33268a-7ff5-4cac-a87a-6bfc4430d34c']) - // // .resolves(permissions); - - // // rolePermissionRepository.create(request).returns(request); - - // // permissionRepository - // // .createQueryBuilder('permission') - // // .returns(permissionQueryBuilder); - // permissionQueryBuilder - // .leftJoinAndSelect( - // RolePermission, - // 'rolePermission', - // 'permission.id = rolePermission.permissionId', - // ) - // .returns(permissionQueryBuilder); - - // permissionQueryBuilder - // .where('rolePermission.roleId = :roleId', { - // roleId: 'ae032b1b-cc3c-4e44-9197-276ca877a7f8', - // }) - // .returns(permissionQueryBuilder); - - // permissionQueryBuilder.getMany().resolves(permissions); - - // mockDataSource.transaction(Arg.any()).resolves(request); - - // const result = await roleService.updateRolePermissions( - // 'ae032b1b-cc3c-4e44-9197-276ca877a7f8', - // input, - // ); - // expect(result).toEqual(permissions); - // }); - - // it('should throw exception when adding invalid permissions to a role', async () => { - // const input: UpdateRolePermissionInput = { - // permissions: ['3e9e78c9-3fcd-4eed-b027-62f794680b03'], - // }; - - // // permissionRepository - // // .findByIds(['3e9e78c9-3fcd-4eed-b027-62f794680b03']) - // // .resolves([]); - - // const result = roleService.updateRolePermissions( - // 'ae032b1b-cc3c-4e44-9197-276ca877a7f8', - // input, - // ); - // await expect(result).rejects.toThrowError( - // new PermissionNotFoundException( - // ['3e9e78c9-3fcd-4eed-b027-62f794680b03'].toString(), - // ), - // ); - // }); - // }); - - // describe('getRolePermissions', () => { - // it('should return all permissions of the role', async () => { - // const permissions: Permission[] = [ - // { - // id: '2b33268a-7ff5-4cac-a87a-6bfc4430d34c', - // name: 'Customers', - // label: 'Customers', - // }, - // ]; - // // permissionRepository - // // .createQueryBuilder('permission') - // // .returns(permissionQueryBuilder); - // permissionQueryBuilder - // .leftJoinAndSelect( - // RolePermission, - // 'rolePermission', - // 'permission.id = rolePermission.permissionId', - // ) - // .returns(permissionQueryBuilder); - // permissionQueryBuilder - // .where('rolePermission.roleId = :roleId', { - // roleId: 'fcd858c6-26c5-462b-8c53-4b544830dca8', - // }) - // .returns(permissionQueryBuilder); - // const result = await roleService.getRolePermissions( - // 'fcd858c6-26c5-462b-8c53-4b544830dca8', - // ); - // expect(result).toEqual(permissions); - // }); - // }); + describe('deleteRole', () => { + it('should delete a role', async () => { + getRoleByIdMock.mockResolvedValue(roles[0]); + getGroupCountForRoleIdMock.mockResolvedValue(0); + + withRepositoryMock.mockResolvedValue(rolePermissionRepository); + rolePermissionSoftDeleteMock.mockResolvedValue(updateResult); + withRepositoryMock.mockResolvedValue(roleRepository); + roleSoftDeleteMock.mockResolvedValue(updateResult); + + const result = await roleService.deleteRole( + 'ae032b1b-cc3c-4e44-9197-276ca877a7f8', + ); + + expect(getRoleByIdMock).toBeCalledWith(VALID_ROLE_ID); + expect(getGroupCountForRoleIdMock).toBeCalledWith(VALID_ROLE_ID); + expect(roleSoftDeleteMock).toBeCalled(); + expect(result).toEqual(roles[0]); + }); + }); + + describe('updateRolePermissions', () => { + it('should add permissions to a role', async () => { + const request = [ + { + roleId: 'ae032b1b-cc3c-4e44-9197-276ca877a7f8', + permissionId: '2b33268a-7ff5-4cac-a87a-6bfc4430d34c', + }, + ]; + + const input: UpdateRolePermissionInput = { + permissions: ['2b33268a-7ff5-4cac-a87a-6bfc4430d34c'], + }; + + getRoleByIdMock.mockResolvedValue(roles[0]); + getPermissionsByIdsMock.mockResolvedValue(permissions); + getPermissionsByRoleIdMock.mockResolvedValue(permissions); + + const result = await roleService.updateRolePermissions( + 'ae032b1b-cc3c-4e44-9197-276ca877a7f8', + input, + ); + + expect(getRoleByIdMock).toBeCalledWith(VALID_ROLE_ID); + expect(getPermissionsByIdsMock).toBeCalledWith([ + '2b33268a-7ff5-4cac-a87a-6bfc4430d34c', + ]); + expect(getPermissionsByRoleIdMock).toBeCalledWith(VALID_ROLE_ID); + + expect(result).toEqual(permissions); + }); + + it('should throw exception when adding invalid permissions to a role', async () => { + const input: UpdateRolePermissionInput = { + permissions: ['3e9e78c9-3fcd-4eed-b027-62f794680b03'], + }; + + getRoleByIdMock.mockResolvedValue(roles[0]); + getPermissionsByIdsMock.mockResolvedValue([ + '3e9e78c9-3fcd-4eed-b027-62f794680b03', + 'b7bcebbd-c260-4517-acea-745c8e6db3ec', + ]); + + try { + await roleService.updateRolePermissions( + 'ae032b1b-cc3c-4e44-9197-276ca877a7f8', + input, + ); + } catch (error) { + expect(getRoleByIdMock).toBeCalledWith(VALID_ROLE_ID); + expect(getPermissionsByIdsMock).toBeCalledWith([ + '3e9e78c9-3fcd-4eed-b027-62f794680b03', + ]); + + expect(error).toBeInstanceOf(PermissionNotFoundException); + expect(error.message).toEqual( + 'Permission 3e9e78c9-3fcd-4eed-b027-62f794680b03 not found', + ); + } + }); + }); + + describe('getRolePermissions', () => { + it('should return all permissions of the role', async () => { + const permissions: Permission[] = [ + { + id: '2b33268a-7ff5-4cac-a87a-6bfc4430d34c', + name: 'Customers', + label: 'Customers', + }, + ]; + + getPermissionsByRoleIdMock.mockResolvedValue(permissions); + + const result = await roleService.getRolePermissions( + 'fcd858c6-26c5-462b-8c53-4b544830dca8', + ); + + expect(getPermissionsByRoleIdMock).toBeCalledWith( + 'fcd858c6-26c5-462b-8c53-4b544830dca8', + ); + + expect(result).toEqual(permissions); + }); + }); });