Skip to content

Commit

Permalink
Test get_configure
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Apr 9, 2020
1 parent f941761 commit 869bcbe
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@ import {
SavedObjectsBulkUpdateObject,
} from 'src/core/server';

import { CASE_COMMENT_SAVED_OBJECT, CASE_SAVED_OBJECT } from '../../../saved_object_types';
import {
CASE_COMMENT_SAVED_OBJECT,
CASE_SAVED_OBJECT,
CASE_CONFIGURE_SAVED_OBJECT,
} from '../../../saved_object_types';

export const createMockSavedObjectsRepository = ({
caseSavedObject = [],
caseCommentSavedObject = [],
caseConfigureSavedObject = [],
}: {
caseSavedObject?: any[];
caseCommentSavedObject?: any[];
caseConfigureSavedObject?: any[];
}) => {
const mockSavedObjectsClientContract = ({
bulkGet: jest.fn((objects: SavedObjectsBulkGetObject[]) => {
Expand Down Expand Up @@ -70,6 +76,7 @@ export const createMockSavedObjectsRepository = ({
}
return result[0];
}

const result = caseSavedObject.filter(s => s.id === id);
if (!result.length) {
throw SavedObjectsErrorHelpers.createGenericNotFoundError(type, id);
Expand All @@ -81,6 +88,15 @@ export const createMockSavedObjectsRepository = ({
throw SavedObjectsErrorHelpers.createBadRequestError('Error thrown for testing');
}

if (findArgs.type === CASE_CONFIGURE_SAVED_OBJECT) {
return {
page: 1,
per_page: 5,
total: caseConfigureSavedObject.length,
saved_objects: caseConfigureSavedObject,
};
}

if (findArgs.type === CASE_COMMENT_SAVED_OBJECT) {
return {
page: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
*/

import { SavedObject } from 'kibana/server';
import { CaseAttributes, CommentAttributes } from '../../../../common/api';
import {
CaseAttributes,
CommentAttributes,
CasesConfigureAttributes,
} from '../../../../common/api';

export const mockCases: Array<SavedObject<CaseAttributes>> = [
{
Expand Down Expand Up @@ -225,7 +229,33 @@ export const mockCaseComments: Array<SavedObject<CommentAttributes>> = [
},
],
updated_at: '2019-11-25T22:32:30.608Z',
version: 'WzYsMV0=',
},
];

export const mockCaseConfigure: Array<SavedObject<CasesConfigureAttributes>> = [
{
type: 'cases-configure',
id: 'mock-configuration-1',
attributes: {
connector_id: '123',
connector_name: 'My connector',
closure_type: 'close-by-user',
created_at: '2020-04-09T09:43:51.778Z',
created_by: {
full_name: 'elastic',
email: 'testemail@elastic.co',
username: 'elastic',
},
updated_at: '2020-04-09T09:43:51.778Z',
updated_by: {
full_name: 'elastic',
email: 'testemail@elastic.co',
username: 'elastic',
},
},
references: [],
updated_at: '2020-04-09T09:43:51.778Z',
version: 'WzYsMV0=',
},
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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, RequestHandler } from 'src/core/server';
import { httpServerMock } from 'src/core/server/mocks';

import {
createMockSavedObjectsRepository,
createRoute,
createRouteContext,
} from '../../__fixtures__';

import { mockCaseConfigure } from '../../__fixtures__/mock_saved_objects';
import { initGetCaseConfigure } from './get_configure';

describe('GET configuration', () => {
let routeHandler: RequestHandler<any, any, any>;
beforeAll(async () => {
routeHandler = await createRoute(initGetCaseConfigure, 'get');
});

it('returns the configuration', async () => {
const req = httpServerMock.createKibanaRequest({
path: '/api/cases/configure',
method: 'get',
});

const context = createRouteContext(
createMockSavedObjectsRepository({
caseConfigureSavedObject: mockCaseConfigure,
})
);

const res = await routeHandler(context, req, kibanaResponseFactory);
expect(res.status).toEqual(200);
expect(res.payload).toEqual({
...mockCaseConfigure[0].attributes,
version: mockCaseConfigure[0].version,
});
});

it('handles undefined version correctly', async () => {
const req = httpServerMock.createKibanaRequest({
path: '/api/cases/configure',
method: 'get',
});

const context = createRouteContext(
createMockSavedObjectsRepository({
caseConfigureSavedObject: [{ ...mockCaseConfigure[0], version: undefined }],
})
);

const res = await routeHandler(context, req, kibanaResponseFactory);
expect(res.status).toEqual(200);
expect(res.payload).toEqual({
...mockCaseConfigure[0].attributes,
version: '',
});
});

it('returns an empty object when there is no configuration', async () => {
const req = httpServerMock.createKibanaRequest({
path: '/api/cases/configure',
method: 'get',
});

const context = createRouteContext(
createMockSavedObjectsRepository({
caseConfigureSavedObject: [],
})
);

const res = await routeHandler(context, req, kibanaResponseFactory);
expect(res.status).toEqual(200);
expect(res.payload).toEqual({});
});
});

0 comments on commit 869bcbe

Please sign in to comment.