-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Add a method for plugins to add injected vars to every app #9071
Merged
spalger
merged 8 commits into
elastic:master
from
spalger:implement/extend-injected-vars
Nov 18, 2016
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7ad237c
[uiExports] add replaceInjectedVars() export type
5ac383d
[ui] do not assume es plugin is always there
3a97f69
[server/status] fix typo
33aa9de
[ui] add errror handling to /app/{id} endpoint
4471453
[ui] add tests for replaceInjectedVars()
d316ff5
Merge branch 'master' of github.com:elastic/kibana into implement/ext…
c0e6a62
[npm] swap out jsdom with cheerio
3d833e8
[server/ui] continue extender => replacer rename
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,13 @@ | ||
module.exports = kibana => new kibana.Plugin({ | ||
uiExports: { | ||
app: { | ||
name: 'test_app', | ||
main: 'plugins/test_app/index.js', | ||
injectVars() { | ||
return { | ||
from_test_app: true | ||
}; | ||
} | ||
} | ||
} | ||
}); |
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,4 @@ | ||
{ | ||
"name": "test_app", | ||
"version": "kibana" | ||
} |
Empty file.
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,125 @@ | ||
import { resolve } from 'path'; | ||
|
||
import { delay } from 'bluebird'; | ||
import expect from 'expect.js'; | ||
import sinon from 'sinon'; | ||
import jsdom from 'jsdom'; | ||
import { noop } from 'lodash'; | ||
|
||
import KbnServer from '../../server/kbn_server'; | ||
|
||
const getInjectedVarsFromResponse = (resp) => { | ||
const document = jsdom.jsdom(resp.payload); | ||
const data = document.querySelector('kbn-initial-state').getAttribute('data'); | ||
return JSON.parse(data).vars; | ||
}; | ||
|
||
const injectReplacer = (kbnServer, replacer) => { | ||
// normally the replacer would be defined in a plugin's uiExports, | ||
// but that requires stubbing out an entire plugin directory for | ||
// each test, so we fake it and jam the replacer into uiExports | ||
kbnServer.uiExports.injectedVarsReplacers.push(replacer); | ||
}; | ||
|
||
describe('UiExports', function () { | ||
describe('#replaceInjectedVars', function () { | ||
this.slow(2000); | ||
this.timeout(10000); | ||
|
||
let kbnServer; | ||
beforeEach(async () => { | ||
kbnServer = new KbnServer({ | ||
server: { port: 0 }, // pick a random open port | ||
logging: { silent: true }, // no logs | ||
optimize: { enabled: false }, | ||
uiSettings: { enabled: false }, | ||
plugins: { | ||
paths: [resolve(__dirname, './fixtures/test_app')] // inject an app so we can hit /app/{id} | ||
}, | ||
}); | ||
|
||
await kbnServer.ready(); | ||
kbnServer.status.get('ui settings').state = 'green'; | ||
kbnServer.server.decorate('server', 'uiSettings', () => { | ||
return { getDefaults: noop }; | ||
}); | ||
}); | ||
|
||
afterEach(async () => { | ||
await kbnServer.close(); | ||
kbnServer = null; | ||
}); | ||
|
||
it('allows sync replacing of injected vars', async () => { | ||
injectReplacer(kbnServer, () => ({ a: 1 })); | ||
|
||
const resp = await kbnServer.inject('/app/test_app'); | ||
const injectedVars = getInjectedVarsFromResponse(resp); | ||
|
||
expect(injectedVars).to.eql({ a: 1 }); | ||
}); | ||
|
||
it('allows async replacing of injected vars', async () => { | ||
const asyncThing = () => delay(100).return('world'); | ||
|
||
injectReplacer(kbnServer, async () => { | ||
return { | ||
hello: await asyncThing() | ||
}; | ||
}); | ||
|
||
const resp = await kbnServer.inject('/app/test_app'); | ||
const injectedVars = getInjectedVarsFromResponse(resp); | ||
|
||
expect(injectedVars).to.eql({ | ||
hello: 'world' | ||
}); | ||
}); | ||
|
||
it('passes originalInjectedVars, request, and server to replacer', async () => { | ||
const stub = sinon.stub(); | ||
injectReplacer(kbnServer, () => ({ foo: 'bar' })); | ||
injectReplacer(kbnServer, stub); | ||
|
||
await kbnServer.inject('/app/test_app'); | ||
|
||
sinon.assert.calledOnce(stub); | ||
expect(stub.firstCall.args[0]).to.eql({ foo: 'bar' }); // originalInjectedVars | ||
expect(stub.firstCall.args[1]).to.have.property('path', '/app/test_app'); // request | ||
expect(stub.firstCall.args[1]).to.have.property('server', kbnServer.server); // request | ||
expect(stub.firstCall.args[2]).to.be(kbnServer.server); | ||
}); | ||
|
||
it('calls the methods sequentially', async () => { | ||
injectReplacer(kbnServer, () => ({ name: '' })); | ||
injectReplacer(kbnServer, orig => ({ name: orig.name + 's' })); | ||
injectReplacer(kbnServer, orig => ({ name: orig.name + 'a' })); | ||
injectReplacer(kbnServer, orig => ({ name: orig.name + 'm' })); | ||
|
||
const resp = await kbnServer.inject('/app/test_app'); | ||
const injectedVars = getInjectedVarsFromResponse(resp); | ||
|
||
expect(injectedVars).to.eql({ name: 'sam' }); | ||
}); | ||
|
||
it('propogates errors thrown in replacers', async () => { | ||
injectReplacer(kbnServer, async () => { | ||
await delay(100); | ||
throw new Error('replacer failed'); | ||
}); | ||
|
||
const resp = await kbnServer.inject('/app/test_app'); | ||
expect(resp).to.have.property('statusCode', 500); | ||
}); | ||
|
||
it('starts off with the injected vars for the app merged with the default injected vars', async () => { | ||
const stub = sinon.stub(); | ||
injectReplacer(kbnServer, stub); | ||
kbnServer.uiExports.defaultInjectedVars.from_defaults = true; | ||
|
||
const resp = await kbnServer.inject('/app/test_app'); | ||
sinon.assert.calledOnce(stub); | ||
expect(stub.firstCall.args[0]).to.eql({ from_defaults: true, from_test_app: true }); | ||
}); | ||
}); | ||
}); |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't you just use jquery to do the css selection and read the data from the DOM here? And if not, wouldn't something like cheerio be a lighter solution for it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests are run in node, so jQuery isn't an option. cheerio looks perfect