Adding functionality to switch user terminal to dom. (#15892)
* Adding functionality to switch user terminal to dom. * Simplify and parametrize setting terminal type. * Abstract common request handling parts, use inversify for differentiation between singe/multi user approach. * Changing to one CheApiRequestHandler and to dynamicaly injecting headers. * Add suggestions Signed-off-by: Ihor Okhrimenko <iokhrime@redhat.com> * Applying changes from comments. Co-authored-by: Igor Ohrimenko <iokhrime@redhat.com>7.20.x
parent
860f5ee173
commit
f2273b26df
|
|
@ -8,6 +8,13 @@ export * from './driver/IDriver';
|
|||
export * from './utils/DriverHelper';
|
||||
export * from './utils/Logger';
|
||||
export * from './utils/NameGenerator';
|
||||
export * from './utils/PreferencesHandler';
|
||||
export * from './utils/requestHandlers/CheApiRequestHandler';
|
||||
export * from './utils/requestHandlers/headers/CheMultiuserAuthorizationHeaderHandler';
|
||||
export * from './utils/requestHandlers/headers/CheSingleUserAuthorizationHeaderHandler';
|
||||
export * from './utils/requestHandlers/headers/IAuthorizationHeaderHandler';
|
||||
export * from './utils/requestHandlers/tokens/CheMultiuserTokenHandler';
|
||||
export * from './utils/requestHandlers/tokens/ITokenHandler';
|
||||
export * from './utils/ScreenCatcher';
|
||||
export * from './utils/workspace/ITestWorkspaceUtil';
|
||||
export * from './utils/workspace/TestWorkspaceUtil';
|
||||
|
|
|
|||
|
|
@ -45,6 +45,13 @@ import { OcpWebConsolePage } from './pageobjects/openshift/OcpWebConsolePage';
|
|||
import { OpenWorkspaceWidget } from './pageobjects/ide/OpenWorkspaceWidget';
|
||||
import { CheLoginPage } from './pageobjects/openshift/CheLoginPage';
|
||||
import { NotificationCenter } from './pageobjects/ide/NotificationCenter';
|
||||
import { PreferencesHandler } from './utils/PreferencesHandler';
|
||||
import { IAuthorizationHeaderHandler } from './utils/requestHandlers/headers/IAuthorizationHeaderHandler';
|
||||
import { CheMultiuserAuthorizationHeaderHandler } from './utils/requestHandlers/headers/CheMultiuserAuthorizationHeaderHandler';
|
||||
import { CheMultiuserTokenHandler } from './utils/requestHandlers/tokens/CheMultiuserTokenHandler';
|
||||
import { CheSingleUserAuthorizationHeaderHandler } from './utils/requestHandlers/headers/CheSingleUserAuthorizationHeaderHandler';
|
||||
import { ITokenHandler } from './utils/requestHandlers/tokens/ITokenHandler';
|
||||
import { CheApiRequestHandler } from './utils/requestHandlers/CheApiRequestHandler';
|
||||
|
||||
|
||||
const e2eContainer: Container = new Container();
|
||||
|
|
@ -55,13 +62,18 @@ e2eContainer.bind<ITestWorkspaceUtil>(TYPES.WorkspaceUtil).to(TestWorkspaceUtil)
|
|||
e2eContainer.bind<IOcpLoginPage>(TYPES.OcpLogin).to(OcpLoginByTempAdmin).inSingletonScope();
|
||||
|
||||
if (TestConstants.TS_SELENIUM_MULTIUSER) {
|
||||
e2eContainer.bind<IAuthorizationHeaderHandler>(TYPES.IAuthorizationHeaderHandler).to(CheMultiuserAuthorizationHeaderHandler).inSingletonScope();
|
||||
e2eContainer.bind<ITokenHandler>(TYPES.ITokenHandler).to(CheMultiuserTokenHandler).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<IAuthorizationHeaderHandler>(TYPES.IAuthorizationHeaderHandler).to(CheSingleUserAuthorizationHeaderHandler).inSingletonScope();
|
||||
}
|
||||
|
||||
e2eContainer.bind<ContextMenu>(CLASSES.ContextMenu).to(ContextMenu).inSingletonScope();
|
||||
|
|
@ -88,6 +100,7 @@ e2eContainer.bind<OcpWebConsolePage>(CLASSES.OcpWebConsolePage).to(OcpWebConsole
|
|||
e2eContainer.bind<OpenWorkspaceWidget>(CLASSES.OpenWorkspaceWidget).to(OpenWorkspaceWidget).inSingletonScope();
|
||||
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();
|
||||
|
||||
export { e2eContainer };
|
||||
|
|
|
|||
|
|
@ -13,7 +13,11 @@ const TYPES = {
|
|||
Driver: Symbol.for('Driver'),
|
||||
CheLogin: Symbol.for('CheLogin'),
|
||||
OcpLogin: Symbol.for('OcpLogin'),
|
||||
WorkspaceUtil: Symbol.for('WorkspaceUtil')
|
||||
WorkspaceUtil: Symbol.for('WorkspaceUtil'),
|
||||
IAuthorizationHeaderHandler: Symbol.for('IAuthorizationHeaderHandler'),
|
||||
ITokenHandler: Symbol.for('ITokenHandler')
|
||||
|
||||
|
||||
};
|
||||
|
||||
const CLASSES = {
|
||||
|
|
@ -41,7 +45,9 @@ const CLASSES = {
|
|||
ContextMenu: 'ContextMenu',
|
||||
CheLoginPage: 'CheLoginPage',
|
||||
TestWorkspaceUtil: 'TestWorkspaceUtil',
|
||||
NotificationCenter: 'NotificationCenter'
|
||||
NotificationCenter: 'NotificationCenter',
|
||||
PreferencesHandler: 'PreferencesHandler',
|
||||
CheApiRequestHandler: 'CheApiRequestHandler'
|
||||
};
|
||||
|
||||
export { TYPES, CLASSES };
|
||||
|
|
|
|||
|
|
@ -0,0 +1,30 @@
|
|||
|
||||
import { injectable, inject } from 'inversify';
|
||||
import { Logger } from './Logger';
|
||||
import { CLASSES } from '../inversify.types';
|
||||
import { CheApiRequestHandler } from './requestHandlers/CheApiRequestHandler';
|
||||
|
||||
@injectable()
|
||||
export class PreferencesHandler {
|
||||
|
||||
constructor(@inject(CLASSES.CheApiRequestHandler) private readonly requestHandler: CheApiRequestHandler) {
|
||||
}
|
||||
|
||||
public async setTerminalType(type: string) {
|
||||
Logger.debug('PreferencesHandler.setTerminalToDom');
|
||||
const response = await this.requestHandler.get('api/preferences');
|
||||
let userPref = response.data;
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
/*********************************************************************
|
||||
* 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 { IAuthorizationHeaderHandler } from './headers/IAuthorizationHeaderHandler';
|
||||
|
||||
@injectable()
|
||||
export class CheApiRequestHandler {
|
||||
constructor(@inject(TYPES.IAuthorizationHeaderHandler) private readonly headerHandler: IAuthorizationHeaderHandler) { }
|
||||
|
||||
async get(relativeUrl: string): Promise<AxiosResponse> {
|
||||
return await axios.get(this.assembleUrl(relativeUrl), await this.headerHandler.get());
|
||||
}
|
||||
|
||||
async post(relativeUrl: string, data?: string): Promise<AxiosResponse> {
|
||||
return await axios.post(this.assembleUrl(relativeUrl), data, await this.headerHandler.get());
|
||||
}
|
||||
|
||||
async delete(relativeUrl: string): Promise<AxiosResponse> {
|
||||
return await axios.delete(this.assembleUrl(relativeUrl), await this.headerHandler.get());
|
||||
}
|
||||
|
||||
private assembleUrl(relativeUrl: string): string {
|
||||
return `${TestConstants.TS_SELENIUM_BASE_URL}/${relativeUrl}`;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
/*********************************************************************
|
||||
* 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 { AxiosRequestConfig } from 'axios';
|
||||
|
||||
import { TYPES } from '../../../inversify.types';
|
||||
import { IAuthorizationHeaderHandler } from './IAuthorizationHeaderHandler';
|
||||
import { injectable, inject } from 'inversify';
|
||||
import { ITokenHandler } from '../tokens/ITokenHandler';
|
||||
|
||||
@injectable()
|
||||
export class CheMultiuserAuthorizationHeaderHandler implements IAuthorizationHeaderHandler {
|
||||
|
||||
constructor(@inject(TYPES.ITokenHandler) private readonly tokenHandler: ITokenHandler) {
|
||||
}
|
||||
|
||||
async get(): Promise<AxiosRequestConfig> {
|
||||
const token = await this.tokenHandler.get();
|
||||
return { headers: { 'Authorization': `Bearer ${token}` } };
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
/*********************************************************************
|
||||
* 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 { IAuthorizationHeaderHandler } from './IAuthorizationHeaderHandler';
|
||||
import { injectable } from 'inversify';
|
||||
import { AxiosRequestConfig } from 'axios';
|
||||
|
||||
@injectable()
|
||||
export class CheSingleUserAuthorizationHeaderHandler implements IAuthorizationHeaderHandler {
|
||||
async get() : Promise<AxiosRequestConfig> {
|
||||
// no headers needs to be set to single user
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
/*********************************************************************
|
||||
* 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 { AxiosRequestConfig } from 'axios';
|
||||
|
||||
export interface IAuthorizationHeaderHandler {
|
||||
get(): Promise<AxiosRequestConfig>;
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
/*********************************************************************
|
||||
* 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 from 'axios';
|
||||
import querystring from 'querystring';
|
||||
import { injectable } from 'inversify';
|
||||
import { TestConstants } from '../../../TestConstants';
|
||||
import { ITokenHandler } from './ITokenHandler';
|
||||
|
||||
@injectable()
|
||||
export class CheMultiuserTokenHandler implements ITokenHandler {
|
||||
async get(): Promise<string> {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
/*********************************************************************
|
||||
* 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
|
||||
**********************************************************************/
|
||||
|
||||
|
||||
export interface ITokenHandler {
|
||||
get(): Promise<string>;
|
||||
}
|
||||
Loading…
Reference in New Issue