|
1 | 1 | import { inject, TestBed } from '@angular/core/testing'; |
2 | 2 | import { Router } from '@angular/router'; |
3 | 3 | import { RouterTestingModule } from '@angular/router/testing'; |
4 | | - |
| 4 | +import { of } from 'rxjs'; |
| 5 | +import { skip, take } from 'rxjs/operators'; |
| 6 | +import { FeatureSwitchGroupService } from 'src/app/modules/shared/feature-toggles/switch-group/feature-switch-group.service'; |
| 7 | +import { UserInfoService } from 'src/app/modules/user/services/user-info.service'; |
5 | 8 | import { AzureAdB2CService } from '../../modules/login/services/azure.ad.b2c.service'; |
6 | | -import { AdminGuard } from './admin-guard'; |
| 9 | +import { AdminGuard } from './admin.guard'; |
7 | 10 |
|
8 | 11 | describe('AdminGuard', () => { |
9 | | - |
10 | 12 | let adminGuard: AdminGuard; |
11 | 13 | let azureAdB2CService: AzureAdB2CService; |
12 | | - |
| 14 | + let userInfoService: UserInfoService; |
| 15 | + let featureSwitchGroupService: FeatureSwitchGroupService; |
13 | 16 | const azureAdB2CServiceStub = { |
14 | 17 | isLogin() { |
15 | 18 | return true; |
16 | 19 | }, |
17 | 20 | isAdmin() { |
18 | 21 | return true; |
19 | | - } |
| 22 | + }, |
| 23 | + }; |
| 24 | + |
| 25 | + const userInfoServiceStub = { |
| 26 | + isAdmin: () => of(false), |
| 27 | + }; |
| 28 | + |
| 29 | + const featureSwitchGroupServiceStub = { |
| 30 | + isActivated: () => of(false), |
20 | 31 | }; |
21 | 32 |
|
22 | 33 | beforeEach(() => { |
23 | 34 | TestBed.configureTestingModule({ |
24 | 35 | imports: [RouterTestingModule], |
25 | 36 | providers: [ |
26 | | - { providers: AzureAdB2CService, useValue: azureAdB2CServiceStub }, |
27 | | - ] |
| 37 | + { provide: AzureAdB2CService, useValue: azureAdB2CServiceStub }, |
| 38 | + { provide: UserInfoService, useValue: userInfoServiceStub }, |
| 39 | + { provide: FeatureSwitchGroupService, useValue: featureSwitchGroupServiceStub }, |
| 40 | + ], |
28 | 41 | }); |
29 | 42 | adminGuard = TestBed.inject(AdminGuard); |
30 | 43 | azureAdB2CService = TestBed.inject(AzureAdB2CService); |
| 44 | + userInfoService = TestBed.inject(UserInfoService); |
| 45 | + featureSwitchGroupService = TestBed.inject(FeatureSwitchGroupService); |
31 | 46 | }); |
32 | 47 |
|
33 | 48 | it('should be created', () => { |
34 | 49 | expect(adminGuard).toBeTruthy(); |
35 | 50 | }); |
36 | 51 |
|
37 | | - it('can activate the route when user is logged-in', () => { |
38 | | - spyOn(azureAdB2CService, 'isAdmin').and.returnValue(true); |
| 52 | + const roleParams = [{ bool: false }, { bool: true }]; |
| 53 | + roleParams.map((param) => { |
| 54 | + it(`isAdminBasedInRole return ${param.bool}`, () => { |
| 55 | + spyOn(azureAdB2CService, 'isAdmin').and.returnValue(param.bool); |
| 56 | + |
| 57 | + adminGuard.isAdminBasedInRole().subscribe((enabled) => { |
| 58 | + expect(azureAdB2CService.isAdmin).toHaveBeenCalled(); |
| 59 | + expect(enabled).toBe(param.bool); |
| 60 | + }); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + const groupParams = [{ bool: false }, { bool: true }]; |
| 65 | + groupParams.map((param) => { |
| 66 | + it(`isAdminBasedInGroup return ${param.bool}`, () => { |
| 67 | + spyOn(userInfoService, 'isAdmin').and.returnValue(of(param.bool)); |
| 68 | + |
| 69 | + adminGuard.isAdminBasedInGroup().subscribe((enabled) => { |
| 70 | + expect(userInfoService.isAdmin).toHaveBeenCalled(); |
| 71 | + expect(enabled).toBe(param.bool); |
| 72 | + }); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + const switchToggleParams = [ |
| 77 | + { switchGroup: false, chosen: 'isAdminBasedInRole', isAdmin: true }, |
| 78 | + { switchGroup: true, chosen: 'isAdminBasedInGroup', isAdmin: false }, |
| 79 | + ]; |
| 80 | + switchToggleParams.map((param) => { |
| 81 | + it(`on switchGroup ${param.switchGroup}, ${param.chosen} should be chosen`, () => { |
| 82 | + const switchGroup$ = of(param.switchGroup); |
| 83 | + |
| 84 | + spyOn(featureSwitchGroupService, 'isActivated').and.returnValue(switchGroup$); |
| 85 | + |
| 86 | + const canActivate = adminGuard.canActivate(); |
39 | 87 |
|
40 | | - const canActivate = adminGuard.canActivate(); |
| 88 | + featureSwitchGroupService.isActivated().pipe(take(1)); |
41 | 89 |
|
42 | | - expect(azureAdB2CService.isAdmin).toHaveBeenCalled(); |
43 | | - expect(canActivate).toEqual(true); |
| 90 | + canActivate.subscribe((enabled) => { |
| 91 | + expect(featureSwitchGroupService.isActivated).toHaveBeenCalled(); |
| 92 | + expect(enabled).toBe(param.isAdmin); |
| 93 | + }); |
| 94 | + }); |
44 | 95 | }); |
45 | 96 |
|
46 | | - it('can not active the route and is redirected to login if user is not logged-in', inject([Router], (router: Router) => { |
47 | | - spyOn(azureAdB2CService, 'isAdmin').and.returnValue(false); |
48 | | - spyOn(router, 'navigate').and.stub(); |
| 97 | + const navigateParams = [ |
| 98 | + { switchGroup: false, chosen: 'activate the route', isAdmin: true }, |
| 99 | + { switchGroup: false, chosen: 'redirect to /login', isAdmin: false }, |
| 100 | + { switchGroup: true, chosen: 'activate the route', isAdmin: true }, |
| 101 | + { switchGroup: true, chosen: 'redirect to /login', isAdmin: false }, |
| 102 | + ]; |
| 103 | + navigateParams.map((param) => { |
| 104 | + it(`on isAdmin: ${param.isAdmin} with toggleSwitch: ${param.switchGroup}, should ${param.chosen} `, inject( |
| 105 | + [Router], |
| 106 | + (router: Router) => { |
| 107 | + const switchGroup$ = of(param.switchGroup); |
| 108 | + const isAdmin$ = of(param.isAdmin); |
49 | 109 |
|
50 | | - const canActivate = adminGuard.canActivate(); |
| 110 | + spyOn(featureSwitchGroupService, 'isActivated').and.returnValue(switchGroup$); |
| 111 | + spyOn(adminGuard, 'isAdminBasedInRole').and.returnValue(isAdmin$); |
| 112 | + spyOn(adminGuard, 'isAdminBasedInGroup').and.returnValue(isAdmin$); |
| 113 | + spyOn(router, 'navigate').and.stub(); |
51 | 114 |
|
52 | | - expect(azureAdB2CService.isAdmin).toHaveBeenCalled(); |
53 | | - expect(canActivate).toEqual(false); |
54 | | - expect(router.navigate).toHaveBeenCalledWith(['login']); |
55 | | - })); |
| 115 | + const canActivate = adminGuard.canActivate(); |
56 | 116 |
|
| 117 | + canActivate.subscribe((enabled) => { |
| 118 | + expect(featureSwitchGroupService.isActivated).toHaveBeenCalled(); |
| 119 | + if (!enabled) { |
| 120 | + expect(router.navigate).toHaveBeenCalledWith(['login']); |
| 121 | + } else { |
| 122 | + expect(router.navigate).not.toHaveBeenCalled(); |
| 123 | + expect(enabled).toBeTrue(); |
| 124 | + } |
| 125 | + }); |
| 126 | + } |
| 127 | + )); |
| 128 | + }); |
57 | 129 | }); |
0 commit comments