Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 3 additions & 63 deletions src/app/guards/admin-guard/admin.guard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,32 @@ 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';

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: [
{ 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();
});

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}`, () => {
Expand All @@ -73,49 +40,22 @@ describe('AdminGuard', () => {
});
});

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();

featureSwitchGroupService.isActivated().pipe(take(1));

canActivate.subscribe((enabled) => {
expect(featureSwitchGroupService.isActivated).toHaveBeenCalled();
expect(enabled).toBe(param.isAdmin);
});
});
});

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 },
{ chosen: 'activate the route', isAdmin: true },
{ chosen: 'redirect to /login', isAdmin: false }
];
navigateParams.map((param) => {
it(`on isAdmin: ${param.isAdmin} with toggleSwitch: ${param.switchGroup}, should ${param.chosen} `, inject(
it(`on isAdmin: ${param.isAdmin}, should ${param.chosen} `, inject(
[Router],
(router: Router) => {
const switchGroup$ = of(param.switchGroup);
const isAdmin$ = of(param.isAdmin);

spyOn(featureSwitchGroupService, 'isActivated').and.returnValue(switchGroup$);
spyOn(adminGuard, 'isAdminBasedInRole').and.returnValue(isAdmin$);
spyOn(adminGuard, 'isAdminBasedInGroup').and.returnValue(isAdmin$);
spyOn(router, 'navigate').and.stub();

const canActivate = adminGuard.canActivate();

canActivate.subscribe((enabled) => {
expect(featureSwitchGroupService.isActivated).toHaveBeenCalled();
if (!enabled) {
expect(router.navigate).toHaveBeenCalledWith(['login']);
} else {
Expand Down
25 changes: 6 additions & 19 deletions src/app/guards/admin-guard/admin.guard.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,27 @@
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 { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
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 this.isAdminBasedInGroup().pipe(
map((isAdmin: boolean) => {
if (!isAdmin) { this.router.navigate(['login']); }
return isAdmin;
})
);
}

isAdminBasedInRole(): Observable<boolean> {
return of(this.azureAdB2CService.isAdmin());
}

isAdminBasedInGroup(): Observable<boolean> {
return this.userInfoService.isAdmin();
}
Expand Down
32 changes: 3 additions & 29 deletions src/app/modules/home/home.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
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(() => {
Expand All @@ -27,7 +21,6 @@ describe('HomeComponent', () => {
providers: [
provideMockStore({ initialState }),
{ provide: AzureAdB2CService, useValue: azureB2CServiceStub },
{ provide: FeatureSwitchGroupService, useValue: featureSwitchGroupServiceStub },
],
}).compileComponents();
})
Expand All @@ -36,7 +29,6 @@ describe('HomeComponent', () => {
beforeEach(() => {
fixture = TestBed.createComponent(HomeComponent);
azureAdB2CService = TestBed.inject(AzureAdB2CService);
featureSwitchGroupService = TestBed.inject(FeatureSwitchGroupService);
store = TestBed.inject(MockStore);

component = fixture.componentInstance;
Expand All @@ -48,32 +40,14 @@ describe('HomeComponent', () => {
expect(component).toBeTruthy();
});

it('onInit, if featureSwitchGroup is true LoadUser action is dispatched', () => {
it('onInit, 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();
});
expect(azureAdB2CService.getUserId).toHaveBeenCalled();
expect(store.dispatch).toHaveBeenCalledWith(new LoadUser(userId));
});
});
22 changes: 5 additions & 17 deletions src/app/modules/home/home.component.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,22 @@
import { Component, OnDestroy, OnInit } from '@angular/core';
import { Component, 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'],
})
export class HomeComponent implements OnInit, OnDestroy {
FTSwitchGroup$: Subscription;
export class HomeComponent implements OnInit {

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();
const userId = this.azureAdB2CService.getUserId();
this.store.dispatch(new LoadUser(userId));
}
}
5 changes: 2 additions & 3 deletions src/app/modules/login/services/azure.ad.b2c.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ import { Injectable } from '@angular/core';
import { UserAgentApplication } from 'msal';
import { from, Observable } from 'rxjs';
import { CookieService } from 'ngx-cookie-service';

import { AUTHORITY, CLIENT_ID, SCOPES } from '../../../../environments/environment';
import { FeatureSwitchGroupService } from '../../shared/feature-toggles/switch-group/feature-switch-group.service';

@Injectable({
providedIn: 'root',
})
export class AzureAdB2CService {
constructor(private cookieService?: CookieService) {}
constructor(private cookieService?: CookieService) { }

msalConfig: any = {
auth: {
Expand Down Expand Up @@ -44,6 +42,7 @@ export class AzureAdB2CService {
return this.msal.getAccount().name;
}

// TODO: inused method
isAdmin() {
return this.msal.getAccount()?.idToken?.extension_role === 'time-tracker-admin';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ import { Router, Routes } from '@angular/router';
import { TimeClockComponent } from '../../../time-clock/pages/time-clock.component';
import { of } from 'rxjs';
import { FeatureManagerService } from '../../feature-toggles/feature-toggle-manager.service';
import { FeatureSwitchGroupService } from '../../feature-toggles/switch-group/feature-switch-group.service';
import { UserInfoService } from 'src/app/modules/user/services/user-info.service';

describe('SidebarComponent', () => {
let component: SidebarComponent;
let fixture: ComponentFixture<SidebarComponent>;
let azureAdB2CServiceStubInjected;
let featureManagerServiceStubInjected: FeatureManagerService;
let featureSwitchGroupService: FeatureSwitchGroupService;
let userInfoService: UserInfoService;
let router;
const routes: Routes = [{ path: 'time-clock', component: TimeClockComponent }];
Expand All @@ -32,17 +30,12 @@ describe('SidebarComponent', () => {
isAdmin: () => of(true),
};

const featureSwitchGroupServiceStub = {
isActivated: () => of(true),
};

beforeEach(
waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [SidebarComponent],
providers: [
{ provide: AzureAdB2CService, useValue: azureAdB2CServiceStub },
{ provide: FeatureSwitchGroupService, useValue: featureSwitchGroupServiceStub },
{ provide: UserInfoService, useValue: userInfoServiceStub },
],
imports: [RouterTestingModule.withRoutes(routes)],
Expand All @@ -55,7 +48,6 @@ describe('SidebarComponent', () => {
fixture = TestBed.createComponent(SidebarComponent);
azureAdB2CServiceStubInjected = TestBed.inject(AzureAdB2CService);
featureManagerServiceStubInjected = TestBed.inject(FeatureManagerService);
featureSwitchGroupService = TestBed.inject(FeatureSwitchGroupService);
userInfoService = TestBed.inject(UserInfoService);
component = fixture.componentInstance;
fixture.detectChanges();
Expand All @@ -67,16 +59,13 @@ describe('SidebarComponent', () => {
});

it('admin users have six menu items', () => {
spyOn(featureSwitchGroupService, 'isActivated').and.returnValue(of(true));

component.getSidebarItems().subscribe(() => {
const menuItems = component.itemsSidebar;
expect(menuItems.length).toBe(6);
});
});

it('non admin users have two menu items', () => {
spyOn(featureSwitchGroupService, 'isActivated').and.returnValue(of(true));
spyOn(userInfoServiceStub, 'isAdmin').and.returnValue(of(false));

component.getSidebarItems().subscribe(() => {
Expand Down
Loading