Phase 3 Completed
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import { RoleAssignPermissionHandler } from '../src/modules/identity/application/handlers/role/assign-permission.handler';
|
||||
import { RoleRemovePermissionHandler } from '../src/modules/identity/application/handlers/role/remove-permission.handler';
|
||||
|
||||
describe('Role permission handlers', () => {
|
||||
test('assign permission invalidates cache and publishes event', async () => {
|
||||
const mockRoleRepo: any = { findById: jest.fn(), update: jest.fn(), getAssignedUserIds: jest.fn().mockResolvedValue(['u1','u2']) };
|
||||
const mockPermRepo: any = { getById: jest.fn() };
|
||||
const mockAuthz: any = { invalidateUserPermissions: jest.fn() };
|
||||
const mockEvents: any = { publish: jest.fn() };
|
||||
|
||||
mockRoleRepo.findById.mockResolvedValue({ id: 'r1', assignPermission: jest.fn() });
|
||||
mockPermRepo.getById.mockResolvedValue({ id: 'p1' });
|
||||
mockRoleRepo.update.mockResolvedValue({ id: 'r1' });
|
||||
|
||||
const handler = new RoleAssignPermissionHandler(mockRoleRepo, mockPermRepo, mockAuthz as any, mockEvents as any);
|
||||
|
||||
const res = await handler.execute('r1', 'p1');
|
||||
|
||||
expect(mockRoleRepo.update).toHaveBeenCalled();
|
||||
expect(mockAuthz.invalidateUserPermissions).toHaveBeenCalledWith('u1');
|
||||
expect(mockAuthz.invalidateUserPermissions).toHaveBeenCalledWith('u2');
|
||||
expect(mockEvents.publish).toHaveBeenCalledWith(expect.objectContaining({ type: 'RoleUpdated' }));
|
||||
});
|
||||
|
||||
test('remove permission invalidates cache and publishes event', async () => {
|
||||
const mockRoleRepo: any = { findById: jest.fn(), update: jest.fn(), getAssignedUserIds: jest.fn().mockResolvedValue(['u1']) };
|
||||
const mockAuthz: any = { invalidateUserPermissions: jest.fn() };
|
||||
const mockEvents: any = { publish: jest.fn() };
|
||||
|
||||
mockRoleRepo.findById.mockResolvedValue({ id: 'r1', removePermission: jest.fn() });
|
||||
mockRoleRepo.update.mockResolvedValue({ id: 'r1' });
|
||||
|
||||
const handler = new RoleRemovePermissionHandler(mockRoleRepo as any, mockAuthz as any, mockEvents as any);
|
||||
|
||||
const res = await handler.execute('r1', 'p1');
|
||||
|
||||
expect(mockRoleRepo.update).toHaveBeenCalled();
|
||||
expect(mockAuthz.invalidateUserPermissions).toHaveBeenCalledWith('u1');
|
||||
expect(mockEvents.publish).toHaveBeenCalledWith(expect.objectContaining({ type: 'RoleUpdated' }));
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user