mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 17:28:26 -04:00
Abstract kbn server setup in tests
The new kbn_server test utility allows us to setup KbnServer instances with consistent defaults and make requests through a common test-only interface. These abstractions allow us to configure global behaviors such as shield authorization for all of our tests rather than re-implementing it in every test.
This commit is contained in:
parent
4a6cb3cf60
commit
f0f2c631eb
5 changed files with 75 additions and 57 deletions
|
@ -1,10 +1,10 @@
|
|||
var src = require('requirefrom')('src');
|
||||
var expect = require('expect.js');
|
||||
var util = require('util');
|
||||
var requireFromTest = require('requirefrom')('test');
|
||||
var kbnTestServer = requireFromTest('utils/kbn_server');
|
||||
|
||||
var format = util.format;
|
||||
|
||||
var KbnServer = src('server/KbnServer');
|
||||
var fromRoot = src('utils/fromRoot');
|
||||
|
||||
describe('plugins/elasticsearch', function () {
|
||||
describe('routes', function () {
|
||||
|
@ -12,27 +12,7 @@ describe('plugins/elasticsearch', function () {
|
|||
var kbnServer;
|
||||
|
||||
before(function () {
|
||||
kbnServer = new KbnServer({
|
||||
server: {
|
||||
autoListen: false,
|
||||
xsrf: {
|
||||
disableProtection: true
|
||||
}
|
||||
},
|
||||
logging: { quiet: true },
|
||||
plugins: {
|
||||
scanDirs: [
|
||||
fromRoot('src/plugins')
|
||||
]
|
||||
},
|
||||
optimize: {
|
||||
enabled: false
|
||||
},
|
||||
elasticsearch: {
|
||||
url: 'http://localhost:9210'
|
||||
}
|
||||
});
|
||||
|
||||
kbnServer = kbnTestServer.createServer();
|
||||
return kbnServer.ready()
|
||||
.then(() => kbnServer.server.plugins.elasticsearch.waitUntilReady());
|
||||
});
|
||||
|
@ -51,7 +31,7 @@ describe('plugins/elasticsearch', function () {
|
|||
var statusCode = options.statusCode || 200;
|
||||
describe(format('%s %s', options.method, options.url), function () {
|
||||
it('should should return ' + statusCode, function (done) {
|
||||
kbnServer.server.inject(options, function (res) {
|
||||
kbnTestServer.makeRequest(kbnServer, options, function (res) {
|
||||
try {
|
||||
expect(res.statusCode).to.be(statusCode);
|
||||
done();
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
import { resolve } from 'path';
|
||||
import { fromNode as fn } from 'bluebird';
|
||||
import expect from 'expect.js';
|
||||
import requirefrom from 'requirefrom';
|
||||
|
||||
import KbnServer from '../KbnServer';
|
||||
|
||||
const src = resolve.bind(__dirname, '../../');
|
||||
const requireFromTest = requirefrom('test');
|
||||
const kbnTestServer = requireFromTest('utils/kbn_server');
|
||||
const basePath = '/kibana';
|
||||
|
||||
describe('Server basePath config', function () {
|
||||
|
@ -13,14 +13,8 @@ describe('Server basePath config', function () {
|
|||
|
||||
let kbnServer;
|
||||
before(async function () {
|
||||
kbnServer = new KbnServer({
|
||||
server: { autoListen: false, basePath },
|
||||
plugins: { scanDirs: [src('plugins')] },
|
||||
logging: { quiet: true },
|
||||
optimize: { enabled: false },
|
||||
elasticsearch: {
|
||||
url: 'http://localhost:9210'
|
||||
}
|
||||
kbnServer = kbnTestServer.createServer({
|
||||
server: { basePath }
|
||||
});
|
||||
await kbnServer.ready();
|
||||
return kbnServer;
|
||||
|
@ -30,12 +24,18 @@ describe('Server basePath config', function () {
|
|||
await kbnServer.close();
|
||||
});
|
||||
|
||||
it('appends the basePath to root redirect', async function () {
|
||||
const response = await kbnServer.inject({
|
||||
it('appends the basePath to root redirect', function (done) {
|
||||
const options = {
|
||||
url: '/',
|
||||
method: 'GET'
|
||||
};
|
||||
kbnTestServer.makeRequest(kbnServer, options, function (res) {
|
||||
try {
|
||||
expect(res.payload).to.match(/defaultRoute = '\/kibana\/app\/kibana'/);
|
||||
done();
|
||||
} catch (e) {
|
||||
done(e);
|
||||
}
|
||||
});
|
||||
|
||||
expect(response.payload).to.match(/defaultRoute = '\/kibana\/app\/kibana'/);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
import expect from 'expect.js';
|
||||
import KbnServer from '../../KbnServer';
|
||||
import requirefrom from 'requirefrom';
|
||||
|
||||
const requireFromTest = requirefrom('test');
|
||||
const kbnTestServer = requireFromTest('utils/kbn_server');
|
||||
|
||||
describe('cookie validation', function () {
|
||||
let kbnServer;
|
||||
beforeEach(function () {
|
||||
kbnServer = new KbnServer({
|
||||
server: { autoListen: false }
|
||||
});
|
||||
kbnServer = kbnTestServer.createServer();
|
||||
return kbnServer.ready();
|
||||
});
|
||||
afterEach(function () {
|
||||
|
@ -14,26 +15,28 @@ describe('cookie validation', function () {
|
|||
});
|
||||
|
||||
it('allows non-strict cookies', function (done) {
|
||||
kbnServer.server.inject({
|
||||
const options = {
|
||||
method: 'GET',
|
||||
url: '/',
|
||||
headers: {
|
||||
cookie: 'test:80=value;test_80=value'
|
||||
}
|
||||
}, (res) => {
|
||||
};
|
||||
kbnTestServer.makeRequest(kbnServer, options, (res) => {
|
||||
expect(res.payload).not.to.contain('Invalid cookie header');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('returns an error if the cookie can\'t be parsed', function (done) {
|
||||
kbnServer.server.inject({
|
||||
const options = {
|
||||
method: 'GET',
|
||||
url: '/',
|
||||
headers: {
|
||||
cookie: 'a'
|
||||
}
|
||||
}, (res) => {
|
||||
};
|
||||
kbnTestServer.makeRequest(kbnServer, options, (res) => {
|
||||
expect(res.payload).to.contain('Invalid cookie header');
|
||||
done();
|
||||
});
|
||||
|
|
|
@ -2,7 +2,8 @@ import expect from 'expect.js';
|
|||
import { fromNode as fn } from 'bluebird';
|
||||
import { resolve } from 'path';
|
||||
|
||||
import KbnServer from '../../KbnServer';
|
||||
const requireFromTest = require('requirefrom')('test');
|
||||
const kbnTestServer = requireFromTest('utils/kbn_server');
|
||||
|
||||
const nonDestructiveMethods = ['GET'];
|
||||
const destructiveMethods = ['POST', 'PUT', 'DELETE'];
|
||||
|
@ -14,20 +15,16 @@ const version = require(src('../package.json')).version;
|
|||
describe('xsrf request filter', function () {
|
||||
function inject(kbnServer, opts) {
|
||||
return fn(cb => {
|
||||
kbnServer.server.inject(opts, (resp) => {
|
||||
kbnTestServer.makeRequest(kbnServer, opts, (resp) => {
|
||||
cb(null, resp);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
const makeServer = async function () {
|
||||
const kbnServer = new KbnServer({
|
||||
server: { autoListen: false },
|
||||
plugins: { scanDirs: [src('plugins')] },
|
||||
logging: { quiet: true },
|
||||
optimize: { enabled: false },
|
||||
elasticsearch: {
|
||||
url: 'http://localhost:9210'
|
||||
const kbnServer = kbnTestServer.createServer({
|
||||
server: {
|
||||
xsrf: { disableProtection: false }
|
||||
}
|
||||
});
|
||||
|
||||
|
|
38
test/utils/kbn_server.js
Normal file
38
test/utils/kbn_server.js
Normal file
|
@ -0,0 +1,38 @@
|
|||
import { defaultsDeep } from 'lodash';
|
||||
import requirefrom from 'requirefrom';
|
||||
|
||||
const src = requirefrom('src');
|
||||
const KbnServer = src('server/KbnServer');
|
||||
const fromRoot = src('utils/fromRoot');
|
||||
|
||||
const SERVER_DEFAULTS = {
|
||||
server: {
|
||||
autoListen: false,
|
||||
xsrf: {
|
||||
disableProtection: true
|
||||
}
|
||||
},
|
||||
logging: {
|
||||
quiet: true
|
||||
},
|
||||
plugins: {
|
||||
scanDirs: [
|
||||
fromRoot('src/plugins')
|
||||
]
|
||||
},
|
||||
optimize: {
|
||||
enabled: false
|
||||
},
|
||||
elasticsearch: {
|
||||
url: 'http://localhost:9210'
|
||||
}
|
||||
};
|
||||
|
||||
export function createServer(params = {}) {
|
||||
params = defaultsDeep({}, params, SERVER_DEFAULTS);
|
||||
return new KbnServer(params);
|
||||
};
|
||||
|
||||
export function makeRequest(kbnServer, options, fn) {
|
||||
return kbnServer.server.inject(options, fn);
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue