Skip to content

Commit

Permalink
Changing to one CheApiRequestHandler and to dynamicaly injecting head…
Browse files Browse the repository at this point in the history
…ers.
  • Loading branch information
Katka92 committed Feb 6, 2020
1 parent 3069830 commit 014a550
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 118 deletions.
9 changes: 4 additions & 5 deletions tests/e2e/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ export * from './TestConstants';

export * from './driver/ChromeDriver';
export * from './driver/IDriver';
export * from './utils/AbstractRequestHandler';
export * from './utils/DriverHelper';
export * from './utils/IRequestHandler';
export * from './utils/Logger';
export * from './utils/MultiUserRequestHandler';
export * from './utils/NameGenerator';
export * from './utils/PreferencesHandler';
export * from './utils/RequestType';
export * from './utils/requestHandlers/CheApiRequestHandler';
export * from './utils/requestHandlers/IHeaderHandler';
export * from './utils/requestHandlers/MultiUserHeaderHandler';
export * from './utils/requestHandlers/SingleUserHeaderHandler';
export * from './utils/ScreenCatcher';
export * from './utils/SingleUserRequestHandler';
export * from './utils/workspace/ITestWorkspaceUtil';
export * from './utils/workspace/TestWorkspaceUtil';
export * from './utils/workspace/WorkspaceStatus';
Expand Down
15 changes: 9 additions & 6 deletions tests/e2e/inversify.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ import { OpenWorkspaceWidget } from './pageobjects/ide/OpenWorkspaceWidget';
import { CheLoginPage } from './pageobjects/openshift/CheLoginPage';
import { NotificationCenter } from './pageobjects/ide/NotificationCenter';
import { PreferencesHandler } from './utils/PreferencesHandler';
import { IRequestHandler } from './utils/IRequestHandler';
import { MultiUserRequestHandler } from './utils/MultiUserRequestHandler';
import { SingleUserRequestHandler } from './utils/SingleUserRequestHandler';
import { CheApiRequestHandler } from './utils/requestHandlers/CheApiRequestHandler';
import { IHeaderHandler } from './utils/requestHandlers/IHeaderHandler';
import { MultiUserHeaderHandler } from './utils/requestHandlers/MultiUserHeaderHandler';
import { SingleUserHeaderHandler } from './utils/requestHandlers/SingleUserHeaderHandler';
import { TokenHandler } from './utils/TokenHandler';


const e2eContainer: Container = new Container();
Expand All @@ -59,15 +61,15 @@ e2eContainer.bind<ITestWorkspaceUtil>(TYPES.WorkspaceUtil).to(TestWorkspaceUtil)
e2eContainer.bind<IOcpLoginPage>(TYPES.OcpLogin).to(OcpLoginByTempAdmin).inSingletonScope();

if (TestConstants.TS_SELENIUM_MULTIUSER) {
e2eContainer.bind<IRequestHandler>(TYPES.RequestHandler).to(MultiUserRequestHandler).inSingletonScope();
e2eContainer.bind<IHeaderHandler>(TYPES.HeaderHandler).to(MultiUserHeaderHandler).inSingletonScope();
if (JSON.parse(TestConstants.TS_SELENIUM_VALUE_OPENSHIFT_OAUTH)) {
e2eContainer.bind<ICheLoginPage>(TYPES.CheLogin).to(RegularUserOcpCheLoginPage).inSingletonScope();
} else {
e2eContainer.bind<ICheLoginPage>(TYPES.CheLogin).to(MultiUserLoginPage).inSingletonScope();
}
} else {
e2eContainer.bind<ICheLoginPage>(TYPES.CheLogin).to(SingleUserLoginPage).inSingletonScope();
e2eContainer.bind<IRequestHandler>(TYPES.RequestHandler).to(SingleUserRequestHandler).inSingletonScope();
e2eContainer.bind<IHeaderHandler>(TYPES.HeaderHandler).to(SingleUserHeaderHandler).inSingletonScope();
}

