-
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
NP Licensing plugin route handler context #46586
Conversation
@@ -80,4 +80,21 @@ describe('licensing plugin', () => { | |||
|
|||
expect(licenseTypes).toEqual(['basic', 'gold', 'platinum']); | |||
}); | |||
|
|||
test('provides a licensing context to http routes', async () => { |
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.
I'm not sure of the best way to test the new context. This test is basically useless, so I'm open to ideas. I could write a functional test that stands up another plugin for the sole purpose of testing this, but that felt like overkill...
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.
It'd probably be easier to test if you export the context provider function separately from registering it. That way you could have this test to ensure it gets registered, and a separate test that verifies the behavior of the provider function
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.
Sounds good to me! Done in 477bd5b
💚 Build Succeeded |
Related, I've been using the kibana/x-pack/plugins/spaces/server/routes/api/external/get.test.ts Lines 25 to 27 in 0b219db
This gets tricky with contexts, however. If I wanted to write a test that ensured a specific route responded in a certain way based on the status of the license (provided by All that to say, is there a way for tests to instruct the |
I'm not too familiar with the kbnTestServer utility, but I think we should probably have an utility in Core similar to Hapi's Example: import { httpServiceMock } from 'src/core/server/mocks';
// The `createSetupContract` function could return the contract and a `inject`
// function.
const { httpSetup, inject } = httpServiceMock.createSetupContract();
// Register your plugin's routes with the httpSetup
import { registerRoutes } from './routes';
const router = httpSetup.createRouter();
registerRoutes(router);
// Use `inject` to make HTTP requests
const res = await inject({ path: '/myroute' });
// Override context with special mocks. The `context` key could be merged with the default context
const licensing = { license: myMock };
const res = await inject({ path: '/myroute', context: { licensing } }); Alternatively, you could export your handler functions and test them directly, but I think it's good to be able to test validation behavior. |
💚 Build Succeeded |
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.
Looks great!
@@ -121,8 +122,12 @@ export class Plugin implements CorePlugin<LicensingPluginSetup> { | |||
const config = await this.config$.pipe(first()).toPromise(); | |||
const poller = this.create(config, core); | |||
|
|||
const license$ = poller.subject$.asObservable(); |
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.
nit: Remove blank line before this declaration.
|
||
describe('licensingRouteHandlerContext', () => { | ||
it('provides the initial license value', async () => { | ||
const { license } = await setup(); |
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.
Could this be satisfied by consuming the license$
available here?
const { license$, license } = await setup();
const context = createRouteHandlerContext(license$);
const { license: contextResult } = await context({});
expect(contextResult).toBe(license);
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.
Ah yep, I missed that license$
was available as part of setup. I'll simplify this!
💚 Build Succeeded |
💚 Build Succeeded |
* introducing a licensing route handler context * extracting route handler context for easier testing * address PR feedback
Summary
This PR adds a http route handler context for the licensing plugin. Plugins which depend on the
licensing
plugin can receive the current license in their route handlers for easy consumption, with the guarantee that their view of the license will remain consistent for the lifetime of the request.