Skip to content

Commit

Permalink
Revert old tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Oct 27, 2020
1 parent c9f58c3 commit 79a3647
Show file tree
Hide file tree
Showing 15 changed files with 548 additions and 185 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,36 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { RequestHandlerContext } from 'src/core/server';
import { RequestHandlerContext, KibanaRequest } from 'src/core/server';
import { loggingSystemMock } from 'src/core/server/mocks';
import { actionsClientMock } from '../../../../../actions/server/mocks';
import { createCaseClientMock } from '../../../client/mocks';
import { createCaseClient } from '../../../client';
import { CaseService, CaseConfigureService } from '../../../services';
import { getActions } from '../__mocks__/request_responses';
import { authenticationMock } from '../__fixtures__';

export const createRouteContext = (client: any) => {
export const createRouteContext = async (client: any, badAuth = false) => {
const actionsMock = actionsClientMock.create();
actionsMock.getAll.mockImplementation(() => Promise.resolve(getActions()));
const caseMock = createCaseClientMock();
const log = loggingSystemMock.create().get('case');

const caseServicePlugin = new CaseService(log);
const caseConfigureServicePlugin = new CaseConfigureService(log);

const caseService = await caseServicePlugin.setup({
authentication: badAuth ? authenticationMock.createInvalid() : authenticationMock.create(),
});
const caseConfigureService = await caseConfigureServicePlugin.setup();
const caseClient = createCaseClient({
savedObjectsClient: client,
request: {} as KibanaRequest,
caseService,
caseConfigureService,
userActionService: {
postUserActions: jest.fn(),
getUserActions: jest.fn(),
},
});

return ({
core: {
Expand All @@ -21,6 +42,8 @@ export const createRouteContext = (client: any) => {
},
},
actions: { getActionsClient: () => actionsMock },
case: { getCaseClient: () => caseMock },
case: {
getCaseClient: () => caseClient,
},
} as unknown) as RequestHandlerContext;
};
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('DELETE comment', () => {
},
});

