|
| 1 | +import { Authorization } from '@rocket.chat/core-services'; |
| 2 | +import { LivechatDepartment, LivechatDepartmentAgents, Users } from '@rocket.chat/models'; |
| 3 | + |
| 4 | +import { canSendOutboundMessage, validateAgentAssignPermissions } from './canSendMessage'; |
| 5 | + |
| 6 | +jest.mock('@rocket.chat/core-services', () => ({ |
| 7 | + Authorization: { |
| 8 | + hasPermission: jest.fn(), |
| 9 | + }, |
| 10 | +})); |
| 11 | + |
| 12 | +jest.mock('@rocket.chat/models', () => ({ |
| 13 | + LivechatDepartment: { |
| 14 | + findOne: jest.fn(), |
| 15 | + }, |
| 16 | + LivechatDepartmentAgents: { |
| 17 | + findOneByAgentIdAndDepartmentId: jest.fn(), |
| 18 | + }, |
| 19 | + Users: { |
| 20 | + findOneAgentById: jest.fn(), |
| 21 | + }, |
| 22 | +})); |
| 23 | + |
| 24 | +const hasPermissionMock = Authorization.hasPermission as unknown as jest.Mock< |
| 25 | + Promise<boolean>, |
| 26 | + [userId: string, permission: string, scope?: string] |
| 27 | +>; |
| 28 | + |
| 29 | +const findDepartmentMock = LivechatDepartment.findOne as unknown as jest.Mock; |
| 30 | +const findDepartmentAgentMock = LivechatDepartmentAgents.findOneByAgentIdAndDepartmentId as unknown as jest.Mock; |
| 31 | +const findUserAgentMock = Users.findOneAgentById as unknown as jest.Mock; |
| 32 | + |
| 33 | +const setPermissions = (map: Record<string, boolean>) => { |
| 34 | + hasPermissionMock.mockImplementation(async (_userId: string, permission: string) => { |
| 35 | + return Boolean(map[permission]); |
| 36 | + }); |
| 37 | +}; |
| 38 | + |
| 39 | +describe('canSendOutboundMessage', () => { |
| 40 | + beforeEach(() => { |
| 41 | + jest.clearAllMocks(); |
| 42 | + // Default: no permissions granted |
| 43 | + setPermissions({}); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('when departmentId is provided', () => { |
| 47 | + test('resolves when user has assign-queues permission and department is enabled', async () => { |
| 48 | + setPermissions({ 'outbound.can-assign-queues': true }); |
| 49 | + findDepartmentMock.mockResolvedValue({ _id: 'dep1', enabled: true }); |
| 50 | + |
| 51 | + await expect(canSendOutboundMessage('u1', undefined, 'dep1')).resolves.toBeUndefined(); |
| 52 | + |
| 53 | + // Should not need to check requester department membership |
| 54 | + expect(findDepartmentAgentMock).not.toHaveBeenCalled(); |
| 55 | + }); |
| 56 | + |
| 57 | + test('throws error-invalid-department when department is disabled', async () => { |
| 58 | + setPermissions({ 'outbound.can-assign-queues': true }); |
| 59 | + findDepartmentMock.mockResolvedValue({ _id: 'dep1', enabled: false }); |
| 60 | + |
| 61 | + await expect(canSendOutboundMessage('u1', undefined, 'dep1')).rejects.toThrow('error-invalid-department'); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('and agentId is provided', () => { |
| 65 | + test('throws error-invalid-agent if user lacks any-agent and has self-only but agentId != userId', async () => { |
| 66 | + setPermissions({ |
| 67 | + 'outbound.can-assign-queues': true, // so it skips requester membership |
| 68 | + 'outbound.can-assign-any-agent': false, |
| 69 | + 'outbound.can-assign-self-only': true, |
| 70 | + }); |
| 71 | + findDepartmentMock.mockResolvedValue({ _id: 'dep1', enabled: true }); |
| 72 | + |
| 73 | + await expect(canSendOutboundMessage('me', 'other', 'dep1')).rejects.toThrow('error-invalid-agent'); |
| 74 | + |
| 75 | + // Should not check selected agent membership after permission denial |
| 76 | + expect(findDepartmentAgentMock).not.toHaveBeenCalled(); |
| 77 | + }); |
| 78 | + |
| 79 | + test('throws error-agent-not-in-department if selected agent is not in the department (any-agent allowed)', async () => { |
| 80 | + setPermissions({ |
| 81 | + 'outbound.can-assign-queues': true, |
| 82 | + 'outbound.can-assign-any-agent': true, |
| 83 | + }); |
| 84 | + findDepartmentMock.mockResolvedValue({ _id: 'dep1', enabled: true }); |
| 85 | + findDepartmentAgentMock.mockResolvedValueOnce(null); // selected agent not in department |
| 86 | + |
| 87 | + await expect(canSendOutboundMessage('me', 'agentX', 'dep1')).rejects.toThrow('error-agent-not-in-department'); |
| 88 | + |
| 89 | + expect(findDepartmentAgentMock).toHaveBeenCalledWith('agentX', 'dep1', { projection: { _id: 1 } }); |
| 90 | + }); |
| 91 | + |
| 92 | + test('resolves if selected agent is in the department (any-agent allowed)', async () => { |
| 93 | + setPermissions({ |
| 94 | + 'outbound.can-assign-queues': true, |
| 95 | + 'outbound.can-assign-any-agent': true, |
| 96 | + }); |
| 97 | + findDepartmentMock.mockResolvedValue({ _id: 'dep1', enabled: true }); |
| 98 | + findDepartmentAgentMock.mockResolvedValueOnce({ _id: 'relX' }); |
| 99 | + |
| 100 | + await expect(canSendOutboundMessage('me', 'agentInDep', 'dep1')).resolves.toBeUndefined(); |
| 101 | + }); |
| 102 | + |
| 103 | + test('throws error-agent-not-in-department when self-only is allowed and agentId == userId but agent is not in department', async () => { |
| 104 | + setPermissions({ |
| 105 | + 'outbound.can-assign-queues': true, |
| 106 | + 'outbound.can-assign-any-agent': false, |
| 107 | + 'outbound.can-assign-self-only': true, |
| 108 | + }); |
| 109 | + findDepartmentMock.mockResolvedValue({ _id: 'dep1', enabled: true }); |
| 110 | + findDepartmentAgentMock.mockResolvedValueOnce(null); // self not in department |
| 111 | + |
| 112 | + await expect(canSendOutboundMessage('me', 'me', 'dep1')).rejects.toThrow('error-agent-not-in-department'); |
| 113 | + }); |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + describe('when departmentId is not provided and agentId is provided', () => { |
| 118 | + test('throws error-invalid-agent if user lacks any-agent and has self-only but agentId != userId', async () => { |
| 119 | + setPermissions({ |
| 120 | + 'outbound.can-assign-any-agent': false, |
| 121 | + 'outbound.can-assign-self-only': true, |
| 122 | + }); |
| 123 | + |
| 124 | + await expect(canSendOutboundMessage('me', 'other')).rejects.toThrow('error-invalid-agent'); |
| 125 | + |
| 126 | + // Should not query Users if permission check fails |
| 127 | + expect(findUserAgentMock).not.toHaveBeenCalled(); |
| 128 | + }); |
| 129 | + |
| 130 | + test('throws error-invalid-agent if selected agent is not an agent (any-agent allowed)', async () => { |
| 131 | + setPermissions({ |
| 132 | + 'outbound.can-assign-any-agent': true, |
| 133 | + }); |
| 134 | + findUserAgentMock.mockResolvedValueOnce(null); |
| 135 | + |
| 136 | + await expect(canSendOutboundMessage('me', 'notAnAgent')).rejects.toThrow('error-invalid-agent'); |
| 137 | + |
| 138 | + expect(findUserAgentMock).toHaveBeenCalledWith('notAnAgent', { projection: { _id: 1 } }); |
| 139 | + }); |
| 140 | + |
| 141 | + test('resolves if selected agent exists (any-agent allowed)', async () => { |
| 142 | + setPermissions({ |
| 143 | + 'outbound.can-assign-any-agent': true, |
| 144 | + }); |
| 145 | + findUserAgentMock.mockResolvedValueOnce({ _id: 'agent1' }); |
| 146 | + |
| 147 | + await expect(canSendOutboundMessage('me', 'agent1')).resolves.toBeUndefined(); |
| 148 | + }); |
| 149 | + }); |
| 150 | + |
| 151 | + test('resolves when neither departmentId nor agentId are provided', async () => { |
| 152 | + await expect(canSendOutboundMessage('u1')).resolves.toBeUndefined(); |
| 153 | + |
| 154 | + expect(hasPermissionMock).not.toHaveBeenCalled(); |
| 155 | + expect(findDepartmentMock).not.toHaveBeenCalled(); |
| 156 | + expect(findDepartmentAgentMock).not.toHaveBeenCalled(); |
| 157 | + expect(findUserAgentMock).not.toHaveBeenCalled(); |
| 158 | + }); |
| 159 | +}); |
| 160 | + |
| 161 | +describe('validateAgentAssignPermissions', () => { |
| 162 | + beforeEach(() => { |
| 163 | + jest.clearAllMocks(); |
| 164 | + setPermissions({}); |
| 165 | + }); |
| 166 | + |
| 167 | + test('resolves when user has any-agent permission', async () => { |
| 168 | + setPermissions({ 'outbound.can-assign-any-agent': true }); |
| 169 | + |
| 170 | + await expect(validateAgentAssignPermissions('u1', 'other')).resolves.toBeUndefined(); |
| 171 | + }); |
| 172 | + |
| 173 | + test('resolves when user has self-only and agentId equals userId', async () => { |
| 174 | + setPermissions({ 'outbound.can-assign-self-only': true }); |
| 175 | + |
| 176 | + await expect(validateAgentAssignPermissions('me', 'me')).resolves.toBeUndefined(); |
| 177 | + }); |
| 178 | + |
| 179 | + test('throws error-invalid-agent when self-only and agentId != userId', async () => { |
| 180 | + setPermissions({ 'outbound.can-assign-self-only': true }); |
| 181 | + |
| 182 | + await expect(validateAgentAssignPermissions('me', 'other')).rejects.toThrow('error-invalid-agent'); |
| 183 | + }); |
| 184 | + |
| 185 | + test('throws error-invalid-agent when user lacks both permissions', async () => { |
| 186 | + setPermissions({}); |
| 187 | + |
| 188 | + await expect(validateAgentAssignPermissions('me', 'other')).rejects.toThrow('error-invalid-agent'); |
| 189 | + }); |
| 190 | +}); |
0 commit comments