@tomei/sso 0.3.4 → 0.4.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.
@@ -1,11 +1,16 @@
1
1
  import { IUserSession } from '../../../../src/interfaces/user-session.interface';
2
- import { LoginHistoryRepository, SessionService, SystemAccessRepository, SystemRepository } from '../../../../src';
2
+ import {
3
+ LoginHistoryRepository,
4
+ SessionService,
5
+ SystemAccessRepository,
6
+ SystemRepository,
7
+ } from '../../../../src';
3
8
  import { LoginUser } from '../../../../src/components/login-user/login-user';
4
9
  import { UserRepository } from '../../../../src/components/login-user/user.repository';
5
10
  import { PasswordHashService } from '../../../../src/components/password-hash/password-hash.service';
6
11
  import { MailService } from '../../../../src/mail/mail.service';
7
12
  describe('login-user', () => {
8
- const user = {
13
+ let user = {
9
14
  id: 755,
10
15
  email: 'ezcash+florence@tomei.com.my',
11
16
  password:
@@ -54,13 +59,12 @@ describe('login-user', () => {
54
59
  updatedAt: new Date('2022-04-18T04:07:40.000Z'),
55
60
  systemPrivilege: {
56
61
  privilegeId: 'ckymxuh8t000137t011w89zgk',
57
- systemId: 11,
58
- code: 'Own',
62
+ systemId: 175,
63
+ code: 'Terminate Data',
59
64
  module: null,
60
- description:
61
- 'Allows the user to only view consignments belonging to themselves',
62
- createdAt: new Date('2022-01-20T12:16:26.000Z'),
63
- updatedAt: new Date('2022-01-20T12:16:26.000Z'),
65
+ description: 'Allows the user to terminate a loan.',
66
+ createdAt: new Date(),
67
+ updatedAt: new Date(),
64
68
  },
65
69
  },
66
70
  ],
@@ -131,18 +135,294 @@ describe('login-user', () => {
131
135
  ],
132
136
  };
133
137
 
138
+ let ezcft = {
139
+ groupCode: 'EZCFT',
140
+ groupDescription: 'EZCASH FINANCE TEAM',
141
+ status: 'Active',
142
+ personInCharge: null,
143
+ fullPath: null,
144
+ parentGroupCode: 'EZCCS',
145
+ groupLevel: 2,
146
+ groupType: null,
147
+ allowInheritFromParentYN: 'Y',
148
+ createdById: '61',
149
+ createdAt: new Date(),
150
+ updatedById: '61',
151
+ updatedAt: new Date(),
152
+ departmentId: 110,
153
+ groupSystemPrivileges: [],
154
+ groupRolePrivileges: [
155
+ {
156
+ groupCode: 'EZCFT',
157
+ roleId: 'clcogtmco00013unyc4ngcnza',
158
+ privilegeId: 'clcogtntx00023unyg98a0ah0',
159
+ createdById: 74,
160
+ createdAt: new Date(),
161
+ systemId: 175,
162
+ systemPrivilege: {
163
+ privilegeId: 'clcogtntx00023unyg98a0ah0',
164
+ systemId: 175,
165
+ code: 'Terminate',
166
+ module: null,
167
+ description: 'Allows the user to terminate a loan.',
168
+ createdAt: new Date(),
169
+ updatedAt: new Date(),
170
+ },
171
+ },
172
+ {
173
+ groupCode: 'EZCFT',
174
+ roleId: 'clcogtmco00013unyc4ngcnza',
175
+ privilegeId: 'clcogtnys00043wp21ejm068j',
176
+ createdById: 74,
177
+ createdAt: new Date(),
178
+ systemId: 175,
179
+ systemPrivilege: {
180
+ privilegeId: 'clcogtnys00043wp21ejm068j',
181
+ systemId: 175,
182
+ code: 'Download - Agreement',
183
+ module: null,
184
+ description: 'Allows the user to download the agreement.',
185
+ createdAt: new Date(),
186
+ updatedAt: new Date(),
187
+ },
188
+ },
189
+ {
190
+ groupCode: 'EZCFT',
191
+ roleId: 'clcogtmco00013unyc4ngcnza',
192
+ privilegeId: 'clcogtoa100063wp2djq10roz',
193
+ createdById: 74,
194
+ createdAt: new Date(),
195
+ systemId: 175,
196
+ systemPrivilege: {
197
+ privilegeId: 'clcogtoa100063wp2djq10roz',
198
+ systemId: 175,
199
+ code: 'Disbursement - Confirmation',
200
+ module: null,
201
+ description:
202
+ 'Allows the user to confirm the disbursement of the loan.',
203
+ createdAt: new Date(),
204
+ updatedAt: new Date(),
205
+ },
206
+ },
207
+ {
208
+ groupCode: 'EZCFT',
209
+ roleId: 'clcogtmco00013unyc4ngcnza',
210
+ privilegeId: 'clcogtob000053uny52atenxa',
211
+ createdById: 74,
212
+ createdAt: new Date(),
213
+ systemId: 175,
214
+ systemPrivilege: {
215
+ privilegeId: 'clcogtob000053uny52atenxa',
216
+ systemId: 175,
217
+ code: 'Invoice Payment Received',
218
+ module: null,
219
+ description:
220
+ 'Allows the user to confirm payment receipt of an invoice.',
221
+ createdAt: new Date(),
222
+ updatedAt: new Date(),
223
+ },
224
+ },
225
+ ],
226
+ };
227
+
228
+ const ezccs = {
229
+ groupCode: 'EZCCS',
230
+ groupDescription: 'EZCASH COMPANY SIGNER',
231
+ status: 'Active',
232
+ personInCharge: null,
233
+ fullPath: null,
234
+ parentGroupCode: 'EZCAT',
235
+ groupLevel: 1,
236
+ groupType: null,
237
+ allowInheritFromParentYN: 'Y',
238
+ createdById: '61',
239
+ createdAt: new Date(),
240
+ updatedById: '61',
241
+ updatedAt: new Date(),
242
+ departmentId: 110,
243
+ groupSystemPrivileges: [],
244
+ groupRolePrivileges: [
245
+ {
246
+ groupCode: 'EZCCS',
247
+ roleId: 'cl34552or00002vlubgs11tc9',
248
+ privilegeId: 'cldu3gd7t000g3spe55jmax51',
249
+ createdById: 700,
250
+ createdAt: new Date(),
251
+ systemId: 42,
252
+ systemPrivilege: {
253
+ privilegeId: 'cldu3gd7t000g3spe55jmax51',
254
+ systemId: 42,
255
+ code: 'View Customer',
256
+ module: null,
257
+ description: 'Allow to view customer details',
258
+ createdAt: new Date(),
259
+ updatedAt: new Date(),
260
+ },
261
+ },
262
+ {
263
+ groupCode: 'EZCCS',
264
+ roleId: 'clcogtm8000003unybenw69nl',
265
+ privilegeId: 'clcogtnsn00023wp21z5n28az',
266
+ createdById: 700,
267
+ createdAt: new Date(),
268
+ systemId: 175,
269
+ systemPrivilege: {
270
+ privilegeId: 'clcogtnsn00023wp21z5n28az',
271
+ systemId: 175,
272
+ code: 'Signer - Company',
273
+ module: null,
274
+ description:
275
+ 'Allows the user to sign loan agreements on behalf of the company.',
276
+ createdAt: new Date(),
277
+ updatedAt: new Date(),
278
+ },
279
+ },
280
+ {
281
+ groupCode: 'EZCCS',
282
+ roleId: 'clcogtm8000003unybenw69nl',
283
+ privilegeId: 'clcogtnys00043wp21ejm068j',
284
+ createdById: 700,
285
+ createdAt: new Date(),
286
+ systemId: 175,
287
+ systemPrivilege: {
288
+ privilegeId: 'clcogtnys00043wp21ejm068j',
289
+ systemId: 175,
290
+ code: 'Download - Agreement',
291
+ module: null,
292
+ description: 'Allows the user to download the agreement.',
293
+ createdAt: new Date(),
294
+ updatedAt: new Date(),
295
+ },
296
+ },
297
+ {
298
+ groupCode: 'EZCCS',
299
+ roleId: 'clcogtm8000003unybenw69nl',
300
+ privilegeId: 'clcogtqb400074dn47ld564nt',
301
+ createdById: 700,
302
+ createdAt: new Date(),
303
+ systemId: 175,
304
+ systemPrivilege: {
305
+ privilegeId: 'clds7oql500003spe3hubafn6',
306
+ systemId: 175,
307
+ code: 'Loan - Retrieve List',
308
+ module: null,
309
+ description:
310
+ 'Allows the user to retrieve the loan listing/search for all loans within the system.',
311
+ createdAt: new Date(),
312
+ updatedAt: new Date(),
313
+ },
314
+ },
315
+ ],
316
+ };
317
+
318
+ const ezcat = {
319
+ groupCode: 'EZCAT',
320
+ groupDescription: 'EZC ATTESTATOR',
321
+ status: 'Active',
322
+ personInCharge: null,
323
+ fullPath: null,
324
+ parentGroupCode: null,
325
+ groupLevel: 0,
326
+ groupType: null,
327
+ allowInheritFromParentYN: 'Y',
328
+ createdById: '61',
329
+ createdAt: new Date(),
330
+ updatedById: '61',
331
+ updatedAt: new Date(),
332
+ departmentId: 110,
333
+ groupSystemPrivileges: [],
334
+ groupRolePrivileges: [
335
+ {
336
+ groupCode: 'EZCAT',
337
+ roleId: 'cl34552or00002vlubgs11tc9',
338
+ privilegeId: 'cldu3gd7t000g3spe55jmax51',
339
+ createdById: 700,
340
+ createdAt: new Date(),
341
+ systemId: 42,
342
+ systemPrivilege: {
343
+ privilegeId: 'cldu3gd7t000g3spe55jmax51',
344
+ systemId: 42,
345
+ code: 'View Customer',
346
+ module: null,
347
+ description: 'Allow to view customer details',
348
+ createdAt: new Date(),
349
+ updatedAt: new Date(),
350
+ },
351
+ },
352
+ {
353
+ groupCode: 'EZCAT',
354
+ roleId: 'clcogtmyx00014dn4798pel3n',
355
+ privilegeId: 'clcogtnuf00033wp27vwb1vwl',
356
+ createdById: 74,
357
+ createdAt: new Date(),
358
+ systemId: 175,
359
+ systemPrivilege: {
360
+ privilegeId: 'clcogtnuf00033wp27vwb1vwl',
361
+ systemId: 175,
362
+ code: 'Signing Session - Schedule',
363
+ module: null,
364
+ description:
365
+ 'Allows the user to schedule or reschedule the signing session.',
366
+ createdAt: new Date(),
367
+ updatedAt: new Date(),
368
+ },
369
+ },
370
+ {
371
+ groupCode: 'EZCAT',
372
+ roleId: 'clcogtmyx00014dn4798pel3n',
373
+ privilegeId: 'clcogto8t00043unyd60c8j6r',
374
+ createdById: 74,
375
+ createdAt: new Date(),
376
+ systemId: 175,
377
+ systemPrivilege: {
378
+ privilegeId: 'clcogto8t00043unyd60c8j6r',
379
+ systemId: 175,
380
+ code: 'Signing Session - No Show',
381
+ module: null,
382
+ description:
383
+ 'Allows the user to mark the signing session as "no show".',
384
+ createdAt: new Date(),
385
+ updatedAt: new Date(),
386
+ },
387
+ },
388
+ {
389
+ groupCode: 'EZCAT',
390
+ roleId: 'clcogtmyx00014dn4798pel3n',
391
+ privilegeId: 'clcogtobc00073wp21lanh2qw',
392
+ createdById: 74,
393
+ createdAt: new Date(),
394
+ systemId: 175,
395
+ systemPrivilege: {
396
+ privilegeId: 'clcogtobc00073wp21lanh2qw',
397
+ systemId: 175,
398
+ code: 'Setup Session',
399
+ module: null,
400
+ description:
401
+ 'Allows the users to set up the predefined slots for the live attestation sessions.',
402
+ createdAt: new Date(),
403
+ updatedAt: new Date(),
404
+ },
405
+ },
406
+ ],
407
+ };
408
+
409
+ let userUserGroups = [
410
+ {
411
+ userId: 755,
412
+ groupCode: 'EZCFT',
413
+ systemId: 175,
414
+ createdAt: new Date(),
415
+ updatedAt: new Date(),
416
+ userGroup: ezcft,
417
+ },
418
+ ];
419
+
134
420
  jest.mock('crypto', () => {
135
421
  return {
136
422
  randomUUID: jest.fn().mockReturnValue('ckymxuh8t000137t011w89zgk'),
137
423
  };
138
424
  });
139
425
 
140
- jest
141
- .spyOn(UserRepository.prototype, 'findOne')
142
- .mockImplementation(async () => {
143
- return user;
144
- });
145
-
146
426
  jest
147
427
  .spyOn(PasswordHashService.prototype, 'verify')
148
428
  .mockImplementation(async (password) => {
@@ -154,7 +434,7 @@ describe('login-user', () => {
154
434
  });
155
435
 
156
436
  jest
157
- .spyOn(SystemRepository .prototype, 'findOne')
437
+ .spyOn(SystemRepository.prototype, 'findOne')
158
438
  .mockImplementation(async () => {
159
439
  return system;
160
440
  });
@@ -163,61 +443,201 @@ describe('login-user', () => {
163
443
  .spyOn(MailService.prototype, 'sendNewLoginAlertEmail')
164
444
  .mockImplementation(async () => {});
165
445
 
166
- jest
167
- .spyOn(LoginUser.prototype, 'getPrivileges')
168
- .mockImplementation(async () => {
169
- return session.systemLogins[0].privileges;
170
- });
446
+ const getUserGroupFromDBMock = jest.spyOn(
447
+ LoginUser.prototype as any,
448
+ 'getUserGroupFromDB',
449
+ );
171
450
 
172
- jest
173
- .spyOn(SystemAccessRepository.prototype, 'findOne')
174
- .mockImplementation(async () => {
175
- return systemAccess;
176
- });
451
+ const getUserUserGroupFromDBMock = jest.spyOn(
452
+ LoginUser.prototype as any,
453
+ 'getUserUserGroupFromDB',
454
+ );
177
455
 
178
- jest
179
- .spyOn(LoginHistoryRepository.prototype, 'findAll')
180
- .mockImplementation(async () => {
181
- const data = [];
182
- return data;
183
- })
184
-
185
- it('should return login-user service when instansiated', async () => {
186
- const loginUser = await LoginUser.init();
187
- expect(loginUser).toBeDefined();
456
+ const userMock = jest.spyOn(UserRepository.prototype, 'findOne');
457
+
458
+ userMock.mockImplementation(async () => {
459
+ return user;
460
+ });
461
+
462
+ getUserGroupFromDBMock.mockImplementation(async (groupCode) => {
463
+ if (groupCode === 'EZCFT') {
464
+ return ezcft;
465
+ } else if (groupCode === 'EZCCS') {
466
+ return ezccs;
467
+ } else if (groupCode === 'EZCAT') {
468
+ return ezcat;
469
+ }
188
470
  });
189
471
 
190
- it('should able to do login process when no session is already available', async () => {
191
- const loginUser = await LoginUser.init('755');
192
- const result = await loginUser.login(
193
- 'EZC',
194
- 'ezcash+florence@tomei.com.my',
195
- 'Abcd@1234',
196
- '1.1.1.1',
197
- );
198
- expect(result).toEqual('ckymxuh8t000137t011w89zgk');
472
+ getUserUserGroupFromDBMock.mockImplementation(async () => {
473
+ return userUserGroups;
199
474
  });
200
475
 
201
- it('should able to do login process when session is already available', async () => {
476
+ describe('login', () => {
202
477
  jest
203
- .spyOn(SessionService.prototype, 'retrieveUserSession')
204
- .mockImplementationOnce(async (userId: string) => {
205
- if (userId === '755') {
206
- return session;
207
- } else {
208
- return {
209
- systemLogins: [],
210
- };
211
- }
478
+ .spyOn(SystemAccessRepository.prototype, 'findOne')
479
+ .mockImplementation(async () => {
480
+ return systemAccess;
212
481
  });
213
482
 
214
- const loginUser = await LoginUser.init('755');
215
- const result = await loginUser.login(
216
- 'EZC',
217
- 'ezcash+florence@tomei.com.my',
218
- 'Abcd@1234',
219
- '1.1.1.1',
220
- );
221
- expect(result).toEqual('ckymxuh8t000137t011w89zgk');
483
+ jest
484
+ .spyOn(LoginHistoryRepository.prototype, 'findAll')
485
+ .mockImplementation(async () => {
486
+ const data = [];
487
+ return data;
488
+ });
489
+
490
+ it('should return login-user service when instansiated', async () => {
491
+ const loginUser = await LoginUser.init();
492
+ expect(loginUser).toBeDefined();
493
+ });
494
+
495
+ it('should able to do login process when no session is already available', async () => {
496
+ const loginUser = await LoginUser.init('755');
497
+ const result = await loginUser.login(
498
+ 'EZC',
499
+ 'ezcash+florence@tomei.com.my',
500
+ 'Abcd@1234',
501
+ '1.1.1.1',
502
+ );
503
+ expect(result).toEqual('ckymxuh8t000137t011w89zgk');
504
+ });
505
+
506
+ it('should able to do login process when session is already available', async () => {
507
+ jest
508
+ .spyOn(SessionService.prototype, 'retrieveUserSession')
509
+ .mockImplementationOnce(async (userId: string) => {
510
+ if (userId === '755') {
511
+ return session;
512
+ } else {
513
+ return {
514
+ systemLogins: [],
515
+ };
516
+ }
517
+ });
518
+
519
+ const loginUser = await LoginUser.init('755');
520
+ const result = await loginUser.login(
521
+ 'EZC',
522
+ 'ezcash+florence@tomei.com.my',
523
+ 'Abcd@1234',
524
+ '1.1.1.1',
525
+ );
526
+ expect(result).toEqual('ckymxuh8t000137t011w89zgk');
527
+ });
528
+ });
529
+
530
+ describe('getPrivileges', () => {
531
+ it('should be able to login when user only have one user group with level 1 or higher', async () => {
532
+ const loginUser = await LoginUser.init('755');
533
+ const result = await loginUser['getPrivileges']('EZC');
534
+ expect(getUserGroupFromDBMock).toBeCalledTimes(2);
535
+ expect(result).toEqual([
536
+ 'View Customer',
537
+ 'Signer - Company',
538
+ 'Download - Agreement',
539
+ 'Loan - Retrieve List',
540
+ 'Signing Session - Schedule',
541
+ 'Signing Session - No Show',
542
+ 'Setup Session',
543
+ 'Terminate',
544
+ 'Disbursement - Confirmation',
545
+ 'Invoice Payment Received',
546
+ "Terminate Data",
547
+ ]);
548
+ });
549
+
550
+ it('should be able to login when user only have multiple user user group', async () => {
551
+ const tempUserUserGroups = userUserGroups;
552
+ userUserGroups.push(userUserGroups[0]);
553
+
554
+ const loginUser = await LoginUser.init('755');
555
+ const result = await loginUser['getPrivileges']('EZC');
556
+ expect(getUserUserGroupFromDBMock).toBeCalledTimes(1);
557
+ expect(getUserGroupFromDBMock).toBeCalledTimes(4);
558
+ expect(result).toEqual([
559
+ 'View Customer',
560
+ 'Signer - Company',
561
+ 'Download - Agreement',
562
+ 'Loan - Retrieve List',
563
+ 'Signing Session - Schedule',
564
+ 'Signing Session - No Show',
565
+ 'Setup Session',
566
+ 'Terminate',
567
+ 'Disbursement - Confirmation',
568
+ 'Invoice Payment Received',
569
+ "Terminate Data",
570
+ ]);
571
+
572
+ userUserGroups = tempUserUserGroups;
573
+ });
574
+
575
+ it('should be able to login when user only have user privilege', async () => {
576
+ const tempUserUserGroup = userUserGroups;
577
+ userUserGroups = [];
578
+
579
+ const loginUser = await LoginUser.init('755');
580
+ const result = await loginUser['getPrivileges']('EZC');
581
+ expect(getUserGroupFromDBMock).toBeCalledTimes(0);
582
+ expect(result).toEqual(['Terminate Data']);
583
+
584
+ userUserGroups = tempUserUserGroup;
585
+ });
586
+
587
+ it('should be able to login when user only have one user group with no parent', async () => {
588
+ const tempEzcft = ezcft;
589
+ ezcft.groupLevel = 0;
590
+ const tempUser = user;
591
+ user.userSystemPrivileges = [];
592
+
593
+ const loginUser = await LoginUser.init('755');
594
+ const result = await loginUser['getPrivileges']('EZC');
595
+ expect(getUserGroupFromDBMock).toBeCalledTimes(0);
596
+ expect(result).toEqual([
597
+ 'Terminate',
598
+ 'Download - Agreement',
599
+ 'Disbursement - Confirmation',
600
+ 'Invoice Payment Received',
601
+ ]);
602
+
603
+ ezcft = tempEzcft;
604
+ user = tempUser;
605
+ });
606
+
607
+ it('should be able to login when user only have one user group that cannot inherit privilege', async () => {
608
+ const tempEzcft = ezcft;
609
+ ezcft.groupLevel = 1;
610
+ ezcft.allowInheritFromParentYN = 'N';
611
+ const tempUser = user;
612
+ user.userSystemPrivileges = [];
613
+
614
+ const loginUser = await LoginUser.init('755');
615
+ const result = await loginUser['getPrivileges']('EZC');
616
+ expect(getUserGroupFromDBMock).toBeCalledTimes(0);
617
+ expect(result).toEqual([
618
+ 'Terminate',
619
+ 'Download - Agreement',
620
+ 'Disbursement - Confirmation',
621
+ 'Invoice Payment Received',
622
+ ]);
623
+
624
+ ezcft = tempEzcft;
625
+ user = tempUser;
626
+ });
627
+
628
+ it('should be able to login when user dont have user privilage or user usergroup', async () => {
629
+ const tempUser = user;
630
+ user.userSystemPrivileges = [];
631
+ const tempUserUserGroup = userUserGroups;
632
+ userUserGroups = [];
633
+
634
+ const loginUser = await LoginUser.init('755');
635
+ const result = await loginUser['getPrivileges']('EZC');
636
+ expect(getUserGroupFromDBMock).toBeCalledTimes(0);
637
+ expect(result).toEqual([]);
638
+
639
+ userUserGroups = tempUserUserGroup;
640
+ user = tempUser;
641
+ });
222
642
  });
223
643
  });