@flusys/nestjs-iam 6.0.2 → 6.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/controllers/action.controller.spec.js +79 -0
- package/cjs/controllers/company-action-permission.controller.js +1 -1
- package/cjs/controllers/company-action-permission.controller.spec.js +65 -0
- package/cjs/controllers/my-permission.controller.spec.js +56 -0
- package/cjs/controllers/role-permission.controller.spec.js +140 -0
- package/cjs/controllers/role.controller.spec.js +86 -0
- package/cjs/controllers/user-action-permission.controller.js +1 -1
- package/cjs/controllers/user-action-permission.controller.spec.js +68 -0
- package/cjs/docs/iam-swagger.config.spec.js +99 -0
- package/cjs/dtos/permission.dto.js +7 -0
- package/cjs/entities/index.spec.js +56 -0
- package/cjs/entities/permission-base.entity.js +1 -10
- package/cjs/entities/permission-with-company.entity.js +0 -3
- package/cjs/entities/user-iam-permission.entity.js +0 -3
- package/cjs/helpers/company-access.helper.spec.js +68 -0
- package/cjs/helpers/permission-mode.helper.spec.js +54 -0
- package/cjs/modules/iam.module.js +2 -2
- package/cjs/modules/iam.module.spec.js +293 -0
- package/cjs/services/action.service.js +4 -4
- package/cjs/services/action.service.spec.js +314 -0
- package/cjs/services/iam-config.service.spec.js +110 -0
- package/cjs/services/iam-datasource.service.spec.js +117 -0
- package/cjs/services/permission-cache.service.js +363 -52
- package/cjs/services/permission-cache.service.spec.js +699 -0
- package/cjs/services/permission.service.js +92 -396
- package/cjs/services/permission.service.spec.js +692 -0
- package/cjs/services/role.service.js +1 -1
- package/cjs/services/role.service.spec.js +309 -0
- package/dtos/permission.dto.d.ts +1 -0
- package/entities/permission-base.entity.d.ts +0 -1
- package/fesm/controllers/action.controller.spec.js +75 -0
- package/fesm/controllers/company-action-permission.controller.js +1 -1
- package/fesm/controllers/company-action-permission.controller.spec.js +61 -0
- package/fesm/controllers/my-permission.controller.spec.js +52 -0
- package/fesm/controllers/role-permission.controller.spec.js +136 -0
- package/fesm/controllers/role.controller.spec.js +82 -0
- package/fesm/controllers/user-action-permission.controller.js +1 -1
- package/fesm/controllers/user-action-permission.controller.spec.js +64 -0
- package/fesm/docs/iam-swagger.config.spec.js +95 -0
- package/fesm/dtos/permission.dto.js +7 -0
- package/fesm/entities/index.spec.js +52 -0
- package/fesm/entities/permission-base.entity.js +1 -10
- package/fesm/entities/permission-with-company.entity.js +0 -3
- package/fesm/entities/user-iam-permission.entity.js +0 -3
- package/fesm/helpers/company-access.helper.spec.js +64 -0
- package/fesm/helpers/permission-mode.helper.spec.js +50 -0
- package/fesm/modules/iam.module.js +4 -4
- package/fesm/modules/iam.module.spec.js +289 -0
- package/fesm/services/action.service.js +4 -4
- package/fesm/services/action.service.spec.js +310 -0
- package/fesm/services/iam-config.service.spec.js +106 -0
- package/fesm/services/iam-datasource.service.spec.js +113 -0
- package/fesm/services/permission-cache.service.js +365 -51
- package/fesm/services/permission-cache.service.spec.js +695 -0
- package/fesm/services/permission.service.js +92 -396
- package/fesm/services/permission.service.spec.js +688 -0
- package/fesm/services/role.service.js +1 -1
- package/fesm/services/role.service.spec.js +305 -0
- package/package.json +3 -3
- package/services/permission-cache.service.d.ts +20 -3
- package/services/permission.service.d.ts +2 -16
|
@@ -0,0 +1,688 @@
|
|
|
1
|
+
import { Test } from '@nestjs/testing';
|
|
2
|
+
import { createMockRepository } from '@test-utils/mocks/repository.mock';
|
|
3
|
+
import { IAM_MODE_MESSAGES, PERMISSION_OPERATION_MESSAGES } from '../config';
|
|
4
|
+
import { PermissionAction } from '../dtos/permission.dto';
|
|
5
|
+
import { Action } from '../entities/action.entity';
|
|
6
|
+
import { Role } from '../entities/role.entity';
|
|
7
|
+
import { RoleWithCompany } from '../entities/role-with-company.entity';
|
|
8
|
+
import { ActionType } from '../enums/action-type.enum';
|
|
9
|
+
import { IamEntityType } from '../enums/iam-entity-type.enum';
|
|
10
|
+
import { IamPermissionType } from '../enums/iam-permission-type.enum';
|
|
11
|
+
import { IAMPermissionMode } from '../enums/permission-type.enum';
|
|
12
|
+
import { IAMConfigService } from './iam-config.service';
|
|
13
|
+
import { IAMDataSourceService } from './iam-datasource.service';
|
|
14
|
+
import { PermissionCacheService } from './permission-cache.service';
|
|
15
|
+
import { PermissionService } from './permission.service';
|
|
16
|
+
function buildPermissionRow(overrides = {}) {
|
|
17
|
+
const row = {
|
|
18
|
+
id: 'perm-uuid-1',
|
|
19
|
+
permissionType: IamPermissionType.USER_ACTION,
|
|
20
|
+
sourceType: IamEntityType.USER,
|
|
21
|
+
sourceId: 'user-1',
|
|
22
|
+
targetType: IamEntityType.ACTION,
|
|
23
|
+
targetId: 'action-1',
|
|
24
|
+
companyId: null,
|
|
25
|
+
branchId: null,
|
|
26
|
+
validFrom: null,
|
|
27
|
+
validUntil: null,
|
|
28
|
+
reason: null,
|
|
29
|
+
createdAt: new Date(),
|
|
30
|
+
...overrides
|
|
31
|
+
};
|
|
32
|
+
row.isValid = (now = new Date())=>{
|
|
33
|
+
if (row.validFrom && now < row.validFrom) return false;
|
|
34
|
+
if (row.validUntil && now > row.validUntil) return false;
|
|
35
|
+
return true;
|
|
36
|
+
};
|
|
37
|
+
return row;
|
|
38
|
+
}
|
|
39
|
+
function buildDeleteResult(affected) {
|
|
40
|
+
return {
|
|
41
|
+
affected,
|
|
42
|
+
raw: []
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function mockRawMany(repo, rows) {
|
|
46
|
+
const builder = {
|
|
47
|
+
select: jest.fn().mockReturnThis(),
|
|
48
|
+
addSelect: jest.fn().mockReturnThis(),
|
|
49
|
+
where: jest.fn().mockReturnThis(),
|
|
50
|
+
andWhere: jest.fn().mockReturnThis(),
|
|
51
|
+
getRawMany: jest.fn().mockResolvedValue(rows)
|
|
52
|
+
};
|
|
53
|
+
repo.createQueryBuilder.mockReturnValue(builder);
|
|
54
|
+
return builder;
|
|
55
|
+
}
|
|
56
|
+
function buildAction(overrides = {}) {
|
|
57
|
+
return {
|
|
58
|
+
id: 'action-1',
|
|
59
|
+
name: 'View Users',
|
|
60
|
+
code: 'user.view',
|
|
61
|
+
description: null,
|
|
62
|
+
actionType: ActionType.BACKEND,
|
|
63
|
+
parentId: null,
|
|
64
|
+
isActive: true,
|
|
65
|
+
...overrides
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
describe('PermissionService', ()=>{
|
|
69
|
+
let service;
|
|
70
|
+
let mockPermissionRepo;
|
|
71
|
+
let mockActionRepo;
|
|
72
|
+
let mockRoleRepo;
|
|
73
|
+
let mockIamConfigService;
|
|
74
|
+
let mockPermissionCacheService;
|
|
75
|
+
let mockDataSourceProvider;
|
|
76
|
+
beforeEach(async ()=>{
|
|
77
|
+
mockPermissionRepo = createMockRepository();
|
|
78
|
+
mockActionRepo = createMockRepository();
|
|
79
|
+
mockRoleRepo = createMockRepository();
|
|
80
|
+
mockDataSourceProvider = {
|
|
81
|
+
getRepository: jest.fn((entity)=>{
|
|
82
|
+
if (entity === Action) return Promise.resolve(mockActionRepo);
|
|
83
|
+
if (entity === Role || entity === RoleWithCompany) return Promise.resolve(mockRoleRepo);
|
|
84
|
+
return Promise.resolve(mockPermissionRepo);
|
|
85
|
+
}),
|
|
86
|
+
getDataSource: jest.fn()
|
|
87
|
+
};
|
|
88
|
+
mockIamConfigService = {
|
|
89
|
+
isCompanyFeatureEnabled: jest.fn().mockReturnValue(false),
|
|
90
|
+
isDirectPermissionEnabled: jest.fn().mockReturnValue(true),
|
|
91
|
+
isRbacEnabled: jest.fn().mockReturnValue(true),
|
|
92
|
+
getPermissionMode: jest.fn().mockReturnValue(IAMPermissionMode.FULL)
|
|
93
|
+
};
|
|
94
|
+
mockPermissionCacheService = {
|
|
95
|
+
invalidateUser: jest.fn(),
|
|
96
|
+
invalidateUsers: jest.fn(),
|
|
97
|
+
invalidateRoleMembersCache: jest.fn(),
|
|
98
|
+
invalidateCompanyMembersCache: jest.fn(),
|
|
99
|
+
getMyPermissionsResponse: jest.fn()
|
|
100
|
+
};
|
|
101
|
+
const module = await Test.createTestingModule({
|
|
102
|
+
providers: [
|
|
103
|
+
PermissionService,
|
|
104
|
+
{
|
|
105
|
+
provide: PermissionCacheService,
|
|
106
|
+
useValue: mockPermissionCacheService
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
provide: IAMConfigService,
|
|
110
|
+
useValue: mockIamConfigService
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
provide: IAMDataSourceService,
|
|
114
|
+
useValue: mockDataSourceProvider
|
|
115
|
+
}
|
|
116
|
+
]
|
|
117
|
+
}).compile();
|
|
118
|
+
service = await module.resolve(PermissionService);
|
|
119
|
+
});
|
|
120
|
+
// ==================== User-Action Permissions ====================
|
|
121
|
+
describe('assignUserActions', ()=>{
|
|
122
|
+
const dto = {
|
|
123
|
+
userId: 'user-1',
|
|
124
|
+
items: [
|
|
125
|
+
{
|
|
126
|
+
id: 'action-1',
|
|
127
|
+
action: PermissionAction.ADD
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
id: 'action-2',
|
|
131
|
+
action: PermissionAction.ADD
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
id: 'action-3',
|
|
135
|
+
action: PermissionAction.REMOVE
|
|
136
|
+
}
|
|
137
|
+
]
|
|
138
|
+
};
|
|
139
|
+
it('throws BadRequestException when direct permissions are disabled (RBAC-only mode)', async ()=>{
|
|
140
|
+
mockIamConfigService.isDirectPermissionEnabled.mockReturnValue(false);
|
|
141
|
+
await expect(service.assignUserActions(dto)).rejects.toMatchObject({
|
|
142
|
+
response: expect.objectContaining({
|
|
143
|
+
messageKey: IAM_MODE_MESSAGES.DIRECT_MODE_UNAVAILABLE
|
|
144
|
+
})
|
|
145
|
+
});
|
|
146
|
+
expect(mockPermissionRepo.find).not.toHaveBeenCalled();
|
|
147
|
+
});
|
|
148
|
+
it('adds only actions not already assigned, removes requested actions, and invalidates the user cache', async ()=>{
|
|
149
|
+
mockPermissionRepo.find.mockResolvedValue([
|
|
150
|
+
{
|
|
151
|
+
targetId: 'action-1'
|
|
152
|
+
}
|
|
153
|
+
]);
|
|
154
|
+
mockPermissionRepo.save.mockResolvedValue([
|
|
155
|
+
{}
|
|
156
|
+
]);
|
|
157
|
+
mockPermissionRepo.delete.mockResolvedValue(buildDeleteResult(1));
|
|
158
|
+
const result = await service.assignUserActions(dto);
|
|
159
|
+
expect(mockPermissionRepo.save).toHaveBeenCalledWith([
|
|
160
|
+
expect.objectContaining({
|
|
161
|
+
targetId: 'action-2',
|
|
162
|
+
companyId: null,
|
|
163
|
+
branchId: null
|
|
164
|
+
})
|
|
165
|
+
]);
|
|
166
|
+
expect(result).toEqual({
|
|
167
|
+
added: 1,
|
|
168
|
+
removed: 1,
|
|
169
|
+
total: 3
|
|
170
|
+
});
|
|
171
|
+
expect(mockPermissionCacheService.invalidateUser).toHaveBeenCalledWith('user-1', null, []);
|
|
172
|
+
});
|
|
173
|
+
it('scopes new permissions and cache invalidation to the company/branch when the company feature is enabled', async ()=>{
|
|
174
|
+
mockIamConfigService.isCompanyFeatureEnabled.mockReturnValue(true);
|
|
175
|
+
mockPermissionRepo.find.mockResolvedValue([]);
|
|
176
|
+
mockPermissionRepo.save.mockResolvedValue([
|
|
177
|
+
{},
|
|
178
|
+
{}
|
|
179
|
+
]);
|
|
180
|
+
mockPermissionRepo.delete.mockResolvedValue(buildDeleteResult(1));
|
|
181
|
+
await service.assignUserActions({
|
|
182
|
+
userId: 'user-1',
|
|
183
|
+
companyId: 'company-1',
|
|
184
|
+
branchId: 'branch-1',
|
|
185
|
+
items: dto.items
|
|
186
|
+
});
|
|
187
|
+
expect(mockPermissionRepo.find).toHaveBeenCalledWith(expect.objectContaining({
|
|
188
|
+
where: expect.objectContaining({
|
|
189
|
+
companyId: 'company-1',
|
|
190
|
+
branchId: 'branch-1'
|
|
191
|
+
})
|
|
192
|
+
}));
|
|
193
|
+
expect(mockPermissionRepo.save).toHaveBeenCalledWith([
|
|
194
|
+
expect.objectContaining({
|
|
195
|
+
companyId: 'company-1',
|
|
196
|
+
branchId: 'branch-1'
|
|
197
|
+
}),
|
|
198
|
+
expect.objectContaining({
|
|
199
|
+
companyId: 'company-1',
|
|
200
|
+
branchId: 'branch-1'
|
|
201
|
+
})
|
|
202
|
+
]);
|
|
203
|
+
expect(mockPermissionCacheService.invalidateUser).toHaveBeenCalledWith('user-1', 'company-1', [
|
|
204
|
+
'branch-1'
|
|
205
|
+
]);
|
|
206
|
+
});
|
|
207
|
+
it('translates a duplicate-entry DB error into ConflictException', async ()=>{
|
|
208
|
+
mockPermissionRepo.find.mockResolvedValue([]);
|
|
209
|
+
mockPermissionRepo.save.mockRejectedValue({
|
|
210
|
+
code: 'ER_DUP_ENTRY'
|
|
211
|
+
});
|
|
212
|
+
await expect(service.assignUserActions(dto)).rejects.toMatchObject({
|
|
213
|
+
response: expect.objectContaining({
|
|
214
|
+
messageKey: PERMISSION_OPERATION_MESSAGES.ALREADY_EXISTS
|
|
215
|
+
})
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
it('rethrows unknown errors from save as-is', async ()=>{
|
|
219
|
+
mockPermissionRepo.find.mockResolvedValue([]);
|
|
220
|
+
const unexpected = new Error('connection lost');
|
|
221
|
+
mockPermissionRepo.save.mockRejectedValue(unexpected);
|
|
222
|
+
await expect(service.assignUserActions(dto)).rejects.toBe(unexpected);
|
|
223
|
+
});
|
|
224
|
+
it('does nothing but still invalidates the cache when items is empty', async ()=>{
|
|
225
|
+
const result = await service.assignUserActions({
|
|
226
|
+
userId: 'user-1',
|
|
227
|
+
items: []
|
|
228
|
+
});
|
|
229
|
+
expect(result).toEqual({
|
|
230
|
+
added: 0,
|
|
231
|
+
removed: 0,
|
|
232
|
+
total: 0
|
|
233
|
+
});
|
|
234
|
+
expect(mockPermissionRepo.save).not.toHaveBeenCalled();
|
|
235
|
+
expect(mockPermissionRepo.delete).not.toHaveBeenCalled();
|
|
236
|
+
expect(mockPermissionCacheService.invalidateUser).toHaveBeenCalled();
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
describe('getUserActions', ()=>{
|
|
240
|
+
it('returns an empty array without querying actions when the user has no permissions', async ()=>{
|
|
241
|
+
mockPermissionRepo.find.mockResolvedValue([]);
|
|
242
|
+
const result = await service.getUserActions('user-1', undefined, undefined);
|
|
243
|
+
expect(result).toEqual([]);
|
|
244
|
+
expect(mockActionRepo.find).not.toHaveBeenCalled();
|
|
245
|
+
});
|
|
246
|
+
it('joins permissions with their actions and filters out permissions for deleted actions', async ()=>{
|
|
247
|
+
mockPermissionRepo.find.mockResolvedValue([
|
|
248
|
+
buildPermissionRow({
|
|
249
|
+
targetId: 'action-1'
|
|
250
|
+
}),
|
|
251
|
+
buildPermissionRow({
|
|
252
|
+
id: 'perm-2',
|
|
253
|
+
targetId: 'action-missing'
|
|
254
|
+
})
|
|
255
|
+
]);
|
|
256
|
+
mockActionRepo.find.mockResolvedValue([
|
|
257
|
+
buildAction({
|
|
258
|
+
id: 'action-1'
|
|
259
|
+
})
|
|
260
|
+
]);
|
|
261
|
+
const result = await service.getUserActions('user-1', undefined, undefined);
|
|
262
|
+
expect(result).toHaveLength(1);
|
|
263
|
+
expect(result[0]).toEqual(expect.objectContaining({
|
|
264
|
+
actionId: 'action-1',
|
|
265
|
+
actionCode: 'user.view'
|
|
266
|
+
}));
|
|
267
|
+
});
|
|
268
|
+
it('filters by companyId/branchId when the company feature is enabled', async ()=>{
|
|
269
|
+
mockIamConfigService.isCompanyFeatureEnabled.mockReturnValue(true);
|
|
270
|
+
mockPermissionRepo.find.mockResolvedValue([]);
|
|
271
|
+
await service.getUserActions('user-1', 'company-1', 'branch-1');
|
|
272
|
+
expect(mockPermissionRepo.find).toHaveBeenCalledWith({
|
|
273
|
+
where: expect.objectContaining({
|
|
274
|
+
companyId: 'company-1',
|
|
275
|
+
branchId: 'branch-1'
|
|
276
|
+
})
|
|
277
|
+
});
|
|
278
|
+
});
|
|
279
|
+
});
|
|
280
|
+
// ==================== Role-Action Permissions ====================
|
|
281
|
+
describe('assignRoleActions', ()=>{
|
|
282
|
+
const dto = {
|
|
283
|
+
roleId: 'role-1',
|
|
284
|
+
items: [
|
|
285
|
+
{
|
|
286
|
+
id: 'action-1',
|
|
287
|
+
action: PermissionAction.ADD
|
|
288
|
+
},
|
|
289
|
+
{
|
|
290
|
+
id: 'action-2',
|
|
291
|
+
action: PermissionAction.REMOVE
|
|
292
|
+
}
|
|
293
|
+
]
|
|
294
|
+
};
|
|
295
|
+
it('throws BadRequestException when RBAC is disabled (DIRECT-only mode)', async ()=>{
|
|
296
|
+
mockIamConfigService.isRbacEnabled.mockReturnValue(false);
|
|
297
|
+
await expect(service.assignRoleActions(dto)).rejects.toMatchObject({
|
|
298
|
+
response: expect.objectContaining({
|
|
299
|
+
messageKey: IAM_MODE_MESSAGES.RBAC_MODE_UNAVAILABLE
|
|
300
|
+
})
|
|
301
|
+
});
|
|
302
|
+
});
|
|
303
|
+
it('resolves the role company scope from the role repository when the company feature is enabled', async ()=>{
|
|
304
|
+
mockIamConfigService.isCompanyFeatureEnabled.mockReturnValue(true);
|
|
305
|
+
mockRoleRepo.findOne.mockResolvedValue({
|
|
306
|
+
id: 'role-1',
|
|
307
|
+
companyId: 'company-9'
|
|
308
|
+
});
|
|
309
|
+
mockPermissionRepo.find.mockResolvedValue([]);
|
|
310
|
+
mockPermissionRepo.save.mockResolvedValue([
|
|
311
|
+
{}
|
|
312
|
+
]);
|
|
313
|
+
mockPermissionRepo.delete.mockResolvedValue(buildDeleteResult(1));
|
|
314
|
+
await service.assignRoleActions(dto);
|
|
315
|
+
expect(mockPermissionRepo.save).toHaveBeenCalledWith([
|
|
316
|
+
expect.objectContaining({
|
|
317
|
+
companyId: 'company-9'
|
|
318
|
+
})
|
|
319
|
+
]);
|
|
320
|
+
});
|
|
321
|
+
it('translates duplicate role-action entries into ConflictException', async ()=>{
|
|
322
|
+
mockPermissionRepo.find.mockResolvedValue([]);
|
|
323
|
+
mockPermissionRepo.save.mockRejectedValue({
|
|
324
|
+
message: 'Duplicate entry for key'
|
|
325
|
+
});
|
|
326
|
+
await expect(service.assignRoleActions(dto)).rejects.toMatchObject({
|
|
327
|
+
response: expect.objectContaining({
|
|
328
|
+
messageKey: PERMISSION_OPERATION_MESSAGES.ALREADY_EXISTS
|
|
329
|
+
})
|
|
330
|
+
});
|
|
331
|
+
});
|
|
332
|
+
it('removes role actions and reports counts and invalidates role member caches', async ()=>{
|
|
333
|
+
mockPermissionRepo.find.mockResolvedValue([]); // existing check for add
|
|
334
|
+
mockPermissionRepo.save.mockResolvedValue([
|
|
335
|
+
{}
|
|
336
|
+
]);
|
|
337
|
+
mockPermissionRepo.delete.mockResolvedValue(buildDeleteResult(1));
|
|
338
|
+
const result = await service.assignRoleActions(dto);
|
|
339
|
+
expect(result).toEqual({
|
|
340
|
+
added: 1,
|
|
341
|
+
removed: 1,
|
|
342
|
+
total: 2
|
|
343
|
+
});
|
|
344
|
+
expect(mockPermissionCacheService.invalidateRoleMembersCache).toHaveBeenCalledWith('role-1');
|
|
345
|
+
});
|
|
346
|
+
});
|
|
347
|
+
describe('getRoleActions', ()=>{
|
|
348
|
+
it('returns an empty array when the role has no actions assigned', async ()=>{
|
|
349
|
+
mockPermissionRepo.find.mockResolvedValue([]);
|
|
350
|
+
const result = await service.getRoleActions('role-1');
|
|
351
|
+
expect(result).toEqual([]);
|
|
352
|
+
});
|
|
353
|
+
it('maps role-action permissions to their action details', async ()=>{
|
|
354
|
+
mockPermissionRepo.find.mockResolvedValue([
|
|
355
|
+
buildPermissionRow({
|
|
356
|
+
sourceId: 'role-1',
|
|
357
|
+
targetId: 'action-1'
|
|
358
|
+
})
|
|
359
|
+
]);
|
|
360
|
+
mockActionRepo.find.mockResolvedValue([
|
|
361
|
+
buildAction({
|
|
362
|
+
id: 'action-1'
|
|
363
|
+
})
|
|
364
|
+
]);
|
|
365
|
+
const result = await service.getRoleActions('role-1');
|
|
366
|
+
expect(result).toEqual([
|
|
367
|
+
expect.objectContaining({
|
|
368
|
+
roleId: 'role-1',
|
|
369
|
+
actionCode: 'user.view'
|
|
370
|
+
})
|
|
371
|
+
]);
|
|
372
|
+
});
|
|
373
|
+
});
|
|
374
|
+
// ==================== Company-Action Permissions ====================
|
|
375
|
+
describe('assignCompanyActions', ()=>{
|
|
376
|
+
function mockTransaction() {
|
|
377
|
+
const fakeManager = {
|
|
378
|
+
getRepository: jest.fn((entity)=>{
|
|
379
|
+
if (entity === Role || entity === RoleWithCompany) return mockRoleRepo;
|
|
380
|
+
return mockPermissionRepo;
|
|
381
|
+
})
|
|
382
|
+
};
|
|
383
|
+
mockDataSourceProvider.getDataSource.mockResolvedValue({
|
|
384
|
+
transaction: jest.fn().mockImplementation((cb)=>cb(fakeManager))
|
|
385
|
+
});
|
|
386
|
+
return fakeManager;
|
|
387
|
+
}
|
|
388
|
+
it('adds new company-whitelisted actions inside a transaction, skipping duplicates', async ()=>{
|
|
389
|
+
mockTransaction();
|
|
390
|
+
mockPermissionRepo.find.mockResolvedValue([
|
|
391
|
+
{
|
|
392
|
+
targetId: 'action-1'
|
|
393
|
+
}
|
|
394
|
+
]);
|
|
395
|
+
mockPermissionRepo.create.mockImplementation((data)=>data);
|
|
396
|
+
mockPermissionRepo.save.mockResolvedValue([
|
|
397
|
+
{}
|
|
398
|
+
]);
|
|
399
|
+
const result = await service.assignCompanyActions({
|
|
400
|
+
companyId: 'company-1',
|
|
401
|
+
items: [
|
|
402
|
+
{
|
|
403
|
+
id: 'action-1',
|
|
404
|
+
action: PermissionAction.ADD
|
|
405
|
+
},
|
|
406
|
+
{
|
|
407
|
+
id: 'action-2',
|
|
408
|
+
action: PermissionAction.ADD
|
|
409
|
+
}
|
|
410
|
+
]
|
|
411
|
+
});
|
|
412
|
+
expect(mockPermissionRepo.save).toHaveBeenCalledWith([
|
|
413
|
+
expect.objectContaining({
|
|
414
|
+
targetId: 'action-2',
|
|
415
|
+
sourceId: 'company-1'
|
|
416
|
+
})
|
|
417
|
+
]);
|
|
418
|
+
expect(result.added).toBe(1);
|
|
419
|
+
expect(mockPermissionCacheService.invalidateCompanyMembersCache).toHaveBeenCalledWith('company-1');
|
|
420
|
+
});
|
|
421
|
+
it('cascades removal to role-action and user-action permissions for the company', async ()=>{
|
|
422
|
+
mockIamConfigService.isCompanyFeatureEnabled.mockReturnValue(true);
|
|
423
|
+
mockTransaction();
|
|
424
|
+
mockPermissionRepo.delete.mockResolvedValue(buildDeleteResult(2));
|
|
425
|
+
mockRoleRepo.find.mockResolvedValue([
|
|
426
|
+
{
|
|
427
|
+
id: 'role-1'
|
|
428
|
+
}
|
|
429
|
+
]);
|
|
430
|
+
mockRawMany(mockPermissionRepo, []);
|
|
431
|
+
const result = await service.assignCompanyActions({
|
|
432
|
+
companyId: 'company-1',
|
|
433
|
+
items: [
|
|
434
|
+
{
|
|
435
|
+
id: 'action-1',
|
|
436
|
+
action: PermissionAction.REMOVE
|
|
437
|
+
}
|
|
438
|
+
]
|
|
439
|
+
});
|
|
440
|
+
expect(mockPermissionRepo.delete).toHaveBeenCalledWith(expect.objectContaining({
|
|
441
|
+
permissionType: IamPermissionType.COMPANY_ACTION
|
|
442
|
+
}));
|
|
443
|
+
expect(mockPermissionRepo.delete).toHaveBeenCalledWith(expect.objectContaining({
|
|
444
|
+
permissionType: IamPermissionType.ROLE_ACTION
|
|
445
|
+
}));
|
|
446
|
+
expect(mockPermissionRepo.delete).toHaveBeenCalledWith(expect.objectContaining({
|
|
447
|
+
permissionType: IamPermissionType.USER_ACTION
|
|
448
|
+
}));
|
|
449
|
+
expect(result.removed).toBe(2);
|
|
450
|
+
});
|
|
451
|
+
});
|
|
452
|
+
describe('getCompanyActions / getCompanyActionIds', ()=>{
|
|
453
|
+
it('getCompanyActions returns an empty array when the company has no whitelist entries', async ()=>{
|
|
454
|
+
mockPermissionRepo.find.mockResolvedValue([]);
|
|
455
|
+
expect(await service.getCompanyActions('company-1')).toEqual([]);
|
|
456
|
+
});
|
|
457
|
+
it('getCompanyActions maps whitelist entries to action details', async ()=>{
|
|
458
|
+
mockPermissionRepo.find.mockResolvedValue([
|
|
459
|
+
buildPermissionRow({
|
|
460
|
+
sourceType: IamEntityType.COMPANY,
|
|
461
|
+
sourceId: 'company-1',
|
|
462
|
+
targetId: 'action-1'
|
|
463
|
+
})
|
|
464
|
+
]);
|
|
465
|
+
mockActionRepo.find.mockResolvedValue([
|
|
466
|
+
buildAction({
|
|
467
|
+
id: 'action-1'
|
|
468
|
+
})
|
|
469
|
+
]);
|
|
470
|
+
const result = await service.getCompanyActions('company-1');
|
|
471
|
+
expect(result).toEqual([
|
|
472
|
+
expect.objectContaining({
|
|
473
|
+
companyId: 'company-1',
|
|
474
|
+
actionCode: 'user.view'
|
|
475
|
+
})
|
|
476
|
+
]);
|
|
477
|
+
});
|
|
478
|
+
it('getCompanyActionIds returns just the whitelisted action ids', async ()=>{
|
|
479
|
+
mockPermissionRepo.find.mockResolvedValue([
|
|
480
|
+
{
|
|
481
|
+
targetId: 'action-1'
|
|
482
|
+
},
|
|
483
|
+
{
|
|
484
|
+
targetId: 'action-2'
|
|
485
|
+
}
|
|
486
|
+
]);
|
|
487
|
+
const result = await service.getCompanyActions('company-1', true);
|
|
488
|
+
expect(result).toEqual([
|
|
489
|
+
'action-1',
|
|
490
|
+
'action-2'
|
|
491
|
+
]);
|
|
492
|
+
});
|
|
493
|
+
});
|
|
494
|
+
// ==================== User-Role Permissions ====================
|
|
495
|
+
describe('assignUserRoles', ()=>{
|
|
496
|
+
const dto = {
|
|
497
|
+
userId: 'user-1',
|
|
498
|
+
items: [
|
|
499
|
+
{
|
|
500
|
+
id: 'role-1',
|
|
501
|
+
action: PermissionAction.ADD
|
|
502
|
+
}
|
|
503
|
+
]
|
|
504
|
+
};
|
|
505
|
+
it('throws BadRequestException when RBAC is disabled (DIRECT-only mode)', async ()=>{
|
|
506
|
+
mockIamConfigService.isRbacEnabled.mockReturnValue(false);
|
|
507
|
+
await expect(service.assignUserRoles(dto)).rejects.toMatchObject({
|
|
508
|
+
response: expect.objectContaining({
|
|
509
|
+
messageKey: IAM_MODE_MESSAGES.ROLE_ASSIGNMENT_UNAVAILABLE
|
|
510
|
+
})
|
|
511
|
+
});
|
|
512
|
+
});
|
|
513
|
+
it('adds new role assignments and invalidates the user cache', async ()=>{
|
|
514
|
+
mockPermissionRepo.find.mockResolvedValue([]);
|
|
515
|
+
mockPermissionRepo.save.mockResolvedValue([
|
|
516
|
+
{}
|
|
517
|
+
]);
|
|
518
|
+
const result = await service.assignUserRoles(dto);
|
|
519
|
+
expect(result).toEqual({
|
|
520
|
+
added: 1,
|
|
521
|
+
removed: 0,
|
|
522
|
+
total: 1
|
|
523
|
+
});
|
|
524
|
+
expect(mockPermissionCacheService.invalidateUser).toHaveBeenCalledWith('user-1', null, [
|
|
525
|
+
null
|
|
526
|
+
]);
|
|
527
|
+
});
|
|
528
|
+
it('translates duplicate user-role entries into ConflictException', async ()=>{
|
|
529
|
+
mockPermissionRepo.find.mockResolvedValue([]);
|
|
530
|
+
mockPermissionRepo.save.mockRejectedValue({
|
|
531
|
+
code: 'ER_DUP_ENTRY'
|
|
532
|
+
});
|
|
533
|
+
await expect(service.assignUserRoles(dto)).rejects.toMatchObject({
|
|
534
|
+
response: expect.objectContaining({
|
|
535
|
+
messageKey: PERMISSION_OPERATION_MESSAGES.ALREADY_EXISTS
|
|
536
|
+
})
|
|
537
|
+
});
|
|
538
|
+
});
|
|
539
|
+
});
|
|
540
|
+
describe('getUserRoles', ()=>{
|
|
541
|
+
it('returns an empty array when the user has no role assignments', async ()=>{
|
|
542
|
+
mockPermissionRepo.find.mockResolvedValue([]);
|
|
543
|
+
expect(await service.getUserRoles('user-1')).toEqual([]);
|
|
544
|
+
expect(mockRoleRepo.find).not.toHaveBeenCalled();
|
|
545
|
+
});
|
|
546
|
+
it('joins role permissions with role details and filters out permissions for deleted roles', async ()=>{
|
|
547
|
+
mockPermissionRepo.find.mockResolvedValue([
|
|
548
|
+
buildPermissionRow({
|
|
549
|
+
targetId: 'role-1',
|
|
550
|
+
targetType: IamEntityType.ROLE
|
|
551
|
+
}),
|
|
552
|
+
buildPermissionRow({
|
|
553
|
+
id: 'perm-2',
|
|
554
|
+
targetId: 'role-missing',
|
|
555
|
+
targetType: IamEntityType.ROLE
|
|
556
|
+
})
|
|
557
|
+
]);
|
|
558
|
+
mockRoleRepo.find.mockResolvedValue([
|
|
559
|
+
{
|
|
560
|
+
id: 'role-1',
|
|
561
|
+
name: 'Manager'
|
|
562
|
+
}
|
|
563
|
+
]);
|
|
564
|
+
const result = await service.getUserRoles('user-1');
|
|
565
|
+
expect(result).toHaveLength(1);
|
|
566
|
+
expect(result[0]).toEqual(expect.objectContaining({
|
|
567
|
+
roleId: 'role-1',
|
|
568
|
+
roleName: 'Manager'
|
|
569
|
+
}));
|
|
570
|
+
});
|
|
571
|
+
});
|
|
572
|
+
// ==================== My Permissions / permission-mode branching ====================
|
|
573
|
+
describe('getMyPermissions', ()=>{
|
|
574
|
+
it('delegates to the cache service and returns its response', async ()=>{
|
|
575
|
+
const response = {
|
|
576
|
+
frontendActions: [
|
|
577
|
+
{
|
|
578
|
+
id: 'action-1',
|
|
579
|
+
code: 'user.view',
|
|
580
|
+
name: 'View',
|
|
581
|
+
description: null
|
|
582
|
+
}
|
|
583
|
+
],
|
|
584
|
+
cachedEndpoints: 1
|
|
585
|
+
};
|
|
586
|
+
mockPermissionCacheService.getMyPermissionsResponse.mockResolvedValue(response);
|
|
587
|
+
const result = await service.getMyPermissions('user-1', null, 'company-1', [
|
|
588
|
+
'parent-code'
|
|
589
|
+
]);
|
|
590
|
+
expect(mockPermissionCacheService.getMyPermissionsResponse).toHaveBeenCalledWith('user-1', null, 'company-1', [
|
|
591
|
+
'parent-code'
|
|
592
|
+
]);
|
|
593
|
+
expect(result).toBe(response);
|
|
594
|
+
});
|
|
595
|
+
});
|
|
596
|
+
describe('revokeCompanyPermissions', ()=>{
|
|
597
|
+
it('does nothing when the company feature is disabled', async ()=>{
|
|
598
|
+
await service.revokeCompanyPermissions('company-1');
|
|
599
|
+
expect(mockPermissionRepo.find).not.toHaveBeenCalled();
|
|
600
|
+
expect(mockRoleRepo.find).not.toHaveBeenCalled();
|
|
601
|
+
});
|
|
602
|
+
it('deletes company/role/user permissions transactionally and invalidates affected users', async ()=>{
|
|
603
|
+
mockIamConfigService.isCompanyFeatureEnabled.mockReturnValue(true);
|
|
604
|
+
mockRoleRepo.find.mockResolvedValue([
|
|
605
|
+
{
|
|
606
|
+
id: 'role-1'
|
|
607
|
+
}
|
|
608
|
+
]);
|
|
609
|
+
mockPermissionRepo.find.mockResolvedValue([
|
|
610
|
+
{
|
|
611
|
+
sourceId: 'user-1'
|
|
612
|
+
}
|
|
613
|
+
]);
|
|
614
|
+
const fakeTxRepo = {
|
|
615
|
+
delete: jest.fn().mockResolvedValue(buildDeleteResult(1))
|
|
616
|
+
};
|
|
617
|
+
const fakeTxRoleRepo = {
|
|
618
|
+
delete: jest.fn().mockResolvedValue(buildDeleteResult(1))
|
|
619
|
+
};
|
|
620
|
+
mockDataSourceProvider.getDataSource.mockResolvedValue({
|
|
621
|
+
transaction: jest.fn().mockImplementation((cb)=>cb({
|
|
622
|
+
getRepository: jest.fn((target)=>{
|
|
623
|
+
if (target === mockRoleRepo.target) return fakeTxRoleRepo;
|
|
624
|
+
return fakeTxRepo;
|
|
625
|
+
})
|
|
626
|
+
}))
|
|
627
|
+
});
|
|
628
|
+
await service.revokeCompanyPermissions('company-1');
|
|
629
|
+
expect(fakeTxRepo.delete).toHaveBeenCalledWith(expect.objectContaining({
|
|
630
|
+
permissionType: IamPermissionType.COMPANY_ACTION
|
|
631
|
+
}));
|
|
632
|
+
expect(fakeTxRoleRepo.delete).toHaveBeenCalledWith([
|
|
633
|
+
'role-1'
|
|
634
|
+
]);
|
|
635
|
+
expect(mockPermissionCacheService.invalidateUsers).toHaveBeenCalledWith([
|
|
636
|
+
'user-1'
|
|
637
|
+
], 'company-1');
|
|
638
|
+
});
|
|
639
|
+
});
|
|
640
|
+
describe('revokeBranchPermissions', ()=>{
|
|
641
|
+
it('does nothing when the company feature is disabled', async ()=>{
|
|
642
|
+
await service.revokeBranchPermissions('branch-1', 'company-1');
|
|
643
|
+
expect(mockPermissionRepo.delete).not.toHaveBeenCalled();
|
|
644
|
+
});
|
|
645
|
+
it('deletes matching branch permissions and invalidates affected users', async ()=>{
|
|
646
|
+
mockIamConfigService.isCompanyFeatureEnabled.mockReturnValue(true);
|
|
647
|
+
mockPermissionRepo.find.mockResolvedValue([
|
|
648
|
+
{
|
|
649
|
+
sourceId: 'user-1'
|
|
650
|
+
}
|
|
651
|
+
]);
|
|
652
|
+
mockPermissionRepo.delete.mockResolvedValue(buildDeleteResult(1));
|
|
653
|
+
await service.revokeBranchPermissions('branch-1', 'company-1');
|
|
654
|
+
expect(mockPermissionRepo.delete).toHaveBeenCalledWith(expect.objectContaining({
|
|
655
|
+
branchId: 'branch-1',
|
|
656
|
+
companyId: 'company-1'
|
|
657
|
+
}));
|
|
658
|
+
expect(mockPermissionCacheService.invalidateUsers).toHaveBeenCalledWith([
|
|
659
|
+
'user-1'
|
|
660
|
+
], 'company-1', [
|
|
661
|
+
'branch-1'
|
|
662
|
+
]);
|
|
663
|
+
});
|
|
664
|
+
});
|
|
665
|
+
describe('revokeUserCompanyAccess', ()=>{
|
|
666
|
+
it('deletes user-role and user-action permissions for the company and invalidates the user cache', async ()=>{
|
|
667
|
+
await service.revokeUserCompanyAccess('user-1', 'company-1');
|
|
668
|
+
expect(mockPermissionRepo.delete).toHaveBeenCalledWith(expect.objectContaining({
|
|
669
|
+
sourceId: 'user-1',
|
|
670
|
+
companyId: 'company-1'
|
|
671
|
+
}));
|
|
672
|
+
expect(mockPermissionCacheService.invalidateUser).toHaveBeenCalledWith('user-1', 'company-1');
|
|
673
|
+
});
|
|
674
|
+
});
|
|
675
|
+
describe('revokeUserBranchAccess', ()=>{
|
|
676
|
+
it('deletes branch-scoped permissions for the user and invalidates the user+branch cache', async ()=>{
|
|
677
|
+
await service.revokeUserBranchAccess('user-1', 'branch-1', 'company-1');
|
|
678
|
+
expect(mockPermissionRepo.delete).toHaveBeenCalledWith(expect.objectContaining({
|
|
679
|
+
sourceId: 'user-1',
|
|
680
|
+
branchId: 'branch-1',
|
|
681
|
+
companyId: 'company-1'
|
|
682
|
+
}));
|
|
683
|
+
expect(mockPermissionCacheService.invalidateUser).toHaveBeenCalledWith('user-1', 'company-1', [
|
|
684
|
+
'branch-1'
|
|
685
|
+
]);
|
|
686
|
+
});
|
|
687
|
+
});
|
|
688
|
+
});
|
|
@@ -60,7 +60,7 @@ export class RoleService extends ApiService {
|
|
|
60
60
|
}
|
|
61
61
|
});
|
|
62
62
|
const affectedUserIds = [
|
|
63
|
-
...new Set(userRoleRows.map((row)=>row.
|
|
63
|
+
...new Set(userRoleRows.map((row)=>row.sourceId))
|
|
64
64
|
];
|
|
65
65
|
await permissionRepo.delete({
|
|
66
66
|
permissionType: IamPermissionType.ROLE_ACTION,
|