diff --git a/tests/e2e/index.ts b/tests/e2e/index.ts index fe64dc639381..aaa372d0f750 100644 --- a/tests/e2e/index.ts +++ b/tests/e2e/index.ts @@ -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'; diff --git a/tests/e2e/inversify.config.ts b/tests/e2e/inversify.config.ts index e6865998ffa3..4d372130eb57 100644 --- a/tests/e2e/inversify.config.ts +++ b/tests/e2e/inversify.config.ts @@ -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(); @@ -59,7 +61,7 @@ e2eContainer.bind(TYPES.WorkspaceUtil).to(TestWorkspaceUtil) e2eContainer.bind(TYPES.OcpLogin).to(OcpLoginByTempAdmin).inSingletonScope(); if (TestConstants.TS_SELENIUM_MULTIUSER) { - e2eContainer.bind(TYPES.RequestHandler).to(MultiUserRequestHandler).inSingletonScope(); + e2eContainer.bind(TYPES.HeaderHandler).to(MultiUserHeaderHandler).inSingletonScope(); if (JSON.parse(TestConstants.TS_SELENIUM_VALUE_OPENSHIFT_OAUTH)) { e2eContainer.bind(TYPES.CheLogin).to(RegularUserOcpCheLoginPage).inSingletonScope(); } else { @@ -67,7 +69,7 @@ if (TestConstants.TS_SELENIUM_MULTIUSER) { } } else { e2eContainer.bind(TYPES.CheLogin).to(SingleUserLoginPage).inSingletonScope(); - e2eContainer.bind(TYPES.RequestHandler).to(SingleUserRequestHandler).inSingletonScope(); + e2eContainer.bind(TYPES.HeaderHandler).to(SingleUserHeaderHandler).inSingletonScope(); } e2eContainer.bind(CLASSES.ContextMenu).to(ContextMenu).inSingletonScope(); @@ -95,6 +97,7 @@ e2eContainer.bind(CLASSES.OpenWorkspaceWidget).to(OpenWorks e2eContainer.bind(CLASSES.CheLoginPage).to(CheLoginPage).inSingletonScope(); e2eContainer.bind(CLASSES.NotificationCenter).to(NotificationCenter).inSingletonScope(); e2eContainer.bind(CLASSES.PreferencesHandler).to(PreferencesHandler).inSingletonScope(); - +e2eContainer.bind(CLASSES.CheApiRequestHandler).to(CheApiRequestHandler).inSingletonScope(); +e2eContainer.bind(CLASSES.TokenHandler).to(TokenHandler).inSingletonScope(); export { e2eContainer }; diff --git a/tests/e2e/inversify.types.ts b/tests/e2e/inversify.types.ts index 01a43dfbc165..3c79aade8d27 100644 --- a/tests/e2e/inversify.types.ts +++ b/tests/e2e/inversify.types.ts @@ -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 = { @@ -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 }; diff --git a/tests/e2e/utils/AbstractRequestHandler.ts b/tests/e2e/utils/AbstractRequestHandler.ts deleted file mode 100644 index 8e29c7533811..000000000000 --- a/tests/e2e/utils/AbstractRequestHandler.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { RequestType } from '..'; -import axios from 'axios'; -import { injectable } from 'inversify'; -import { IRequestHandler } from './IRequestHandler'; - -@injectable() -export class AbstractRequestHandler implements IRequestHandler { - setHeaders(): void { - // by default nothing to set - } - - async processRequest(reqType: RequestType, url: string, data?: string) { - let response; - await this.setHeaders(); - switch (reqType) { - case RequestType.GET: { - response = await axios.get(url); - break; - } - case RequestType.DELETE: { - response = await axios.delete(url); - break; - } - case RequestType.POST: { - if ( data === undefined ) { - response = axios.post(url); - } else { - response = axios.post(url, data); - } - break; - } - default: { - throw new Error('Unknown RequestType: ' + reqType); - } - } - return response; - } -} diff --git a/tests/e2e/utils/MultiUserRequestHandler.ts b/tests/e2e/utils/MultiUserRequestHandler.ts deleted file mode 100644 index 8771268525e2..000000000000 --- a/tests/e2e/utils/MultiUserRequestHandler.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { TestConstants } from '..'; -import axios from 'axios'; -import querystring from 'querystring'; -import { injectable } from 'inversify'; -import { IRequestHandler } from './IRequestHandler'; -import { AbstractRequestHandler } from './AbstractRequestHandler'; - -@injectable() -export class MultiUserRequestHandler extends AbstractRequestHandler implements IRequestHandler { - - async setHeaders() { - let authorization = 'Authorization'; - axios.defaults.headers.common[authorization] = 'Bearer ' + await this.getCheBearerToken(); - } - - async getCheBearerToken(): Promise { - let params = {}; - - let keycloakUrl = TestConstants.TS_SELENIUM_BASE_URL; - const keycloakAuthSuffix = '/auth/realms/che/protocol/openid-connect/token'; - keycloakUrl = keycloakUrl.replace('che', 'keycloak') + keycloakAuthSuffix; - params = { - client_id: 'che-public', - username: TestConstants.TS_SELENIUM_USERNAME, - password: TestConstants.TS_SELENIUM_PASSWORD, - grant_type: 'password' - }; - - try { - const responseToObtainBearerToken = await axios.post(keycloakUrl, querystring.stringify(params)); - return responseToObtainBearerToken.data.access_token; - } catch (err) { - console.log(`Can not get bearer token. URL used: ${keycloakUrl}`); - throw err; - } - - } -} diff --git a/tests/e2e/utils/PreferencesHandler.ts b/tests/e2e/utils/PreferencesHandler.ts index 449c92718c99..35ef38dc9e40 100644 --- a/tests/e2e/utils/PreferencesHandler.ts +++ b/tests/e2e/utils/PreferencesHandler.ts @@ -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); + } + } } diff --git a/tests/e2e/utils/RequestType.ts b/tests/e2e/utils/RequestType.ts deleted file mode 100644 index fd2c9dc687cc..000000000000 --- a/tests/e2e/utils/RequestType.ts +++ /dev/null @@ -1,5 +0,0 @@ -export enum RequestType { - GET, - POST, - DELETE -} diff --git a/tests/e2e/utils/SingleUserRequestHandler.ts b/tests/e2e/utils/SingleUserRequestHandler.ts deleted file mode 100644 index b6fa916ab4d5..000000000000 --- a/tests/e2e/utils/SingleUserRequestHandler.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { injectable } from 'inversify'; -import { IRequestHandler } from './IRequestHandler'; -import { AbstractRequestHandler } from './AbstractRequestHandler'; - -@injectable() -export class SingleUserRequestHandler extends AbstractRequestHandler implements IRequestHandler { - -} diff --git a/tests/e2e/utils/requestHandlers/CheApiRequestHandler.ts b/tests/e2e/utils/requestHandlers/CheApiRequestHandler.ts new file mode 100644 index 000000000000..bf577b6751ea --- /dev/null +++ b/tests/e2e/utils/requestHandlers/CheApiRequestHandler.ts @@ -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 { + return await axios.get(this.assembleUrl(url), await this.headerHandler.getHeaders()); + } + + async post(url: string, data?: string) : Promise { + 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 { + return await axios.delete(this.assembleUrl(url), await this.headerHandler.getHeaders()); + } + + private assembleUrl(url: string) : string { + return `${TestConstants.TS_SELENIUM_BASE_URL}/${url}`; + } + + } diff --git a/tests/e2e/utils/IRequestHandler.ts b/tests/e2e/utils/requestHandlers/IHeaderHandler.ts similarity index 63% rename from tests/e2e/utils/IRequestHandler.ts rename to tests/e2e/utils/requestHandlers/IHeaderHandler.ts index cb09b848e89f..fa55b1323efa 100644 --- a/tests/e2e/utils/IRequestHandler.ts +++ b/tests/e2e/utils/requestHandlers/IHeaderHandler.ts @@ -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; - setHeaders(): void; +export interface IHeaderHandler { + getHeaders() : Promise ; } + + diff --git a/tests/e2e/utils/requestHandlers/MultiUserHeaderHandler.ts b/tests/e2e/utils/requestHandlers/MultiUserHeaderHandler.ts new file mode 100644 index 000000000000..b8e7c1d6e9fb --- /dev/null +++ b/tests/e2e/utils/requestHandlers/MultiUserHeaderHandler.ts @@ -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 { + let token = await this.tokenHandler.getCheBearerToken(); + return { headers: {'Authorization' : `Bearer ${token}`}}; + } +} + + diff --git a/tests/e2e/utils/requestHandlers/SingleUserHeaderHandler.ts b/tests/e2e/utils/requestHandlers/SingleUserHeaderHandler.ts new file mode 100644 index 000000000000..ad0edd9e4390 --- /dev/null +++ b/tests/e2e/utils/requestHandlers/SingleUserHeaderHandler.ts @@ -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 {}; + } +} + +