-
Notifications
You must be signed in to change notification settings - Fork 1
feat: TT-190 user user-info service to check if user is admin in UI #658
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
scastillo-jp
merged 2 commits into
master
from
TT-189-use-user-info-service-to-check-if-user-is-admin-in-UI
Apr 7, 2021
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
feat: TT-190 add store persistence & switch-group FT to preserve old …
…logic
- Loading branch information
commit 21c4d93af3034cdd7d597de3900d5deea6526490
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,57 +1,128 @@ | ||
| import { inject, TestBed } from '@angular/core/testing'; | ||
| import { Router } from '@angular/router'; | ||
| import { RouterTestingModule } from '@angular/router/testing'; | ||
|
|
||
| import { of } from 'rxjs'; | ||
| import { skip, take } from 'rxjs/operators'; | ||
| import { FeatureSwitchGroupService } from 'src/app/modules/shared/feature-toggles/switch-group/feature-switch-group.service'; | ||
| import { UserInfoService } from 'src/app/modules/user/services/user-info.service'; | ||
| import { AzureAdB2CService } from '../../modules/login/services/azure.ad.b2c.service'; | ||
| import { AdminGuard } from './admin-guard'; | ||
| import { AdminGuard } from './admin.guard'; | ||
|
|
||
| describe('AdminGuard', () => { | ||
|
|
||
| let adminGuard: AdminGuard; | ||
| let azureAdB2CService: AzureAdB2CService; | ||
|
|
||
| let userInfoService: UserInfoService; | ||
| let featureSwitchGroupService: FeatureSwitchGroupService; | ||
| const azureAdB2CServiceStub = { | ||
| isLogin() { | ||
| return true; | ||
| }, | ||
| isAdmin() { | ||
| return true; | ||
| } | ||
| }, | ||
| }; | ||
|
|
||
| const userInfoServiceStub = { | ||
| isAdmin: () => of(false), | ||
| }; | ||
|
|
||
| const featureSwitchGroupServiceStub = { | ||
| isActivated: () => of(false), | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| TestBed.configureTestingModule({ | ||
| imports: [RouterTestingModule], | ||
| providers: [ | ||
| { providers: AzureAdB2CService, useValue: azureAdB2CServiceStub }, | ||
| ] | ||
| { provide: AzureAdB2CService, useValue: azureAdB2CServiceStub }, | ||
| { provide: UserInfoService, useValue: userInfoServiceStub }, | ||
| { provide: FeatureSwitchGroupService, useValue: featureSwitchGroupServiceStub }, | ||
| ], | ||
| }); | ||
| adminGuard = TestBed.inject(AdminGuard); | ||
| azureAdB2CService = TestBed.inject(AzureAdB2CService); | ||
| userInfoService = TestBed.inject(UserInfoService); | ||
| featureSwitchGroupService = TestBed.inject(FeatureSwitchGroupService); | ||
| }); | ||
|
|
||
| it('should be created', () => { | ||
| expect(adminGuard).toBeTruthy(); | ||
| }); | ||
|
|
||
| it('can activate the route when user is logged-in', () => { | ||
| spyOn(azureAdB2CService, 'isAdmin').and.returnValue(true); | ||
| const roleParams = [{ bool: false }, { bool: true }]; | ||
| roleParams.map((param) => { | ||
| it(`isAdminBasedInRole return ${param.bool}`, () => { | ||
| spyOn(azureAdB2CService, 'isAdmin').and.returnValue(param.bool); | ||
|
|
||
| adminGuard.isAdminBasedInRole().subscribe((enabled) => { | ||
| expect(azureAdB2CService.isAdmin).toHaveBeenCalled(); | ||
| expect(enabled).toBe(param.bool); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| const groupParams = [{ bool: false }, { bool: true }]; | ||
| groupParams.map((param) => { | ||
| it(`isAdminBasedInGroup return ${param.bool}`, () => { | ||
| spyOn(userInfoService, 'isAdmin').and.returnValue(of(param.bool)); | ||
|
|
||
| adminGuard.isAdminBasedInGroup().subscribe((enabled) => { | ||
| expect(userInfoService.isAdmin).toHaveBeenCalled(); | ||
| expect(enabled).toBe(param.bool); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| const switchToggleParams = [ | ||
| { switchGroup: false, chosen: 'isAdminBasedInRole', isAdmin: true }, | ||
| { switchGroup: true, chosen: 'isAdminBasedInGroup', isAdmin: false }, | ||
| ]; | ||
| switchToggleParams.map((param) => { | ||
| it(`on switchGroup ${param.switchGroup}, ${param.chosen} should be chosen`, () => { | ||
| const switchGroup$ = of(param.switchGroup); | ||
|
|
||
| spyOn(featureSwitchGroupService, 'isActivated').and.returnValue(switchGroup$); | ||
|
|
||
| const canActivate = adminGuard.canActivate(); | ||
|
|
||
| const canActivate = adminGuard.canActivate(); | ||
| featureSwitchGroupService.isActivated().pipe(take(1)); | ||
|
|
||
| expect(azureAdB2CService.isAdmin).toHaveBeenCalled(); | ||
| expect(canActivate).toEqual(true); | ||
| canActivate.subscribe((enabled) => { | ||
| expect(featureSwitchGroupService.isActivated).toHaveBeenCalled(); | ||
| expect(enabled).toBe(param.isAdmin); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| it('can not active the route and is redirected to login if user is not logged-in', inject([Router], (router: Router) => { | ||
| spyOn(azureAdB2CService, 'isAdmin').and.returnValue(false); | ||
| spyOn(router, 'navigate').and.stub(); | ||
| const navigateParams = [ | ||
| { switchGroup: false, chosen: 'activate the route', isAdmin: true }, | ||
| { switchGroup: false, chosen: 'redirect to /login', isAdmin: false }, | ||
| { switchGroup: true, chosen: 'activate the route', isAdmin: true }, | ||
| { switchGroup: true, chosen: 'redirect to /login', isAdmin: false }, | ||
| ]; | ||
| navigateParams.map((param) => { | ||
| it(`on isAdmin: ${param.isAdmin} with toggleSwitch: ${param.switchGroup}, should ${param.chosen} `, inject( | ||
| [Router], | ||
| (router: Router) => { | ||
| const switchGroup$ = of(param.switchGroup); | ||
| const isAdmin$ = of(param.isAdmin); | ||
|
|
||
| const canActivate = adminGuard.canActivate(); | ||
| spyOn(featureSwitchGroupService, 'isActivated').and.returnValue(switchGroup$); | ||
| spyOn(adminGuard, 'isAdminBasedInRole').and.returnValue(isAdmin$); | ||
| spyOn(adminGuard, 'isAdminBasedInGroup').and.returnValue(isAdmin$); | ||
| spyOn(router, 'navigate').and.stub(); | ||
|
|
||
| expect(azureAdB2CService.isAdmin).toHaveBeenCalled(); | ||
| expect(canActivate).toEqual(false); | ||
| expect(router.navigate).toHaveBeenCalledWith(['login']); | ||
| })); | ||
| const canActivate = adminGuard.canActivate(); | ||
|
|
||
| canActivate.subscribe((enabled) => { | ||
| expect(featureSwitchGroupService.isActivated).toHaveBeenCalled(); | ||
| if (!enabled) { | ||
| expect(router.navigate).toHaveBeenCalledWith(['login']); | ||
| } else { | ||
| expect(router.navigate).not.toHaveBeenCalled(); | ||
| } | ||
| }); | ||
| } | ||
| )); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import { Injectable } from '@angular/core'; | ||
| import { Router, CanActivate } from '@angular/router'; | ||
| import { Observable, of } from 'rxjs'; | ||
| import { map, mergeMap } from 'rxjs/operators'; | ||
| import { FeatureSwitchGroupService } from 'src/app/modules/shared/feature-toggles/switch-group/feature-switch-group.service'; | ||
| import { UserInfoService } from 'src/app/modules/user/services/user-info.service'; | ||
| import { AzureAdB2CService } from '../../modules/login/services/azure.ad.b2c.service'; | ||
|
|
||
| @Injectable({ | ||
| providedIn: 'root', | ||
| }) | ||
| export class AdminGuard implements CanActivate { | ||
| constructor( | ||
| private azureAdB2CService: AzureAdB2CService, | ||
| private router: Router, | ||
| private userInfoService: UserInfoService, | ||
| private featureSwitchGroup: FeatureSwitchGroupService | ||
| ) {} | ||
|
|
||
| canActivate(): Observable<boolean> { | ||
| return this.featureSwitchGroup.isActivated().pipe( | ||
| mergeMap((enabled: boolean) => { | ||
| return enabled ? this.isAdminBasedInGroup() : this.isAdminBasedInRole(); | ||
| }), | ||
| map((isAdmin: boolean): boolean => { | ||
| if (!isAdmin) { | ||
| this.router.navigate(['login']); | ||
| } | ||
| return isAdmin; | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| isAdminBasedInRole(): Observable<boolean> { | ||
| return of(this.azureAdB2CService.isAdmin()); | ||
| } | ||
|
|
||
| isAdminBasedInGroup(): Observable<boolean> { | ||
| return this.userInfoService.isAdmin(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,79 @@ | ||
| import { waitForAsync, ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
|
||
| import { MockStore, provideMockStore } from '@ngrx/store/testing'; | ||
| import { of } from 'rxjs'; | ||
| import { AzureAdB2CService } from '../login/services/azure.ad.b2c.service'; | ||
| import { FeatureSwitchGroupService } from '../shared/feature-toggles/switch-group/feature-switch-group.service'; | ||
| import { LoadUser } from '../user/store/user.actions'; | ||
| import { HomeComponent } from './home.component'; | ||
|
|
||
| describe('HomeComponent', () => { | ||
| let component: HomeComponent; | ||
| let azureAdB2CService: AzureAdB2CService; | ||
| let featureSwitchGroupService: FeatureSwitchGroupService; | ||
| let store: MockStore; | ||
| let fixture: ComponentFixture<HomeComponent>; | ||
| const initialState = {}; | ||
| const azureB2CServiceStub = { | ||
| getUserId: () => 'user_id', | ||
| }; | ||
| const featureSwitchGroupServiceStub = { | ||
| isActivated: () => of(false), | ||
| }; | ||
|
|
||
| beforeEach(waitForAsync(() => { | ||
| TestBed.configureTestingModule({ | ||
| declarations: [ HomeComponent ] | ||
| beforeEach( | ||
| waitForAsync(() => { | ||
| TestBed.configureTestingModule({ | ||
| declarations: [HomeComponent], | ||
| providers: [ | ||
| provideMockStore({ initialState }), | ||
| { provide: AzureAdB2CService, useValue: azureB2CServiceStub }, | ||
| { provide: FeatureSwitchGroupService, useValue: featureSwitchGroupServiceStub }, | ||
| ], | ||
| }).compileComponents(); | ||
| }) | ||
| .compileComponents(); | ||
| })); | ||
| ); | ||
|
|
||
| beforeEach(() => { | ||
| fixture = TestBed.createComponent(HomeComponent); | ||
| azureAdB2CService = TestBed.inject(AzureAdB2CService); | ||
| featureSwitchGroupService = TestBed.inject(FeatureSwitchGroupService); | ||
| store = TestBed.inject(MockStore); | ||
|
|
||
| component = fixture.componentInstance; | ||
| fixture.detectChanges(); | ||
| store.setState(initialState); | ||
| }); | ||
|
|
||
| it('should be created', () => { | ||
| expect(component).toBeTruthy(); | ||
| }); | ||
|
|
||
| it('onInit, if featureSwitchGroup is true LoadUser action is dispatched', () => { | ||
| const userId = 'user_id'; | ||
| spyOn(featureSwitchGroupService, 'isActivated').and.returnValue(of(true)); | ||
| spyOn(azureAdB2CService, 'getUserId').and.returnValue(userId); | ||
| spyOn(store, 'dispatch'); | ||
|
|
||
| component.ngOnInit(); | ||
|
|
||
| featureSwitchGroupService.isActivated().subscribe(() => { | ||
| expect(featureSwitchGroupService.isActivated).toHaveBeenCalled(); | ||
| expect(azureAdB2CService.getUserId).toHaveBeenCalled(); | ||
| expect(store.dispatch).toHaveBeenCalledWith(new LoadUser(userId)); | ||
| }); | ||
| }); | ||
|
|
||
| it('onInit, if featureSwitchGroup is false nothing happens', () => { | ||
| spyOn(featureSwitchGroupService, 'isActivated').and.returnValue(of(false)); | ||
| spyOn(azureAdB2CService, 'getUserId'); | ||
| spyOn(store, 'dispatch'); | ||
|
|
||
| component.ngOnInit(); | ||
|
|
||
| featureSwitchGroupService.isActivated().subscribe(() => { | ||
| expect(featureSwitchGroupService.isActivated).toHaveBeenCalled(); | ||
| expect(azureAdB2CService.getUserId).not.toHaveBeenCalled(); | ||
| expect(store.dispatch).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,34 @@ | ||
| import { Component, OnInit } from '@angular/core'; | ||
| import { Component, OnDestroy, OnInit } from '@angular/core'; | ||
| import { Store } from '@ngrx/store'; | ||
| import { Subscription } from 'rxjs'; | ||
| import { LoadUser } from 'src/app/modules/user/store/user.actions'; | ||
| import { AzureAdB2CService } from '../login/services/azure.ad.b2c.service'; | ||
| import { FeatureSwitchGroupService } from '../shared/feature-toggles/switch-group/feature-switch-group.service'; | ||
|
|
||
| @Component({ | ||
| selector: 'app-home', | ||
| templateUrl: './home.component.html', | ||
| styleUrls: ['./home.component.scss'] | ||
| styleUrls: ['./home.component.scss'], | ||
| }) | ||
| export class HomeComponent implements OnInit { | ||
| export class HomeComponent implements OnInit, OnDestroy { | ||
| ftSwitchGroup$: Subscription; | ||
|
|
||
| constructor() { } | ||
| constructor( | ||
| private featureSwitchGroup: FeatureSwitchGroupService, | ||
| private azureAdB2CService: AzureAdB2CService, | ||
| private store: Store | ||
| ) {} | ||
|
|
||
| ngOnInit(): void { | ||
| this.ftSwitchGroup$ = this.featureSwitchGroup.isActivated().subscribe((enabled) => { | ||
| if (enabled) { | ||
| const userId = this.azureAdB2CService.getUserId(); | ||
| this.store.dispatch(new LoadUser(userId)); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| ngOnDestroy() { | ||
| this.ftSwitchGroup$.unsubscribe(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.