From d99ee26cc87c657fcfaba0656d55dd9ace26cb10 Mon Sep 17 00:00:00 2001 From: Josh Dover Date: Wed, 24 Jun 2020 13:04:11 -0600 Subject: [PATCH 1/3] Add featureUsage API to licensing context provider --- .../server/licensing_route_handler_context.test.ts | 13 ++++++++++++- .../server/licensing_route_handler_context.ts | 9 +++++++-- x-pack/plugins/licensing/server/plugin.ts | 5 ++++- x-pack/plugins/licensing/server/types.ts | 1 + 4 files changed, 24 insertions(+), 4 deletions(-) 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 index 29bff40293958..6306578e0a9b7 100644 --- a/x-pack/plugins/licensing/server/licensing_route_handler_context.test.ts +++ b/x-pack/plugins/licensing/server/licensing_route_handler_context.test.ts @@ -8,6 +8,7 @@ import { BehaviorSubject } from 'rxjs'; import { licenseMock } from '../common/licensing.mock'; import { createRouteHandlerContext } from './licensing_route_handler_context'; +import { featureUsageMock } from './services/feature_usage_service.mock'; describe('createRouteHandlerContext', () => { it('returns a function providing the last license value', async () => { @@ -15,7 +16,7 @@ describe('createRouteHandlerContext', () => { const secondLicense = licenseMock.createLicense(); const license$ = new BehaviorSubject(firstLicense); - const routeHandler = createRouteHandlerContext(license$); + const routeHandler = createRouteHandlerContext(license$, featureUsageMock.createStart()); const firstCtx = await routeHandler({} as any, {} as any, {} as any); license$.next(secondLicense); @@ -24,4 +25,14 @@ describe('createRouteHandlerContext', () => { expect(firstCtx.license).toBe(firstLicense); expect(secondCtx.license).toBe(secondLicense); }); + + it('returns a the feature usage API', async () => { + const license$ = new BehaviorSubject(licenseMock.createLicense()); + const featureUsage = featureUsageMock.createStart(); + + const routeHandler = createRouteHandlerContext(license$, featureUsage); + const ctx = await routeHandler({} as any, {} as any, {} as any); + + expect(ctx.featureUsage).toBe(featureUsage); + }); }); diff --git a/x-pack/plugins/licensing/server/licensing_route_handler_context.ts b/x-pack/plugins/licensing/server/licensing_route_handler_context.ts index 42cb0959fc373..33b800465905f 100644 --- a/x-pack/plugins/licensing/server/licensing_route_handler_context.ts +++ b/x-pack/plugins/licensing/server/licensing_route_handler_context.ts @@ -9,6 +9,7 @@ import { Observable } from 'rxjs'; import { take } from 'rxjs/operators'; import { ILicense } from '../common/types'; +import { FeatureUsageServiceStart } from './services'; /** * Create a route handler context for access to Kibana license information. @@ -16,9 +17,13 @@ import { ILicense } from '../common/types'; * @public */ export function createRouteHandlerContext( - license$: Observable + license$: Observable, + featureUsage: FeatureUsageServiceStart ): IContextProvider, 'licensing'> { return async function licensingRouteHandlerContext() { - return { license: await license$.pipe(take(1)).toPromise() }; + return { + license: await license$.pipe(take(1)).toPromise(), + featureUsage, + }; }; } diff --git a/x-pack/plugins/licensing/server/plugin.ts b/x-pack/plugins/licensing/server/plugin.ts index e1aa4a1b32517..13b34057fb8c2 100644 --- a/x-pack/plugins/licensing/server/plugin.ts +++ b/x-pack/plugins/licensing/server/plugin.ts @@ -128,7 +128,10 @@ export class LicensingPlugin implements Plugin Date: Thu, 25 Jun 2020 10:21:30 -0600 Subject: [PATCH 2/3] Use getStartServices --- .../licensing_route_handler_context.test.ts | 15 ++++++++++++--- .../server/licensing_route_handler_context.ts | 11 +++++++---- x-pack/plugins/licensing/server/plugin.ts | 2 +- x-pack/plugins/licensing/server/types.ts | 2 +- 4 files changed, 21 insertions(+), 9 deletions(-) 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 index 6306578e0a9b7..4942d21f64ee2 100644 --- a/x-pack/plugins/licensing/server/licensing_route_handler_context.test.ts +++ b/x-pack/plugins/licensing/server/licensing_route_handler_context.test.ts @@ -5,10 +5,19 @@ */ import { BehaviorSubject } from 'rxjs'; -import { licenseMock } from '../common/licensing.mock'; +import { licenseMock } from '../common/licensing.mock'; import { createRouteHandlerContext } from './licensing_route_handler_context'; import { featureUsageMock } from './services/feature_usage_service.mock'; +import { FeatureUsageServiceStart } from './services'; +import { StartServicesAccessor } from 'src/core/server'; +import { LicensingPluginStart } from './types'; + +const createStartServices = ( + featureUsage: FeatureUsageServiceStart = featureUsageMock.createStart() +): StartServicesAccessor<{}, LicensingPluginStart> => { + return async () => [{} as any, {}, { featureUsage } as LicensingPluginStart]; +}; describe('createRouteHandlerContext', () => { it('returns a function providing the last license value', async () => { @@ -16,7 +25,7 @@ describe('createRouteHandlerContext', () => { const secondLicense = licenseMock.createLicense(); const license$ = new BehaviorSubject(firstLicense); - const routeHandler = createRouteHandlerContext(license$, featureUsageMock.createStart()); + const routeHandler = createRouteHandlerContext(license$, createStartServices()); const firstCtx = await routeHandler({} as any, {} as any, {} as any); license$.next(secondLicense); @@ -30,7 +39,7 @@ describe('createRouteHandlerContext', () => { const license$ = new BehaviorSubject(licenseMock.createLicense()); const featureUsage = featureUsageMock.createStart(); - const routeHandler = createRouteHandlerContext(license$, featureUsage); + const routeHandler = createRouteHandlerContext(license$, createStartServices(featureUsage)); const ctx = await routeHandler({} as any, {} as any, {} as any); expect(ctx.featureUsage).toBe(featureUsage); diff --git a/x-pack/plugins/licensing/server/licensing_route_handler_context.ts b/x-pack/plugins/licensing/server/licensing_route_handler_context.ts index 33b800465905f..736a2151a3dbd 100644 --- a/x-pack/plugins/licensing/server/licensing_route_handler_context.ts +++ b/x-pack/plugins/licensing/server/licensing_route_handler_context.ts @@ -4,12 +4,12 @@ * you may not use this file except in compliance with the Elastic License. */ -import { IContextProvider, RequestHandler } from 'src/core/server'; +import { IContextProvider, RequestHandler, StartServicesAccessor } from 'src/core/server'; import { Observable } from 'rxjs'; import { take } from 'rxjs/operators'; import { ILicense } from '../common/types'; -import { FeatureUsageServiceStart } from './services'; +import { LicensingPluginStart } from './types'; /** * Create a route handler context for access to Kibana license information. @@ -18,12 +18,15 @@ import { FeatureUsageServiceStart } from './services'; */ export function createRouteHandlerContext( license$: Observable, - featureUsage: FeatureUsageServiceStart + getStartServices: StartServicesAccessor<{}, LicensingPluginStart> ): IContextProvider, 'licensing'> { return async function licensingRouteHandlerContext() { + const [, , { featureUsage }] = await getStartServices(); + const license = await license$.pipe(take(1)).toPromise(); + return { - license: await license$.pipe(take(1)).toPromise(), featureUsage, + license, }; }; } diff --git a/x-pack/plugins/licensing/server/plugin.ts b/x-pack/plugins/licensing/server/plugin.ts index 13b34057fb8c2..0a6964b1b829d 100644 --- a/x-pack/plugins/licensing/server/plugin.ts +++ b/x-pack/plugins/licensing/server/plugin.ts @@ -130,7 +130,7 @@ export class LicensingPlugin implements Plugin Date: Thu, 25 Jun 2020 12:23:47 -0600 Subject: [PATCH 3/3] Fix mocks --- x-pack/mocks.ts | 2 +- .../features/server/routes/index.test.ts | 4 +--- x-pack/plugins/licensing/server/mocks.ts | 18 +++++++++++++++++- x-pack/plugins/licensing/server/types.ts | 14 ++++++++++---- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/x-pack/mocks.ts b/x-pack/mocks.ts index 28c589bee4baa..777c8d0a08131 100644 --- a/x-pack/mocks.ts +++ b/x-pack/mocks.ts @@ -9,7 +9,7 @@ import { licensingMock } from './plugins/licensing/server/mocks'; function createCoreRequestHandlerContextMock() { return { core: coreMock.createRequestHandlerContext(), - licensing: { license: licensingMock.createLicense() }, + licensing: licensingMock.createRequestHandlerContext(), }; } diff --git a/x-pack/plugins/features/server/routes/index.test.ts b/x-pack/plugins/features/server/routes/index.test.ts index c2e8cd6129d80..3d1efc8a479b2 100644 --- a/x-pack/plugins/features/server/routes/index.test.ts +++ b/x-pack/plugins/features/server/routes/index.test.ts @@ -16,9 +16,7 @@ import { FeatureConfig } from '../../common'; function createContextMock(licenseType: LicenseType = 'gold') { return { core: coreMock.createRequestHandlerContext(), - licensing: { - license: licensingMock.createLicense({ license: { type: licenseType } }), - }, + licensing: licensingMock.createRequestHandlerContext({ license: { type: licenseType } }), }; } diff --git a/x-pack/plugins/licensing/server/mocks.ts b/x-pack/plugins/licensing/server/mocks.ts index 0d154f76d5134..1a2b543b47df5 100644 --- a/x-pack/plugins/licensing/server/mocks.ts +++ b/x-pack/plugins/licensing/server/mocks.ts @@ -4,7 +4,11 @@ * you may not use this file except in compliance with the Elastic License. */ import { BehaviorSubject } from 'rxjs'; -import { LicensingPluginSetup, LicensingPluginStart } from './types'; +import { + LicensingPluginSetup, + LicensingPluginStart, + LicensingRequestHandlerContext, +} from './types'; import { licenseMock } from '../common/licensing.mock'; import { featureUsageMock } from './services/feature_usage_service.mock'; @@ -43,8 +47,20 @@ const createStartMock = (): jest.Mocked => { return mock; }; +const createRequestHandlerContextMock = ( + ...options: Parameters +): jest.Mocked => { + const mock: jest.Mocked = { + license: licenseMock.createLicense(...options), + featureUsage: featureUsageMock.createStart(), + }; + + return mock; +}; + export const licensingMock = { createSetup: createSetupMock, createStart: createStartMock, + createRequestHandlerContext: createRequestHandlerContextMock, ...licenseMock, }; diff --git a/x-pack/plugins/licensing/server/types.ts b/x-pack/plugins/licensing/server/types.ts index 98c47e716ba3c..53d9a8ddfcb87 100644 --- a/x-pack/plugins/licensing/server/types.ts +++ b/x-pack/plugins/licensing/server/types.ts @@ -40,12 +40,18 @@ export interface RawLicense { mode: LicenseType; } +/** + * The APIs exposed on the `licensing` key of {@link RequestHandlerContext} for plugins that depend on licensing. + * @public + */ +export interface LicensingRequestHandlerContext { + featureUsage: FeatureUsageServiceStart; + license: ILicense; +} + declare module 'src/core/server' { interface RequestHandlerContext { - licensing: { - featureUsage: FeatureUsageServiceStart; - license: ILicense; - }; + licensing: LicensingRequestHandlerContext; } }