Created
April 13, 2019 10:17
-
-
Save BigBadJock/cb54e6121c8f3bb383c5c58d4829ebd8 to your computer and use it in GitHub Desktop.
Angular Authentication Guard Service Spec
This file contains 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
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