[7.x] NP Licensing plugin route handler context (#46586) (#46946)

* introducing a licensing route handler context

* extracting route handler context for easier testing

* address PR feedback
This commit is contained in:
Larry Gregory 2019-09-30 13:32:15 -04:00 committed by GitHub
parent 8814b0c2b5
commit 24edb62274
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 88 additions and 1 deletions

View file

@ -0,0 +1,36 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { BehaviorSubject } from 'rxjs';
import { ILicense } from './types';
import { setup } from './__fixtures__/setup';
import { createRouteHandlerContext } from './licensing_route_handler_context';
describe('licensingRouteHandlerContext', () => {
it('provides the initial license value', async () => {
const { license$, license } = await setup();
const context = createRouteHandlerContext(license$);
const { license: contextResult } = await context({});
expect(contextResult).toBe(license);
});
it('provides the latest license value', async () => {
const { license } = await setup();
const license$ = new BehaviorSubject<ILicense>(license);
const context = createRouteHandlerContext(license$);
const latestLicense = (Symbol() as unknown) as ILicense;
license$.next(latestLicense);
const { license: contextResult } = await context({});
expect(contextResult).toBe(latestLicense);
});
});

View file

@ -0,0 +1,19 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { RequestHandlerContext, IContextProvider } from 'src/core/server';
import { Observable } from 'rxjs';
import { take } from 'rxjs/operators';
import { ILicense } from './types';
export function createRouteHandlerContext(
license$: Observable<ILicense>
): IContextProvider<RequestHandlerContext, 'licensing'> {
return async function licensingRouteHandlerContext() {
const license = await license$.pipe(take(1)).toPromise();
return { license };
};
}

View file

@ -80,4 +80,21 @@ describe('licensing plugin', () => {
expect(licenseTypes).toEqual(['basic', 'gold', 'platinum']);
});
test('provides a licensing context to http routes', async () => {
const { coreSetup, plugin: _plugin } = await setupOnly();
plugin = _plugin;
await plugin.setup(coreSetup);
expect(coreSetup.http.registerRouteHandlerContext.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"licensing",
[Function],
],
]
`);
});
});

View file

@ -18,6 +18,7 @@ import { Poller } from '../../../../src/core/utils/poller';
import { LicensingConfigType, LicensingPluginSetup, ILicense } from './types';
import { LicensingConfig } from './licensing_config';
import { License } from './license';
import { createRouteHandlerContext } from './licensing_route_handler_context';
export class Plugin implements CorePlugin<LicensingPluginSetup> {
private readonly logger: Logger;
@ -120,9 +121,12 @@ export class Plugin implements CorePlugin<LicensingPluginSetup> {
public async setup(core: CoreSetup) {
const config = await this.config$.pipe(first()).toPromise();
const poller = this.create(config, core);
const license$ = poller.subject$.asObservable();
core.http.registerRouteHandlerContext('licensing', createRouteHandlerContext(license$));
return {
license$: poller.subject$.asObservable(),
license$,
};
}

View file

@ -118,3 +118,14 @@ export type LicensingConfigType = TypeOf<typeof schema>;
export type LicenseType = keyof typeof LICENSE_TYPE;
/** @public */
export type LicenseFeatureSerializer = (licensing: ILicense) => any;
/** @public */
export interface LicensingRequestContext {
license: ILicense;
}
declare module 'src/core/server' {
interface RequestHandlerContext {
licensing: LicensingRequestContext;
}
}