Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Angular Authentication Guard Service Spec
import { HttpClientModule } from '@angular/common/http';
import { fakeAsync, TestBed } from '@angular/core/testing';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { AuthGuard } from './auth.guard';
class MockActivatedRouteSnapshot {
private _data: any;
get data(){
return this._data;
}
}
let mockRouterStateSnapshot : RouterStateSnapshot;
describe('AuthGuard', () => {
let authGuard: AuthGuard;
let route: ActivatedRouteSnapshot;
let authService;
let router;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
HttpClientModule,
],
providers: [
AuthGuard,
{ provide: ActivatedRouteSnapshot, useClass: MockActivatedRouteSnapshot},
]
});
});
it('should be created', () => {
authService = { checkLoggedIn: () => true };
authGuard = new AuthGuard(authService, router);
expect(authGuard).toBeTruthy();
});
it('should not be able to activate an invalid route', fakeAsync(() => {
authService = { checkLoggedIn: () => true };
authGuard = new AuthGuard(authService, router);
route=TestBed.get(ActivatedRouteSnapshot);
let state= null;
expect(authGuard.canActivate(route, state)).toBeFalsy();
}));
it('should not be able to activate a valid route when logged out', fakeAsync(() => {
authService = { checkLoggedIn: () => false };
authGuard = new AuthGuard(authService, router);
route=TestBed.get(ActivatedRouteSnapshot);
spyOnProperty(route,'data','get').and.returnValue({expectedClaim: 'policy'});
mockRouterStateSnapshot = jasmine.createSpyObj<RouterStateSnapshot>('RouterStateSnapshot', ['toString']);
expect(authGuard.canActivate(route, mockRouterStateSnapshot)).toBeFalsy();
}));
it('should be able to activate an valid route when logged in', fakeAsync(() => {
authService = { checkLoggedIn: () => true };
authGuard = new AuthGuard(authService, router);
route=TestBed.get(ActivatedRouteSnapshot);
mockRouterStateSnapshot = jasmine.createSpyObj<RouterStateSnapshot>('RouterStateSnapshot', ['toString']);
mockRouterStateSnapshot.url = "test";
expect(authGuard.canActivate(route, mockRouterStateSnapshot)).toBeTruthy();
}));
it('should not be able to activate an valid route needing claim when logged in without claim', fakeAsync(() => {
authService = { checkLoggedIn: () => true };
authGuard = new AuthGuard(authService, router);
route=TestBed.get(ActivatedRouteSnapshot);
spyOnProperty(route,'data','get').and.returnValue({expectedClaim: 'policy'});
mockRouterStateSnapshot = jasmine.createSpyObj<RouterStateSnapshot>('RouterStateSnapshot', ['toString']);
mockRouterStateSnapshot.url = "test";
expect(authGuard.canActivate(route, mockRouterStateSnapshot)).toBeFalsy();
}));
it('should be able to activate an valid route needing claim when logged in with claim', fakeAsync(() => {
authService = { checkLoggedIn: () => true, currentUser : {claims: ['policy']} };
authGuard = new AuthGuard(authService, router);
route=TestBed.get(ActivatedRouteSnapshot);
spyOnProperty(route,'data','get').and.returnValue({expectedClaim: 'policy'});
mockRouterStateSnapshot = jasmine.createSpyObj<RouterStateSnapshot>('RouterStateSnapshot', ['toString']);
mockRouterStateSnapshot.url = "test";
expect(authGuard.canActivate(route, mockRouterStateSnapshot)).toBeTruthy();
}));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.