Skip to content

Commit

Permalink
extracting route handler context for easier testing
Browse files Browse the repository at this point in the history
  • Loading branch information
legrego committed Sep 27, 2019
1 parent 392184a commit 477bd5b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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<ILicense>(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<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 };
};
}
8 changes: 3 additions & 5 deletions x-pack/plugins/licensing/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 @@ -123,10 +124,7 @@ export class Plugin implements CorePlugin<LicensingPluginSetup> {

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$,
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/licensing/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export interface LicensingRequestContext {
license: ILicense;
}

declare module 'kibana/server' {
declare module 'src/core/server' {
interface RequestHandlerContext {
licensing: LicensingRequestContext;
}
Expand Down

0 comments on commit 477bd5b

Please sign in to comment.