Skip to content

Commit

Permalink
Licensing uses es from start contract. (#67291)
Browse files Browse the repository at this point in the history
* use es client provided from start

* expose licengin API from start contract and deprecate setup counterparts

* update tests

* provide licensing API from start contract on the client-side

* update tests

* update mocks
  • Loading branch information
mshustov committed May 26, 2020
1 parent ce0e4f6 commit 5dac38c
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 86 deletions.
2 changes: 1 addition & 1 deletion x-pack/plugins/licensing/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ import { PluginInitializerContext } from 'src/core/public';
import { LicensingPlugin } from './plugin';

export * from '../common/types';
export * from './types';
export { LicensingPluginSetup, LicensingPluginStart } from './types';
export const plugin = (context: PluginInitializerContext) => new LicensingPlugin(context);
46 changes: 27 additions & 19 deletions x-pack/plugins/licensing/public/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { licenseMock } from '../common/licensing.mock';
import { coreMock } from '../../../../src/core/public/mocks';
import { HttpInterceptor } from 'src/core/public';

const coreStart = coreMock.createStart();
describe('licensing plugin', () => {
let plugin: LicensingPlugin;

Expand All @@ -23,7 +24,7 @@ describe('licensing plugin', () => {
await plugin.stop();
});

describe('#setup', () => {
describe('#start', () => {
describe('#refresh', () => {
it('forces data re-fetch', async () => {
const sessionStorage = coreMock.createStorage();
Expand All @@ -38,7 +39,8 @@ describe('licensing plugin', () => {
});
coreSetup.http.get.mockResolvedValueOnce(firstLicense).mockResolvedValueOnce(secondLicense);

const { license$, refresh } = await plugin.setup(coreSetup);
await plugin.setup(coreSetup);
const { license$, refresh } = await plugin.start(coreStart);

let fromObservable;
license$.subscribe((license) => (fromObservable = license));
Expand All @@ -60,7 +62,8 @@ describe('licensing plugin', () => {
const fetchedLicense = licenseMock.createLicense();
coreSetup.http.get.mockResolvedValue(fetchedLicense);

const { refresh } = await plugin.setup(coreSetup);
await plugin.setup(coreSetup);
const { refresh } = await plugin.start(coreStart);

await refresh();

Expand All @@ -78,7 +81,8 @@ describe('licensing plugin', () => {
plugin = new LicensingPlugin(coreMock.createPluginInitializerContext(), sessionStorage);

const coreSetup = coreMock.createSetup();
const { license$ } = await plugin.setup(coreSetup);
await plugin.setup(coreSetup);
const { license$ } = await plugin.start(coreStart);

const license = await license$.pipe(take(1)).toPromise();
expect(license.isAvailable).toBe(true);
Expand All @@ -99,7 +103,9 @@ describe('licensing plugin', () => {
coreSetup.http.get.mockImplementation(() =>
Promise.resolve(licenseMock.createLicense({ license: { type: types.shift() } }))
);
const { license$, refresh } = await plugin.setup(coreSetup);

await plugin.setup(coreSetup);
const { refresh, license$ } = await plugin.start(coreStart);

let i = 0;
license$.subscribe((value) => {
Expand Down Expand Up @@ -128,7 +134,8 @@ describe('licensing plugin', () => {
const fetchedLicense = licenseMock.createLicense({ license: { uid: 'fresh' } });
coreSetup.http.get.mockResolvedValue(fetchedLicense);

const { license$, refresh } = await plugin.setup(coreSetup);
await plugin.setup(coreSetup);
const { license$, refresh } = await plugin.start(coreStart);

await refresh();
const license = await license$.pipe(take(1)).toPromise();
Expand All @@ -153,7 +160,8 @@ describe('licensing plugin', () => {
const coreSetup = coreMock.createSetup();
coreSetup.http.get.mockRejectedValue(new Error('reason'));

const { license$, refresh } = await plugin.setup(coreSetup);
await plugin.setup(coreSetup);
const { license$, refresh } = await plugin.start(coreStart);
await refresh();

const license = await license$.pipe(take(1)).toPromise();
Expand All @@ -169,7 +177,8 @@ describe('licensing plugin', () => {
const coreSetup = coreMock.createSetup();
coreSetup.http.get.mockRejectedValue(new Error('sorry'));

const { license$, refresh } = await plugin.setup(coreSetup);
await plugin.setup(coreSetup);
const { license$, refresh } = await plugin.start(coreStart);
expect(sessionStorage.removeItem).toHaveBeenCalledTimes(0);

await refresh();
Expand Down Expand Up @@ -206,7 +215,8 @@ describe('licensing plugin', () => {
return () => undefined;
});

const { license$ } = await plugin.setup(coreSetup);
await plugin.setup(coreSetup);
const { license$ } = await plugin.start(coreStart);
expect(registeredInterceptor!.response).toBeDefined();

const httpResponse = {
Expand Down Expand Up @@ -284,7 +294,8 @@ describe('licensing plugin', () => {
return () => undefined;
});

const { license$ } = await plugin.setup(coreSetup);
await plugin.setup(coreSetup);
const { license$ } = await plugin.start(coreStart);

let updated = false;
license$.subscribe(() => (updated = true));
Expand Down Expand Up @@ -326,10 +337,8 @@ describe('licensing plugin', () => {
licenseMock.createLicense({ license: { status: 'active', type: 'gold' } })
);

const { refresh } = await plugin.setup(coreSetup);

const coreStart = coreMock.createStart();
await plugin.start(coreStart);
await plugin.setup(coreSetup);
const { refresh } = await plugin.start(coreStart);

await refresh();
expect(coreStart.overlays.banners.add).toHaveBeenCalledTimes(0);
Expand All @@ -352,10 +361,8 @@ describe('licensing plugin', () => {
.mockResolvedValueOnce(activeLicense)
.mockResolvedValueOnce(expiredLicense);

const { refresh } = await plugin.setup(coreSetup);

const coreStart = coreMock.createStart();
await plugin.start(coreStart);
await plugin.setup(coreSetup);
const { refresh } = await plugin.start(coreStart);

await refresh();
expect(coreStart.overlays.banners.add).toHaveBeenCalledTimes(0);
Expand All @@ -377,7 +384,8 @@ describe('licensing plugin', () => {
const sessionStorage = coreMock.createStorage();
plugin = new LicensingPlugin(coreMock.createPluginInitializerContext(), sessionStorage);
const coreSetup = coreMock.createSetup();
const { license$ } = await plugin.setup(coreSetup);
await plugin.setup(coreSetup);
const { license$ } = await plugin.start(coreStart);

let completed = false;
license$.subscribe({ complete: () => (completed = true) });
Expand Down
19 changes: 16 additions & 3 deletions x-pack/plugins/licensing/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { Subject, Subscription } from 'rxjs';
import { Observable, Subject, Subscription } from 'rxjs';

import { CoreSetup, CoreStart, Plugin, PluginInitializerContext } from 'src/core/public';

import { ILicense } from '../common/types';
import { LicensingPluginSetup } from './types';
import { LicensingPluginSetup, LicensingPluginStart } from './types';
import { createLicenseUpdate } from '../common/license_update';
import { License } from '../common/license';
import { mountExpiredBanner } from './expired_banner';
Expand All @@ -20,7 +20,7 @@ export const licensingSessionStorageKey = 'xpack.licensing';
* A plugin for fetching, refreshing, and receiving information about the license for the
* current Kibana instance.
*/
export class LicensingPlugin implements Plugin<LicensingPluginSetup> {
export class LicensingPlugin implements Plugin<LicensingPluginSetup, LicensingPluginStart> {
/**
* Used as a flag to halt all other plugin observables.
*/
Expand All @@ -37,6 +37,9 @@ export class LicensingPlugin implements Plugin<LicensingPluginSetup> {
private coreStart?: CoreStart;
private prevSignature?: string;

private refresh?: () => Promise<ILicense>;
private license$?: Observable<ILicense>;

constructor(
context: PluginInitializerContext,
private readonly storage: Storage = sessionStorage
Expand Down Expand Up @@ -107,6 +110,9 @@ export class LicensingPlugin implements Plugin<LicensingPluginSetup> {
},
});

this.refresh = refreshManually;
this.license$ = license$;

return {
refresh: refreshManually,
license$,
Expand All @@ -115,6 +121,13 @@ export class LicensingPlugin implements Plugin<LicensingPluginSetup> {

public async start(core: CoreStart) {
this.coreStart = core;
if (!this.refresh || !this.license$) {
throw new Error('Setup has not been completed');
}
return {
refresh: this.refresh,
license$: this.license$,
};
}

public stop() {
Expand Down
14 changes: 14 additions & 0 deletions x-pack/plugins/licensing/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ import { ILicense } from '../common/types';

/** @public */
export interface LicensingPluginSetup {
/**
* Steam of licensing information {@link ILicense}.
* @deprecated in favour of the counterpart provided from start contract
*/
license$: Observable<ILicense>;
/**
* Triggers licensing information re-fetch.
* @deprecated in favour of the counterpart provided from start contract
*/
refresh(): Promise<ILicense>;
}

/** @public */
export interface LicensingPluginStart {
/**
* Steam of licensing information {@link ILicense}.
*/
Expand Down
10 changes: 10 additions & 0 deletions x-pack/plugins/licensing/server/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,20 @@ const createSetupMock = (): jest.Mocked<LicensingPluginSetup> => {
};

const createStartMock = (): jest.Mocked<LicensingPluginStart> => {
const license = licenseMock.createLicense();
const mock = {
license$: new BehaviorSubject(license),
refresh: jest.fn(),
createLicensePoller: jest.fn(),
featureUsage: featureUsageMock.createStart(),
};

mock.refresh.mockResolvedValue(license);
mock.createLicensePoller.mockReturnValue({
license$: mock.license$,
refresh: mock.refresh,
});

return mock;
};

Expand Down
Loading

0 comments on commit 5dac38c

Please sign in to comment.