Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functional tests - add supertest for test_user #77584

Merged
merged 1 commit into from
Sep 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion test/common/services/security/security.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
})();
}
28 changes: 23 additions & 5 deletions test/common/services/security/test_user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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',
});
Expand All @@ -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',
});
Expand All @@ -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}`,
})
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});

Expand Down
4 changes: 2 additions & 2 deletions x-pack/test/functional/apps/visualize/reporting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
Expand Down
54 changes: 18 additions & 36 deletions x-pack/test/functional/page_objects/reporting_page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']);

Expand Down Expand Up @@ -42,45 +46,23 @@ export function ReportingPageProvider({ getService, getPageObjects }: FtrProvide
`);
}

getResponse(url: string): Promise<IncomingMessage> {
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<supertestAsPromised.Response> {
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<Buffer> {
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() {
Expand Down