-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[uiSettings/routes] add tests that verify error behaviors
- Loading branch information
Showing
10 changed files
with
438 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
}); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
})); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import Chance from 'chance'; | ||
|
||
export const chance = new Chance(); |
Oops, something went wrong.