From c3b6034189f1f26a1aec38de51829b2fdae7b837 Mon Sep 17 00:00:00 2001 From: Mike Cote Date: Fri, 7 Feb 2020 07:52:02 -0500 Subject: [PATCH] Cleanup tests a bit --- x-pack/plugins/actions/server/plugin.test.ts | 175 ++++++++++--------- 1 file changed, 92 insertions(+), 83 deletions(-) diff --git a/x-pack/plugins/actions/server/plugin.test.ts b/x-pack/plugins/actions/server/plugin.test.ts index e6ce4d3de5d46..cc2047bc922fb 100644 --- a/x-pack/plugins/actions/server/plugin.test.ts +++ b/x-pack/plugins/actions/server/plugin.test.ts @@ -4,76 +4,85 @@ * 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; + let pluginsSetup: jest.Mocked; + + 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"` ); @@ -81,59 +90,59 @@ describe('Actions Plugin', () => { }); }); describe('start()', () => { + let plugin: ActionsPlugin; + let coreSetup: ReturnType; + let coreStart: ReturnType; + let pluginsSetup: jest.Mocked; + let pluginsStart: jest.Mocked; + + 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"` );