Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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 expect from '@kbn/expect';
import { FtrProviderContext } from '../../../../common/ftr_provider_context';

import { CASES_URL } from '../../../../../../plugins/case/common/constants';
import { postCaseReq, postCommentReq } from '../../../../common/lib/mock';
import { deleteCases, deleteCasesUserActions, deleteComments } from '../../../../common/lib/utils';

// eslint-disable-next-line import/no-default-export
export default ({ getService }: FtrProviderContext): void => {
const supertest = getService('supertest');
const es = getService('es');

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

it('should delete a comment', async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should add two tests that check error handling: 1) test if the comment does not exist 2) test if the comment does not belong to the specific case

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're actually not allowing this in the UI and could remove the endpoint entirely.... it's lucky to get one test

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true but users can call the API directly if they want to, so we should test the API independently of the UI, to make sure that the API is doing what we expect without considering the UI.

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(postCommentReq);

const { body: comment } = await supertest
.delete(`${CASES_URL}/${postedCase.id}/comments/${patchedCase.comments[0].id}`)
.set('kbn-xsrf', 'true')
.send();

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

it('unhappy path - 404s when comment belongs to different case', 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(postCommentReq);

const { body } = await supertest
.delete(`${CASES_URL}/fake-id/comments/${patchedCase.comments[0].id}`)
.set('kbn-xsrf', 'true')
.send()
.expect(404);
expect(body.message).to.eql(
`This comment ${patchedCase.comments[0].id} does not exist in fake-id).`
);
});

it('unhappy path - 404s when comment is not there', async () => {
await supertest
.delete(`${CASES_URL}/fake-id/comments/fake-id`)
.set('kbn-xsrf', 'true')
.send()
.expect(404);
});
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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 expect from '@kbn/expect';
import { FtrProviderContext } from '../../../../common/ftr_provider_context';

import { CASES_URL } from '../../../../../../plugins/case/common/constants';
import { postCaseReq, postCommentReq } from '../../../../common/lib/mock';
import { deleteCases, deleteCasesUserActions, deleteComments } from '../../../../common/lib/utils';

// eslint-disable-next-line import/no-default-export
export default ({ getService }: FtrProviderContext): void => {
const supertest = getService('supertest');
const es = getService('es');

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

it('should find all case comment', async () => {
const { body: postedCase } = await supertest
.post(CASES_URL)
.set('kbn-xsrf', 'true')
.send(postCaseReq)
.expect(200);
// post 2 comments
await supertest
.post(`${CASES_URL}/${postedCase.id}/comments`)
.set('kbn-xsrf', 'true')
.send(postCommentReq);

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

const { body: caseComments } = await supertest
.get(`${CASES_URL}/${postedCase.id}/comments/_find`)
.set('kbn-xsrf', 'true')
.send();

expect(caseComments.comments).to.eql(patchedCase.comments);
});

it('should filter case comments', async () => {
const { body: postedCase } = await supertest
.post(CASES_URL)
.set('kbn-xsrf', 'true')
.send(postCaseReq)
.expect(200);
// post 2 comments
await supertest
.post(`${CASES_URL}/${postedCase.id}/comments`)
.set('kbn-xsrf', 'true')
.send(postCommentReq);

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

const { body: caseComments } = await supertest
.get(`${CASES_URL}/${postedCase.id}/comments/_find?search=unique`)
.set('kbn-xsrf', 'true')
.send();

expect(caseComments.comments).to.eql([patchedCase.comments[1]]);
});

it('unhappy path - 400s when query is bad', async () => {
const { body: postedCase } = await supertest
.post(CASES_URL)
.set('kbn-xsrf', 'true')
.send(postCaseReq)
.expect(200);
await supertest
.post(`${CASES_URL}/${postedCase.id}/comments`)
.set('kbn-xsrf', 'true')
.send(postCommentReq);
await supertest
.get(`${CASES_URL}/${postedCase.id}/comments/_find?perPage=true`)
.set('kbn-xsrf', 'true')
.send()
.expect(400);
});
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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 expect from '@kbn/expect';
import { FtrProviderContext } from '../../../../common/ftr_provider_context';

import { CASES_URL } from '../../../../../../plugins/case/common/constants';
import { postCaseReq, postCommentReq } from '../../../../common/lib/mock';
import { deleteCases, deleteCasesUserActions, deleteComments } from '../../../../common/lib/utils';

// eslint-disable-next-line import/no-default-export
export default ({ getService }: FtrProviderContext): void => {
const supertest = getService('supertest');
const es = getService('es');

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

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

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

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

expect(comment).to.eql(patchedCase.comments[0]);
});
it('unhappy path - 404s when comment is not there', async () => {
await supertest
.get(`${CASES_URL}/fake-id/comments/fake-comment`)
.set('kbn-xsrf', 'true')
.send()
.expect(404);
});
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* 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 expect from '@kbn/expect';
import { FtrProviderContext } from '../../../../common/ftr_provider_context';

import { CASES_URL } from '../../../../../../plugins/case/common/constants';
import { defaultUser, postCaseReq, postCommentReq } from '../../../../common/lib/mock';
import { deleteCases, deleteCasesUserActions, deleteComments } from '../../../../common/lib/utils';

// eslint-disable-next-line import/no-default-export
export default ({ getService }: FtrProviderContext): void => {
const supertest = getService('supertest');
const es = getService('es');

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

it('should patch a comment', 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(postCommentReq);
const newComment = 'Well I decided to update my comment. So what? Deal with it.';
const { body } = await supertest
.patch(`${CASES_URL}/${postedCase.id}/comments`)
.set('kbn-xsrf', 'true')
.send({
id: patchedCase.comments[0].id,
version: patchedCase.comments[0].version,
comment: newComment,
});
expect(body.comments[0].comment).to.eql(newComment);
expect(body.updated_by).to.eql(defaultUser);
});

it('unhappy path - 404s when comment is not there', async () => {
const { body: postedCase } = await supertest
.post(CASES_URL)
.set('kbn-xsrf', 'true')
.send(postCaseReq);
await supertest
.patch(`${CASES_URL}/${postedCase.id}/comments`)
.set('kbn-xsrf', 'true')
.send({
id: 'id',
version: 'version',
comment: 'comment',
})
.expect(404);
});

it('unhappy path - 404s when case is not there', async () => {
await supertest
.patch(`${CASES_URL}/fake-id/comments`)
.set('kbn-xsrf', 'true')
.send({
id: 'id',
version: 'version',
comment: 'comment',
})
.expect(404);
});

it('unhappy path - 400s when patch body is bad', 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(postCommentReq);
await supertest
.patch(`${CASES_URL}/${postedCase.id}/comments`)
.set('kbn-xsrf', 'true')
.send({
id: patchedCase.comments[0].id,
version: patchedCase.comments[0].version,
comment: true,
})
.expect(400);
});

it('unhappy path - 409s when conflict', 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(postCommentReq);
const newComment = 'Well I decided to update my comment. So what? Deal with it.';
await supertest
.patch(`${CASES_URL}/${postedCase.id}/comments`)
.set('kbn-xsrf', 'true')
.send({
id: patchedCase.comments[0].id,
version: 'version-mismatch',
comment: newComment,
})
.expect(409);
});
});
};
Loading