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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"minimist": "^1.2.5",
"moment": "^2.25.3",
"msal": "^1.2.1",
"ngrx-store-localstorage": "^11.0.0",
"ngx-cookie-service": "^11.0.2",
"ngx-mask": "^9.1.2",
"ngx-material-timepicker": "^5.5.3",
Expand Down Expand Up @@ -88,7 +89,7 @@
"popper.js": "^1.16.0",
"prettier": "^2.0.2",
"protractor": "^7.0.0",
"semantic-release": "^17.3.0",
"semantic-release": "^17.4.2",
"ts-node": "~8.3.0",
"tslint": "~6.1.0",
"typescript": "4.0.5"
Expand Down
6 changes: 3 additions & 3 deletions src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AdminGuard } from './guards/admin-guard/admin-guard';
import { AdminGuard } from './guards/admin-guard/admin.guard';
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

Expand Down Expand Up @@ -26,7 +26,7 @@ const routes: Routes = [
{ path: 'activities-management', component: ActivitiesManagementComponent },
{ path: 'customers-management', canActivate: [AdminGuard], component: CustomerComponent },
{ path: 'users', canActivate: [AdminGuard], component: UsersComponent },
{ path: 'technology-report', canActivate: [AdminGuard, TechnologiesReportGuard], component: TechnologyReportComponent},
{ path: 'technology-report', canActivate: [AdminGuard, TechnologiesReportGuard], component: TechnologyReportComponent },
{ path: '', pathMatch: 'full', redirectTo: 'time-clock' },
],
},
Expand All @@ -37,4 +37,4 @@ const routes: Routes = [
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
})
export class AppRoutingModule {}
export class AppRoutingModule { }
20 changes: 0 additions & 20 deletions src/app/guards/admin-guard/admin-guard.ts

This file was deleted.

112 changes: 92 additions & 20 deletions src/app/guards/admin-guard/admin.guard.spec.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,129 @@
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();
expect(enabled).toBeTrue();
}
});
}
));
});
});
41 changes: 41 additions & 0 deletions src/app/guards/admin-guard/admin.guard.ts
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();
}
}
66 changes: 60 additions & 6 deletions src/app/modules/home/home.component.spec.ts
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();
});
});
});
27 changes: 23 additions & 4 deletions src/app/modules/home/home.component.ts
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();
}
}
7 changes: 5 additions & 2 deletions src/app/modules/login/services/azure.ad.b2c.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ describe('AzureAdB2CService', () => {
expect(UserAgentApplication.prototype.loginPopup).toHaveBeenCalled();
});

it('on logout should call msal logout', () => {
it('on logout should call msal logout and verify if user localStorage is removed', () => {
spyOn(UserAgentApplication.prototype, 'logout').and.returnValue();
spyOn(localStorage, 'removeItem').withArgs('user');
service.logout();

expect(localStorage.removeItem).toHaveBeenCalledWith('user');
expect(UserAgentApplication.prototype.logout).toHaveBeenCalled();
});

Expand All @@ -66,7 +69,7 @@ describe('AzureAdB2CService', () => {
});

it('isAdmin when extension_role === time-tracker-admin', async () => {
const adminAccount = {...account};
const adminAccount = { ...account };
adminAccount.idToken.extension_role = 'time-tracker-admin';

spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(adminAccount);
Expand Down
Loading