const theContext = createRouteContext(
const theContext = await createRouteContext(
createMockSavedObjectsRepository({
caseSavedObject: mockCases,
caseCommentSavedObject: mockCaseComments,
Expand All @@ -52,7 +52,7 @@ describe('DELETE comment', () => {
},
});

const theContext = createRouteContext(
const theContext = await createRouteContext(
createMockSavedObjectsRepository({
caseSavedObject: mockCases,
caseCommentSavedObject: mockCaseComments,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('GET comment', () => {
},
});

const theContext = createRouteContext(
const theContext = await createRouteContext(
createMockSavedObjectsRepository({
caseSavedObject: mockCases,
caseCommentSavedObject: mockCaseComments,
Expand All @@ -57,7 +57,7 @@ describe('GET comment', () => {
},
});

const theContext = createRouteContext(
const theContext = await createRouteContext(
createMockSavedObjectsRepository({
caseCommentSavedObject: mockCaseComments,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('PATCH comment', () => {
},
});

const theContext = createRouteContext(
const theContext = await createRouteContext(
createMockSavedObjectsRepository({
caseSavedObject: mockCases,
caseCommentSavedObject: mockCaseComments,
Expand Down Expand Up @@ -63,7 +63,7 @@ describe('PATCH comment', () => {
},
});

const theContext = createRouteContext(
const theContext = await createRouteContext(
createMockSavedObjectsRepository({
caseSavedObject: mockCases,
caseCommentSavedObject: mockCaseComments,
Expand All @@ -87,7 +87,7 @@ describe('PATCH comment', () => {
},
});

const theContext = createRouteContext(
const theContext = await createRouteContext(
createMockSavedObjectsRepository({
caseSavedObject: mockCases,
caseCommentSavedObject: mockCaseComments,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,88 +14,95 @@ import {
mockCases,
mockCaseComments,
} from '../../__fixtures__';
import { CASE_COMMENTS_URL } from '../../../../../common/constants';
import { CaseClient } from '../../../../client';
import { initPostCommentApi } from './post_comment';
import { ConnectorTypes } from '../../../../../common/api';
import { CASE_COMMENTS_URL } from '../../../../../common/constants';

describe('POST comment', () => {
let routeHandler: RequestHandler<any, any, any>;
beforeAll(async () => {
routeHandler = await createRoute(initPostCommentApi, 'post');
const spyOnDate = jest.spyOn(global, 'Date') as jest.SpyInstance<{}, []>;
spyOnDate.mockImplementation(() => ({
toISOString: jest.fn().mockReturnValue('2020-10-23T21:54:48.952Z'),
toISOString: jest.fn().mockReturnValue('2019-11-25T21:54:48.952Z'),
}));
});

it(`it adds a new comment`, async () => {
const addCommentResult = {
id: 'mock-id-1',
version: 'WzE3LDFd',
comments: [
{
id: 'mock-comment-1',
version: 'WzEsMV0=',
comment: 'Wow, good luck catching that bad meanie!',
created_at: '2019-11-25T21:55:00.177Z',
created_by: {
full_name: 'elastic',
email: 'testemail@elastic.co',
username: 'elastic',
},
pushed_at: null,
pushed_by: null,
updated_at: '2019-11-25T21:55:00.177Z',
updated_by: {
full_name: 'elastic',
email: 'testemail@elastic.co',
username: 'elastic',
},
},
{
id: 'mock-comment',
version: 'WzksMV0=',
comment: 'Wow, good luck catching that bad meanie!',
created_at: '2020-10-23T21:54:48.952Z',
created_by: {
email: 'd00d@awesome.com',
full_name: 'Awesome D00d',
username: 'awesome',
},
pushed_at: null,
pushed_by: null,
updated_at: null,
updated_by: null,
},
],
totalComment: 2,
closed_at: null,
closed_by: null,
connector: {
id: 'none',
name: 'none',
type: ConnectorTypes.none,
fields: null,
it(`Posts a new comment`, async () => {
const request = httpServerMock.createKibanaRequest({
path: CASE_COMMENTS_URL,
method: 'post',
params: {
case_id: 'mock-id-1',
},
created_at: '2019-11-25T21:54:48.952Z',
created_by: {
full_name: 'elastic',
email: 'testemail@elastic.co',
username: 'elastic',
body: {
comment: 'Wow, good luck catching that bad meanie!',
},
});

const theContext = await createRouteContext(
createMockSavedObjectsRepository({
caseSavedObject: mockCases,
caseCommentSavedObject: mockCaseComments,
})
);

const response = await routeHandler(theContext, request, kibanaResponseFactory);
expect(response.status).toEqual(200);
expect(response.payload.comments[response.payload.comments.length - 1].id).toEqual(
'mock-comment'
);
});

it(`Returns an error if the case does not exist`, async () => {
const request = httpServerMock.createKibanaRequest({
path: CASE_COMMENTS_URL,
method: 'post',
params: {
case_id: 'this-is-not-real',
},
body: {
comment: 'Wow, good luck catching that bad meanie!',
},
});

const theContext = await createRouteContext(
createMockSavedObjectsRepository({
caseSavedObject: mockCases,
caseCommentSavedObject: mockCaseComments,
})
);

const response = await routeHandler(theContext, request, kibanaResponseFactory);
expect(response.status).toEqual(404);
expect(response.payload.isBoom).toEqual(true);
});

it(`Returns an error if postNewCase throws`, async () => {
const request = httpServerMock.createKibanaRequest({
path: CASE_COMMENTS_URL,
method: 'post',
params: {
case_id: 'mock-id-1',
},
description: 'This is a brand new case of a bad meanie defacing data',
external_service: null,
title: 'Super Bad Security Issue',
status: 'open' as const,
tags: ['defacement'],
updated_at: '2020-10-23T21:54:48.952Z',
updated_by: {
username: 'awesome',
full_name: 'Awesome D00d',
email: 'd00d@awesome.com',
body: {
comment: 'Throw an error',
},
};
});

const theContext = await createRouteContext(
createMockSavedObjectsRepository({
caseSavedObject: mockCases,
caseCommentSavedObject: mockCaseComments,
})
);

const response = await routeHandler(theContext, request, kibanaResponseFactory);
expect(response.status).toEqual(400);
expect(response.payload.isBoom).toEqual(true);
});

it(`Allow user to create comments without authentications`, async () => {
routeHandler = await createRoute(initPostCommentApi, 'post', true);

const request = httpServerMock.createKibanaRequest({
path: CASE_COMMENTS_URL,
Expand All @@ -108,22 +115,29 @@ describe('POST comment', () => {
},
});

const context = createRouteContext(
const theContext = await createRouteContext(
createMockSavedObjectsRepository({
caseSavedObject: mockCases,
caseCommentSavedObject: mockCaseComments,
})
);
const caseClient = context.case!.getCaseClient() as jest.Mocked<CaseClient>;
caseClient.addComment.mockResolvedValueOnce(addCommentResult);
const response = await routeHandler(context, request, kibanaResponseFactory);

expect(caseClient.addComment).toHaveBeenCalledTimes(1);
expect(caseClient.addComment).toHaveBeenCalledWith({
caseId: request.params.case_id,
comment: request.body,
});

const response = await routeHandler(theContext, request, kibanaResponseFactory);
expect(response.status).toEqual(200);
expect(response.payload).toEqual(addCommentResult);
expect(response.payload.comments[response.payload.comments.length - 1]).toEqual({
comment: 'Wow, good luck catching that bad meanie!',
created_at: '2019-11-25T21:54:48.952Z',
created_by: {
email: null,
full_name: null,
username: null,
},
id: 'mock-comment',
pushed_at: null,
pushed_by: null,
updated_at: null,
updated_by: null,
version: 'WzksMV0=',
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('GET configuration', () => {
method: 'get',
});

const context = createRouteContext(
const context = await createRouteContext(
createMockSavedObjectsRepository({
caseConfigureSavedObject: mockCaseConfigure,
})
Expand All @@ -49,7 +49,7 @@ describe('GET configuration', () => {
method: 'get',
});

const context = createRouteContext(
const context = await createRouteContext(
createMockSavedObjectsRepository({
caseConfigureSavedObject: [{ ...mockCaseConfigure[0], version: undefined }],
})
Expand Down Expand Up @@ -87,7 +87,7 @@ describe('GET configuration', () => {
method: 'get',
});

const context = createRouteContext(
const context = await createRouteContext(
createMockSavedObjectsRepository({
caseConfigureSavedObject: [],
})
Expand All @@ -105,7 +105,7 @@ describe('GET configuration', () => {
method: 'get',
});

const context = createRouteContext(
const context = await createRouteContext(
createMockSavedObjectsRepository({
caseConfigureSavedObject: [{ ...mockCaseConfigure[0], id: 'throw-error-find' }],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('GET connectors', () => {
method: 'get',
});

const context = createRouteContext(
const context = await createRouteContext(
createMockSavedObjectsRepository({
caseConfigureSavedObject: mockCaseConfigure,
})
Expand Down Expand Up @@ -106,7 +106,7 @@ describe('GET connectors', () => {
method: 'get',
});

const context = createRouteContext(
const context = await createRouteContext(
createMockSavedObjectsRepository({
caseConfigureSavedObject: mockCaseConfigure,
})
Expand Down
Loading

0 comments on commit 79a3647

Please sign in to comment.