From 3603141fb76d3cf4fdb4f33ba2ef27aa86bf45d3 Mon Sep 17 00:00:00 2001 From: Christos Nasikas Date: Thu, 30 Apr 2020 13:51:44 +0300 Subject: [PATCH] Fix integration tests --- .../server/builtin_action_types/case/utils.ts | 2 +- .../builtin_action_types/jira/api.test.ts | 24 +++---- .../server/builtin_action_types/jira/mocks.ts | 72 +++++++++++-------- .../servicenow/api.test.ts | 5 +- .../builtin_action_types/servicenow/mocks.ts | 60 +++++++++------- .../jira_simulation.ts | 0 .../actions/builtin_action_types/jira.ts | 21 +++--- .../builtin_action_types/servicenow.ts | 19 +++-- 8 files changed, 114 insertions(+), 89 deletions(-) rename x-pack/test/alerting_api_integration/common/fixtures/plugins/{actions => actions_simulators}/jira_simulation.ts (100%) diff --git a/x-pack/plugins/actions/server/builtin_action_types/case/utils.ts b/x-pack/plugins/actions/server/builtin_action_types/case/utils.ts index ebb33efbee6e5f..7d69b2791f6240 100644 --- a/x-pack/plugins/actions/server/builtin_action_types/case/utils.ts +++ b/x-pack/plugins/actions/server/builtin_action_types/case/utils.ts @@ -53,7 +53,7 @@ export const mapParams = ( return Object.keys(params).reduce((prev: AnyParams, curr: string): AnyParams => { const field = mapping.get(curr); if (field) { - prev[field.target] = get(curr, params); + prev[field.target] = get(params, curr); } return prev; }, {}); diff --git a/x-pack/plugins/actions/server/builtin_action_types/jira/api.test.ts b/x-pack/plugins/actions/server/builtin_action_types/jira/api.test.ts index f5534e34435f67..755e0660171b88 100644 --- a/x-pack/plugins/actions/server/builtin_action_types/jira/api.test.ts +++ b/x-pack/plugins/actions/server/builtin_action_types/jira/api.test.ts @@ -11,11 +11,11 @@ import { ExternalService } from '../case/types'; describe('api', () => { let externalService: jest.Mocked; - beforeAll(() => { + beforeEach(() => { externalService = externalServiceMock.create(); }); - beforeEach(() => { + afterEach(() => { jest.clearAllMocks(); }); @@ -26,7 +26,7 @@ describe('api', () => { const res = await api.pushToService({ externalService, mapping, params }); expect(res).toEqual({ - id: '1', + id: 'incident-1', title: 'CK-1', pushedDate: '2020-04-27T10:59:46.202Z', url: 'https://siem-kibana.atlassian.net/browse/CK-1', @@ -48,7 +48,7 @@ describe('api', () => { const res = await api.pushToService({ externalService, mapping, params }); expect(res).toEqual({ - id: '1', + id: 'incident-1', title: 'CK-1', pushedDate: '2020-04-27T10:59:46.202Z', url: 'https://siem-kibana.atlassian.net/browse/CK-1', @@ -74,7 +74,7 @@ describe('api', () => { await api.pushToService({ externalService, mapping, params }); expect(externalService.createComment).toHaveBeenCalledTimes(2); expect(externalService.createComment).toHaveBeenNthCalledWith(1, { - incidentId: '1', + incidentId: 'incident-1', comment: { commentId: 'case-comment-1', version: 'WzU3LDFd', @@ -94,7 +94,7 @@ describe('api', () => { }); expect(externalService.createComment).toHaveBeenNthCalledWith(2, { - incidentId: '1', + incidentId: 'incident-1', comment: { commentId: 'case-comment-2', version: 'WlK3LDFd', @@ -120,8 +120,8 @@ describe('api', () => { const res = await api.pushToService({ externalService, mapping, params: apiParams }); expect(res).toEqual({ - id: 'incident-2', - title: 'INC02', + id: 'incident-1', + title: 'CK-1', pushedDate: '2020-04-27T10:59:46.202Z', url: 'https://siem-kibana.atlassian.net/browse/CK-1', comments: [ @@ -142,8 +142,8 @@ describe('api', () => { const res = await api.pushToService({ externalService, mapping, params }); expect(res).toEqual({ - id: 'incident-2', - title: 'INC02', + id: 'incident-1', + title: 'CK-1', pushedDate: '2020-04-27T10:59:46.202Z', url: 'https://siem-kibana.atlassian.net/browse/CK-1', }); @@ -169,7 +169,7 @@ describe('api', () => { await api.pushToService({ externalService, mapping, params }); expect(externalService.createComment).toHaveBeenCalledTimes(2); expect(externalService.createComment).toHaveBeenNthCalledWith(1, { - incidentId: 'incident-2', + incidentId: 'incident-1', comment: { commentId: 'case-comment-1', version: 'WzU3LDFd', @@ -189,7 +189,7 @@ describe('api', () => { }); expect(externalService.createComment).toHaveBeenNthCalledWith(2, { - incidentId: 'incident-2', + incidentId: 'incident-1', comment: { commentId: 'case-comment-2', version: 'WlK3LDFd', diff --git a/x-pack/plugins/actions/server/builtin_action_types/jira/mocks.ts b/x-pack/plugins/actions/server/builtin_action_types/jira/mocks.ts index 5c86ade03dda21..43d3a1d5b0a286 100644 --- a/x-pack/plugins/actions/server/builtin_action_types/jira/mocks.ts +++ b/x-pack/plugins/actions/server/builtin_action_types/jira/mocks.ts @@ -11,41 +11,55 @@ import { MapRecord, } from '../case/types'; -const createMock = (): jest.Mocked => ({ - getIncident: jest.fn().mockImplementation(() => - Promise.resolve({ - id: '1', - key: 'CK-1', - summary: 'title from jira', - description: 'description from jira', - created: '2020-04-27T10:59:46.202Z', - updated: '2020-04-27T10:59:46.202Z', - }) - ), - createIncident: jest.fn().mockImplementation(() => - Promise.resolve({ - id: '1', - title: 'CK-1', - pushedDate: '2020-04-27T10:59:46.202Z', - url: 'https://siem-kibana.atlassian.net/browse/CK-1', - }) - ), - updateIncident: jest.fn().mockImplementation(() => +const createMock = (): jest.Mocked => { + const service = { + getIncident: jest.fn().mockImplementation(() => + Promise.resolve({ + id: 'incident-1', + key: 'CK-1', + summary: 'title from jira', + description: 'description from jira', + created: '2020-04-27T10:59:46.202Z', + updated: '2020-04-27T10:59:46.202Z', + }) + ), + createIncident: jest.fn().mockImplementation(() => + Promise.resolve({ + id: 'incident-1', + title: 'CK-1', + pushedDate: '2020-04-27T10:59:46.202Z', + url: 'https://siem-kibana.atlassian.net/browse/CK-1', + }) + ), + updateIncident: jest.fn().mockImplementation(() => + Promise.resolve({ + id: 'incident-1', + title: 'CK-1', + pushedDate: '2020-04-27T10:59:46.202Z', + url: 'https://siem-kibana.atlassian.net/browse/CK-1', + }) + ), + createComment: jest.fn(), + }; + + service.createComment.mockImplementationOnce(() => Promise.resolve({ - id: 'incident-2', - title: 'INC02', + commentId: 'case-comment-1', pushedDate: '2020-04-27T10:59:46.202Z', - url: 'https://siem-kibana.atlassian.net/browse/CK-1', + externalCommentId: '1', }) - ), - createComment: jest.fn().mockImplementation(() => + ); + + service.createComment.mockImplementationOnce(() => Promise.resolve({ - commentId: 'comment-1', + commentId: 'case-comment-2', pushedDate: '2020-04-27T10:59:46.202Z', - externalCommentId: '1', + externalCommentId: '2', }) - ), -}); + ); + + return service; +}; const externalServiceMock = { create: createMock, diff --git a/x-pack/plugins/actions/server/builtin_action_types/servicenow/api.test.ts b/x-pack/plugins/actions/server/builtin_action_types/servicenow/api.test.ts index e95771a5d51c70..76a3a9ab2cb8d9 100644 --- a/x-pack/plugins/actions/server/builtin_action_types/servicenow/api.test.ts +++ b/x-pack/plugins/actions/server/builtin_action_types/servicenow/api.test.ts @@ -11,11 +11,12 @@ import { ExternalService } from '../case/types'; describe('api', () => { let externalService: jest.Mocked; - beforeAll(() => { + beforeEach(() => { externalService = externalServiceMock.create(); + jest.clearAllMocks(); }); - beforeEach(() => { + afterEach(() => { jest.clearAllMocks(); }); diff --git a/x-pack/plugins/actions/server/builtin_action_types/servicenow/mocks.ts b/x-pack/plugins/actions/server/builtin_action_types/servicenow/mocks.ts index 7cc8caea259058..6cbfcb9a8766a4 100644 --- a/x-pack/plugins/actions/server/builtin_action_types/servicenow/mocks.ts +++ b/x-pack/plugins/actions/server/builtin_action_types/servicenow/mocks.ts @@ -11,36 +11,48 @@ import { MapRecord, } from '../case/types'; -const createMock = (): jest.Mocked => ({ - getIncident: jest.fn().mockImplementation(() => - Promise.resolve({ - short_description: 'title from servicenow', - description: 'description from servicenow', - }) - ), - createIncident: jest.fn().mockImplementation(() => - Promise.resolve({ - id: 'incident-1', - title: 'INC01', - pushedDate: '2020-03-10T12:24:20.000Z', - url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123', - }) - ), - updateIncident: jest.fn().mockImplementation(() => +const createMock = (): jest.Mocked => { + const service = { + getIncident: jest.fn().mockImplementation(() => + Promise.resolve({ + short_description: 'title from servicenow', + description: 'description from servicenow', + }) + ), + createIncident: jest.fn().mockImplementation(() => + Promise.resolve({ + id: 'incident-1', + title: 'INC01', + pushedDate: '2020-03-10T12:24:20.000Z', + url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123', + }) + ), + updateIncident: jest.fn().mockImplementation(() => + Promise.resolve({ + id: 'incident-2', + title: 'INC02', + pushedDate: '2020-03-10T12:24:20.000Z', + url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123', + }) + ), + createComment: jest.fn(), + }; + + service.createComment.mockImplementationOnce(() => Promise.resolve({ - id: 'incident-2', - title: 'INC02', + commentId: 'case-comment-1', pushedDate: '2020-03-10T12:24:20.000Z', - url: 'https://instance.service-now.com/nav_to.do?uri=incident.do?sys_id=123', }) - ), - createComment: jest.fn().mockImplementation(() => + ); + + service.createComment.mockImplementationOnce(() => Promise.resolve({ - commentId: 'comment-1', + commentId: 'case-comment-2', pushedDate: '2020-03-10T12:24:20.000Z', }) - ), -}); + ); + return service; +}; const externalServiceMock = { create: createMock, diff --git a/x-pack/test/alerting_api_integration/common/fixtures/plugins/actions/jira_simulation.ts b/x-pack/test/alerting_api_integration/common/fixtures/plugins/actions_simulators/jira_simulation.ts similarity index 100% rename from x-pack/test/alerting_api_integration/common/fixtures/plugins/actions/jira_simulation.ts rename to x-pack/test/alerting_api_integration/common/fixtures/plugins/actions_simulators/jira_simulation.ts diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/jira.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/jira.ts index c6a6db1ec451a9..fd4b5ff2c1e665 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/jira.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/jira.ts @@ -11,7 +11,7 @@ import { FtrProviderContext } from '../../../../common/ftr_provider_context'; import { getExternalServiceSimulatorPath, ExternalServiceSimulator, -} from '../../../../common/fixtures/plugins/actions'; +} from '../../../../common/fixtures/plugins/actions_simulators'; const mapping = [ { @@ -325,8 +325,7 @@ export default function jiraTest({ getService }: FtrProviderContext) { actionId: simulatedActionId, status: 'error', retry: false, - message: - 'error validating action params: [subAction]: expected at least one defined value but got [undefined]', + message: `error validating action params: Cannot read property 'Symbol(Symbol.iterator)' of undefined`, }); }); }); @@ -344,7 +343,7 @@ export default function jiraTest({ getService }: FtrProviderContext) { status: 'error', retry: false, message: - 'error validating action params: [subAction]: types that failed validation:\n- [subAction.0]: expected value to equal [getIncident]\n- [subAction.1]: expected value to equal [pushToService]\n- [subAction.2]: expected value to equal [handshake]', + 'error validating action params: types that failed validation:\n- [0.subAction]: expected value to equal [getIncident]\n- [1.subAction]: expected value to equal [handshake]\n- [2.subAction]: expected value to equal [pushToService]', }); }); }); @@ -362,7 +361,7 @@ export default function jiraTest({ getService }: FtrProviderContext) { status: 'error', retry: false, message: - 'error validating action params: [subActionParams.caseId]: expected value of type [string] but got [undefined]', + 'error validating action params: types that failed validation:\n- [0.subAction]: expected value to equal [getIncident]\n- [1.subAction]: expected value to equal [handshake]\n- [2.subActionParams.caseId]: expected value of type [string] but got [undefined]', }); }); }); @@ -380,7 +379,7 @@ export default function jiraTest({ getService }: FtrProviderContext) { status: 'error', retry: false, message: - 'error validating action params: [subActionParams.caseId]: expected value of type [string] but got [undefined]', + 'error validating action params: types that failed validation:\n- [0.subAction]: expected value to equal [getIncident]\n- [1.subAction]: expected value to equal [handshake]\n- [2.subActionParams.caseId]: expected value of type [string] but got [undefined]', }); }); }); @@ -403,7 +402,7 @@ export default function jiraTest({ getService }: FtrProviderContext) { status: 'error', retry: false, message: - 'error validating action params: [subActionParams.title]: expected value of type [string] but got [undefined]', + 'error validating action params: types that failed validation:\n- [0.subAction]: expected value to equal [getIncident]\n- [1.subAction]: expected value to equal [handshake]\n- [2.subActionParams.title]: expected value of type [string] but got [undefined]', }); }); }); @@ -427,7 +426,7 @@ export default function jiraTest({ getService }: FtrProviderContext) { status: 'error', retry: false, message: - 'error validating action params: [subActionParams.createdAt]: expected value of type [string] but got [undefined]', + 'error validating action params: types that failed validation:\n- [0.subAction]: expected value to equal [getIncident]\n- [1.subAction]: expected value to equal [handshake]\n- [2.subActionParams.createdAt]: expected value of type [string] but got [undefined]', }); }); }); @@ -455,7 +454,7 @@ export default function jiraTest({ getService }: FtrProviderContext) { status: 'error', retry: false, message: - 'error validating action params: [subActionParams.comments.0.commentId]: expected value of type [string] but got [undefined]', + 'error validating action params: types that failed validation:\n- [0.subAction]: expected value to equal [getIncident]\n- [1.subAction]: expected value to equal [handshake]\n- [2.subActionParams.comments.0.commentId]: expected value of type [string] but got [undefined]', }); }); }); @@ -483,7 +482,7 @@ export default function jiraTest({ getService }: FtrProviderContext) { status: 'error', retry: false, message: - 'error validating action params: [subActionParams.comments.0.comment]: expected value of type [string] but got [undefined]', + 'error validating action params: types that failed validation:\n- [0.subAction]: expected value to equal [getIncident]\n- [1.subAction]: expected value to equal [handshake]\n- [2.subActionParams.comments.0.comment]: expected value of type [string] but got [undefined]', }); }); }); @@ -511,7 +510,7 @@ export default function jiraTest({ getService }: FtrProviderContext) { status: 'error', retry: false, message: - 'error validating action params: [subActionParams.comments.0.createdAt]: expected value of type [string] but got [undefined]', + 'error validating action params: types that failed validation:\n- [0.subAction]: expected value to equal [getIncident]\n- [1.subAction]: expected value to equal [handshake]\n- [2.subActionParams.comments.0.createdAt]: expected value of type [string] but got [undefined]', }); }); }); diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/servicenow.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/servicenow.ts index 38c66a9ae8d83d..69d54384f2984c 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/servicenow.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/servicenow.ts @@ -296,8 +296,7 @@ export default function servicenowTest({ getService }: FtrProviderContext) { actionId: simulatedActionId, status: 'error', retry: false, - message: - 'error validating action params: [subAction]: expected at least one defined value but got [undefined]', + message: `error validating action params: Cannot read property 'Symbol(Symbol.iterator)' of undefined`, }); }); }); @@ -315,7 +314,7 @@ export default function servicenowTest({ getService }: FtrProviderContext) { status: 'error', retry: false, message: - 'error validating action params: [subAction]: types that failed validation:\n- [subAction.0]: expected value to equal [getIncident]\n- [subAction.1]: expected value to equal [pushToService]\n- [subAction.2]: expected value to equal [handshake]', + 'error validating action params: types that failed validation:\n- [0.subAction]: expected value to equal [getIncident]\n- [1.subAction]: expected value to equal [handshake]\n- [2.subAction]: expected value to equal [pushToService]', }); }); }); @@ -333,7 +332,7 @@ export default function servicenowTest({ getService }: FtrProviderContext) { status: 'error', retry: false, message: - 'error validating action params: [subActionParams.caseId]: expected value of type [string] but got [undefined]', + 'error validating action params: types that failed validation:\n- [0.subAction]: expected value to equal [getIncident]\n- [1.subAction]: expected value to equal [handshake]\n- [2.subActionParams.caseId]: expected value of type [string] but got [undefined]', }); }); }); @@ -351,7 +350,7 @@ export default function servicenowTest({ getService }: FtrProviderContext) { status: 'error', retry: false, message: - 'error validating action params: [subActionParams.caseId]: expected value of type [string] but got [undefined]', + 'error validating action params: types that failed validation:\n- [0.subAction]: expected value to equal [getIncident]\n- [1.subAction]: expected value to equal [handshake]\n- [2.subActionParams.caseId]: expected value of type [string] but got [undefined]', }); }); }); @@ -374,7 +373,7 @@ export default function servicenowTest({ getService }: FtrProviderContext) { status: 'error', retry: false, message: - 'error validating action params: [subActionParams.title]: expected value of type [string] but got [undefined]', + 'error validating action params: types that failed validation:\n- [0.subAction]: expected value to equal [getIncident]\n- [1.subAction]: expected value to equal [handshake]\n- [2.subActionParams.title]: expected value of type [string] but got [undefined]', }); }); }); @@ -398,7 +397,7 @@ export default function servicenowTest({ getService }: FtrProviderContext) { status: 'error', retry: false, message: - 'error validating action params: [subActionParams.createdAt]: expected value of type [string] but got [undefined]', + 'error validating action params: types that failed validation:\n- [0.subAction]: expected value to equal [getIncident]\n- [1.subAction]: expected value to equal [handshake]\n- [2.subActionParams.createdAt]: expected value of type [string] but got [undefined]', }); }); }); @@ -426,7 +425,7 @@ export default function servicenowTest({ getService }: FtrProviderContext) { status: 'error', retry: false, message: - 'error validating action params: [subActionParams.comments.0.commentId]: expected value of type [string] but got [undefined]', + 'error validating action params: types that failed validation:\n- [0.subAction]: expected value to equal [getIncident]\n- [1.subAction]: expected value to equal [handshake]\n- [2.subActionParams.comments.0.commentId]: expected value of type [string] but got [undefined]', }); }); }); @@ -454,7 +453,7 @@ export default function servicenowTest({ getService }: FtrProviderContext) { status: 'error', retry: false, message: - 'error validating action params: [subActionParams.comments.0.comment]: expected value of type [string] but got [undefined]', + 'error validating action params: types that failed validation:\n- [0.subAction]: expected value to equal [getIncident]\n- [1.subAction]: expected value to equal [handshake]\n- [2.subActionParams.comments.0.comment]: expected value of type [string] but got [undefined]', }); }); }); @@ -482,7 +481,7 @@ export default function servicenowTest({ getService }: FtrProviderContext) { status: 'error', retry: false, message: - 'error validating action params: [subActionParams.comments.0.createdAt]: expected value of type [string] but got [undefined]', + 'error validating action params: types that failed validation:\n- [0.subAction]: expected value to equal [getIncident]\n- [1.subAction]: expected value to equal [handshake]\n- [2.subActionParams.comments.0.createdAt]: expected value of type [string] but got [undefined]', }); }); });