Skip to content

Commit

Permalink
fix: delete role permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
marrouchi committed Sep 25, 2024
1 parent ae15a3b commit 1d2865e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
35 changes: 33 additions & 2 deletions api/src/user/repositories/role.repository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import { PermissionRepository } from '../repositories/permission.repository';
import { RoleRepository } from '../repositories/role.repository';
import { UserRepository } from '../repositories/user.repository';
import { PermissionModel } from '../schemas/permission.schema';
import { RoleModel, Role } from '../schemas/role.schema';
import { UserModel, User } from '../schemas/user.schema';
import { Role, RoleModel } from '../schemas/role.schema';
import { User, UserModel } from '../schemas/user.schema';

describe('RoleRepository', () => {
let roleRepository: RoleRepository;
Expand All @@ -34,6 +34,7 @@ describe('RoleRepository', () => {
let roleModel: Model<Role>;
let role: Role;
let users: User[];
let roleToDelete: Role;

beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
Expand All @@ -57,6 +58,9 @@ describe('RoleRepository', () => {
users = (await userRepository.findAll()).filter((user) =>
user.roles.includes(role.id),
);
roleToDelete = await roleRepository.findOne({
name: 'manager',
});
});

afterAll(async () => {
Expand Down Expand Up @@ -106,4 +110,31 @@ describe('RoleRepository', () => {
expect(result).toEqualPayload(rolesWithPermissionsAndUsers);
});
});

describe('deleteOne', () => {
it('should delete a role by id', async () => {
jest.spyOn(roleModel, 'deleteOne');
const result = await roleRepository.deleteOne(roleToDelete.id);

expect(roleModel.deleteOne).toHaveBeenCalledWith({
_id: roleToDelete.id,
});
expect(result).toEqual({
acknowledged: true,
deletedCount: 1,
});

const permissions = await permissionRepository.find({
role: roleToDelete.id,
});
expect(permissions.length).toEqual(0);
});

it('should fail to delete a role that does not exist', async () => {
expect(await roleRepository.deleteOne(roleToDelete.id)).toEqual({
acknowledged: true,
deletedCount: 0,
});
});
});
});
2 changes: 1 addition & 1 deletion api/src/user/repositories/role.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class RoleRepository extends BaseRepository<
*
* @returns The result of the delete operation.
*/
async deleteOneQuery(id: string) {
async deleteOne(id: string) {
const result = await this.model.deleteOne({ _id: id }).exec();
if (result.deletedCount > 0) {
await this.permissionModel.deleteMany({ role: id });
Expand Down

0 comments on commit 1d2865e

Please sign in to comment.