Skip to content

Commit

Permalink
Add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Apr 20, 2022
1 parent 4465e02 commit c621b3a
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 6 deletions.
1 change: 1 addition & 0 deletions x-pack/test/cases_api_integration/common/lib/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export const postCaseResp = (
...req,
...(id != null ? { id } : {}),
comments: [],
duration: null,
totalAlerts: 0,
totalComment: 0,
closed_by: null,
Expand Down
17 changes: 17 additions & 0 deletions x-pack/test/cases_api_integration/common/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
};
Original file line number Diff line number Diff line change
Expand Up @@ -368,5 +368,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);
});
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import {
removeServerGeneratedPropertiesFromUserAction,
findCases,
superUserSpace1Auth,
delay,
calculateDuration,
} from '../../../../common/lib/utils';
import {
createSignalsIndex,
Expand Down Expand Up @@ -198,6 +200,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', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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="
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"created_by": {
"email": "",
"full_name": "",
"username": "cnasikas"
"username": "elastic"
},
"description": "test",
"external_service": null,
Expand All @@ -26,7 +26,7 @@
"updated_by": {
"email": "",
"full_name": "",
"username": "cnasikas"
"username": "elastic"
}
},
"coreMigrationVersion": "8.2.0",
Expand All @@ -53,7 +53,7 @@
"created_by": {
"email": "",
"full_name": "",
"username": "cnasikas"
"username": "elastic"
},
"description": "test 2",
"external_service": null,
Expand All @@ -68,7 +68,7 @@
"updated_by": {
"email": "",
"full_name": "",
"username": "cnasikas"
"username": "elastic"
}
},
"coreMigrationVersion": "8.2.0",
Expand All @@ -95,7 +95,7 @@
"created_by": {
"email": "",
"full_name": "",
"username": "cnasikas"
"username": "elastic"
},
"description": "test 2",
"external_service": null,
Expand All @@ -110,7 +110,7 @@
"updated_by": {
"email": "",
"full_name": "",
"username": "cnasikas"
"username": "elastic"
}
},
"coreMigrationVersion": "8.2.0",
Expand Down

0 comments on commit c621b3a

Please sign in to comment.