From 2416d90a6f29ded19b8a06f9a5c20dd17a1283ac Mon Sep 17 00:00:00 2001 From: Christos Nasikas Date: Mon, 18 Apr 2022 14:14:00 +0300 Subject: [PATCH] Add integration tests --- .../cases_api_integration/common/lib/mock.ts | 1 + .../cases_api_integration/common/lib/utils.ts | 17 +++ .../tests/common/cases/migrations.ts | 45 ++++++ .../tests/common/cases/patch_cases.ts | 61 +++++++++ .../cases/8.2.0/cases_duration.json | 129 ++++++++++++++++++ .../cases/8.2.0/cases_various_dates.json | 12 +- 6 files changed, 259 insertions(+), 6 deletions(-) create mode 100644 x-pack/test/functional/fixtures/kbn_archiver/cases/8.2.0/cases_duration.json diff --git a/x-pack/test/cases_api_integration/common/lib/mock.ts b/x-pack/test/cases_api_integration/common/lib/mock.ts index 713d910f0bd1de4..5e1da9fc7b0227f 100644 --- a/x-pack/test/cases_api_integration/common/lib/mock.ts +++ b/x-pack/test/cases_api_integration/common/lib/mock.ts @@ -85,6 +85,7 @@ export const postCaseResp = ( ...req, ...(id != null ? { id } : {}), comments: [], + duration: null, totalAlerts: 0, totalComment: 0, closed_by: null, diff --git a/x-pack/test/cases_api_integration/common/lib/utils.ts b/x-pack/test/cases_api_integration/common/lib/utils.ts index 824b49d28b76c74..18c3a6c0236d083 100644 --- a/x-pack/test/cases_api_integration/common/lib/utils.ts +++ b/x-pack/test/cases_api_integration/common/lib/utils.ts @@ -1216,3 +1216,20 @@ export const createCaseAndBulkCreateAttachments = async ({ return { theCase: patchedCase, attachments }; }; + +export const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); + +export const calculateDuration = (closedAt: string | null, createdAt: string | null): number => { + if (closedAt == null || createdAt == null) { + throw new Error('Dates are null'); + } + + const createdAtMillis = new Date(createdAt).getTime(); + const closedAtMillis = new Date(closedAt).getTime(); + + if (isNaN(createdAtMillis) || isNaN(closedAtMillis)) { + throw new Error('Dates are invalid'); + } + + return Math.floor(Math.abs((closedAtMillis - createdAtMillis) / 1000)); +}; diff --git a/x-pack/test/cases_api_integration/security_and_spaces/tests/common/cases/migrations.ts b/x-pack/test/cases_api_integration/security_and_spaces/tests/common/cases/migrations.ts index 08e76cd5ab4fde0..47f089feb841e0a 100644 --- a/x-pack/test/cases_api_integration/security_and_spaces/tests/common/cases/migrations.ts +++ b/x-pack/test/cases_api_integration/security_and_spaces/tests/common/cases/migrations.ts @@ -371,5 +371,50 @@ export default function createGetTests({ getService }: FtrProviderContext) { expect(caseInfo).not.to.have.property('type'); }); }); + + describe('8.3.0 adding duration', () => { + before(async () => { + await kibanaServer.importExport.load( + 'x-pack/test/functional/fixtures/kbn_archiver/cases/8.2.0/cases_duration.json' + ); + }); + + after(async () => { + await kibanaServer.importExport.unload( + 'x-pack/test/functional/fixtures/kbn_archiver/cases/8.2.0/cases_duration.json' + ); + await deleteAllCaseItems(es); + }); + + it('calculates the correct duration for closed cases', async () => { + const caseInfo = await getCase({ + supertest, + caseId: '4537b380-a512-11ec-b92f-859b9e89e434', + }); + + expect(caseInfo).to.have.property('duration'); + expect(caseInfo.duration).to.be(120); + }); + + it('sets the duration to null to open cases', async () => { + const caseInfo = await getCase({ + supertest, + caseId: '7537b580-a512-11ec-b94f-85979e89e434', + }); + + expect(caseInfo).to.have.property('duration'); + expect(caseInfo.duration).to.be(null); + }); + + it('sets the duration to null to in-progress cases', async () => { + const caseInfo = await getCase({ + supertest, + caseId: '1537b580-a512-11ec-b94f-85979e89e434', + }); + + expect(caseInfo).to.have.property('duration'); + expect(caseInfo.duration).to.be(null); + }); + }); }); } diff --git a/x-pack/test/cases_api_integration/security_and_spaces/tests/common/cases/patch_cases.ts b/x-pack/test/cases_api_integration/security_and_spaces/tests/common/cases/patch_cases.ts index c93823822e44048..844d1249d0fe638 100644 --- a/x-pack/test/cases_api_integration/security_and_spaces/tests/common/cases/patch_cases.ts +++ b/x-pack/test/cases_api_integration/security_and_spaces/tests/common/cases/patch_cases.ts @@ -35,6 +35,8 @@ import { removeServerGeneratedPropertiesFromUserAction, findCases, superUserSpace1Auth, + delay, + calculateDuration, } from '../../../../common/lib/utils'; import { createSignalsIndex, @@ -199,6 +201,65 @@ export default ({ getService }: FtrProviderContext): void => { updated_by: defaultUser, }); }); + + describe('duration', () => { + it('updates the duration correctly when the case closes', async () => { + const postedCase = await createCase(supertest, postCaseReq); + await delay(1000); + + const patchedCases = await updateCase({ + supertest, + params: { + cases: [ + { + id: postedCase.id, + version: postedCase.version, + status: CaseStatuses.closed, + }, + ], + }, + }); + + const duration = calculateDuration(patchedCases[0].closed_at, postedCase.created_at); + expect(duration).to.be(patchedCases[0].duration); + }); + + for (const status of [CaseStatuses.open, CaseStatuses['in-progress']]) { + it(`sets the duration to null when the case status changes to ${status}`, async () => { + const postedCase = await createCase(supertest, postCaseReq); + + const closedCases = await updateCase({ + supertest, + params: { + cases: [ + { + id: postedCase.id, + version: postedCase.version, + status: CaseStatuses.closed, + }, + ], + }, + }); + + expect(closedCases[0].duration).to.not.be(null); + + const openCases = await updateCase({ + supertest, + params: { + cases: [ + { + id: postedCase.id, + version: closedCases[0].version, + status, + }, + ], + }, + }); + + expect(openCases[0].duration).to.be(null); + }); + } + }); }); describe('unhappy path', () => { diff --git a/x-pack/test/functional/fixtures/kbn_archiver/cases/8.2.0/cases_duration.json b/x-pack/test/functional/fixtures/kbn_archiver/cases/8.2.0/cases_duration.json new file mode 100644 index 000000000000000..ba8bab9227fbbb6 --- /dev/null +++ b/x-pack/test/functional/fixtures/kbn_archiver/cases/8.2.0/cases_duration.json @@ -0,0 +1,129 @@ +{ + "attributes": { + "closed_at": "2022-03-25T10:18:00.000Z", + "closed_by": { + "email": "", + "full_name": "", + "username": "elastic" + }, + "connector": { + "fields": null, + "name": "none", + "type": ".none" + }, + "created_at": "2022-03-25T10:16:00.000Z", + "created_by": { + "email": "", + "full_name": "", + "username": "elastic" + }, + "description": "test 2", + "external_service": null, + "owner": "securitySolutionFixture", + "settings": { + "syncAlerts": false + }, + "status": "closed", + "tags": [], + "title": "stack", + "updated_at": "2022-03-29T10:33:09.754Z", + "updated_by": { + "email": "", + "full_name": "", + "username": "elastic" + } + }, + "coreMigrationVersion": "8.2.0", + "id": "4537b380-a512-11ec-b92f-859b9e89e434", + "migrationVersion": { + "cases": "8.1.0" + }, + "references": [], + "type": "cases", + "updated_at": "2022-03-29T10:33:09.754Z", + "version": "WzE2OTYyNCwxNF0=" +} + +{ + "attributes": { + "closed_at": null, + "closed_by": null, + "connector": { + "fields": null, + "name": "none", + "type": ".none" + }, + "created_at": "2022-03-20T10:16:56.252Z", + "created_by": { + "email": "", + "full_name": "", + "username": "elastic" + }, + "description": "test 2", + "external_service": null, + "owner": "observabilityFixture", + "settings": { + "syncAlerts": false + }, + "status": "open", + "tags": [], + "title": "stack", + "updated_at": "2022-03-29T10:33:09.754Z", + "updated_by": { + "email": "", + "full_name": "", + "username": "elastic" + } + }, + "coreMigrationVersion": "8.2.0", + "id": "7537b580-a512-11ec-b94f-85979e89e434", + "migrationVersion": { + "cases": "8.1.0" + }, + "references": [], + "type": "cases", + "updated_at": "2022-03-29T10:33:09.754Z", + "version": "WzE2OTYyNCwxNF0=" +} + +{ + "attributes": { + "closed_at": null, + "closed_by": null, + "connector": { + "fields": null, + "name": "none", + "type": ".none" + }, + "created_at": "2022-03-20T10:16:56.252Z", + "created_by": { + "email": "", + "full_name": "", + "username": "elastic" + }, + "description": "test 2", + "external_service": null, + "owner": "observabilityFixture", + "settings": { + "syncAlerts": false + }, + "status": "in-progress", + "tags": [], + "title": "stack", + "updated_at": "2022-03-29T10:33:09.754Z", + "updated_by": { + "email": "", + "full_name": "", + "username": "elastic" + } + }, + "coreMigrationVersion": "8.2.0", + "id": "1537b580-a512-11ec-b94f-85979e89e434", + "migrationVersion": { + "cases": "8.1.0" + }, + "references": [], + "type": "cases", + "updated_at": "2022-03-29T10:33:09.754Z", + "version": "WzE2OTYyNCwxNF0=" +} diff --git a/x-pack/test/functional/fixtures/kbn_archiver/cases/8.2.0/cases_various_dates.json b/x-pack/test/functional/fixtures/kbn_archiver/cases/8.2.0/cases_various_dates.json index ffdfef08735fd59..cd87b6c44de1bf2 100644 --- a/x-pack/test/functional/fixtures/kbn_archiver/cases/8.2.0/cases_various_dates.json +++ b/x-pack/test/functional/fixtures/kbn_archiver/cases/8.2.0/cases_various_dates.json @@ -11,7 +11,7 @@ "created_by": { "email": "", "full_name": "", - "username": "cnasikas" + "username": "elastic" }, "description": "test", "external_service": null, @@ -26,7 +26,7 @@ "updated_by": { "email": "", "full_name": "", - "username": "cnasikas" + "username": "elastic" } }, "coreMigrationVersion": "8.2.0", @@ -53,7 +53,7 @@ "created_by": { "email": "", "full_name": "", - "username": "cnasikas" + "username": "elastic" }, "description": "test 2", "external_service": null, @@ -68,7 +68,7 @@ "updated_by": { "email": "", "full_name": "", - "username": "cnasikas" + "username": "elastic" } }, "coreMigrationVersion": "8.2.0", @@ -95,7 +95,7 @@ "created_by": { "email": "", "full_name": "", - "username": "cnasikas" + "username": "elastic" }, "description": "test 2", "external_service": null, @@ -110,7 +110,7 @@ "updated_by": { "email": "", "full_name": "", - "username": "cnasikas" + "username": "elastic" } }, "coreMigrationVersion": "8.2.0",