Skip to content

Commit

Permalink
[uiSettings/routes] add tests that verify error behaviors
Browse files Browse the repository at this point in the history
  • Loading branch information
spalger committed Jul 31, 2017
1 parent e902cd6 commit 256a0b2
Show file tree
Hide file tree
Showing 10 changed files with 438 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('plugins/elasticsearch', () => {
const sandbox = sinon.sandbox.create();

function getTimerCount() {
return Object.keys(sandbox.clock.timers || {});
return Object.keys(sandbox.clock.timers || {}).length;
}

beforeEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ describe('uiSettingsMixin()', () => {
// mock hapi server
const server = {
log: sinon.stub(),
route: sinon.stub(),
config: () => config,
addMemoizedFactoryToRequest(name, factory) {
this.decorate('request', name, function () {
Expand Down
134 changes: 134 additions & 0 deletions src/ui/ui_settings/routes/__tests__/doc_exists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import expect from 'expect.js';
import sinon from 'sinon';

import {
getServices,
chance,
assertSinonMatch,
} from './lib';

export function docExistsSuite() {
async function setup(options = {}) {
const {
initialSettings
} = options;

const { kbnServer, uiSettings } = getServices();

if (initialSettings) {
await uiSettings.setMany(initialSettings);
}

return { kbnServer, uiSettings };
}

describe('get route', () => {
it('returns a 200 and includes userValues', async () => {
const defaultIndex = chance.word({ length: 10 });
const { kbnServer } = await setup({
initialSettings: {
defaultIndex
}
});

const { statusCode, result } = await kbnServer.inject({
method: 'GET',
url: '/api/kibana/settings'
});

expect(statusCode).to.be(200);
assertSinonMatch(result, {
settings: {
buildNum: {
userValue: sinon.match.number
},
defaultIndex: {
userValue: defaultIndex
}
}
});
});
});

describe('set route', () => {
it('returns a 200 and all values including update', async () => {
const { kbnServer } = await setup();

const defaultIndex = chance.word();
const { statusCode, result } = await kbnServer.inject({
method: 'POST',
url: '/api/kibana/settings/defaultIndex',
payload: {
value: defaultIndex
}
});

expect(statusCode).to.be(200);
assertSinonMatch(result, {
settings: {
buildNum: {
userValue: sinon.match.number
},
defaultIndex: {
userValue: defaultIndex
}
}
});
});
});

describe('setMany route', () => {
it('returns a 200 and all values including updates', async () => {
const { kbnServer } = await setup();

const defaultIndex = chance.word();
const { statusCode, result } = await kbnServer.inject({
method: 'POST',
url: '/api/kibana/settings',
payload: {
changes: {
defaultIndex
}
}
});

expect(statusCode).to.be(200);
assertSinonMatch(result, {
settings: {
buildNum: {
userValue: sinon.match.number
},
defaultIndex: {
userValue: defaultIndex
}
}
});
});
});

describe('delete route', () => {
it('returns a 200 and deletes the setting', async () => {
const defaultIndex = chance.word({ length: 10 });

const { kbnServer, uiSettings } = await setup({
initialSettings: { defaultIndex }
});

expect(await uiSettings.get('defaultIndex')).to.be(defaultIndex);

const { statusCode, result } = await kbnServer.inject({
method: 'DELETE',
url: '/api/kibana/settings/defaultIndex'
});

expect(statusCode).to.be(200);
assertSinonMatch(result, {
settings: {
buildNum: {
userValue: sinon.match.number
}
}
});
});
});
}
77 changes: 77 additions & 0 deletions src/ui/ui_settings/routes/__tests__/doc_missing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import expect from 'expect.js';

import {
getServices,
chance,
assertDocMissingResponse
} from './lib';

export function docMissingSuite() {
async function setup() {
const { kbnServer, savedObjectsClient } = getServices();

// delete all config docs
const { saved_objects: objs } = await savedObjectsClient.find({ type: 'config' });

for (const obj of objs) {
await savedObjectsClient.delete(obj.type, obj.id);
}

return { kbnServer };
}

describe('get route', () => {
it('returns a 200 with empty values', async () => {
const { kbnServer } = await setup();

const { statusCode, result } = await kbnServer.inject({
method: 'GET',
url: '/api/kibana/settings'
});

expect(statusCode).to.be(200);
expect(result).to.eql({ settings: {} });
});
});

describe('set route', () => {
it('returns a 404', async () => {
const { kbnServer } = await setup();

assertDocMissingResponse(await kbnServer.inject({
method: 'POST',
url: '/api/kibana/settings/defaultIndex',
payload: {
value: chance.word()
}
}));
});
});

describe('setMany route', () => {
it('returns a 404', async () => {
const { kbnServer } = await setup();

assertDocMissingResponse(await kbnServer.inject({
method: 'POST',
url: '/api/kibana/settings',
payload: {
changes: {
defaultIndex: chance.word()
}
}
}));
});
});

describe('delete route', () => {
it('returns a 404', async () => {
const { kbnServer } = await setup();

assertDocMissingResponse(await kbnServer.inject({
method: 'DELETE',
url: '/api/kibana/settings/defaultIndex'
}));
});
});
}
23 changes: 23 additions & 0 deletions src/ui/ui_settings/routes/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {
startServers,
stopServers,
} from './lib';

import { docExistsSuite } from './doc_exists';
import { docMissingSuite } from './doc_missing';
import { indexMissingSuite } from './index_missing';

describe('uiSettings/routes', function () {
this.slow(2000);
this.timeout(10000);

// these tests rely on getting sort of lucky with
// the healthcheck, so we retry if they fail
this.retries(3);

before(startServers);
describe('doc exists', docExistsSuite);
describe('doc missing', docMissingSuite);
describe('index missing', indexMissingSuite);
after(stopServers);
});
116 changes: 116 additions & 0 deletions src/ui/ui_settings/routes/__tests__/index_missing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import expect from 'expect.js';

import {
getServices,
chance,
assertDocMissingResponse
} from './lib';

export function indexMissingSuite() {
beforeEach(async function () {
const { kbnServer } = getServices();
await kbnServer.server.plugins.elasticsearch.waitUntilReady();
});

function getNumberOfShards(index) {
return parseInt(Object.values(index)[0].settings.index.number_of_shards, 10);
}

async function getIndex(callCluster, indexName) {
return await callCluster('indices.get', {
index: indexName,
});
}

async function setup() {
const { callCluster, kbnServer } = getServices();
const indexName = kbnServer.config.get('kibana.index');
const initialIndex = await getIndex(callCluster, indexName);

await callCluster('indices.delete', {
index: indexName,
});

return {
kbnServer,

// an incorrect number of shards is how we determine when the index was not created by Kibana,
// but automatically by writing to es when index didn't exist
async assertInvalidKibanaIndex() {
const index = await getIndex(callCluster, indexName);

expect(getNumberOfShards(index))
.to.not.be(getNumberOfShards(initialIndex));
}
};
}

afterEach(async () => {
const { kbnServer, callCluster } = getServices();
await callCluster('indices.delete', {
index: kbnServer.config.get('kibana.index'),
ignore: 404
});
});

describe('get route', () => {
it('returns a 200 and with empty values', async () => {
const { kbnServer } = await setup();

const { statusCode, result } = await kbnServer.inject({
method: 'GET',
url: '/api/kibana/settings'
});

expect(statusCode).to.be(200);
expect(result).to.eql({ settings: {} });
});
});

describe('set route', () => {
it('creates an invalid Kibana index and returns a 404 document missing error', async () => {
const { kbnServer, assertInvalidKibanaIndex } = await setup();

assertDocMissingResponse(await kbnServer.inject({
method: 'POST',
url: '/api/kibana/settings/defaultIndex',
payload: {
value: chance.word()
}
}));

await assertInvalidKibanaIndex();
});
});

describe('setMany route', () => {
it('creates an invalid Kibana index and returns a 404 document missing error', async () => {
const { kbnServer, assertInvalidKibanaIndex } = await setup();

assertDocMissingResponse(await kbnServer.inject({
method: 'POST',
url: '/api/kibana/settings',
payload: {
changes: {
defaultIndex: chance.word()
}
}
}));

await assertInvalidKibanaIndex();
});
});

describe('delete route', () => {
it('creates an invalid Kibana index and returns a 404 document missing error', async () => {
const { kbnServer, assertInvalidKibanaIndex } = await setup();

assertDocMissingResponse(await kbnServer.inject({
method: 'DELETE',
url: '/api/kibana/settings/defaultIndex'
}));

await assertInvalidKibanaIndex();
});
});
}
16 changes: 16 additions & 0 deletions src/ui/ui_settings/routes/__tests__/lib/assert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import sinon from 'sinon';

export function assertSinonMatch(value, match) {
const stub = sinon.stub();
stub(value);
sinon.assert.calledWithExactly(stub, match);
}

export function assertDocMissingResponse({ result }) {
assertSinonMatch(result, {
statusCode: 404,
error: 'Not Found',
message: sinon.match('document_missing_exception')
.and(sinon.match('document missing'))
});
}
3 changes: 3 additions & 0 deletions src/ui/ui_settings/routes/__tests__/lib/chance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Chance from 'chance';

export const chance = new Chance();
Loading

0 comments on commit 256a0b2

Please sign in to comment.