440993dc6c46eb4da64cf1b31fc8fd54128a9ccd
[appc/cdt.git] / src / app / vnfs / GCAuthGuard / gcauth.guard.spec.ts
1 /*
2 ============LICENSE_START==========================================
3 ===================================================================
4 Copyright (C) 2018 IBM.
5 ===================================================================
6
7 Unless otherwise specified, all software contained herein is licensed
8 under the Apache License, Version 2.0 (the License);
9 you may not use this software except in compliance with the License.
10 You may obtain a copy of the License at
11
12 http://www.apache.org/licenses/LICENSE-2.0
13
14 Unless required by applicable law or agreed to in writing, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19 ============LICENSE_END============================================
20 */
21
22 import { async, TestBed, inject } from '@angular/core/testing';
23 import { Router, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
24 import {FormsModule} from '@angular/forms';
25 import {HttpModule} from '@angular/http';
26 import {CommonModule} from '@angular/common';
27 import 'rxjs/Rx';
28 import 'rxjs/add/observable/throw';
29 import 'rxjs/add/operator/map';
30 import { NgbModule, NgbModal } from '@ng-bootstrap/ng-bootstrap';
31 import { GCAuthGuardService } from './gcauth-guard.service';
32 import {MappingEditorService} from '../../shared/services/mapping-editor.service';
33 import { promise } from 'protractor';
34
35 describe('LogginGuard', () => {
36     let routerMock = {
37         navigate: jasmine.createSpy('navigate')
38     };
39     let loggedInGuard: GCAuthGuardService;
40
41     beforeEach(() => {
42         TestBed.configureTestingModule({
43             imports: [FormsModule, CommonModule, HttpModule, NgbModule.forRoot()],
44             providers: [GCAuthGuardService, MappingEditorService, {provide: Router, useValue: routerMock}]
45         });
46         TestBed.compileComponents();
47     });
48
49     beforeEach(() => {
50         loggedInGuard = TestBed.get(GCAuthGuardService);
51     });
52
53     it('be able to return true when referenceNameObjects is defined', inject([GCAuthGuardService, MappingEditorService], (service: GCAuthGuardService, mapService: MappingEditorService) => {
54         localStorage['userId'] = 'abc@xyz.com';
55         mapService.referenceNameObjects = { data : 'data'};
56         let route : ActivatedRouteSnapshot;
57         let state: RouterStateSnapshot;
58         service.canActivate(route, state).then((value)=>{
59             expect(value).toBe(true);
60         })
61     }));
62
63     it('stop routing if referenceNameObjects is not defined', inject([GCAuthGuardService, MappingEditorService, NgbModal], (service: GCAuthGuardService, mapService: MappingEditorService, ngbModal: NgbModal) => {
64         localStorage['userId'] = 'abc@xyz.com';
65         mapService.referenceNameObjects = undefined;
66        let spy =  spyOn(NgbModal.prototype, 'open').and.returnValue(Promise.resolve(true));
67         let route : ActivatedRouteSnapshot;
68         let state: RouterStateSnapshot;
69         service.canActivate(route, state).then((value)=>{
70             expect(value).toBe(true);
71         })
72     }));
73 });