From 31bb406783d270b22bd120b0916014f03b0099c0 Mon Sep 17 00:00:00 2001 From: Robert Oskamp Date: Wed, 16 Sep 2020 10:33:47 +0200 Subject: [PATCH] Functional tests - add supertest for test_user --- test/common/services/security/security.ts | 4 +- test/common/services/security/test_user.ts | 28 ++++++++-- .../apps/dashboard/reporting/screenshots.ts | 4 +- .../functional/apps/visualize/reporting.ts | 4 +- .../functional/page_objects/reporting_page.ts | 54 +++++++------------ 5 files changed, 48 insertions(+), 46 deletions(-) diff --git a/test/common/services/security/security.ts b/test/common/services/security/security.ts index fae4c9198cab6..d94822611f80b 100644 --- a/test/common/services/security/security.ts +++ b/test/common/services/security/security.ts @@ -21,7 +21,7 @@ import { Role } from './role'; import { User } from './user'; import { RoleMappings } from './role_mappings'; import { FtrProviderContext } from '../../ftr_provider_context'; -import { createTestUserService } from './test_user'; +import { createTestUserService, TestUserSupertestProvider } from './test_user'; export async function SecurityServiceProvider(context: FtrProviderContext) { const { getService } = context; @@ -31,11 +31,13 @@ export async function SecurityServiceProvider(context: FtrProviderContext) { const role = new Role(log, kibanaServer); const user = new User(log, kibanaServer); const testUser = await createTestUserService(role, user, context); + const testUserSupertest = TestUserSupertestProvider(context); return new (class SecurityService { roleMappings = new RoleMappings(log, kibanaServer); testUser = testUser; role = role; user = user; + testUserSupertest = testUserSupertest; })(); } diff --git a/test/common/services/security/test_user.ts b/test/common/services/security/test_user.ts index 83eac78621a53..103987cf7839a 100644 --- a/test/common/services/security/test_user.ts +++ b/test/common/services/security/test_user.ts @@ -16,12 +16,18 @@ * specific language governing permissions and limitations * under the License. */ +import { format as formatUrl } from 'url'; +import supertestAsPromised from 'supertest-as-promised'; + import { Role } from './role'; import { User } from './user'; import { FtrProviderContext } from '../../ftr_provider_context'; import { Browser } from '../../../functional/services/common'; import { TestSubjects } from '../../../functional/services/common'; +const TEST_USER_NAME = 'test_user'; +const TEST_USER_PASSWORD = 'changeme'; + export async function createTestUserService( role: Role, user: User, @@ -50,15 +56,15 @@ export async function createTestUserService( } try { // delete the test_user if present (will it error if the user doesn't exist?) - await user.delete('test_user'); + await user.delete(TEST_USER_NAME); } catch (exception) { log.debug('no test user to delete'); } // create test_user with username and pwd log.debug(`default roles = ${config.get('security.defaultRoles')}`); - await user.create('test_user', { - password: 'changeme', + await user.create(TEST_USER_NAME, { + password: TEST_USER_PASSWORD, roles: config.get('security.defaultRoles'), full_name: 'test user', }); @@ -74,8 +80,8 @@ export async function createTestUserService( async setRoles(roles: string[], shouldRefreshBrowser: boolean = true) { if (isEnabled()) { log.debug(`set roles = ${roles}`); - await user.create('test_user', { - password: 'changeme', + await user.create(TEST_USER_NAME, { + password: TEST_USER_PASSWORD, roles, full_name: 'test user', }); @@ -90,3 +96,15 @@ export async function createTestUserService( } })(); } + +export function TestUserSupertestProvider({ getService }: FtrProviderContext) { + const config = getService('config'); + const kibanaServerConfig = config.get('servers.kibana'); + + return supertestAsPromised( + formatUrl({ + ...kibanaServerConfig, + auth: `${TEST_USER_NAME}:${TEST_USER_PASSWORD}`, + }) + ); +} diff --git a/x-pack/test/functional/apps/dashboard/reporting/screenshots.ts b/x-pack/test/functional/apps/dashboard/reporting/screenshots.ts index 912bce22b1b21..5c9934d6e6dc8 100644 --- a/x-pack/test/functional/apps/dashboard/reporting/screenshots.ts +++ b/x-pack/test/functional/apps/dashboard/reporting/screenshots.ts @@ -72,8 +72,8 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) { const url = await PageObjects.reporting.getReportURL(60000); const res = await PageObjects.reporting.getResponse(url); - expect(res.statusCode).to.equal(200); - expect(res.headers['content-type']).to.equal('application/pdf'); + expect(res.status).to.equal(200); + expect(res.get('content-type')).to.equal('application/pdf'); }); }); diff --git a/x-pack/test/functional/apps/visualize/reporting.ts b/x-pack/test/functional/apps/visualize/reporting.ts index d1cea5a4481b1..0752971ba832b 100644 --- a/x-pack/test/functional/apps/visualize/reporting.ts +++ b/x-pack/test/functional/apps/visualize/reporting.ts @@ -68,8 +68,8 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { const url = await PageObjects.reporting.getReportURL(60000); const res = await PageObjects.reporting.getResponse(url); - expect(res.statusCode).to.equal(200); - expect(res.headers['content-type']).to.equal('application/pdf'); + expect(res.status).to.equal(200); + expect(res.get('content-type')).to.equal('application/pdf'); }); }); }); diff --git a/x-pack/test/functional/page_objects/reporting_page.ts b/x-pack/test/functional/page_objects/reporting_page.ts index 5828871d7c685..c6662c9639f21 100644 --- a/x-pack/test/functional/page_objects/reporting_page.ts +++ b/x-pack/test/functional/page_objects/reporting_page.ts @@ -4,14 +4,18 @@ * you may not use this file except in compliance with the Elastic License. */ -import http, { IncomingMessage } from 'http'; -import { parse } from 'url'; +import expect from '@kbn/expect'; +import { format as formatUrl } from 'url'; +import supertestAsPromised from 'supertest-as-promised'; + import { FtrProviderContext } from '../ftr_provider_context'; export function ReportingPageProvider({ getService, getPageObjects }: FtrProviderContext) { const browser = getService('browser'); + const config = getService('config'); const log = getService('log'); const retry = getService('retry'); + const security = getService('security'); const testSubjects = getService('testSubjects'); const PageObjects = getPageObjects(['security', 'share', 'timePicker']); @@ -42,45 +46,23 @@ export function ReportingPageProvider({ getService, getPageObjects }: FtrProvide `); } - getResponse(url: string): Promise { - log.debug(`getResponse for ${url}`); - const auth = 'test_user:changeme'; // FIXME not sure why there is no config that can be read for this - const headers = { - Authorization: `Basic ${Buffer.from(auth).toString('base64')}`, - }; - const parsedUrl = parse(url); - return new Promise((resolve, reject) => { - http - .get( - { - hostname: parsedUrl.hostname, - path: parsedUrl.path, - port: parsedUrl.port, - headers, - }, - (res: IncomingMessage) => { - resolve(res); - } - ) - .on('error', (e: Error) => { - log.error(e); - reject(e); - }); + async getResponse(fullUrl: string): Promise { + log.debug(`getResponse for ${fullUrl}`); + const kibanaServerConfig = config.get('servers.kibana'); + const baseURL = formatUrl({ + ...kibanaServerConfig, + auth: false, }); + const urlWithoutBase = fullUrl.replace(baseURL, ''); + const res = await security.testUserSupertest.get(urlWithoutBase); + return res; } async getRawPdfReportData(url: string): Promise { - const data: Buffer[] = []; // List of Buffer objects log.debug(`getRawPdfReportData for ${url}`); - - return new Promise(async (resolve, reject) => { - const response = await this.getResponse(url).catch(reject); - - if (response) { - response.on('data', (chunk: Buffer) => data.push(chunk)); - response.on('end', () => resolve(Buffer.concat(data))); - } - }); + const response = await this.getResponse(url); + expect(response.body).to.be.a(Buffer); + return response.body as Buffer; } async openCsvReportingPanel() {