Skip to content

Commit

Permalink
Cleanup tests a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecote committed Feb 7, 2020
1 parent 4583dae commit c3b6034
Showing 1 changed file with 92 additions and 83 deletions.
175 changes: 92 additions & 83 deletions x-pack/plugins/actions/server/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,136 +4,145 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { ActionsPlugin } from './plugin';
import { coreMock } from '../../../../src/core/server/mocks';
import { ActionsPlugin, ActionsPluginsSetup, ActionsPluginsStart } from './plugin';
import { coreMock, httpServerMock } from '../../../../src/core/server/mocks';
import { licensingMock } from '../../licensing/server/mocks';
import { encryptedSavedObjectsMock } from '../../encrypted_saved_objects/server/mocks';
import { taskManagerMock } from '../../task_manager/server/mocks';

/**
* HACK: These tests were put together to ensuire the routeHandlerContext and the function
* "getActionsClientWithRequest" throws when needed. There's a lot of blockers for writing
* a proper test like misisng plugin start/setup mocks for taskManager and event log.
* This test contains what is needed to get to the necessary function within setup() and start().
*/
describe('Actions Plugin', () => {
describe('setup()', () => {
let plugin: ActionsPlugin;
let coreSetup: ReturnType<typeof coreMock.createSetup>;
let pluginsSetup: jest.Mocked<ActionsPluginsSetup>;

beforeEach(() => {
const context = coreMock.createPluginInitializerContext();
plugin = new ActionsPlugin(context);
coreSetup = coreMock.createSetup();
pluginsSetup = {
taskManager: taskManagerMock.createSetup(),
encryptedSavedObjects: encryptedSavedObjectsMock.createSetup(),
licensing: licensingMock.createSetup(),
// TODO: Pull event log mock once https://github.com/elastic/kibana/pull/57048 merged
event_log: {
isEnabled: jest.fn(),
isLoggingEntries: jest.fn(),
isIndexingEntries: jest.fn(),
registerProviderActions: jest.fn(),
isProviderActionRegistered: jest.fn(),
getProviderActions: jest.fn(),
getLogger: jest.fn(),
},
};
});

describe('routeHandlerContext.getActionsClient()', () => {
it('should not throw error when ESO plugin not using a generated key', async () => {
const context = coreMock.createPluginInitializerContext();
const plugin = new ActionsPlugin(context);

const coreSetup = coreMock.createSetup();
await plugin.setup(coreSetup, {
taskManager: {
registerTaskDefinitions: jest.fn(),
} as any,
...pluginsSetup,
encryptedSavedObjects: {
...encryptedSavedObjectsMock.createSetup(),
...pluginsSetup.encryptedSavedObjects,
usingEphemeralEncryptionKey: false,
},
licensing: licensingMock.createSetup(),
event_log: {
getLogger: jest.fn(),
registerProviderActions: jest.fn(),
} as any,
});

expect(coreSetup.http.registerRouteHandlerContext).toHaveBeenCalledTimes(1);
const handler = coreSetup.http.registerRouteHandlerContext.mock.calls[0];
expect(handler[0]).toEqual('actions');

const actionsContextHandler = await (handler[1](
const actionsContextHandler = (await handler[1](
{
core: coreMock.createStart(),
core: {
savedObjects: {
client: {},
},
},
} as any,
{} as any,
{} as any
) as any)!;
httpServerMock.createKibanaRequest(),
httpServerMock.createResponseFactory()
)) as any;
actionsContextHandler.getActionsClient();
});

it('should throw error when ESO plugin using a generated key', async () => {
const context = coreMock.createPluginInitializerContext();
const plugin = new ActionsPlugin(context);

const coreSetup = coreMock.createSetup();
await plugin.setup(coreSetup, {
taskManager: {
registerTaskDefinitions: jest.fn(),
} as any,
encryptedSavedObjects: encryptedSavedObjectsMock.createSetup(),
licensing: licensingMock.createSetup(),
event_log: {
getLogger: jest.fn(),
registerProviderActions: jest.fn(),
} as any,
});
await plugin.setup(coreSetup, pluginsSetup);

expect(coreSetup.http.registerRouteHandlerContext).toHaveBeenCalledTimes(1);
const handler = coreSetup.http.registerRouteHandlerContext.mock.calls[0];
expect(handler[0]).toEqual('actions');

const actionsContextHandler = await (handler[1]({} as any, {} as any, {} as any) as any)!;
const actionsContextHandler = (await handler[1](
{
core: {
savedObjects: {
client: {},
},
},
} as any,
httpServerMock.createKibanaRequest(),
httpServerMock.createResponseFactory()
)) as any;
expect(() => actionsContextHandler.getActionsClient()).toThrowErrorMatchingInlineSnapshot(
`"Unable to create actions client due to the Encrypted Saved Objects plugin using an ephemeral encryption key. Please set xpack.encryptedSavedObjects.encryptionKey in kibana.yml"`
);
});
});
});
describe('start()', () => {
let plugin: ActionsPlugin;
let coreSetup: ReturnType<typeof coreMock.createSetup>;
let coreStart: ReturnType<typeof coreMock.createStart>;
let pluginsSetup: jest.Mocked<ActionsPluginsSetup>;
let pluginsStart: jest.Mocked<ActionsPluginsStart>;

beforeEach(() => {
const context = coreMock.createPluginInitializerContext();
plugin = new ActionsPlugin(context);
coreSetup = coreMock.createSetup();
coreStart = coreMock.createStart();
pluginsSetup = {
taskManager: taskManagerMock.createSetup(),
encryptedSavedObjects: encryptedSavedObjectsMock.createSetup(),
licensing: licensingMock.createSetup(),
// TODO: Pull event log mock once https://github.com/elastic/kibana/pull/57048 merged
event_log: {
isEnabled: jest.fn(),
isLoggingEntries: jest.fn(),
isIndexingEntries: jest.fn(),
registerProviderActions: jest.fn(),
isProviderActionRegistered: jest.fn(),
getProviderActions: jest.fn(),
getLogger: jest.fn(),
},
};
pluginsStart = {
taskManager: taskManagerMock.createStart(),
encryptedSavedObjects: encryptedSavedObjectsMock.createStart(),
};
});

describe('getActionsClientWithRequest()', () => {
it('should not throw error when ESO plugin not using a generated key', async () => {
const context = coreMock.createPluginInitializerContext();
const plugin = new ActionsPlugin(context);

await plugin.setup(coreMock.createSetup(), {
taskManager: {
registerTaskDefinitions: jest.fn(),
} as any,
await plugin.setup(coreSetup, {
...pluginsSetup,
encryptedSavedObjects: {
...encryptedSavedObjectsMock.createSetup(),
...pluginsSetup.encryptedSavedObjects,
usingEphemeralEncryptionKey: false,
},
licensing: licensingMock.createSetup(),
event_log: {
getLogger: jest.fn(),
registerProviderActions: jest.fn(),
} as any,
});

const pluginStart = plugin.start(coreMock.createStart(), {
taskManager: {} as any,
encryptedSavedObjects: encryptedSavedObjectsMock.createStart(),
});
const pluginStart = plugin.start(coreStart, pluginsStart);

await pluginStart.getActionsClientWithRequest({} as any);
await pluginStart.getActionsClientWithRequest(httpServerMock.createKibanaRequest());
});

it('should throw error when ESO plugin using generated key', async () => {
const context = coreMock.createPluginInitializerContext();
const plugin = new ActionsPlugin(context);

const esoPluginSetup = encryptedSavedObjectsMock.createSetup();
await plugin.setup(coreMock.createSetup(), {
taskManager: {
registerTaskDefinitions: jest.fn(),
} as any,
encryptedSavedObjects: esoPluginSetup,
licensing: licensingMock.createSetup(),
event_log: {
getLogger: jest.fn(),
registerProviderActions: jest.fn(),
} as any,
});

const pluginStart = plugin.start(coreMock.createStart(), {
taskManager: {} as any,
encryptedSavedObjects: encryptedSavedObjectsMock.createStart(),
});
await plugin.setup(coreSetup, pluginsSetup);
const pluginStart = plugin.start(coreStart, pluginsStart);

expect(esoPluginSetup.usingEphemeralEncryptionKey).toEqual(true);
expect(pluginsSetup.encryptedSavedObjects.usingEphemeralEncryptionKey).toEqual(true);
await expect(
pluginStart.getActionsClientWithRequest({} as any)
pluginStart.getActionsClientWithRequest(httpServerMock.createKibanaRequest())
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Unable to create actions client due to the Encrypted Saved Objects plugin using an ephemeral encryption key. Please set xpack.encryptedSavedObjects.encryptionKey in kibana.yml"`
);
Expand Down

0 comments on commit c3b6034

Please sign in to comment.