blob: cde624b7162956ed454b044f5c31683051dd900a [file] [log] [blame]
/********************************************************************************
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
********************************************************************************/
import { async } from '@angular/core/testing';
import { User } from '@grid-failure-information-app/shared/models/user';
import { of } from 'rxjs';
import { AppComponent } from '@grid-failure-information-app/app/app.component';
class FakeRouter {
public events: any = of({ url: '/overview' });
navigate(commands: any[]) {
return commands[0];
}
}
class FakeSandbox {
public user: User;
public setUser(user: User) {
this.user = user;
}
public setupLanguage() {}
}
describe('AppComponent', () => {
let component: AppComponent;
const fakeRouter: any = new FakeRouter();
const sandbox = new FakeSandbox();
const fakeActivatedRoute: any = { params: of(1) };
beforeEach(() => {
component = new AppComponent(fakeRouter, sandbox as any, fakeActivatedRoute);
});
it('should create the app', async(() => {
expect(component).toBeTruthy();
}));
it('should extract the user data from the JWT-AccessToken', async(() => {
const accessToken =
'eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJodVl0e' +
'VByUEVLQ1phY3FfMW5sOGZscENETnFHdmZEZHctYUxGQXNoWHZVIn0.eyJqdGkiOiI4ZmY5NTlhZC' +
'02ODQ1LTRlOGEtYjRiYi02ODQ0YjAwMjU0ZjgiLCJleHAiOjE1MDY2MDA0NTAsIm5iZiI6MCwiaWF' +
'0IjoxNTA2NjAwMTUwLCJpc3MiOiJodHRwOi8vZW50amF2YTAwMjo4MDgwL2F1dGgvcmVhbG1zL2Vs' +
'b2dib29rIiwiYXVkIjoiZWxvZ2Jvb2stYmFja2VuZCIsInN1YiI6IjM1OWVmOWM5LTc3ZGYtNGEzZ' +
'C1hOWM5LWY5NmQ4MzdkMmQ1NyIsInR5cCI6IkJlYXJlciIsImF6cCI6ImVsb2dib29rLWJhY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kATwnY2NKLRbequnh8Z6wLXjcmQNLgrgknXB_gogWAqH29dqKexwceMNIbq-kjaeLsmHSXM9TE9' +
'q7_Ln9el04OlkpOVspVguedfINcNFg0DmYLJWyD2ORkOHLmYigN6YnyB9P2NFOnKGlLuQ87GjosI0' +
'0zBniRGi3PhE9NGd51Qggdbcsm0aM8GiMaZ7SO5i8iQWL10TRFRFyTEfy6hSO8g';
const incognito: any = component;
incognito._processAccessToken(accessToken);
expect(sandbox.user.name).toBe('Administrator Administratowich');
}));
it('read the param access token', () => {
const token = 'AJDHDND';
const uri = 'a=X&b=y&accessToken=' + token;
const inkognito: any = component;
const token2 = inkognito._readParamAccessToken(uri, 'accessToken');
expect(token2).toBe(token);
});
it('getParametersFromURL is entered', () => {
const inkognito: any = component;
const param1 = inkognito._getParametersFromUrl();
expect(param1).toBe(null);
});
it('should set up componet on ngOnInit call', () => {
const spy = spyOn(sandbox, 'setupLanguage');
const spy2 = spyOn(component as any, '_extractTokenFromParameters');
const spy3 = spyOn(component as any, '_registerEvents');
component.ngOnInit();
expect(spy).toHaveBeenCalled();
expect(spy2).toHaveBeenCalled();
expect(spy3).toHaveBeenCalled();
});
it('should extract token from parameters', () => {
const spy = spyOn(component as any, '_getParametersFromUrl').and.returnValue('im_an_accessToken');
const spy2 = spyOn(component as any, '_processAccessToken');
(component as any)._extractTokenFromParameters();
expect(spy).toHaveBeenCalled();
expect(spy2).toHaveBeenCalled();
});
it('should set user if _getParametersFromUrl doeasnt return an accestoken', () => {
const spy = spyOn(component as any, '_getParametersFromUrl');
const spy2 = spyOn(sandbox, 'setUser');
(component as any)._extractTokenFromParameters();
expect(spy).toHaveBeenCalled();
expect(spy2).toHaveBeenCalled();
});
it('should register events', () => {
(component as any)._registerEvents();
expect(component.isLoginPage).toBeTruthy();
});
});