@flusys/nestjs-iam 6.0.2 → 6.1.0
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.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.spec.js +68 -0
- package/cjs/docs/iam-swagger.config.spec.js +99 -0
- package/cjs/entities/index.spec.js +56 -0
- 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.spec.js +293 -0
- 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.spec.js +252 -0
- package/cjs/services/permission.service.spec.js +967 -0
- package/cjs/services/role.service.spec.js +312 -0
- package/fesm/controllers/action.controller.spec.js +75 -0
- 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.spec.js +64 -0
- package/fesm/docs/iam-swagger.config.spec.js +95 -0
- package/fesm/entities/index.spec.js +52 -0
- 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.spec.js +289 -0
- 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.spec.js +248 -0
- package/fesm/services/permission.service.spec.js +963 -0
- package/fesm/services/role.service.spec.js +308 -0
- package/package.json +3 -3
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
const _nestjsshared = require("@flusys/nestjs-shared");
|
|
6
|
+
const _permissioncacheservice = require("./permission-cache.service");
|
|
7
|
+
function buildCacheData(overrides = {}) {
|
|
8
|
+
return {
|
|
9
|
+
frontendActions: [],
|
|
10
|
+
backendCodes: [],
|
|
11
|
+
...overrides
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
describe('PermissionCacheService', ()=>{
|
|
15
|
+
let mockCacheManager;
|
|
16
|
+
beforeEach(()=>{
|
|
17
|
+
mockCacheManager = {
|
|
18
|
+
get: jest.fn(),
|
|
19
|
+
set: jest.fn(),
|
|
20
|
+
del: jest.fn()
|
|
21
|
+
};
|
|
22
|
+
});
|
|
23
|
+
function buildService(guardConfig) {
|
|
24
|
+
return new _permissioncacheservice.PermissionCacheService(mockCacheManager, guardConfig);
|
|
25
|
+
}
|
|
26
|
+
describe('setMyPermissions / getMyPermissions', ()=>{
|
|
27
|
+
it('stores data under the user-scoped key and tracks it', async ()=>{
|
|
28
|
+
const service = buildService();
|
|
29
|
+
mockCacheManager.get.mockResolvedValue(undefined);
|
|
30
|
+
const data = buildCacheData({
|
|
31
|
+
backendCodes: [
|
|
32
|
+
'user.read'
|
|
33
|
+
]
|
|
34
|
+
});
|
|
35
|
+
await service.setMyPermissions({
|
|
36
|
+
userId: 'user-1'
|
|
37
|
+
}, data);
|
|
38
|
+
const expectedKey = (0, _nestjsshared.buildPermissionCacheKey)({
|
|
39
|
+
userId: 'user-1'
|
|
40
|
+
});
|
|
41
|
+
expect(mockCacheManager.set).toHaveBeenCalledWith(expectedKey, data, expect.any(Number));
|
|
42
|
+
// tracked keys + tracked scopes get written too
|
|
43
|
+
expect(mockCacheManager.set).toHaveBeenCalledWith(expect.stringContaining('permission-tracked-keys:user:user-1'), [
|
|
44
|
+
expectedKey
|
|
45
|
+
], expect.any(Number));
|
|
46
|
+
expect(mockCacheManager.set).toHaveBeenCalledWith(expect.stringContaining('permission-tracked-scopes:user:user-1'), [
|
|
47
|
+
'none'
|
|
48
|
+
], expect.any(Number));
|
|
49
|
+
});
|
|
50
|
+
it('does not duplicate an already-tracked key', async ()=>{
|
|
51
|
+
const service = buildService();
|
|
52
|
+
const expectedKey = (0, _nestjsshared.buildPermissionCacheKey)({
|
|
53
|
+
userId: 'user-1'
|
|
54
|
+
});
|
|
55
|
+
mockCacheManager.get.mockImplementation(async (key)=>{
|
|
56
|
+
if (key.includes('permission-tracked-keys')) return [
|
|
57
|
+
expectedKey
|
|
58
|
+
];
|
|
59
|
+
if (key.includes('permission-tracked-scopes')) return [
|
|
60
|
+
'none'
|
|
61
|
+
];
|
|
62
|
+
return undefined;
|
|
63
|
+
});
|
|
64
|
+
await service.setMyPermissions({
|
|
65
|
+
userId: 'user-1'
|
|
66
|
+
}, buildCacheData());
|
|
67
|
+
const trackedKeysCalls = mockCacheManager.set.mock.calls.filter(([key])=>String(key).includes('permission-tracked-keys'));
|
|
68
|
+
expect(trackedKeysCalls).toHaveLength(0);
|
|
69
|
+
});
|
|
70
|
+
it('returns cached data on a hit', async ()=>{
|
|
71
|
+
const service = buildService();
|
|
72
|
+
const data = buildCacheData({
|
|
73
|
+
backendCodes: [
|
|
74
|
+
'a.b'
|
|
75
|
+
]
|
|
76
|
+
});
|
|
77
|
+
mockCacheManager.get.mockResolvedValue(data);
|
|
78
|
+
const result = await service.getMyPermissions({
|
|
79
|
+
userId: 'user-1'
|
|
80
|
+
});
|
|
81
|
+
expect(result).toBe(data);
|
|
82
|
+
});
|
|
83
|
+
it('returns null on a cache miss', async ()=>{
|
|
84
|
+
const service = buildService();
|
|
85
|
+
mockCacheManager.get.mockResolvedValue(undefined);
|
|
86
|
+
const result = await service.getMyPermissions({
|
|
87
|
+
userId: 'user-1'
|
|
88
|
+
});
|
|
89
|
+
expect(result).toBeNull();
|
|
90
|
+
});
|
|
91
|
+
it('returns null (not throw) when the cache backend errors', async ()=>{
|
|
92
|
+
const service = buildService();
|
|
93
|
+
mockCacheManager.get.mockRejectedValue(new Error('cache down'));
|
|
94
|
+
const result = await service.getMyPermissions({
|
|
95
|
+
userId: 'user-1'
|
|
96
|
+
});
|
|
97
|
+
expect(result).toBeNull();
|
|
98
|
+
});
|
|
99
|
+
it('uses the company-scoped key when guard config enables company scope', async ()=>{
|
|
100
|
+
const guardConfig = {
|
|
101
|
+
enableCompanyFeature: true
|
|
102
|
+
};
|
|
103
|
+
const service = buildService(guardConfig);
|
|
104
|
+
mockCacheManager.get.mockResolvedValue(undefined);
|
|
105
|
+
await service.setMyPermissions({
|
|
106
|
+
userId: 'user-1',
|
|
107
|
+
companyId: 'company-1'
|
|
108
|
+
}, buildCacheData());
|
|
109
|
+
const expectedKey = (0, _nestjsshared.buildPermissionCacheKey)({
|
|
110
|
+
userId: 'user-1',
|
|
111
|
+
companyId: 'company-1'
|
|
112
|
+
}, guardConfig);
|
|
113
|
+
expect(mockCacheManager.set).toHaveBeenCalledWith(expectedKey, expect.anything(), expect.any(Number));
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
describe('invalidateUser', ()=>{
|
|
117
|
+
it('invalidates only the given company scope when companyId is provided', async ()=>{
|
|
118
|
+
const service = buildService();
|
|
119
|
+
mockCacheManager.get.mockImplementation(async (key)=>{
|
|
120
|
+
if (key.includes('permission-tracked-keys')) return [
|
|
121
|
+
'tracked-key-1',
|
|
122
|
+
'tracked-key-2'
|
|
123
|
+
];
|
|
124
|
+
return undefined;
|
|
125
|
+
});
|
|
126
|
+
await service.invalidateUser('user-1', 'company-1');
|
|
127
|
+
expect(mockCacheManager.del).toHaveBeenCalledWith('tracked-key-1');
|
|
128
|
+
expect(mockCacheManager.del).toHaveBeenCalledWith('tracked-key-2');
|
|
129
|
+
expect(mockCacheManager.del).toHaveBeenCalledWith(expect.stringContaining('permission-tracked-keys:user:user-1:company:company-1'));
|
|
130
|
+
// no company scope given -> tracked-scopes key should not be touched in this branch
|
|
131
|
+
expect(mockCacheManager.del).not.toHaveBeenCalledWith(expect.stringContaining('permission-tracked-scopes'));
|
|
132
|
+
});
|
|
133
|
+
it('invalidates only the requested branch keys, keeping remaining branches tracked', async ()=>{
|
|
134
|
+
// company-scoped key format only differentiates by branchId when the guard config
|
|
135
|
+
// enables the company feature — otherwise every branch maps to the same cache key.
|
|
136
|
+
const guardConfig = {
|
|
137
|
+
enableCompanyFeature: true
|
|
138
|
+
};
|
|
139
|
+
const service = buildService(guardConfig);
|
|
140
|
+
const keptKey = (0, _nestjsshared.buildPermissionCacheKey)({
|
|
141
|
+
userId: 'user-1',
|
|
142
|
+
companyId: 'company-1',
|
|
143
|
+
branchId: 'branch-keep'
|
|
144
|
+
}, guardConfig);
|
|
145
|
+
mockCacheManager.get.mockImplementation(async (key)=>{
|
|
146
|
+
if (key.includes('permission-tracked-keys')) return [
|
|
147
|
+
keptKey,
|
|
148
|
+
'some-other-tracked-key'
|
|
149
|
+
];
|
|
150
|
+
return undefined;
|
|
151
|
+
});
|
|
152
|
+
// companyId given directly invokes invalidateScope (bypasses the tracked-scopes fan-out)
|
|
153
|
+
await service.invalidateUser('user-1', 'company-1', [
|
|
154
|
+
'branch-a'
|
|
155
|
+
]);
|
|
156
|
+
// the specific branch key gets deleted
|
|
157
|
+
const deletedKeys = mockCacheManager.del.mock.calls.map(([key])=>key);
|
|
158
|
+
expect(deletedKeys).toContain((0, _nestjsshared.buildPermissionCacheKey)({
|
|
159
|
+
userId: 'user-1',
|
|
160
|
+
companyId: 'company-1',
|
|
161
|
+
branchId: 'branch-a'
|
|
162
|
+
}, guardConfig));
|
|
163
|
+
// remaining tracked keys get re-saved rather than the tracked-keys index being wiped
|
|
164
|
+
expect(mockCacheManager.set).toHaveBeenCalledWith(expect.stringContaining('permission-tracked-keys'), expect.arrayContaining([
|
|
165
|
+
keptKey,
|
|
166
|
+
'some-other-tracked-key'
|
|
167
|
+
]), expect.any(Number));
|
|
168
|
+
});
|
|
169
|
+
it('deletes the tracked-keys index entirely when no remaining keys are left after branch filtering', async ()=>{
|
|
170
|
+
const guardConfig = {
|
|
171
|
+
enableCompanyFeature: true
|
|
172
|
+
};
|
|
173
|
+
const service = buildService(guardConfig);
|
|
174
|
+
const onlyKey = (0, _nestjsshared.buildPermissionCacheKey)({
|
|
175
|
+
userId: 'user-1',
|
|
176
|
+
companyId: 'company-1',
|
|
177
|
+
branchId: 'branch-a'
|
|
178
|
+
}, guardConfig);
|
|
179
|
+
mockCacheManager.get.mockImplementation(async (key)=>{
|
|
180
|
+
if (key.includes('permission-tracked-keys')) return [
|
|
181
|
+
onlyKey
|
|
182
|
+
];
|
|
183
|
+
return undefined;
|
|
184
|
+
});
|
|
185
|
+
await service.invalidateUser('user-1', 'company-1', [
|
|
186
|
+
'branch-a'
|
|
187
|
+
]);
|
|
188
|
+
const deletedKeys = mockCacheManager.del.mock.calls.map(([key])=>key);
|
|
189
|
+
expect(deletedKeys).toContain(onlyKey);
|
|
190
|
+
expect(deletedKeys.some((k)=>String(k).includes('permission-tracked-keys'))).toBe(true);
|
|
191
|
+
});
|
|
192
|
+
it('without a companyId, invalidates every tracked scope for the user and clears the scopes index', async ()=>{
|
|
193
|
+
const service = buildService();
|
|
194
|
+
mockCacheManager.get.mockImplementation(async (key)=>{
|
|
195
|
+
if (key.includes('permission-tracked-scopes')) return [
|
|
196
|
+
'none',
|
|
197
|
+
'company-a'
|
|
198
|
+
];
|
|
199
|
+
if (key.includes('company:none')) return [
|
|
200
|
+
'key-none-1'
|
|
201
|
+
];
|
|
202
|
+
if (key.includes('company:company-a')) return [
|
|
203
|
+
'key-company-a-1'
|
|
204
|
+
];
|
|
205
|
+
return undefined;
|
|
206
|
+
});
|
|
207
|
+
await service.invalidateUser('user-1');
|
|
208
|
+
expect(mockCacheManager.del).toHaveBeenCalledWith('key-none-1');
|
|
209
|
+
expect(mockCacheManager.del).toHaveBeenCalledWith('key-company-a-1');
|
|
210
|
+
expect(mockCacheManager.del).toHaveBeenCalledWith(expect.stringContaining('permission-tracked-scopes:user:user-1'));
|
|
211
|
+
});
|
|
212
|
+
it('treats a cache read error for tracked scopes as an empty list (no throw)', async ()=>{
|
|
213
|
+
const service = buildService();
|
|
214
|
+
mockCacheManager.get.mockRejectedValue(new Error('down'));
|
|
215
|
+
await expect(service.invalidateUser('user-1')).resolves.toBeUndefined();
|
|
216
|
+
expect(mockCacheManager.del).toHaveBeenCalledWith(expect.stringContaining('permission-tracked-scopes:user:user-1'));
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
describe('invalidateUsers', ()=>{
|
|
220
|
+
it('returns 0 immediately for an empty user list', async ()=>{
|
|
221
|
+
const service = buildService();
|
|
222
|
+
const result = await service.invalidateUsers([]);
|
|
223
|
+
expect(result).toBe(0);
|
|
224
|
+
expect(mockCacheManager.get).not.toHaveBeenCalled();
|
|
225
|
+
});
|
|
226
|
+
it('invalidates every user and returns the count of successful invalidations', async ()=>{
|
|
227
|
+
const service = buildService();
|
|
228
|
+
mockCacheManager.get.mockResolvedValue(undefined);
|
|
229
|
+
const result = await service.invalidateUsers([
|
|
230
|
+
'user-1',
|
|
231
|
+
'user-2',
|
|
232
|
+
'user-3'
|
|
233
|
+
], 'company-1');
|
|
234
|
+
expect(result).toBe(3);
|
|
235
|
+
});
|
|
236
|
+
it('does not let one user failing prevent the others from being counted', async ()=>{
|
|
237
|
+
const service = buildService();
|
|
238
|
+
let callCount = 0;
|
|
239
|
+
mockCacheManager.get.mockImplementation(async ()=>{
|
|
240
|
+
callCount += 1;
|
|
241
|
+
if (callCount === 1) throw new Error('boom');
|
|
242
|
+
return undefined;
|
|
243
|
+
});
|
|
244
|
+
const result = await service.invalidateUsers([
|
|
245
|
+
'user-1',
|
|
246
|
+
'user-2'
|
|
247
|
+
], 'company-1');
|
|
248
|
+
// invalidateUser swallows its own read errors, so both still resolve successfully
|
|
249
|
+
expect(result).toBe(2);
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
});
|