This PR adds a supertest instance that runs with test_user permissions to the security service.
This commit is contained in:
parent
71556ba38f
commit
832ebb0999
|
@ -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;
|
||||
})();
|
||||
}
|
||||
|
|
|
@ -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',
|
||||
});
|
||||
|
@ -93,3 +99,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}`,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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');
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -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');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -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<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() {
|
||||
|
|
Loading…
Reference in a new issue