|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +import { kibanaResponseFactory, RequestHandler } from 'src/core/server'; |
| 8 | +import { httpServerMock } from 'src/core/server/mocks'; |
| 9 | + |
| 10 | +import { |
| 11 | + createMockSavedObjectsRepository, |
| 12 | + createRoute, |
| 13 | + createRouteContext, |
| 14 | + mockCases, |
| 15 | +} from '../__fixtures__'; |
| 16 | +import { initPushCaseUserActionApi } from './push_case'; |
| 17 | +import { CASE_DETAILS_URL } from '../../../../common/constants'; |
| 18 | +import { mockCaseConfigure } from '../__fixtures__/mock_saved_objects'; |
| 19 | + |
| 20 | +describe('Push case', () => { |
| 21 | + let routeHandler: RequestHandler<any, any, any>; |
| 22 | + const mockDate = '2019-11-25T21:54:48.952Z'; |
| 23 | + const caseExternalServiceRequestBody = { |
| 24 | + connector_id: 'connector_id', |
| 25 | + connector_name: 'connector_name', |
| 26 | + external_id: 'external_id', |
| 27 | + external_title: 'external_title', |
| 28 | + external_url: 'external_url', |
| 29 | + }; |
| 30 | + beforeAll(async () => { |
| 31 | + routeHandler = await createRoute(initPushCaseUserActionApi, 'post'); |
| 32 | + const spyOnDate = jest.spyOn(global, 'Date') as jest.SpyInstance<{}, []>; |
| 33 | + spyOnDate.mockImplementation(() => ({ |
| 34 | + toISOString: jest.fn().mockReturnValue(mockDate), |
| 35 | + })); |
| 36 | + }); |
| 37 | + it(`Pushes a case`, async () => { |
| 38 | + const request = httpServerMock.createKibanaRequest({ |
| 39 | + path: `${CASE_DETAILS_URL}/_push`, |
| 40 | + method: 'post', |
| 41 | + params: { |
| 42 | + case_id: 'mock-id-3', |
| 43 | + }, |
| 44 | + body: caseExternalServiceRequestBody, |
| 45 | + }); |
| 46 | + |
| 47 | + const theContext = createRouteContext( |
| 48 | + createMockSavedObjectsRepository({ |
| 49 | + caseSavedObject: mockCases, |
| 50 | + }) |
| 51 | + ); |
| 52 | + |
| 53 | + const response = await routeHandler(theContext, request, kibanaResponseFactory); |
| 54 | + expect(response.status).toEqual(200); |
| 55 | + expect(response.payload.external_service.pushed_at).toEqual(mockDate); |
| 56 | + expect(response.payload.external_service.connector_id).toEqual('connector_id'); |
| 57 | + expect(response.payload.closed_at).toEqual(null); |
| 58 | + }); |
| 59 | + it(`Pushes a case and closes when closure_type: 'close-by-pushing'`, async () => { |
| 60 | + const request = httpServerMock.createKibanaRequest({ |
| 61 | + path: `${CASE_DETAILS_URL}/_push`, |
| 62 | + method: 'post', |
| 63 | + params: { |
| 64 | + case_id: 'mock-id-3', |
| 65 | + }, |
| 66 | + body: caseExternalServiceRequestBody, |
| 67 | + }); |
| 68 | + |
| 69 | + const theContext = createRouteContext( |
| 70 | + createMockSavedObjectsRepository({ |
| 71 | + caseSavedObject: mockCases, |
| 72 | + caseConfigureSavedObject: [ |
| 73 | + { |
| 74 | + ...mockCaseConfigure[0], |
| 75 | + attributes: { |
| 76 | + ...mockCaseConfigure[0].attributes, |
| 77 | + closure_type: 'close-by-pushing', |
| 78 | + }, |
| 79 | + }, |
| 80 | + ], |
| 81 | + }) |
| 82 | + ); |
| 83 | + |
| 84 | + const response = await routeHandler(theContext, request, kibanaResponseFactory); |
| 85 | + expect(response.status).toEqual(200); |
| 86 | + expect(response.payload.external_service.pushed_at).toEqual(mockDate); |
| 87 | + expect(response.payload.external_service.connector_id).toEqual('connector_id'); |
| 88 | + expect(response.payload.closed_at).toEqual(mockDate); |
| 89 | + }); |
| 90 | + |
| 91 | + it(`Returns an error if pushCaseUserAction throws`, async () => { |
| 92 | + const request = httpServerMock.createKibanaRequest({ |
| 93 | + path: `${CASE_DETAILS_URL}/_push`, |
| 94 | + method: 'post', |
| 95 | + body: { |
| 96 | + notagoodbody: 'Throw an error', |
| 97 | + }, |
| 98 | + }); |
| 99 | + |
| 100 | + const theContext = createRouteContext( |
| 101 | + createMockSavedObjectsRepository({ |
| 102 | + caseSavedObject: mockCases, |
| 103 | + }) |
| 104 | + ); |
| 105 | + |
| 106 | + const response = await routeHandler(theContext, request, kibanaResponseFactory); |
| 107 | + expect(response.status).toEqual(400); |
| 108 | + expect(response.payload.isBoom).toEqual(true); |
| 109 | + }); |
| 110 | +}); |
0 commit comments