@tomei/sso 0.34.2 → 0.34.6
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/__tests__/unit/components/login-user/login.spec.ts +474 -375
- package/dist/__tests__/unit/components/login-user/login.spec.js +71 -219
- package/dist/__tests__/unit/components/login-user/login.spec.js.map +1 -1
- package/dist/src/components/group/group.d.ts +1 -1
- package/dist/src/components/group/group.js +2 -2
- package/dist/src/components/group/group.js.map +1 -1
- package/dist/src/components/login-user/index.d.ts +1 -0
- package/dist/src/components/login-user/index.js +1 -0
- package/dist/src/components/login-user/index.js.map +1 -1
- package/dist/src/components/login-user/login-user.d.ts +7 -121
- package/dist/src/components/login-user/login-user.js +11 -1477
- package/dist/src/components/login-user/login-user.js.map +1 -1
- package/dist/src/components/login-user/user.d.ts +133 -0
- package/dist/src/components/login-user/user.js +1617 -0
- package/dist/src/components/login-user/user.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/components/group/group.ts +2 -2
- package/src/components/login-user/index.ts +1 -0
- package/src/components/login-user/login-user.ts +110 -2235
- package/src/components/login-user/user.ts +2343 -0
|
@@ -58,7 +58,11 @@ describe('LoginUser', () => {
|
|
|
58
58
|
.spyOn(UserRepository.prototype, 'findOne')
|
|
59
59
|
.mockResolvedValueOnce(user as any);
|
|
60
60
|
|
|
61
|
-
const result = await LoginUser.init(
|
|
61
|
+
const result = await LoginUser.init(
|
|
62
|
+
sessionService,
|
|
63
|
+
userId,
|
|
64
|
+
dbTransaction,
|
|
65
|
+
);
|
|
62
66
|
|
|
63
67
|
expect(findOneMock).toHaveBeenCalledTimes(1);
|
|
64
68
|
expect(findOneMock).toHaveBeenCalledWith({
|
|
@@ -77,7 +81,9 @@ describe('LoginUser', () => {
|
|
|
77
81
|
expect(result.Email).toBe(user.Email);
|
|
78
82
|
expect(result.Password).toBe(user.Password);
|
|
79
83
|
expect(result.Status).toBe(user.Status);
|
|
80
|
-
expect(result.DefaultPasswordChangedYN).toBe(
|
|
84
|
+
expect(result.DefaultPasswordChangedYN).toBe(
|
|
85
|
+
user.DefaultPasswordChangedYN,
|
|
86
|
+
);
|
|
81
87
|
expect(result.FirstLoginAt).toBe(user.FirstLoginAt);
|
|
82
88
|
expect(result.LastLoginAt).toBe(user.LastLoginAt);
|
|
83
89
|
expect(result.MFAEnabled).toBe(user.MFAEnabled);
|
|
@@ -98,7 +104,9 @@ describe('LoginUser', () => {
|
|
|
98
104
|
.spyOn(UserRepository.prototype, 'findOne')
|
|
99
105
|
.mockResolvedValueOnce(null);
|
|
100
106
|
|
|
101
|
-
await expect(
|
|
107
|
+
await expect(
|
|
108
|
+
LoginUser.init(sessionService, userId, dbTransaction),
|
|
109
|
+
).rejects.toThrow(Error);
|
|
102
110
|
|
|
103
111
|
expect(findOneMock).toHaveBeenCalledTimes(1);
|
|
104
112
|
expect(findOneMock).toHaveBeenCalledWith({
|
|
@@ -114,57 +122,71 @@ describe('LoginUser', () => {
|
|
|
114
122
|
});
|
|
115
123
|
});
|
|
116
124
|
|
|
117
|
-
describe('checkSession', () => {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
125
|
+
// describe('checkSession', () => {
|
|
126
|
+
// it('should throw an error if session expired', async () => {
|
|
127
|
+
// // Arrange
|
|
128
|
+
// const systemCode = 'EZC';
|
|
129
|
+
// const sessionId = 'ckymxuh8t000137t011w89zgk';
|
|
130
|
+
// const userId = '755';
|
|
131
|
+
// const sessionService = await SessionService.init();
|
|
132
|
+
// const loginUser = await LoginUser.init(sessionService);
|
|
133
|
+
|
|
134
|
+
// // Act
|
|
135
|
+
// const checkSessionPromise = loginUser.checkSession(
|
|
136
|
+
// systemCode,
|
|
137
|
+
// sessionId,
|
|
138
|
+
// userId,
|
|
139
|
+
// );
|
|
140
|
+
|
|
141
|
+
// // Assert
|
|
142
|
+
// await expect(checkSessionPromise).rejects.toThrow('Session expired.');
|
|
143
|
+
// });
|
|
144
|
+
|
|
145
|
+
// it('should refresh the session duration if session is valid', async () => {
|
|
146
|
+
// // Arrange
|
|
147
|
+
// const systemCode = 'EZC';
|
|
148
|
+
// const sessionId = 'ckymxuh8t000137t011w89zgk';
|
|
149
|
+
// const userId = '755';
|
|
150
|
+
// const sessionService = await SessionService.init();
|
|
151
|
+
// const loginUser = await LoginUser.init(sessionService);
|
|
152
|
+
|
|
153
|
+
// // Mock the _SessionService.retrieveUserSession method
|
|
154
|
+
// loginUser['_SessionService'].retrieveUserSession = jest
|
|
155
|
+
// .fn()
|
|
156
|
+
// .mockResolvedValue({
|
|
157
|
+
// systemLogins: [
|
|
158
|
+
// {
|
|
159
|
+
// code: systemCode,
|
|
160
|
+
// sessionId: sessionId,
|
|
161
|
+
// privileges: [],
|
|
162
|
+
// },
|
|
163
|
+
// ],
|
|
164
|
+
// });
|
|
165
|
+
|
|
166
|
+
// // Mock the _SessionService.refreshDuration method
|
|
167
|
+
// loginUser['_SessionService'].refreshDuration = jest.fn();
|
|
168
|
+
|
|
169
|
+
// // Act
|
|
170
|
+
// const systemLogin = await loginUser.checkSession(
|
|
171
|
+
// systemCode,
|
|
172
|
+
// sessionId,
|
|
173
|
+
// userId,
|
|
174
|
+
// );
|
|
175
|
+
|
|
176
|
+
// // Assert
|
|
177
|
+
// expect(
|
|
178
|
+
// loginUser['_SessionService'].retrieveUserSession,
|
|
179
|
+
// ).toHaveBeenCalledWith(userId);
|
|
180
|
+
// expect(loginUser['_SessionService'].refreshDuration).toHaveBeenCalledWith(
|
|
181
|
+
// userId,
|
|
182
|
+
// );
|
|
183
|
+
// expect(systemLogin).toEqual({
|
|
184
|
+
// code: systemCode,
|
|
185
|
+
// sessionId: sessionId,
|
|
186
|
+
// privileges: [],
|
|
187
|
+
// });
|
|
188
|
+
// });
|
|
189
|
+
// });
|
|
168
190
|
|
|
169
191
|
describe('shouldReleaseLock', () => {
|
|
170
192
|
const minuteToAutoRelease = 5;
|
|
@@ -224,7 +246,9 @@ describe('LoginUser', () => {
|
|
|
224
246
|
const UserId = 1;
|
|
225
247
|
const dbTransaction = null;
|
|
226
248
|
|
|
227
|
-
const updateMock = jest
|
|
249
|
+
const updateMock = jest
|
|
250
|
+
.spyOn(LoginUser['_Repository'], 'update')
|
|
251
|
+
.mockImplementationOnce(() => Promise.resolve({}) as any);
|
|
228
252
|
|
|
229
253
|
// Call the method under test
|
|
230
254
|
LoginUser['releaseLock'](UserId, dbTransaction);
|
|
@@ -241,7 +265,7 @@ describe('LoginUser', () => {
|
|
|
241
265
|
UserId,
|
|
242
266
|
},
|
|
243
267
|
transaction: dbTransaction,
|
|
244
|
-
}
|
|
268
|
+
},
|
|
245
269
|
);
|
|
246
270
|
});
|
|
247
271
|
});
|
|
@@ -258,10 +282,14 @@ describe('LoginUser', () => {
|
|
|
258
282
|
};
|
|
259
283
|
|
|
260
284
|
// Mock the LoginUser['_Repository'].findAll method to return a user
|
|
261
|
-
jest
|
|
285
|
+
jest
|
|
286
|
+
.spyOn(LoginUser['_Repository'], 'findAll')
|
|
287
|
+
.mockResolvedValueOnce([{ id: 1 }] as any);
|
|
262
288
|
|
|
263
289
|
// Assert that the method throws an error
|
|
264
|
-
await expect(
|
|
290
|
+
await expect(
|
|
291
|
+
LoginUser['checkUserInfoDuplicated'](dbTransaction, query),
|
|
292
|
+
).rejects.toThrowError();
|
|
265
293
|
});
|
|
266
294
|
|
|
267
295
|
it('should not throw an error if duplicate user info is not found', async () => {
|
|
@@ -278,7 +306,9 @@ describe('LoginUser', () => {
|
|
|
278
306
|
jest.spyOn(LoginUser['_Repository'], 'findAll').mockResolvedValueOnce([]);
|
|
279
307
|
|
|
280
308
|
// Assert that the method does not throw an error
|
|
281
|
-
await expect(
|
|
309
|
+
await expect(
|
|
310
|
+
LoginUser['checkUserInfoDuplicated'](dbTransaction, query),
|
|
311
|
+
).resolves.not.toThrowError();
|
|
282
312
|
});
|
|
283
313
|
});
|
|
284
314
|
|
|
@@ -292,11 +322,13 @@ describe('LoginUser', () => {
|
|
|
292
322
|
numOfSpecialChars: 1,
|
|
293
323
|
};
|
|
294
324
|
beforeEach(() => {
|
|
295
|
-
jest
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
325
|
+
jest
|
|
326
|
+
.spyOn(ComponentConfig, 'getComponentConfigValue')
|
|
327
|
+
.mockImplementation((componentName: string, configKey: string) => {
|
|
328
|
+
if (configKey === 'passwordPolicy') {
|
|
329
|
+
return passwordPolicy as any;
|
|
330
|
+
}
|
|
331
|
+
});
|
|
300
332
|
});
|
|
301
333
|
|
|
302
334
|
it('should generate a default password with the specified length', () => {
|
|
@@ -323,7 +355,9 @@ describe('LoginUser', () => {
|
|
|
323
355
|
it('should generate a default password without any non-acceptable characters', () => {
|
|
324
356
|
const password = LoginUser['generateDefaultPassword']();
|
|
325
357
|
const nonAcceptableChars = ['i', 'l', 'o'];
|
|
326
|
-
expect(nonAcceptableChars.some(char => password.includes(char))).toBe(
|
|
358
|
+
expect(nonAcceptableChars.some((char) => password.includes(char))).toBe(
|
|
359
|
+
false,
|
|
360
|
+
);
|
|
327
361
|
});
|
|
328
362
|
});
|
|
329
363
|
|
|
@@ -337,11 +371,13 @@ describe('LoginUser', () => {
|
|
|
337
371
|
numOfSpecialChars: 1,
|
|
338
372
|
};
|
|
339
373
|
beforeEach(() => {
|
|
340
|
-
jest
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
374
|
+
jest
|
|
375
|
+
.spyOn(ComponentConfig, 'getComponentConfigValue')
|
|
376
|
+
.mockImplementation((componentName: string, configKey: string) => {
|
|
377
|
+
if (configKey === 'passwordPolicy') {
|
|
378
|
+
return passwordPolicy as any;
|
|
379
|
+
}
|
|
380
|
+
});
|
|
345
381
|
});
|
|
346
382
|
|
|
347
383
|
it('should set the password for the user', async () => {
|
|
@@ -352,11 +388,17 @@ describe('LoginUser', () => {
|
|
|
352
388
|
const password = 'N3wP@ssw0';
|
|
353
389
|
|
|
354
390
|
// Act
|
|
355
|
-
const result = await LoginUser['setPassword'](
|
|
391
|
+
const result = await LoginUser['setPassword'](
|
|
392
|
+
dbTransaction,
|
|
393
|
+
user,
|
|
394
|
+
password,
|
|
395
|
+
);
|
|
356
396
|
|
|
357
397
|
// Assert
|
|
358
398
|
expect(result).toBeInstanceOf(LoginUser);
|
|
359
|
-
await expect(
|
|
399
|
+
await expect(
|
|
400
|
+
LoginUser['setPassword'](dbTransaction, user, password),
|
|
401
|
+
).resolves.not.toThrowError();
|
|
360
402
|
expect(result.Password).toBeDefined();
|
|
361
403
|
});
|
|
362
404
|
|
|
@@ -368,217 +410,212 @@ describe('LoginUser', () => {
|
|
|
368
410
|
const password = 'weakpassword';
|
|
369
411
|
|
|
370
412
|
// Act & Assert
|
|
371
|
-
await expect(LoginUser['setPassword'](dbTransaction, user, password)).rejects.toThrow();
|
|
372
|
-
});
|
|
373
|
-
});
|
|
374
|
-
|
|
375
|
-
describe('create', () => {
|
|
376
|
-
let loginUser: LoginUser;
|
|
377
|
-
let dbTransaction: any;
|
|
378
|
-
let user: LoginUser;
|
|
379
|
-
let newUser: any;
|
|
380
|
-
|
|
381
|
-
beforeEach(async () => {
|
|
382
|
-
const sessionService = await SessionService.init();
|
|
383
|
-
loginUser = await LoginUser.init(sessionService);
|
|
384
|
-
dbTransaction = null;
|
|
385
|
-
newUser = {
|
|
386
|
-
UserId: 1,
|
|
387
|
-
FullName: 'John Doe',
|
|
388
|
-
Email: 'john.doe@example.com',
|
|
389
|
-
Password: 'password',
|
|
390
|
-
Status: 'Active',
|
|
391
|
-
DefaultPasswordChangedYN: 'N',
|
|
392
|
-
FirstLoginAt: null,
|
|
393
|
-
LastLoginAt: null,
|
|
394
|
-
MFAEnabled: null,
|
|
395
|
-
MFAConfig: null,
|
|
396
|
-
RecoveryEmail: null,
|
|
397
|
-
FailedLoginAttemptCount: 0,
|
|
398
|
-
LastFailedLoginAt: null,
|
|
399
|
-
LastPasswordChangedAt: new Date(),
|
|
400
|
-
NeedToChangePasswordYN: 'N',
|
|
401
|
-
CreatedById: 1,
|
|
402
|
-
CreatedAt: new Date(),
|
|
403
|
-
UpdatedById: 1,
|
|
404
|
-
UpdatedAt: new Date(),
|
|
405
|
-
Staff: {
|
|
406
|
-
FullName: 'John Doe',
|
|
407
|
-
IdNo: '1234567890',
|
|
408
|
-
Mobile: '1234567890',
|
|
409
|
-
},
|
|
410
|
-
};
|
|
411
|
-
user = new (LoginUser as any)(sessionService, null, newUser);
|
|
412
|
-
});
|
|
413
|
-
|
|
414
|
-
afterEach(() => {
|
|
415
|
-
jest.clearAllMocks();
|
|
416
|
-
});
|
|
417
|
-
|
|
418
|
-
it('should create a new user record', async () => {
|
|
419
|
-
const checkPrivilegesMock = jest
|
|
420
|
-
.spyOn(loginUser, 'checkPrivileges')
|
|
421
|
-
.mockResolvedValueOnce(true);
|
|
422
|
-
|
|
423
|
-
const checkUserInfoDuplicatedMock = jest
|
|
424
|
-
.spyOn((LoginUser as any), 'checkUserInfoDuplicated')
|
|
425
|
-
.mockResolvedValueOnce(undefined);
|
|
426
|
-
|
|
427
|
-
const generateDefaultPasswordMock = jest
|
|
428
|
-
.spyOn((LoginUser as any), 'generateDefaultPassword')
|
|
429
|
-
.mockReturnValueOnce('defaultPassword');
|
|
430
|
-
|
|
431
|
-
const setPasswordMock = jest
|
|
432
|
-
.spyOn((LoginUser as any), 'setPassword')
|
|
433
|
-
.mockResolvedValueOnce(user);
|
|
434
|
-
|
|
435
|
-
const createMock = jest
|
|
436
|
-
.spyOn((LoginUser as any)['_Repository'], 'create')
|
|
437
|
-
.mockResolvedValueOnce({
|
|
438
|
-
...newUser,
|
|
439
|
-
get: () => newUser,
|
|
440
|
-
});
|
|
441
|
-
|
|
442
|
-
const activityCreateMock = jest
|
|
443
|
-
.spyOn((Activity as any).prototype, 'create')
|
|
444
|
-
.mockResolvedValueOnce(undefined);
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
jest.spyOn(ApplicationConfig, 'getComponentConfigValue').mockImplementation((configKey: string) => {
|
|
448
|
-
if (configKey === 'system-code') {
|
|
449
|
-
return 'SC';
|
|
450
|
-
}
|
|
451
|
-
});
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
const result = await LoginUser.create(loginUser, dbTransaction, user);
|
|
455
|
-
|
|
456
|
-
expect(checkPrivilegesMock).toHaveBeenCalledTimes(1);
|
|
457
|
-
expect(checkPrivilegesMock).toHaveBeenCalledWith(
|
|
458
|
-
ApplicationConfig.getComponentConfigValue('system-code'),
|
|
459
|
-
'User - Create'
|
|
460
|
-
);
|
|
461
|
-
|
|
462
|
-
expect(checkUserInfoDuplicatedMock).toHaveBeenCalledTimes(1);
|
|
463
|
-
expect(checkUserInfoDuplicatedMock).toHaveBeenCalledWith(
|
|
464
|
-
dbTransaction,
|
|
465
|
-
{
|
|
466
|
-
Email: user.Email,
|
|
467
|
-
IdType: user.IDType,
|
|
468
|
-
IdNo: user.IDNo,
|
|
469
|
-
ContactNo: user.ContactNo,
|
|
470
|
-
}
|
|
471
|
-
);
|
|
472
|
-
|
|
473
|
-
expect(generateDefaultPasswordMock).toHaveBeenCalledTimes(1);
|
|
474
|
-
|
|
475
|
-
expect(setPasswordMock).toHaveBeenCalledTimes(1);
|
|
476
|
-
expect(setPasswordMock).toHaveBeenCalledWith(
|
|
477
|
-
dbTransaction,
|
|
478
|
-
user,
|
|
479
|
-
'defaultPassword'
|
|
480
|
-
);
|
|
481
|
-
|
|
482
|
-
expect(createMock).toHaveBeenCalledTimes(1);
|
|
483
|
-
|
|
484
|
-
const userInfo = {
|
|
485
|
-
FullName: user.FullName,
|
|
486
|
-
IDNo: user.IDNo,
|
|
487
|
-
Email: user.Email,
|
|
488
|
-
ContactNo: user.ContactNo,
|
|
489
|
-
Password: user.Password,
|
|
490
|
-
Status: UserStatus.ACTIVE,
|
|
491
|
-
FirstLoginAt: null,
|
|
492
|
-
LastLoginAt: null,
|
|
493
|
-
MFAEnabled: null,
|
|
494
|
-
MFAConfig: null,
|
|
495
|
-
RecoveryEmail: null,
|
|
496
|
-
FailedLoginAttemptCount: 0,
|
|
497
|
-
LastFailedLoginAt: null,
|
|
498
|
-
LastPasswordChangedAt: null,
|
|
499
|
-
DefaultPasswordChangedYN: YN.No,
|
|
500
|
-
NeedToChangePasswordYN: YN.Yes,
|
|
501
|
-
CreatedById: loginUser.UserId,
|
|
502
|
-
CreatedAt: new Date(),
|
|
503
|
-
UpdatedById: loginUser.UserId,
|
|
504
|
-
UpdatedAt: new Date(),
|
|
505
|
-
UserId: null,
|
|
506
|
-
};
|
|
507
|
-
|
|
508
|
-
expect(createMock).toHaveBeenCalledWith(
|
|
509
|
-
{
|
|
510
|
-
Email: userInfo.Email,
|
|
511
|
-
Password: userInfo.Password,
|
|
512
|
-
Status: userInfo.Status,
|
|
513
|
-
DefaultPasswordChangedYN: userInfo.DefaultPasswordChangedYN,
|
|
514
|
-
FirstLoginAt: userInfo.FirstLoginAt,
|
|
515
|
-
LastLoginAt: userInfo.LastLoginAt,
|
|
516
|
-
MFAEnabled: userInfo.MFAEnabled,
|
|
517
|
-
MFAConfig: userInfo.MFAConfig,
|
|
518
|
-
RecoveryEmail: userInfo.RecoveryEmail,
|
|
519
|
-
FailedLoginAttemptCount: userInfo.FailedLoginAttemptCount,
|
|
520
|
-
LastFailedLoginAt: userInfo.LastFailedLoginAt,
|
|
521
|
-
LastPasswordChangedAt: userInfo.LastPasswordChangedAt,
|
|
522
|
-
NeedToChangePasswordYN: userInfo.NeedToChangePasswordYN,
|
|
523
|
-
CreatedById: userInfo.CreatedById,
|
|
524
|
-
CreatedAt: expect.any(Date),
|
|
525
|
-
UpdatedById: userInfo.UpdatedById,
|
|
526
|
-
UpdatedAt: expect.any(Date),
|
|
527
|
-
},
|
|
528
|
-
{
|
|
529
|
-
transaction: dbTransaction,
|
|
530
|
-
}
|
|
531
|
-
);
|
|
532
|
-
|
|
533
|
-
expect(activityCreateMock).toHaveBeenCalledTimes(1);
|
|
534
|
-
|
|
535
|
-
expect(result).toBeInstanceOf(LoginUser);
|
|
536
|
-
expect(result.Email).toBe(userInfo.Email);
|
|
537
|
-
expect(result.Password).toBe(userInfo.Password);
|
|
538
|
-
expect(result.Status).toBe(userInfo.Status);
|
|
539
|
-
expect(result.DefaultPasswordChangedYN).toBe(
|
|
540
|
-
userInfo.DefaultPasswordChangedYN
|
|
541
|
-
);
|
|
542
|
-
expect(result.FirstLoginAt).toBe(null);
|
|
543
|
-
expect(result.LastLoginAt).toBe(null);
|
|
544
|
-
expect(result.MFAEnabled).toBe(userInfo.MFAEnabled);
|
|
545
|
-
expect(result.MFAConfig).toBe(userInfo.MFAConfig);
|
|
546
|
-
expect(result.RecoveryEmail).toBe(userInfo.RecoveryEmail);
|
|
547
|
-
expect(result.FailedLoginAttemptCount).toBe(
|
|
548
|
-
userInfo.FailedLoginAttemptCount
|
|
549
|
-
);
|
|
550
|
-
expect(result.LastFailedLoginAt).toBe(userInfo.LastFailedLoginAt);
|
|
551
|
-
expect(result.LastPasswordChangedAt).toBe(userInfo.LastPasswordChangedAt);
|
|
552
|
-
expect(result.NeedToChangePasswordYN).toBe(
|
|
553
|
-
userInfo.NeedToChangePasswordYN
|
|
554
|
-
);
|
|
555
|
-
expect(result.CreatedById).toBe(userInfo.CreatedById);
|
|
556
|
-
expect(result.UpdatedById).toBe(userInfo.UpdatedById);
|
|
557
|
-
});
|
|
558
|
-
|
|
559
|
-
it('should throw an error if user dont have the privilege to create new user', async () => {
|
|
560
|
-
jest
|
|
561
|
-
.spyOn(loginUser, 'checkPrivileges')
|
|
562
|
-
.mockResolvedValueOnce(false);
|
|
563
|
-
|
|
564
413
|
await expect(
|
|
565
|
-
LoginUser
|
|
566
|
-
).rejects.toThrow(
|
|
567
|
-
});
|
|
568
|
-
|
|
569
|
-
it('should throw an error if user email is missing', async () => {
|
|
570
|
-
user.Email = undefined;
|
|
571
|
-
|
|
572
|
-
jest
|
|
573
|
-
.spyOn(loginUser, 'checkPrivileges')
|
|
574
|
-
.mockResolvedValueOnce(true);
|
|
575
|
-
|
|
576
|
-
await expect(
|
|
577
|
-
LoginUser.create(loginUser, dbTransaction, user)
|
|
578
|
-
).rejects.toThrow(ClassError);
|
|
414
|
+
LoginUser['setPassword'](dbTransaction, user, password),
|
|
415
|
+
).rejects.toThrow();
|
|
579
416
|
});
|
|
580
417
|
});
|
|
581
418
|
|
|
419
|
+
// describe('create', () => {
|
|
420
|
+
// let loginUser: LoginUser;
|
|
421
|
+
// let dbTransaction: any;
|
|
422
|
+
// let user: LoginUser;
|
|
423
|
+
// let newUser: any;
|
|
424
|
+
|
|
425
|
+
// beforeEach(async () => {
|
|
426
|
+
// const sessionService = await SessionService.init();
|
|
427
|
+
// loginUser = await LoginUser.init(sessionService);
|
|
428
|
+
// dbTransaction = null;
|
|
429
|
+
// newUser = {
|
|
430
|
+
// UserId: 1,
|
|
431
|
+
// FullName: 'John Doe',
|
|
432
|
+
// Email: 'john.doe@example.com',
|
|
433
|
+
// Password: 'password',
|
|
434
|
+
// Status: 'Active',
|
|
435
|
+
// DefaultPasswordChangedYN: 'N',
|
|
436
|
+
// FirstLoginAt: null,
|
|
437
|
+
// LastLoginAt: null,
|
|
438
|
+
// MFAEnabled: null,
|
|
439
|
+
// MFAConfig: null,
|
|
440
|
+
// RecoveryEmail: null,
|
|
441
|
+
// FailedLoginAttemptCount: 0,
|
|
442
|
+
// LastFailedLoginAt: null,
|
|
443
|
+
// LastPasswordChangedAt: new Date(),
|
|
444
|
+
// NeedToChangePasswordYN: 'N',
|
|
445
|
+
// CreatedById: 1,
|
|
446
|
+
// CreatedAt: new Date(),
|
|
447
|
+
// UpdatedById: 1,
|
|
448
|
+
// UpdatedAt: new Date(),
|
|
449
|
+
// Staff: {
|
|
450
|
+
// FullName: 'John Doe',
|
|
451
|
+
// IdNo: '1234567890',
|
|
452
|
+
// Mobile: '1234567890',
|
|
453
|
+
// },
|
|
454
|
+
// };
|
|
455
|
+
// user = new (LoginUser as any)(sessionService, null, newUser);
|
|
456
|
+
// });
|
|
457
|
+
|
|
458
|
+
// afterEach(() => {
|
|
459
|
+
// jest.clearAllMocks();
|
|
460
|
+
// });
|
|
461
|
+
|
|
462
|
+
// it('should create a new user record', async () => {
|
|
463
|
+
// const checkPrivilegesMock = jest
|
|
464
|
+
// .spyOn(loginUser, 'checkPrivileges')
|
|
465
|
+
// .mockResolvedValueOnce(true);
|
|
466
|
+
|
|
467
|
+
// const checkUserInfoDuplicatedMock = jest
|
|
468
|
+
// .spyOn(LoginUser as any, 'checkUserInfoDuplicated')
|
|
469
|
+
// .mockResolvedValueOnce(undefined);
|
|
470
|
+
|
|
471
|
+
// const generateDefaultPasswordMock = jest
|
|
472
|
+
// .spyOn(LoginUser as any, 'generateDefaultPassword')
|
|
473
|
+
// .mockReturnValueOnce('defaultPassword');
|
|
474
|
+
|
|
475
|
+
// const setPasswordMock = jest
|
|
476
|
+
// .spyOn(LoginUser as any, 'setPassword')
|
|
477
|
+
// .mockResolvedValueOnce(user);
|
|
478
|
+
|
|
479
|
+
// const createMock = jest
|
|
480
|
+
// .spyOn((LoginUser as any)['_Repository'], 'create')
|
|
481
|
+
// .mockResolvedValueOnce({
|
|
482
|
+
// ...newUser,
|
|
483
|
+
// get: () => newUser,
|
|
484
|
+
// });
|
|
485
|
+
|
|
486
|
+
// const activityCreateMock = jest
|
|
487
|
+
// .spyOn((Activity as any).prototype, 'create')
|
|
488
|
+
// .mockResolvedValueOnce(undefined);
|
|
489
|
+
|
|
490
|
+
// jest
|
|
491
|
+
// .spyOn(ApplicationConfig, 'getComponentConfigValue')
|
|
492
|
+
// .mockImplementation((configKey: string) => {
|
|
493
|
+
// if (configKey === 'system-code') {
|
|
494
|
+
// return 'SC';
|
|
495
|
+
// }
|
|
496
|
+
// });
|
|
497
|
+
|
|
498
|
+
// const result = await LoginUser.create(loginUser, dbTransaction, user);
|
|
499
|
+
|
|
500
|
+
// expect(checkPrivilegesMock).toHaveBeenCalledTimes(1);
|
|
501
|
+
// expect(checkPrivilegesMock).toHaveBeenCalledWith(
|
|
502
|
+
// ApplicationConfig.getComponentConfigValue('system-code'),
|
|
503
|
+
// 'User - Create',
|
|
504
|
+
// );
|
|
505
|
+
|
|
506
|
+
// expect(checkUserInfoDuplicatedMock).toHaveBeenCalledTimes(1);
|
|
507
|
+
// expect(checkUserInfoDuplicatedMock).toHaveBeenCalledWith(dbTransaction, {
|
|
508
|
+
// Email: user.Email,
|
|
509
|
+
// IdType: user.IDType,
|
|
510
|
+
// IdNo: user.IDNo,
|
|
511
|
+
// ContactNo: user.ContactNo,
|
|
512
|
+
// });
|
|
513
|
+
|
|
514
|
+
// expect(generateDefaultPasswordMock).toHaveBeenCalledTimes(1);
|
|
515
|
+
|
|
516
|
+
// expect(setPasswordMock).toHaveBeenCalledTimes(1);
|
|
517
|
+
// expect(setPasswordMock).toHaveBeenCalledWith(
|
|
518
|
+
// dbTransaction,
|
|
519
|
+
// user,
|
|
520
|
+
// 'defaultPassword',
|
|
521
|
+
// );
|
|
522
|
+
|
|
523
|
+
// expect(createMock).toHaveBeenCalledTimes(1);
|
|
524
|
+
|
|
525
|
+
// const userInfo = {
|
|
526
|
+
// FullName: user.FullName,
|
|
527
|
+
// IDNo: user.IDNo,
|
|
528
|
+
// Email: user.Email,
|
|
529
|
+
// ContactNo: user.ContactNo,
|
|
530
|
+
// Password: user.Password,
|
|
531
|
+
// Status: UserStatus.ACTIVE,
|
|
532
|
+
// FirstLoginAt: null,
|
|
533
|
+
// LastLoginAt: null,
|
|
534
|
+
// MFAEnabled: null,
|
|
535
|
+
// MFAConfig: null,
|
|
536
|
+
// RecoveryEmail: null,
|
|
537
|
+
// FailedLoginAttemptCount: 0,
|
|
538
|
+
// LastFailedLoginAt: null,
|
|
539
|
+
// LastPasswordChangedAt: null,
|
|
540
|
+
// DefaultPasswordChangedYN: YN.No,
|
|
541
|
+
// NeedToChangePasswordYN: YN.Yes,
|
|
542
|
+
// CreatedById: loginUser.UserId,
|
|
543
|
+
// CreatedAt: new Date(),
|
|
544
|
+
// UpdatedById: loginUser.UserId,
|
|
545
|
+
// UpdatedAt: new Date(),
|
|
546
|
+
// UserId: null,
|
|
547
|
+
// };
|
|
548
|
+
|
|
549
|
+
// expect(createMock).toHaveBeenCalledWith(
|
|
550
|
+
// {
|
|
551
|
+
// Email: userInfo.Email,
|
|
552
|
+
// Password: userInfo.Password,
|
|
553
|
+
// Status: userInfo.Status,
|
|
554
|
+
// DefaultPasswordChangedYN: userInfo.DefaultPasswordChangedYN,
|
|
555
|
+
// FirstLoginAt: userInfo.FirstLoginAt,
|
|
556
|
+
// LastLoginAt: userInfo.LastLoginAt,
|
|
557
|
+
// MFAEnabled: userInfo.MFAEnabled,
|
|
558
|
+
// MFAConfig: userInfo.MFAConfig,
|
|
559
|
+
// RecoveryEmail: userInfo.RecoveryEmail,
|
|
560
|
+
// FailedLoginAttemptCount: userInfo.FailedLoginAttemptCount,
|
|
561
|
+
// LastFailedLoginAt: userInfo.LastFailedLoginAt,
|
|
562
|
+
// LastPasswordChangedAt: userInfo.LastPasswordChangedAt,
|
|
563
|
+
// NeedToChangePasswordYN: userInfo.NeedToChangePasswordYN,
|
|
564
|
+
// CreatedById: userInfo.CreatedById,
|
|
565
|
+
// CreatedAt: expect.any(Date),
|
|
566
|
+
// UpdatedById: userInfo.UpdatedById,
|
|
567
|
+
// UpdatedAt: expect.any(Date),
|
|
568
|
+
// },
|
|
569
|
+
// {
|
|
570
|
+
// transaction: dbTransaction,
|
|
571
|
+
// },
|
|
572
|
+
// );
|
|
573
|
+
|
|
574
|
+
// expect(activityCreateMock).toHaveBeenCalledTimes(1);
|
|
575
|
+
|
|
576
|
+
// expect(result).toBeInstanceOf(LoginUser);
|
|
577
|
+
// expect(result.Email).toBe(userInfo.Email);
|
|
578
|
+
// expect(result.Password).toBe(userInfo.Password);
|
|
579
|
+
// expect(result.Status).toBe(userInfo.Status);
|
|
580
|
+
// expect(result.DefaultPasswordChangedYN).toBe(
|
|
581
|
+
// userInfo.DefaultPasswordChangedYN,
|
|
582
|
+
// );
|
|
583
|
+
// expect(result.FirstLoginAt).toBe(null);
|
|
584
|
+
// expect(result.LastLoginAt).toBe(null);
|
|
585
|
+
// expect(result.MFAEnabled).toBe(userInfo.MFAEnabled);
|
|
586
|
+
// expect(result.MFAConfig).toBe(userInfo.MFAConfig);
|
|
587
|
+
// expect(result.RecoveryEmail).toBe(userInfo.RecoveryEmail);
|
|
588
|
+
// expect(result.FailedLoginAttemptCount).toBe(
|
|
589
|
+
// userInfo.FailedLoginAttemptCount,
|
|
590
|
+
// );
|
|
591
|
+
// expect(result.LastFailedLoginAt).toBe(userInfo.LastFailedLoginAt);
|
|
592
|
+
// expect(result.LastPasswordChangedAt).toBe(userInfo.LastPasswordChangedAt);
|
|
593
|
+
// expect(result.NeedToChangePasswordYN).toBe(
|
|
594
|
+
// userInfo.NeedToChangePasswordYN,
|
|
595
|
+
// );
|
|
596
|
+
// expect(result.CreatedById).toBe(userInfo.CreatedById);
|
|
597
|
+
// expect(result.UpdatedById).toBe(userInfo.UpdatedById);
|
|
598
|
+
// });
|
|
599
|
+
|
|
600
|
+
// it('should throw an error if user dont have the privilege to create new user', async () => {
|
|
601
|
+
// jest.spyOn(loginUser, 'checkPrivileges').mockResolvedValueOnce(false);
|
|
602
|
+
|
|
603
|
+
// await expect(
|
|
604
|
+
// LoginUser.create(loginUser, dbTransaction, user),
|
|
605
|
+
// ).rejects.toThrow(ClassError);
|
|
606
|
+
// });
|
|
607
|
+
|
|
608
|
+
// it('should throw an error if user email is missing', async () => {
|
|
609
|
+
// user.Email = undefined;
|
|
610
|
+
|
|
611
|
+
// jest.spyOn(loginUser, 'checkPrivileges').mockResolvedValueOnce(true);
|
|
612
|
+
|
|
613
|
+
// await expect(
|
|
614
|
+
// LoginUser.create(loginUser, dbTransaction, user),
|
|
615
|
+
// ).rejects.toThrow(ClassError);
|
|
616
|
+
// });
|
|
617
|
+
// });
|
|
618
|
+
|
|
582
619
|
describe('incrementFailedLoginAttemptCount', () => {
|
|
583
620
|
afterAll(() => {
|
|
584
621
|
jest.restoreAllMocks();
|
|
@@ -595,17 +632,20 @@ describe('LoginUser', () => {
|
|
|
595
632
|
const dbTransaction = null;
|
|
596
633
|
|
|
597
634
|
// Mock the static methods and properties
|
|
598
|
-
jest
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
if (configKey === 'maxFailedLoginAttempts') {
|
|
602
|
-
return 3;
|
|
603
|
-
}
|
|
604
|
-
if (configKey === 'autoReleaseYN') {
|
|
605
|
-
return 'Y';
|
|
606
|
-
}
|
|
607
|
-
});
|
|
635
|
+
jest
|
|
636
|
+
.spyOn((LoginUser as any)['_Repository'], 'update')
|
|
637
|
+
.mockReturnValueOnce(null);
|
|
608
638
|
|
|
639
|
+
jest
|
|
640
|
+
.spyOn(ComponentConfig, 'getComponentConfigValue')
|
|
641
|
+
.mockImplementation((componentName: string, configKey: string) => {
|
|
642
|
+
if (configKey === 'maxFailedLoginAttempts') {
|
|
643
|
+
return 3;
|
|
644
|
+
}
|
|
645
|
+
if (configKey === 'autoReleaseYN') {
|
|
646
|
+
return 'Y';
|
|
647
|
+
}
|
|
648
|
+
});
|
|
609
649
|
|
|
610
650
|
// Act
|
|
611
651
|
await loginUser['incrementFailedLoginAttemptCount'](dbTransaction);
|
|
@@ -622,7 +662,7 @@ describe('LoginUser', () => {
|
|
|
622
662
|
UserId: loginUser.UserId,
|
|
623
663
|
},
|
|
624
664
|
transaction: dbTransaction,
|
|
625
|
-
}
|
|
665
|
+
},
|
|
626
666
|
);
|
|
627
667
|
});
|
|
628
668
|
|
|
@@ -637,26 +677,31 @@ describe('LoginUser', () => {
|
|
|
637
677
|
const dbTransaction = null;
|
|
638
678
|
|
|
639
679
|
// Mock the static methods and properties
|
|
640
|
-
jest
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
680
|
+
jest
|
|
681
|
+
.spyOn((LoginUser as any)['_Repository'], 'update')
|
|
682
|
+
.mockReturnValueOnce(null);
|
|
683
|
+
|
|
684
|
+
jest
|
|
685
|
+
.spyOn(ComponentConfig, 'getComponentConfigValue')
|
|
686
|
+
.mockImplementationOnce((componentName: string, configKey: string) => {
|
|
687
|
+
if (configKey === 'maxFailedLoginAttempts') {
|
|
688
|
+
return undefined;
|
|
689
|
+
}
|
|
690
|
+
if (configKey === 'autoReleaseYN') {
|
|
691
|
+
return undefined;
|
|
692
|
+
}
|
|
693
|
+
});
|
|
650
694
|
|
|
651
695
|
// Act and Assert
|
|
652
696
|
await expect(
|
|
653
|
-
loginUser['incrementFailedLoginAttemptCount'](dbTransaction)
|
|
697
|
+
loginUser['incrementFailedLoginAttemptCount'](dbTransaction),
|
|
654
698
|
).rejects.toThrow(
|
|
655
699
|
new ClassError(
|
|
656
700
|
'LoginUser',
|
|
657
701
|
'LoginUserErrMsg0X',
|
|
658
702
|
'Missing maxFailedLoginAttempts and or autoReleaseYN. Please set in config file.',
|
|
659
|
-
)
|
|
703
|
+
),
|
|
704
|
+
);
|
|
660
705
|
});
|
|
661
706
|
|
|
662
707
|
it('should lock the user account if the failed login attempts exceed the maximum allowed', async () => {
|
|
@@ -670,16 +715,20 @@ describe('LoginUser', () => {
|
|
|
670
715
|
const dbTransaction = null;
|
|
671
716
|
|
|
672
717
|
// Mock the static methods and properties
|
|
673
|
-
jest
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
718
|
+
jest
|
|
719
|
+
.spyOn((LoginUser as any)['_Repository'], 'update')
|
|
720
|
+
.mockReturnValueOnce(null);
|
|
721
|
+
|
|
722
|
+
jest
|
|
723
|
+
.spyOn(ComponentConfig, 'getComponentConfigValue')
|
|
724
|
+
.mockImplementation((componentName: string, configKey: string) => {
|
|
725
|
+
if (configKey === 'maxFailedLoginAttempts') {
|
|
726
|
+
return 3;
|
|
727
|
+
}
|
|
728
|
+
if (configKey === 'autoReleaseYN') {
|
|
729
|
+
return 'Y';
|
|
730
|
+
}
|
|
731
|
+
});
|
|
683
732
|
|
|
684
733
|
// Act
|
|
685
734
|
try {
|
|
@@ -698,10 +747,12 @@ describe('LoginUser', () => {
|
|
|
698
747
|
UserId: loginUser.UserId,
|
|
699
748
|
},
|
|
700
749
|
transaction: dbTransaction,
|
|
701
|
-
}
|
|
750
|
+
},
|
|
702
751
|
);
|
|
703
752
|
expect(error).toBeInstanceOf(ClassError);
|
|
704
|
-
expect(error.message).toBe(
|
|
753
|
+
expect(error.message).toBe(
|
|
754
|
+
'Your account has been temporarily locked due to too many failed login attempts, please try again later.',
|
|
755
|
+
);
|
|
705
756
|
}
|
|
706
757
|
});
|
|
707
758
|
|
|
@@ -716,16 +767,20 @@ describe('LoginUser', () => {
|
|
|
716
767
|
const dbTransaction = null;
|
|
717
768
|
|
|
718
769
|
// Mock the static methods and properties
|
|
719
|
-
jest
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
770
|
+
jest
|
|
771
|
+
.spyOn((LoginUser as any)['_Repository'], 'update')
|
|
772
|
+
.mockReturnValueOnce(null);
|
|
773
|
+
|
|
774
|
+
jest
|
|
775
|
+
.spyOn(ComponentConfig, 'getComponentConfigValue')
|
|
776
|
+
.mockImplementation((componentName: string, configKey: string) => {
|
|
777
|
+
if (configKey === 'maxFailedLoginAttempts') {
|
|
778
|
+
return 3;
|
|
779
|
+
}
|
|
780
|
+
if (configKey === 'autoReleaseYN') {
|
|
781
|
+
return 'N';
|
|
782
|
+
}
|
|
783
|
+
});
|
|
729
784
|
|
|
730
785
|
// Act
|
|
731
786
|
try {
|
|
@@ -744,10 +799,12 @@ describe('LoginUser', () => {
|
|
|
744
799
|
UserId: loginUser.UserId,
|
|
745
800
|
},
|
|
746
801
|
transaction: dbTransaction,
|
|
747
|
-
}
|
|
802
|
+
},
|
|
748
803
|
);
|
|
749
804
|
expect(error).toBeInstanceOf(ClassError);
|
|
750
|
-
expect(error.message).toBe(
|
|
805
|
+
expect(error.message).toBe(
|
|
806
|
+
'Your account has been locked due to too many failed login attempts, please contact IT Support for instructions on how to unlock your account',
|
|
807
|
+
);
|
|
751
808
|
}
|
|
752
809
|
});
|
|
753
810
|
});
|
|
@@ -764,19 +821,21 @@ describe('LoginUser', () => {
|
|
|
764
821
|
];
|
|
765
822
|
|
|
766
823
|
// Mock the necessary repository methods
|
|
767
|
-
jest
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
824
|
+
jest
|
|
825
|
+
.spyOn(LoginUser as any, 'getInheritedSystemAccess')
|
|
826
|
+
.mockResolvedValueOnce([
|
|
827
|
+
{ SystemCode: 'system1' },
|
|
828
|
+
{ SystemCode: 'system2' },
|
|
829
|
+
]);
|
|
830
|
+
jest
|
|
831
|
+
.spyOn((LoginUser as any)['_UserSystemAccessRepo'], 'findAll')
|
|
832
|
+
.mockResolvedValueOnce([{ SystemCode: 'system3' }]);
|
|
774
833
|
|
|
775
834
|
// Call the method
|
|
776
835
|
const result = await LoginUser['combineSystemAccess'](
|
|
777
836
|
loginUser,
|
|
778
837
|
dbTransaction,
|
|
779
|
-
groups
|
|
838
|
+
groups,
|
|
780
839
|
);
|
|
781
840
|
|
|
782
841
|
// Assert the result
|
|
@@ -798,18 +857,23 @@ describe('LoginUser', () => {
|
|
|
798
857
|
loginUser.ObjectId = '1';
|
|
799
858
|
|
|
800
859
|
// Mock the necessary methods
|
|
801
|
-
const rus = jest
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
860
|
+
const rus = jest
|
|
861
|
+
.spyOn(SessionService.prototype, 'retrieveUserSession')
|
|
862
|
+
.mockResolvedValueOnce({
|
|
863
|
+
systemLogins: [
|
|
864
|
+
{
|
|
865
|
+
id: '1',
|
|
866
|
+
sessionId: 'sessionId',
|
|
867
|
+
code: systemCode,
|
|
868
|
+
privileges: [privilegeName],
|
|
869
|
+
},
|
|
870
|
+
],
|
|
871
|
+
});
|
|
811
872
|
// Act
|
|
812
|
-
const hasPrivilege = await loginUser.checkPrivileges(
|
|
873
|
+
const hasPrivilege = await loginUser.checkPrivileges(
|
|
874
|
+
systemCode,
|
|
875
|
+
privilegeName,
|
|
876
|
+
);
|
|
813
877
|
|
|
814
878
|
// Assert
|
|
815
879
|
expect(hasPrivilege).toBe(true);
|
|
@@ -825,18 +889,23 @@ describe('LoginUser', () => {
|
|
|
825
889
|
loginUser.ObjectId = '1';
|
|
826
890
|
|
|
827
891
|
// Mock the necessary methods
|
|
828
|
-
const rus = jest
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
892
|
+
const rus = jest
|
|
893
|
+
.spyOn(SessionService.prototype, 'retrieveUserSession')
|
|
894
|
+
.mockResolvedValueOnce({
|
|
895
|
+
systemLogins: [
|
|
896
|
+
{
|
|
897
|
+
id: '1',
|
|
898
|
+
sessionId: 'sessionId',
|
|
899
|
+
code: systemCode,
|
|
900
|
+
privileges: [],
|
|
901
|
+
},
|
|
902
|
+
],
|
|
903
|
+
});
|
|
838
904
|
// Act
|
|
839
|
-
const hasPrivilege = await loginUser.checkPrivileges(
|
|
905
|
+
const hasPrivilege = await loginUser.checkPrivileges(
|
|
906
|
+
systemCode,
|
|
907
|
+
privilegeName,
|
|
908
|
+
);
|
|
840
909
|
|
|
841
910
|
// Assert
|
|
842
911
|
expect(hasPrivilege).toBe(false);
|
|
@@ -852,7 +921,9 @@ describe('LoginUser', () => {
|
|
|
852
921
|
loginUser.ObjectId = null;
|
|
853
922
|
|
|
854
923
|
// Act & Assert
|
|
855
|
-
await expect(
|
|
924
|
+
await expect(
|
|
925
|
+
loginUser.checkPrivileges(systemCode, privilegeName),
|
|
926
|
+
).rejects.toThrowError();
|
|
856
927
|
});
|
|
857
928
|
});
|
|
858
929
|
|
|
@@ -886,7 +957,10 @@ describe('LoginUser', () => {
|
|
|
886
957
|
] as any);
|
|
887
958
|
|
|
888
959
|
// Call the getObjectPrivileges method
|
|
889
|
-
const result = await loginUser['getObjectPrivileges'](
|
|
960
|
+
const result = await loginUser['getObjectPrivileges'](
|
|
961
|
+
systemCode,
|
|
962
|
+
dbTransaction,
|
|
963
|
+
);
|
|
890
964
|
|
|
891
965
|
// Assertions
|
|
892
966
|
expect(findAllMock).toHaveBeenCalledTimes(1);
|
|
@@ -914,10 +988,14 @@ describe('LoginUser', () => {
|
|
|
914
988
|
const loginUser = await LoginUser.init(sessionService);
|
|
915
989
|
|
|
916
990
|
// Mock the UserObjectPrivilegeRepo findAll method to throw an error
|
|
917
|
-
jest
|
|
991
|
+
jest
|
|
992
|
+
.spyOn(LoginUser['_UserObjectPrivilegeRepo'], 'findAll')
|
|
993
|
+
.mockRejectedValue(new Error('Database error'));
|
|
918
994
|
|
|
919
995
|
// Call the getObjectPrivileges method and expect it to throw an error
|
|
920
|
-
await expect(
|
|
996
|
+
await expect(
|
|
997
|
+
loginUser['getObjectPrivileges'](systemCode, dbTransaction),
|
|
998
|
+
).rejects.toThrow(Error);
|
|
921
999
|
});
|
|
922
1000
|
});
|
|
923
1001
|
|
|
@@ -930,7 +1008,10 @@ describe('LoginUser', () => {
|
|
|
930
1008
|
const dbTransaction = null;
|
|
931
1009
|
|
|
932
1010
|
// Mock the findAll method of UserPrivilegeRepo
|
|
933
|
-
const findAllMock = jest.spyOn(
|
|
1011
|
+
const findAllMock = jest.spyOn(
|
|
1012
|
+
LoginUser['_UserPrivilegeRepo'],
|
|
1013
|
+
'findAll',
|
|
1014
|
+
);
|
|
934
1015
|
findAllMock.mockResolvedValue([
|
|
935
1016
|
{ Privilege: { Name: 'privilege1' } },
|
|
936
1017
|
{ Privilege: { Name: 'privilege2' } },
|
|
@@ -939,7 +1020,7 @@ describe('LoginUser', () => {
|
|
|
939
1020
|
// Act
|
|
940
1021
|
const privileges = await loginUser['getUserPersonalPrivileges'](
|
|
941
1022
|
systemCode,
|
|
942
|
-
dbTransaction
|
|
1023
|
+
dbTransaction,
|
|
943
1024
|
);
|
|
944
1025
|
|
|
945
1026
|
// Assert
|
|
@@ -969,12 +1050,15 @@ describe('LoginUser', () => {
|
|
|
969
1050
|
const dbTransaction = null;
|
|
970
1051
|
|
|
971
1052
|
// Mock the findAll method of UserPrivilegeRepo to throw an error
|
|
972
|
-
const findAllMock = jest.spyOn(
|
|
1053
|
+
const findAllMock = jest.spyOn(
|
|
1054
|
+
LoginUser['_UserPrivilegeRepo'],
|
|
1055
|
+
'findAll',
|
|
1056
|
+
);
|
|
973
1057
|
findAllMock.mockRejectedValue(new Error('Database error'));
|
|
974
1058
|
|
|
975
1059
|
// Act and Assert
|
|
976
1060
|
await expect(
|
|
977
|
-
loginUser['getUserPersonalPrivileges'](systemCode, dbTransaction)
|
|
1061
|
+
loginUser['getUserPersonalPrivileges'](systemCode, dbTransaction),
|
|
978
1062
|
).rejects.toThrow(Error);
|
|
979
1063
|
|
|
980
1064
|
expect(findAllMock).toHaveBeenCalledTimes(1);
|
|
@@ -1011,12 +1095,24 @@ describe('LoginUser', () => {
|
|
|
1011
1095
|
} as any;
|
|
1012
1096
|
|
|
1013
1097
|
const systemAccess = [
|
|
1014
|
-
{
|
|
1015
|
-
|
|
1098
|
+
{
|
|
1099
|
+
SystemCode: 'system1',
|
|
1100
|
+
GroupCode: 'group1',
|
|
1101
|
+
System: { SystemCode: 'system1' },
|
|
1102
|
+
},
|
|
1103
|
+
{
|
|
1104
|
+
SystemCode: 'system2',
|
|
1105
|
+
GroupCode: 'group1',
|
|
1106
|
+
System: { SystemCode: 'system1' },
|
|
1107
|
+
},
|
|
1016
1108
|
] as any;
|
|
1017
1109
|
|
|
1018
1110
|
const parentSystemAccess = [
|
|
1019
|
-
{
|
|
1111
|
+
{
|
|
1112
|
+
SystemCode: 'system3',
|
|
1113
|
+
GroupCode: 'parentGroup',
|
|
1114
|
+
System: { SystemCode: 'system3' },
|
|
1115
|
+
},
|
|
1020
1116
|
] as any;
|
|
1021
1117
|
|
|
1022
1118
|
// Mock the necessary repository methods
|
|
@@ -1036,7 +1132,7 @@ describe('LoginUser', () => {
|
|
|
1036
1132
|
// Call the method
|
|
1037
1133
|
const result = await LoginUser['getInheritedSystemAccess'](
|
|
1038
1134
|
dbTransaction,
|
|
1039
|
-
group
|
|
1135
|
+
group,
|
|
1040
1136
|
);
|
|
1041
1137
|
|
|
1042
1138
|
console.log(result);
|
|
@@ -1045,21 +1141,24 @@ describe('LoginUser', () => {
|
|
|
1045
1141
|
{
|
|
1046
1142
|
SystemCode: 'system1',
|
|
1047
1143
|
GroupCode: 'group1',
|
|
1048
|
-
System: { SystemCode: 'system1' }
|
|
1144
|
+
System: { SystemCode: 'system1' },
|
|
1049
1145
|
},
|
|
1050
1146
|
{
|
|
1051
1147
|
SystemCode: 'system2',
|
|
1052
1148
|
GroupCode: 'group1',
|
|
1053
|
-
System: { SystemCode: 'system1' }
|
|
1149
|
+
System: { SystemCode: 'system1' },
|
|
1054
1150
|
},
|
|
1055
1151
|
{
|
|
1056
1152
|
SystemCode: 'system3',
|
|
1057
1153
|
GroupCode: 'parentGroup',
|
|
1058
|
-
System: { SystemCode: 'system3' }
|
|
1059
|
-
}
|
|
1154
|
+
System: { SystemCode: 'system3' },
|
|
1155
|
+
},
|
|
1060
1156
|
]);
|
|
1061
|
-
expect(groupFindByPkMock).toHaveBeenCalledWith(
|
|
1157
|
+
expect(groupFindByPkMock).toHaveBeenCalledWith(
|
|
1158
|
+
group.ParentGroupCode,
|
|
1159
|
+
dbTransaction,
|
|
1160
|
+
);
|
|
1062
1161
|
expect(systemAccessFindAllMock).toHaveBeenCalledTimes(2);
|
|
1063
|
-
})
|
|
1162
|
+
});
|
|
1064
1163
|
});
|
|
1065
|
-
});
|
|
1164
|
+
});
|