Skip to content

Instantly share code, notes, and snippets.

@BigBadJock
Created April 13, 2019 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save BigBadJock/cb54e6121c8f3bb383c5c58d4829ebd8 to your computer and use it in GitHub Desktop.
Save BigBadJock/cb54e6121c8f3bb383c5c58d4829ebd8 to your computer and use it in GitHub Desktop.
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