From 477bd5b27f7259c2af20f2d5253d077224558325 Mon Sep 17 00:00:00 2001 From: Larry Gregory Date: Fri, 27 Sep 2019 08:36:15 -0400 Subject: [PATCH] extracting route handler context for easier testing --- .../licensing_route_handler_context.test.ts | 37 +++++++++++++++++++ .../server/licensing_route_handler_context.ts | 19 ++++++++++ x-pack/plugins/licensing/server/plugin.ts | 8 ++-- x-pack/plugins/licensing/server/types.ts | 2 +- 4 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 x-pack/plugins/licensing/server/licensing_route_handler_context.test.ts create mode 100644 x-pack/plugins/licensing/server/licensing_route_handler_context.ts diff --git a/x-pack/plugins/licensing/server/licensing_route_handler_context.test.ts b/x-pack/plugins/licensing/server/licensing_route_handler_context.test.ts new file mode 100644 index 0000000000000..1159e45d31476 --- /dev/null +++ b/x-pack/plugins/licensing/server/licensing_route_handler_context.test.ts @@ -0,0 +1,37 @@ +/* + * 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 } = await setup(); + const license$ = new BehaviorSubject(license); + + 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(license); + + const context = createRouteHandlerContext(license$); + + const latestLicense = (Symbol() as unknown) as ILicense; + license$.next(latestLicense); + + const { license: contextResult } = await context({}); + + expect(contextResult).toBe(latestLicense); + }); +}); diff --git a/x-pack/plugins/licensing/server/licensing_route_handler_context.ts b/x-pack/plugins/licensing/server/licensing_route_handler_context.ts new file mode 100644 index 0000000000000..4705a6705da01 --- /dev/null +++ b/x-pack/plugins/licensing/server/licensing_route_handler_context.ts @@ -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 +): IContextProvider { + return async function licensingRouteHandlerContext() { + const license = await license$.pipe(take(1)).toPromise(); + return { license }; + }; +} diff --git a/x-pack/plugins/licensing/server/plugin.ts b/x-pack/plugins/licensing/server/plugin.ts index 7e97ac11cbf8b..33fd8c6b090bf 100644 --- a/x-pack/plugins/licensing/server/plugin.ts +++ b/x-pack/plugins/licensing/server/plugin.ts @@ -5,7 +5,7 @@ */ import { Observable } from 'rxjs'; -import { first, map, take } from 'rxjs/operators'; +import { first, map } from 'rxjs/operators'; import moment from 'moment'; import { CoreSetup, @@ -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 { private readonly logger: Logger; @@ -123,10 +124,7 @@ export class Plugin implements CorePlugin { const license$ = poller.subject$.asObservable(); - core.http.registerRouteHandlerContext('licensing', async () => { - const license = await license$.pipe(take(1)).toPromise(); - return { license }; - }); + core.http.registerRouteHandlerContext('licensing', createRouteHandlerContext(license$)); return { license$, diff --git a/x-pack/plugins/licensing/server/types.ts b/x-pack/plugins/licensing/server/types.ts index c024e2b8d5ec9..27d3502b44779 100644 --- a/x-pack/plugins/licensing/server/types.ts +++ b/x-pack/plugins/licensing/server/types.ts @@ -124,7 +124,7 @@ export interface LicensingRequestContext { license: ILicense; } -declare module 'kibana/server' { +declare module 'src/core/server' { interface RequestHandlerContext { licensing: LicensingRequestContext; }