| /* |
| ****************************************************************************** |
| * Copyright © 2018 PTA GmbH. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| ****************************************************************************** |
| */ |
| import { TestBed, async, inject } from '@angular/core/testing'; |
| import { Router } from '@angular/router'; |
| import { AuthGuard } from './auth-guard.service'; |
| import { SessionContext } from '../common/session-context'; |
| |
| class FakeRouter { |
| navigate(commands: any[]) { |
| return commands[0]; |
| } |
| } |
| |
| describe('AuthGuard', () => { |
| beforeEach(() => { |
| TestBed.configureTestingModule({ |
| providers: [ |
| AuthGuard, |
| SessionContext, |
| { provide: Router, useClass: FakeRouter } |
| ] |
| }) |
| .compileComponents(); |
| }); |
| |
| it('should instantiate the service', inject([AuthGuard], (authGuard: AuthGuard) => { |
| expect(authGuard instanceof AuthGuard).toBe(true); |
| })); |
| |
| it('should allow activating route if user in session exists', |
| inject([AuthGuard, SessionContext], (authGuard: AuthGuard, sessionContext: SessionContext) => { |
| spyOn(sessionContext, 'getCurrUser').and.returnValue('dummy'); |
| |
| expect(authGuard.canActivate()).toBe(true); |
| }) |
| ); |
| |
| it('should redirect to loggedout page if no user in session exists', |
| inject([AuthGuard, SessionContext, Router], (authGuard: AuthGuard, sessionContext: SessionContext, router: Router) => { |
| spyOn(sessionContext, 'getCurrUser').and.returnValue(null); |
| spyOn(router, 'navigate'); |
| |
| expect(authGuard.canActivate()).toBe(false); |
| expect(router.navigate).toHaveBeenCalledWith(['/loggedout']); |
| }) |
| ); |
| |
| }); |