e2eContainer.bind<ContextMenu>(CLASSES.ContextMenu).to(ContextMenu).inSingletonScope();
Expand Down Expand Up @@ -95,6 +97,7 @@ e2eContainer.bind<OpenWorkspaceWidget>(CLASSES.OpenWorkspaceWidget).to(OpenWorks
e2eContainer.bind<CheLoginPage>(CLASSES.CheLoginPage).to(CheLoginPage).inSingletonScope();
e2eContainer.bind<NotificationCenter>(CLASSES.NotificationCenter).to(NotificationCenter).inSingletonScope();
e2eContainer.bind<PreferencesHandler>(CLASSES.PreferencesHandler).to(PreferencesHandler).inSingletonScope();

e2eContainer.bind<CheApiRequestHandler>(CLASSES.CheApiRequestHandler).to(CheApiRequestHandler).inSingletonScope();
e2eContainer.bind<TokenHandler>(CLASSES.TokenHandler).to(TokenHandler).inSingletonScope();

export { e2eContainer };
7 changes: 4 additions & 3 deletions tests/e2e/inversify.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const TYPES = {
CheLogin: Symbol.for('CheLogin'),
OcpLogin: Symbol.for('OcpLogin'),
WorkspaceUtil: Symbol.for('WorkspaceUtil'),
RequestHandler: Symbol.for('RequestHandler')
HeaderHandler: Symbol.for('HeaderHandler')
};

const CLASSES = {
Expand Down Expand Up @@ -43,8 +43,9 @@ const CLASSES = {
CheLoginPage: 'CheLoginPage',
TestWorkspaceUtil: 'TestWorkspaceUtil',
NotificationCenter: 'NotificationCenter',
RequestType: 'RequestType',
PreferencesHandler: 'PreferencesHandler'
PreferencesHandler: 'PreferencesHandler',
CheApiRequestHandler: 'CheApiRequestHandler',
TokenHandler: 'TokenHandler'
};

export { TYPES, CLASSES };
38 changes: 0 additions & 38 deletions tests/e2e/utils/AbstractRequestHandler.ts

This file was deleted.

38 changes: 0 additions & 38 deletions tests/e2e/utils/MultiUserRequestHandler.ts

This file was deleted.

26 changes: 16 additions & 10 deletions tests/e2e/utils/PreferencesHandler.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@

import { injectable, inject } from 'inversify';
import { Logger } from './Logger';
import { TYPES } from '../inversify.types';
import { IRequestHandler } from './IRequestHandler';
import { TestConstants } from '../TestConstants';
import { RequestType } from './RequestType';
import { CLASSES } from '../inversify.types';
import { CheApiRequestHandler } from './requestHandlers/CheApiRequestHandler';

@injectable()
export class PreferencesHandler {

constructor(@inject(TYPES.RequestHandler) private readonly requestHandler: IRequestHandler) {
constructor(@inject(CLASSES.CheApiRequestHandler) private readonly requestHandler: CheApiRequestHandler) {
}

public async setTerminalType(type: string) {
Logger.debug('PreferencesHandler.setTerminalToDom');
const response = await this.requestHandler.processRequest(RequestType.GET, `${TestConstants.TS_SELENIUM_BASE_URL}/api/preferences`);
const response = await this.requestHandler.get('api/preferences');
let userPref = response.data;
let theiaPref = JSON.parse(userPref['theia-user-preferences']);
theiaPref['terminal.integrated.rendererType'] = type;
userPref['theia-user-preferences'] = JSON.stringify(theiaPref);
this.requestHandler.processRequest(RequestType.POST, `${TestConstants.TS_SELENIUM_BASE_URL}/api/preferences`, userPref);
try {
let theiaPref = JSON.parse(userPref['theia-user-preferences']);
theiaPref['terminal.integrated.rendererType'] = type;
userPref['theia-user-preferences'] = JSON.stringify(theiaPref);
this.requestHandler.post('api/preferences', userPref);
} catch (e) {
//setting terminal before running a workspace, so no theia preferences are set
let theiaPref = `{ "terminal.integrated.rendererType":"${type}" }`;
userPref['theia-user-preferences'] = JSON.stringify(JSON.parse(theiaPref));
this.requestHandler.post('api/preferences', userPref);
}

}
}
5 changes: 0 additions & 5 deletions tests/e2e/utils/RequestType.ts

This file was deleted.

8 changes: 0 additions & 8 deletions tests/e2e/utils/SingleUserRequestHandler.ts

This file was deleted.

43 changes: 43 additions & 0 deletions tests/e2e/utils/requestHandlers/CheApiRequestHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*********************************************************************
* Copyright (c) 2019 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/

import axios, { AxiosResponse } from 'axios';
import { TestConstants } from '../../TestConstants';
import { TYPES } from '../../inversify.types';
import { inject, injectable } from 'inversify';
import { IHeaderHandler } from './IHeaderHandler';

@injectable()
export class CheApiRequestHandler {

constructor(@inject(TYPES.HeaderHandler) private readonly headerHandler: IHeaderHandler) {
}

async get(url: string) : Promise<AxiosResponse> {
return await axios.get(this.assembleUrl(url), await this.headerHandler.getHeaders());
}

async post(url: string, data?: string) : Promise<AxiosResponse> {
if ( data === undefined ) {
return await axios.post(this.assembleUrl(url), await this.headerHandler.getHeaders());
} else {
return await axios.post(this.assembleUrl(url), data, await this.headerHandler.getHeaders());
}
}

async delete(url: string) : Promise<AxiosResponse> {
return await axios.delete(this.assembleUrl(url), await this.headerHandler.getHeaders());
}

private assembleUrl(url: string) : string {
return `${TestConstants.TS_SELENIUM_BASE_URL}/${url}`;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/

import { RequestType } from './RequestType';
import { AxiosResponse } from 'axios';
import { AxiosRequestConfig } from "axios";

export interface IRequestHandler {
processRequest(reqType: RequestType, url: string, data?: string) : Promise<AxiosResponse>;
setHeaders(): void;
export interface IHeaderHandler {
getHeaders() : Promise<AxiosRequestConfig> ;
}


29 changes: 29 additions & 0 deletions tests/e2e/utils/requestHandlers/MultiUserHeaderHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*********************************************************************
* Copyright (c) 2019 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/
import axios, { AxiosRequestConfig } from 'axios';
import querystring from 'querystring';

import { TestConstants, CLASSES } from '../..';
import { IHeaderHandler } from './IHeaderHandler';
import { injectable, inject } from 'inversify';
import { TokenHandler } from '../TokenHandler';

@injectable()
export class MultiUserHeaderHandler implements IHeaderHandler {

constructor(@inject(CLASSES.TokenHandler) private readonly tokenHandler: TokenHandler) {
}
async getHeaders() : Promise<AxiosRequestConfig> {
let token = await this.tokenHandler.getCheBearerToken();
return { headers: {'Authorization' : `Bearer ${token}`}};
}
}


22 changes: 22 additions & 0 deletions tests/e2e/utils/requestHandlers/SingleUserHeaderHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*********************************************************************
* Copyright (c) 2019 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/

import { IHeaderHandler } from './IHeaderHandler';
import { injectable } from 'inversify';

@injectable()
export class SingleUserHeaderHandler implements IHeaderHandler {
async getHeaders() {
// no headers needs to be set to single user
return {};
}
}


0 comments on commit 014a550

Please sign in to comment.