Skip to content

Commit

Permalink
Merge pull request #2 from jonathan-buttner/convert-cases-unit-tests
Browse files Browse the repository at this point in the history
[Cases] Migrate deletion unit tests to integration tests
  • Loading branch information
jonathan-buttner committed Apr 13, 2021
2 parents 25736b3 + a099313 commit 77e9a93
Show file tree
Hide file tree
Showing 16 changed files with 70 additions and 200 deletions.
114 changes: 0 additions & 114 deletions x-pack/plugins/cases/server/routes/api/cases/delete_cases.test.ts

This file was deleted.

25 changes: 23 additions & 2 deletions x-pack/test/case_api_integration/common/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ export const removeServerGeneratedPropertiesFromComments = (

export const deleteAllCaseItems = async (es: KibanaClient) => {
await Promise.all([
deleteCases(es),
deleteCasesByESQuery(es),
deleteSubCases(es),
deleteCasesUserActions(es),
deleteComments(es),
Expand All @@ -468,7 +468,7 @@ export const deleteCasesUserActions = async (es: KibanaClient): Promise<void> =>
});
};

export const deleteCases = async (es: KibanaClient): Promise<void> => {
export const deleteCasesByESQuery = async (es: KibanaClient): Promise<void> => {
await es.deleteByQuery({
index: '.kibana',
// @ts-expect-error @elastic/elasticsearch DeleteByQueryRequest doesn't accept q parameter
Expand Down Expand Up @@ -600,6 +600,27 @@ export const createCase = async (
return theCase;
};

/**
* Sends a delete request for the specified case IDs.
*/
export const deleteCases = async ({
supertest,
caseIDs,
expectedHttpCode = 204,
}: {
supertest: st.SuperTest<supertestAsPromised.Test>;
caseIDs: string[];
expectedHttpCode?: number;
}) => {
const { body } = await supertest
.delete(`${CASES_URL}?ids=${JSON.stringify(caseIDs)}`)
.set('kbn-xsrf', 'true')
.send()
.expect(expectedHttpCode);

return body;
};

export const createComment = async (
supertest: st.SuperTest<supertestAsPromised.Test>,
caseId: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { FtrProviderContext } from '../../../../../common/ftr_provider_context';

import { postCaseReq } from '../../../../common/lib/mock';
import {
deleteCases,
deleteCasesByESQuery,
deleteCasesUserActions,
deleteComments,
deleteConfiguration,
Expand All @@ -29,7 +29,7 @@ export default ({ getService }: FtrProviderContext): void => {

describe('push_case', () => {
afterEach(async () => {
await deleteCases(es);
await deleteCasesByESQuery(es);
await deleteComments(es);
await deleteConfiguration(es);
await deleteCasesUserActions(es);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ import expect from '@kbn/expect';
import { FtrProviderContext } from '../../../../../common/ftr_provider_context';

import { CASES_URL } from '../../../../../../plugins/cases/common/constants';
import { postCaseReq, postCommentUserReq } from '../../../../common/lib/mock';
import { getPostCaseRequest, postCommentUserReq } from '../../../../common/lib/mock';
import {
createCaseAction,
createSubCase,
deleteAllCaseItems,
deleteCaseAction,
deleteCases,
deleteCasesByESQuery,
deleteCasesUserActions,
deleteComments,
createCase,
deleteCases,
createComment,
getComment,
} from '../../../../common/lib/utils';
import { getSubCaseDetailsUrl } from '../../../../../../plugins/cases/common/api/helpers';
import { CaseResponse } from '../../../../../../plugins/cases/common/api';
Expand All @@ -29,65 +33,32 @@ export default ({ getService }: FtrProviderContext): void => {

describe('delete_cases', () => {
afterEach(async () => {
await deleteCases(es);
await deleteCasesByESQuery(es);
await deleteComments(es);
await deleteCasesUserActions(es);
});

it('should delete a case', async () => {
const { body: postedCase } = await supertest
.post(CASES_URL)
.set('kbn-xsrf', 'true')
.send(postCaseReq)
.expect(200);

const { body } = await supertest
.delete(`${CASES_URL}?ids=["${postedCase.id}"]`)
.set('kbn-xsrf', 'true')
.send()
.expect(204);
const postedCase = await createCase(supertest, getPostCaseRequest());
const body = await deleteCases({ supertest, caseIDs: [postedCase.id] });

expect(body).to.eql({});
});

it(`should delete a case's comments when that case gets deleted`, async () => {
const { body: postedCase } = await supertest
.post(CASES_URL)
.set('kbn-xsrf', 'true')
.send(postCaseReq)
.expect(200);

const { body: patchedCase } = await supertest
.post(`${CASES_URL}/${postedCase.id}/comments`)
.set('kbn-xsrf', 'true')
.send(postCommentUserReq)
.expect(200);

await supertest
.get(`${CASES_URL}/${postedCase.id}/comments/${patchedCase.comments[0].id}`)
.set('kbn-xsrf', 'true')
.send()
.expect(200);

await supertest
.delete(`${CASES_URL}?ids=["${postedCase.id}"]`)
.set('kbn-xsrf', 'true')
.send()
.expect(204);

await supertest
.get(`${CASES_URL}/${postedCase.id}/comments/${patchedCase.comments[0].id}`)
.set('kbn-xsrf', 'true')
.send()
.expect(404);
const postedCase = await createCase(supertest, getPostCaseRequest());
const patchedCase = await createComment(supertest, postedCase.id, postCommentUserReq);
// ensure that we can get the comment before deleting the case
await getComment(supertest, postedCase.id, patchedCase.comments![0].id);

await deleteCases({ supertest, caseIDs: [postedCase.id] });

// make sure the comment is now gone
await getComment(supertest, postedCase.id, patchedCase.comments![0].id, 404);
});

it('unhappy path - 404s when case is not there', async () => {
await supertest
.delete(`${CASES_URL}?ids=["fake-id"]`)
.set('kbn-xsrf', 'true')
.send()
.expect(404);
await deleteCases({ supertest, caseIDs: ['fake-id'], expectedHttpCode: 404 });
});

// ENABLE_CASE_CONNECTOR: once the case connector feature is completed unskip these tests
Expand All @@ -107,11 +78,7 @@ export default ({ getService }: FtrProviderContext): void => {
const { newSubCaseInfo: caseInfo } = await createSubCase({ supertest, actionID });
expect(caseInfo.subCases![0].id).to.not.eql(undefined);

const { body } = await supertest
.delete(`${CASES_URL}?ids=["${caseInfo.id}"]`)
.set('kbn-xsrf', 'true')
.send()
.expect(204);
const body = await deleteCases({ supertest, caseIDs: [caseInfo.id] });

expect(body).to.eql({});
await supertest
Expand All @@ -138,11 +105,7 @@ export default ({ getService }: FtrProviderContext): void => {
// make sure we can get the second comment
await supertest.get(subCaseCommentUrl).set('kbn-xsrf', 'true').send().expect(200);

await supertest
.delete(`${CASES_URL}?ids=["${caseInfo.id}"]`)
.set('kbn-xsrf', 'true')
.send()
.expect(204);
await deleteCases({ supertest, caseIDs: [caseInfo.id] });

await supertest.get(subCaseCommentUrl).set('kbn-xsrf', 'true').send().expect(404);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
postCommentUserReq,
} from '../../../../common/lib/mock';
import {
deleteCases,
deleteCasesByESQuery,
createCase,
getCase,
createComment,
Expand All @@ -32,7 +32,7 @@ export default ({ getService }: FtrProviderContext): void => {

describe('get_case', () => {
afterEach(async () => {
await deleteCases(es);
await deleteCasesByESQuery(es);
});

it('should return a case with no comments', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
import { getPostCaseRequest, postCaseResp, defaultUser } from '../../../../common/lib/mock';
import {
createCaseAsUser,
deleteCases,
deleteCasesByESQuery,
createCase,
removeServerGeneratedPropertiesFromCase,
removeServerGeneratedPropertiesFromUserAction,
Expand All @@ -40,7 +40,7 @@ export default ({ getService }: FtrProviderContext): void => {

describe('post_case', () => {
afterEach(async () => {
await deleteCases(es);
await deleteCasesByESQuery(es);
});

describe('happy path', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { FtrProviderContext } from '../../../../../../common/ftr_provider_contex

import { CASES_URL, CASE_REPORTERS_URL } from '../../../../../../../plugins/cases/common/constants';
import { defaultUser, postCaseReq } from '../../../../../common/lib/mock';
import { deleteCases } from '../../../../../common/lib/utils';
import { deleteCasesByESQuery } from '../../../../../common/lib/utils';

// eslint-disable-next-line import/no-default-export
export default ({ getService }: FtrProviderContext): void => {
Expand All @@ -19,7 +19,7 @@ export default ({ getService }: FtrProviderContext): void => {

describe('get_reporters', () => {
afterEach(async () => {
await deleteCases(es);
await deleteCasesByESQuery(es);
});

it('should return reporters', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { FtrProviderContext } from '../../../../../../common/ftr_provider_contex
import { CaseStatuses } from '../../../../../../../plugins/cases/common/api';
import { postCaseReq } from '../../../../../common/lib/mock';
import {
deleteCases,
deleteCasesByESQuery,
createCase,
updateCase,
getAllCasesStatuses,
Expand All @@ -24,7 +24,7 @@ export default ({ getService }: FtrProviderContext): void => {

describe('get_status', () => {
afterEach(async () => {
await deleteCases(es);
await deleteCasesByESQuery(es);
});

it('should return case statuses', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { FtrProviderContext } from '../../../../../../common/ftr_provider_contex

import { CASES_URL, CASE_TAGS_URL } from '../../../../../../../plugins/cases/common/constants';
import { postCaseReq } from '../../../../../common/lib/mock';
import { deleteCases } from '../../../../../common/lib/utils';
import { deleteCasesByESQuery } from '../../../../../common/lib/utils';

// eslint-disable-next-line import/no-default-export
export default ({ getService }: FtrProviderContext): void => {
Expand All @@ -19,7 +19,7 @@ export default ({ getService }: FtrProviderContext): void => {

describe('get_tags', () => {
afterEach(async () => {
await deleteCases(es);
await deleteCasesByESQuery(es);
});

it('should return case tags', async () => {
Expand Down
Loading

0 comments on commit 77e9a93

Please sign in to comment.