Skip to content

Commit

Permalink
Add functions test and refactor other router tests
Browse files Browse the repository at this point in the history
  • Loading branch information
poffdeluxe committed Aug 13, 2020
1 parent 073730b commit 230ed0a
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 90 deletions.
51 changes: 51 additions & 0 deletions x-pack/plugins/canvas/server/routes/functions/functions.test.ts
Original file line number Diff line number Diff line change
@@ -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<any, any, any>;
let mockFuncs: Record<string, ExpressionFunction>;

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));
});
});
13 changes: 5 additions & 8 deletions x-pack/plugins/canvas/server/routes/shareables/download.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`;
Expand All @@ -19,14 +20,10 @@ describe('Download Canvas shareables runtime', () => {
let routeHandler: RequestHandler<any, any, any>;

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(() => {
Expand Down
14 changes: 5 additions & 9 deletions x-pack/plugins/canvas/server/routes/shareables/zip.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -26,14 +27,9 @@ describe('Zips Canvas shareables runtime together with workpad', () => {
let routeHandler: RequestHandler<any, any, any>;

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(() => {
Expand Down
29 changes: 29 additions & 0 deletions x-pack/plugins/canvas/server/routes/test_helpers.ts
Original file line number Diff line number Diff line change
@@ -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(),
};
}
19 changes: 5 additions & 14 deletions x-pack/plugins/canvas/server/routes/workpad/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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(() => {
Expand Down
18 changes: 5 additions & 13 deletions x-pack/plugins/canvas/server/routes/workpad/delete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -26,14 +22,10 @@ describe('DELETE workpad', () => {
let routeHandler: RequestHandler<any, any, any>;

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 () => {
Expand Down
18 changes: 5 additions & 13 deletions x-pack/plugins/canvas/server/routes/workpad/find.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -25,14 +21,10 @@ describe('Find workpad', () => {
let routeHandler: RequestHandler<any, any, any>;

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 () => {
Expand Down
18 changes: 5 additions & 13 deletions x-pack/plugins/canvas/server/routes/workpad/get.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -28,14 +24,10 @@ describe('GET workpad', () => {
let routeHandler: RequestHandler<any, any, any>;

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 () => {
Expand Down
29 changes: 9 additions & 20 deletions x-pack/plugins/canvas/server/routes/workpad/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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(() => {
Expand Down Expand Up @@ -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(() => {
Expand Down

0 comments on commit 230ed0a

Please sign in to comment.