From 230ed0a75c3e4d65e04be7f8d36788eb0cf9f4e5 Mon Sep 17 00:00:00 2001 From: Poff Poffenberger Date: Thu, 13 Aug 2020 14:18:44 -0500 Subject: [PATCH] Add functions test and refactor other router tests --- .../server/routes/functions/functions.test.ts | 51 +++++++++++++++++++ .../server/routes/shareables/download.test.ts | 13 ++--- .../server/routes/shareables/zip.test.ts | 14 ++--- .../canvas/server/routes/test_helpers.ts | 29 +++++++++++ .../server/routes/workpad/create.test.ts | 19 ++----- .../server/routes/workpad/delete.test.ts | 18 ++----- .../canvas/server/routes/workpad/find.test.ts | 18 ++----- .../canvas/server/routes/workpad/get.test.ts | 18 ++----- .../server/routes/workpad/update.test.ts | 29 ++++------- 9 files changed, 119 insertions(+), 90 deletions(-) create mode 100644 x-pack/plugins/canvas/server/routes/functions/functions.test.ts create mode 100644 x-pack/plugins/canvas/server/routes/test_helpers.ts diff --git a/x-pack/plugins/canvas/server/routes/functions/functions.test.ts b/x-pack/plugins/canvas/server/routes/functions/functions.test.ts new file mode 100644 index 00000000000000..755af3eb4ef7ef --- /dev/null +++ b/x-pack/plugins/canvas/server/routes/functions/functions.test.ts @@ -0,0 +1,51 @@ +/* + * 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 { kibanaResponseFactory, RequestHandlerContext, RequestHandler } from 'src/core/server'; +import { httpServerMock } from 'src/core/server/mocks'; +import { ExpressionFunction } from 'src/plugins/expressions/common/expression_functions'; +import { initializeGetFunctionsRoute } from './functions'; +import { getMockedRouterDeps } from '../test_helpers'; +import { API_ROUTE_FUNCTIONS } from '../../../common/lib'; +import { functions } from '../../../canvas_plugin_src/functions/server'; + +const mockRouteContext = {} as RequestHandlerContext; +const routePath = API_ROUTE_FUNCTIONS; + +describe('Get list of serverside expression functions', () => { + let routeHandler: RequestHandler; + let mockFuncs: Record; + + beforeEach(() => { + mockFuncs = { + demodata: new ExpressionFunction(functions[0]()), + }; + + const routerDeps = getMockedRouterDeps(); + + routerDeps.expressions.getFunctions.mockReturnValueOnce(mockFuncs); + + initializeGetFunctionsRoute(routerDeps); + + routeHandler = routerDeps.router.get.mock.calls[0][1]; + }); + + afterAll(() => { + jest.restoreAllMocks(); + }); + + it(`returns 200 with list of functions`, async () => { + const request = httpServerMock.createKibanaRequest({ + method: 'get', + path: routePath, + }); + + const response = await routeHandler(mockRouteContext, request, kibanaResponseFactory); + + expect(response.status).toBe(200); + expect(response.payload).toBe(JSON.stringify(mockFuncs)); + }); +}); diff --git a/x-pack/plugins/canvas/server/routes/shareables/download.test.ts b/x-pack/plugins/canvas/server/routes/shareables/download.test.ts index 0267a695ae9fe3..1edf9f52e164a4 100644 --- a/x-pack/plugins/canvas/server/routes/shareables/download.test.ts +++ b/x-pack/plugins/canvas/server/routes/shareables/download.test.ts @@ -8,8 +8,9 @@ jest.mock('fs'); import fs from 'fs'; import { kibanaResponseFactory, RequestHandlerContext, RequestHandler } from 'src/core/server'; -import { httpServiceMock, httpServerMock, loggingSystemMock } from 'src/core/server/mocks'; +import { httpServerMock } from 'src/core/server/mocks'; import { initializeDownloadShareableWorkpadRoute } from './download'; +import { getMockedRouterDeps } from '../test_helpers'; const mockRouteContext = {} as RequestHandlerContext; const path = `api/canvas/workpad/find`; @@ -19,14 +20,10 @@ describe('Download Canvas shareables runtime', () => { let routeHandler: RequestHandler; beforeEach(() => { - const httpService = httpServiceMock.createSetupContract(); - const router = httpService.createRouter(); - initializeDownloadShareableWorkpadRoute({ - router, - logger: loggingSystemMock.create().get(), - }); + const routerDeps = getMockedRouterDeps(); + initializeDownloadShareableWorkpadRoute(routerDeps); - routeHandler = router.get.mock.calls[0][1]; + routeHandler = routerDeps.router.get.mock.calls[0][1]; }); afterAll(() => { diff --git a/x-pack/plugins/canvas/server/routes/shareables/zip.test.ts b/x-pack/plugins/canvas/server/routes/shareables/zip.test.ts index 0c19886f07e5c4..b1649c7524c7eb 100644 --- a/x-pack/plugins/canvas/server/routes/shareables/zip.test.ts +++ b/x-pack/plugins/canvas/server/routes/shareables/zip.test.ts @@ -9,8 +9,9 @@ jest.mock('archiver'); // eslint-disable-next-line @typescript-eslint/no-var-requires const archiver = require('archiver') as jest.Mock; import { kibanaResponseFactory, RequestHandlerContext, RequestHandler } from 'src/core/server'; -import { httpServiceMock, httpServerMock, loggingSystemMock } from 'src/core/server/mocks'; +import { httpServerMock } from 'src/core/server/mocks'; import { initializeZipShareableWorkpadRoute } from './zip'; +import { getMockedRouterDeps } from '../test_helpers'; import { API_ROUTE_SHAREABLE_ZIP } from '../../../common/lib'; import { SHAREABLE_RUNTIME_FILE, @@ -26,14 +27,9 @@ describe('Zips Canvas shareables runtime together with workpad', () => { let routeHandler: RequestHandler; beforeEach(() => { - const httpService = httpServiceMock.createSetupContract(); - const router = httpService.createRouter(); - initializeZipShareableWorkpadRoute({ - router, - logger: loggingSystemMock.create().get(), - }); - - routeHandler = router.post.mock.calls[0][1]; + const routerDeps = getMockedRouterDeps(); + initializeZipShareableWorkpadRoute(routerDeps); + routeHandler = routerDeps.router.post.mock.calls[0][1]; }); afterAll(() => { diff --git a/x-pack/plugins/canvas/server/routes/test_helpers.ts b/x-pack/plugins/canvas/server/routes/test_helpers.ts new file mode 100644 index 00000000000000..695cc79e513dca --- /dev/null +++ b/x-pack/plugins/canvas/server/routes/test_helpers.ts @@ -0,0 +1,29 @@ +/* + * 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 { + httpServiceMock, + loggingSystemMock, + elasticsearchServiceMock, +} from 'src/core/server/mocks'; +import { bfetchPluginMock } from '../../../../../src/plugins/bfetch/server/mocks'; +import { expressionsPluginMock } from '../../../../../src/plugins/expressions/server/mocks'; + +export function getMockedRouterDeps() { + const httpService = httpServiceMock.createSetupContract(); + const elasticsearch = elasticsearchServiceMock.createSetup(); + const bfetch = bfetchPluginMock.createSetupContract(); + const expressions = expressionsPluginMock.createSetupContract(); + const router = httpService.createRouter(); + + return { + router, + expressions, + elasticsearch, + bfetch, + logger: loggingSystemMock.create().get(), + }; +} diff --git a/x-pack/plugins/canvas/server/routes/workpad/create.test.ts b/x-pack/plugins/canvas/server/routes/workpad/create.test.ts index 4756349a8a5ff0..4e927751a026f2 100644 --- a/x-pack/plugins/canvas/server/routes/workpad/create.test.ts +++ b/x-pack/plugins/canvas/server/routes/workpad/create.test.ts @@ -5,15 +5,11 @@ */ import sinon from 'sinon'; -import { - savedObjectsClientMock, - httpServiceMock, - httpServerMock, - loggingSystemMock, -} from 'src/core/server/mocks'; +import { savedObjectsClientMock, httpServerMock } from 'src/core/server/mocks'; import { CANVAS_TYPE } from '../../../common/lib/constants'; import { initializeCreateWorkpadRoute } from './create'; import { kibanaResponseFactory, RequestHandlerContext, RequestHandler } from 'src/core/server'; +import { getMockedRouterDeps } from '../test_helpers'; let mockRouteContext = ({ core: { @@ -44,15 +40,10 @@ describe('POST workpad', () => { clock = sinon.useFakeTimers(now); - const httpService = httpServiceMock.createSetupContract(); + const routerDeps = getMockedRouterDeps(); + initializeCreateWorkpadRoute(routerDeps); - const router = httpService.createRouter(); - initializeCreateWorkpadRoute({ - router, - logger: loggingSystemMock.create().get(), - }); - - routeHandler = router.post.mock.calls[0][1]; + routeHandler = routerDeps.router.post.mock.calls[0][1]; }); afterEach(() => { diff --git a/x-pack/plugins/canvas/server/routes/workpad/delete.test.ts b/x-pack/plugins/canvas/server/routes/workpad/delete.test.ts index 32ce30325b60ad..e66628ffb4d4e4 100644 --- a/x-pack/plugins/canvas/server/routes/workpad/delete.test.ts +++ b/x-pack/plugins/canvas/server/routes/workpad/delete.test.ts @@ -7,12 +7,8 @@ import { CANVAS_TYPE } from '../../../common/lib/constants'; import { initializeDeleteWorkpadRoute } from './delete'; import { kibanaResponseFactory, RequestHandlerContext, RequestHandler } from 'src/core/server'; -import { - savedObjectsClientMock, - httpServiceMock, - httpServerMock, - loggingSystemMock, -} from 'src/core/server/mocks'; +import { savedObjectsClientMock, httpServerMock } from 'src/core/server/mocks'; +import { getMockedRouterDeps } from '../test_helpers'; const mockRouteContext = ({ core: { @@ -26,14 +22,10 @@ describe('DELETE workpad', () => { let routeHandler: RequestHandler; beforeEach(() => { - const httpService = httpServiceMock.createSetupContract(); - const router = httpService.createRouter(); - initializeDeleteWorkpadRoute({ - router, - logger: loggingSystemMock.create().get(), - }); + const routerDeps = getMockedRouterDeps(); + initializeDeleteWorkpadRoute(routerDeps); - routeHandler = router.delete.mock.calls[0][1]; + routeHandler = routerDeps.router.delete.mock.calls[0][1]; }); it(`returns 200 ok when the workpad is deleted`, async () => { diff --git a/x-pack/plugins/canvas/server/routes/workpad/find.test.ts b/x-pack/plugins/canvas/server/routes/workpad/find.test.ts index a87cf7be57d811..593e4062686f91 100644 --- a/x-pack/plugins/canvas/server/routes/workpad/find.test.ts +++ b/x-pack/plugins/canvas/server/routes/workpad/find.test.ts @@ -6,12 +6,8 @@ import { initializeFindWorkpadsRoute } from './find'; import { kibanaResponseFactory, RequestHandlerContext, RequestHandler } from 'src/core/server'; -import { - savedObjectsClientMock, - httpServiceMock, - httpServerMock, - loggingSystemMock, -} from 'src/core/server/mocks'; +import { savedObjectsClientMock, httpServerMock } from 'src/core/server/mocks'; +import { getMockedRouterDeps } from '../test_helpers'; const mockRouteContext = ({ core: { @@ -25,14 +21,10 @@ describe('Find workpad', () => { let routeHandler: RequestHandler; beforeEach(() => { - const httpService = httpServiceMock.createSetupContract(); - const router = httpService.createRouter(); - initializeFindWorkpadsRoute({ - router, - logger: loggingSystemMock.create().get(), - }); + const routerDeps = getMockedRouterDeps(); + initializeFindWorkpadsRoute(routerDeps); - routeHandler = router.get.mock.calls[0][1]; + routeHandler = routerDeps.router.get.mock.calls[0][1]; }); it(`returns 200 with the found workpads`, async () => { diff --git a/x-pack/plugins/canvas/server/routes/workpad/get.test.ts b/x-pack/plugins/canvas/server/routes/workpad/get.test.ts index 8cc190dc6231cc..a51cbefd4031ef 100644 --- a/x-pack/plugins/canvas/server/routes/workpad/get.test.ts +++ b/x-pack/plugins/canvas/server/routes/workpad/get.test.ts @@ -7,14 +7,10 @@ import { CANVAS_TYPE } from '../../../common/lib/constants'; import { initializeGetWorkpadRoute } from './get'; import { kibanaResponseFactory, RequestHandlerContext, RequestHandler } from 'src/core/server'; -import { - savedObjectsClientMock, - httpServiceMock, - httpServerMock, - loggingSystemMock, -} from 'src/core/server/mocks'; +import { savedObjectsClientMock, httpServerMock } from 'src/core/server/mocks'; import { workpadWithGroupAsElement } from '../../../__tests__/fixtures/workpads'; import { CanvasWorkpad } from '../../../types'; +import { getMockedRouterDeps } from '../test_helpers'; const mockRouteContext = ({ core: { @@ -28,14 +24,10 @@ describe('GET workpad', () => { let routeHandler: RequestHandler; beforeEach(() => { - const httpService = httpServiceMock.createSetupContract(); - const router = httpService.createRouter(); - initializeGetWorkpadRoute({ - router, - logger: loggingSystemMock.create().get(), - }); + const routerDeps = getMockedRouterDeps(); + initializeGetWorkpadRoute(routerDeps); - routeHandler = router.get.mock.calls[0][1]; + routeHandler = routerDeps.router.get.mock.calls[0][1]; }); it(`returns 200 when the workpad is found`, async () => { diff --git a/x-pack/plugins/canvas/server/routes/workpad/update.test.ts b/x-pack/plugins/canvas/server/routes/workpad/update.test.ts index 6d7ea06852a5e5..0d97145c902986 100644 --- a/x-pack/plugins/canvas/server/routes/workpad/update.test.ts +++ b/x-pack/plugins/canvas/server/routes/workpad/update.test.ts @@ -8,14 +8,10 @@ import sinon from 'sinon'; import { CANVAS_TYPE } from '../../../common/lib/constants'; import { initializeUpdateWorkpadRoute, initializeUpdateWorkpadAssetsRoute } from './update'; import { kibanaResponseFactory, RequestHandlerContext, RequestHandler } from 'src/core/server'; -import { - savedObjectsClientMock, - httpServiceMock, - httpServerMock, - loggingSystemMock, -} from 'src/core/server/mocks'; +import { savedObjectsClientMock, httpServerMock } from 'src/core/server/mocks'; import { workpads } from '../../../__tests__/fixtures/workpads'; import { okResponse } from '../ok_response'; +import { getMockedRouterDeps } from '../test_helpers'; const mockRouteContext = ({ core: { @@ -38,14 +34,10 @@ describe('PUT workpad', () => { beforeEach(() => { clock = sinon.useFakeTimers(now); - const httpService = httpServiceMock.createSetupContract(); - const router = httpService.createRouter(); - initializeUpdateWorkpadRoute({ - router, - logger: loggingSystemMock.create().get(), - }); + const routerDeps = getMockedRouterDeps(); + initializeUpdateWorkpadRoute(routerDeps); - routeHandler = router.put.mock.calls[0][1]; + routeHandler = routerDeps.router.put.mock.calls[0][1]; }); afterEach(() => { @@ -152,14 +144,11 @@ describe('update assets', () => { beforeEach(() => { clock = sinon.useFakeTimers(now); - const httpService = httpServiceMock.createSetupContract(); - const router = httpService.createRouter(); - initializeUpdateWorkpadAssetsRoute({ - router, - logger: loggingSystemMock.create().get(), - }); - routeHandler = router.put.mock.calls[0][1]; + const routerDeps = getMockedRouterDeps(); + initializeUpdateWorkpadAssetsRoute(routerDeps); + + routeHandler = routerDeps.router.put.mock.calls[0][1]; }); afterEach(() => {