From 3b8ca25ab1aca2fd6a3d4304e88f2836792f0f71 Mon Sep 17 00:00:00 2001 From: Christos Nasikas Date: Thu, 8 Apr 2021 20:23:23 +0300 Subject: [PATCH] Convert get comment test --- .../api/cases/comments/get_comment.test.ts | 71 ------------------- .../case_api_integration/common/lib/utils.ts | 30 ++++++++ .../tests/common/comments/get_all_comments.ts | 30 ++------ .../tests/common/comments/get_comment.ts | 37 +++------- 4 files changed, 47 insertions(+), 121 deletions(-) delete mode 100644 x-pack/plugins/cases/server/routes/api/cases/comments/get_comment.test.ts diff --git a/x-pack/plugins/cases/server/routes/api/cases/comments/get_comment.test.ts b/x-pack/plugins/cases/server/routes/api/cases/comments/get_comment.test.ts deleted file mode 100644 index 8ee43eaba8a827..00000000000000 --- a/x-pack/plugins/cases/server/routes/api/cases/comments/get_comment.test.ts +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import { kibanaResponseFactory, RequestHandler } from 'src/core/server'; -import { httpServerMock } from 'src/core/server/mocks'; - -import { - createMockSavedObjectsRepository, - createRoute, - createRouteContext, - mockCaseComments, - mockCases, -} from '../../__fixtures__'; -import { flattenCommentSavedObject } from '../../utils'; -import { initGetCommentApi } from './get_comment'; -import { CASE_COMMENT_DETAILS_URL } from '../../../../../common/constants'; - -describe('GET comment', () => { - let routeHandler: RequestHandler; - beforeAll(async () => { - routeHandler = await createRoute(initGetCommentApi, 'get'); - }); - it(`returns the comment`, async () => { - const request = httpServerMock.createKibanaRequest({ - path: CASE_COMMENT_DETAILS_URL, - method: 'get', - params: { - case_id: 'mock-id-1', - comment_id: 'mock-comment-1', - }, - }); - - const { context } = await createRouteContext( - createMockSavedObjectsRepository({ - caseSavedObject: mockCases, - caseCommentSavedObject: mockCaseComments, - }) - ); - - const response = await routeHandler(context, request, kibanaResponseFactory); - expect(response.status).toEqual(200); - const myPayload = mockCaseComments.find((s) => s.id === 'mock-comment-1'); - expect(myPayload).not.toBeUndefined(); - if (myPayload != null) { - expect(response.payload).toEqual(flattenCommentSavedObject(myPayload)); - } - }); - it(`returns an error when getComment throws`, async () => { - const request = httpServerMock.createKibanaRequest({ - path: CASE_COMMENT_DETAILS_URL, - method: 'get', - params: { - case_id: 'mock-id-1', - comment_id: 'not-real', - }, - }); - - const { context } = await createRouteContext( - createMockSavedObjectsRepository({ - caseCommentSavedObject: mockCaseComments, - }) - ); - - const response = await routeHandler(context, request, kibanaResponseFactory); - expect(response.status).toEqual(404); - }); -}); diff --git a/x-pack/test/case_api_integration/common/lib/utils.ts b/x-pack/test/case_api_integration/common/lib/utils.ts index 9e8f8afeaf2c8a..7c3067081a1d97 100644 --- a/x-pack/test/case_api_integration/common/lib/utils.ts +++ b/x-pack/test/case_api_integration/common/lib/utils.ts @@ -30,6 +30,7 @@ import { SubCaseResponse, CommentResponse, CasesPatchRequest, + AllCommentsResponse, } from '../../../../plugins/cases/common/api'; import { getPostCaseRequest, postCollectionReq, postCommentGenAlertReq } from './mock'; import { getSubCasesUrl } from '../../../../plugins/cases/common/api/helpers'; @@ -615,3 +616,32 @@ export const deleteComment = async ( return comment; }; + +export const getAllComments = async ( + supertest: st.SuperTest, + caseId: string, + expectedHttpCode: number = 204 +): Promise => { + const { body: comments } = await supertest + .get(`${CASES_URL}/${caseId}/comments`) + .set('kbn-xsrf', 'true') + .send() + .expect(expectedHttpCode); + + return comments; +}; + +export const getComment = async ( + supertest: st.SuperTest, + caseId: string, + commentId: string, + expectedHttpCode: number = 204 +): Promise => { + const { body: comment } = await supertest + .get(`${CASES_URL}/${caseId}/comments/${commentId}`) + .set('kbn-xsrf', 'true') + .send() + .expect(expectedHttpCode); + + return comment; +}; diff --git a/x-pack/test/case_api_integration/security_and_spaces/tests/common/comments/get_all_comments.ts b/x-pack/test/case_api_integration/security_and_spaces/tests/common/comments/get_all_comments.ts index f02022e70e3306..06eb9d0fb4174c 100644 --- a/x-pack/test/case_api_integration/security_and_spaces/tests/common/comments/get_all_comments.ts +++ b/x-pack/test/case_api_integration/security_and_spaces/tests/common/comments/get_all_comments.ts @@ -15,6 +15,9 @@ import { createSubCase, deleteAllCaseItems, deleteCaseAction, + createCase, + createComment, + getAllComments, } from '../../../../common/lib/utils'; import { CommentType } from '../../../../../../plugins/cases/common/api'; @@ -29,29 +32,10 @@ export default ({ getService }: FtrProviderContext): void => { }); it('should get multiple comments for a single case', 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(postCommentUserReq) - .expect(200); - - await supertest - .post(`${CASES_URL}/${postedCase.id}/comments`) - .set('kbn-xsrf', 'true') - .send(postCommentUserReq) - .expect(200); - - const { body: comments } = await supertest - .get(`${CASES_URL}/${postedCase.id}/comments`) - .set('kbn-xsrf', 'true') - .send() - .expect(200); + const postedCase = await createCase(supertest, postCaseReq); + await createComment(supertest, postedCase.id, postCommentUserReq); + await createComment(supertest, postedCase.id, postCommentUserReq); + const comments = await getAllComments(supertest, postedCase.id); expect(comments.length).to.eql(2); }); diff --git a/x-pack/test/case_api_integration/security_and_spaces/tests/common/comments/get_comment.ts b/x-pack/test/case_api_integration/security_and_spaces/tests/common/comments/get_comment.ts index 97face4c8734c7..e843b31d18dfd3 100644 --- a/x-pack/test/case_api_integration/security_and_spaces/tests/common/comments/get_comment.ts +++ b/x-pack/test/case_api_integration/security_and_spaces/tests/common/comments/get_comment.ts @@ -15,8 +15,11 @@ import { createSubCase, deleteAllCaseItems, deleteCaseAction, + createCase, + createComment, + getComment, } from '../../../../common/lib/utils'; -import { CommentResponse, CommentType } from '../../../../../../plugins/cases/common/api'; +import { CommentType } from '../../../../../../plugins/cases/common/api'; // eslint-disable-next-line import/no-default-export export default ({ getService }: FtrProviderContext): void => { @@ -29,33 +32,15 @@ export default ({ getService }: FtrProviderContext): void => { }); it('should get a comment', async () => { - const { body: postedCase } = await supertest - .post(CASES_URL) - .set('kbn-xsrf', 'true') - .send(postCaseReq) - .expect(200); + const postedCase = await createCase(supertest, postCaseReq); + const patchedCase = await createComment(supertest, postedCase.id, postCommentUserReq); + const comment = await getComment(supertest, postedCase.id, patchedCase.comments![0].id); - const { body: patchedCase } = await supertest - .post(`${CASES_URL}/${postedCase.id}/comments`) - .set('kbn-xsrf', 'true') - .send(postCommentUserReq) - .expect(200); - - 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]); + 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); + await getComment(supertest, 'fake-id', 'fake-id', 404); }); // ENABLE_CASE_CONNECTOR: once the case connector feature is completed unskip these tests @@ -69,9 +54,7 @@ export default ({ getService }: FtrProviderContext): void => { }); it('should get a sub case comment', async () => { const { newSubCaseInfo: caseInfo } = await createSubCase({ supertest, actionID }); - const { body: comment }: { body: CommentResponse } = await supertest - .get(`${CASES_URL}/${caseInfo.id}/comments/${caseInfo.comments![0].id}`) - .expect(200); + const comment = await getComment(supertest, caseInfo.id, caseInfo.comments![0].id); expect(comment.type).to.be(CommentType.generatedAlert); }); });