Skip to content

Commit

Permalink
NP Licensing plugin route handler context (#46586)
Browse files Browse the repository at this point in the history
* introducing a licensing route handler context

* extracting route handler context for easier testing

* address PR feedback
  • Loading branch information
legrego authored Sep 30, 2019
1 parent f9cc456 commit 4473b14
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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);
});
});
19 changes: 19 additions & 0 deletions x-pack/plugins/licensing/server/licensing_route_handler_context.ts
Original file line number Diff line number Diff line change
@@ -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 };
};
}
17 changes: 17 additions & 0 deletions x-pack/plugins/licensing/server/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
],
]
`);
});
});
6 changes: 5 additions & 1 deletion x-pack/plugins/licensing/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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$,
};
}

Expand Down
11 changes: 11 additions & 0 deletions x-pack/plugins/licensing/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 4473b14

Please sign in to comment.