Skip to content

Commit

Permalink
[Security Solution] [Cases] Follow up jest tests for elastic#84587 (e…
Browse files Browse the repository at this point in the history
  • Loading branch information
stephmilovic committed Dec 18, 2020
1 parent 38c5cde commit eeb7d4a
Show file tree
Hide file tree
Showing 17 changed files with 1,087 additions and 749 deletions.
2 changes: 2 additions & 0 deletions x-pack/plugins/case/common/api/connectors/mappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ export const PostPushRequestRt = rt.type({
params: ServiceConnectorCaseParamsRt,
});

export type PostPushRequest = rt.TypeOf<typeof PostPushRequestRt>;

export interface SimpleComment {
comment: string;
commentId: string;
Expand Down
60 changes: 60 additions & 0 deletions x-pack/plugins/case/server/client/configure/get_fields.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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 { ConnectorTypes } from '../../../common/api';

import { createMockSavedObjectsRepository, mockCaseMappings } from '../../routes/api/__fixtures__';
import { createCaseClientWithMockSavedObjectsClient } from '../mocks';
import { actionsClientMock } from '../../../../actions/server/actions_client.mock';
import { actionsErrResponse, mappings, mockGetFieldsResponse } from './mock';
describe('get_fields', () => {
const execute = jest.fn().mockReturnValue(mockGetFieldsResponse);
const actionsMock = { ...actionsClientMock.create(), execute };
beforeEach(async () => {
jest.clearAllMocks();
});

describe('happy path', () => {
test('it gets fields', async () => {
const savedObjectsClient = createMockSavedObjectsRepository({
caseMappingsSavedObject: mockCaseMappings,
});
const caseClient = await createCaseClientWithMockSavedObjectsClient(savedObjectsClient);
const res = await caseClient.client.getFields({
actionsClient: actionsMock,
connectorType: ConnectorTypes.jira,
connectorId: '123',
});
expect(res).toEqual({
fields: [
{ id: 'summary', name: 'Summary', required: true, type: 'text' },
{ id: 'description', name: 'Description', required: false, type: 'text' },
],
defaultMappings: mappings[ConnectorTypes.jira],
});
});
});

describe('unhappy path', () => {
test('it throws error', async () => {
const savedObjectsClient = createMockSavedObjectsRepository({
caseMappingsSavedObject: mockCaseMappings,
});
const caseClient = await createCaseClientWithMockSavedObjectsClient(savedObjectsClient);
await caseClient.client
.getFields({
actionsClient: { ...actionsMock, execute: jest.fn().mockReturnValue(actionsErrResponse) },
connectorType: ConnectorTypes.jira,
connectorId: '123',
})
.catch((e) => {
expect(e).not.toBeNull();
expect(e.isBoom).toBe(true);
expect(e.output.statusCode).toBe(424);
});
});
});
});
55 changes: 55 additions & 0 deletions x-pack/plugins/case/server/client/configure/get_mappings.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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 { ConnectorTypes } from '../../../common/api';

import { createMockSavedObjectsRepository, mockCaseMappings } from '../../routes/api/__fixtures__';
import { createCaseClientWithMockSavedObjectsClient } from '../mocks';
import { actionsClientMock } from '../../../../actions/server/actions_client.mock';
import { mappings, mockGetFieldsResponse } from './mock';

describe('get_mappings', () => {
const execute = jest.fn().mockReturnValue(mockGetFieldsResponse);
const actionsMock = { ...actionsClientMock.create(), execute };
beforeEach(async () => {
jest.restoreAllMocks();
const spyOnDate = jest.spyOn(global, 'Date') as jest.SpyInstance<{}, []>;
spyOnDate.mockImplementation(() => ({
toISOString: jest.fn().mockReturnValue('2019-11-25T21:54:48.952Z'),
}));
});

describe('happy path', () => {
test('it gets existing mappings', async () => {
const savedObjectsClient = createMockSavedObjectsRepository({
caseMappingsSavedObject: mockCaseMappings,
});
const caseClient = await createCaseClientWithMockSavedObjectsClient(savedObjectsClient);
const res = await caseClient.client.getMappings({
actionsClient: actionsMock,
caseClient: caseClient.client,
connectorType: ConnectorTypes.jira,
connectorId: '123',
});

expect(res).toEqual(mappings[ConnectorTypes.jira]);
});
test('it creates new mappings', async () => {
const savedObjectsClient = createMockSavedObjectsRepository({
caseMappingsSavedObject: [],
});
const caseClient = await createCaseClientWithMockSavedObjectsClient(savedObjectsClient);
const res = await caseClient.client.getMappings({
actionsClient: actionsMock,
caseClient: caseClient.client,
connectorType: ConnectorTypes.jira,
connectorId: '123',
});

expect(res).toEqual(mappings[ConnectorTypes.jira]);
});
});
});
Loading

0 comments on commit eeb7d4a

Please sign in to comment.