Skip to content

Commit

Permalink
[Cases] Fix get case API totalComment to only return user comments (#…
Browse files Browse the repository at this point in the history
…160167)

This PR fixes a bug so that the get case API `totalComment` field only
includes user comments in the total. Prior to this fix the value
included all comment types.

Fixes: #156361
#158344

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
jonathan-buttner and kibanamachine authored Jun 22, 2023
1 parent 10d81a1 commit c5d330b
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
4 changes: 2 additions & 2 deletions x-pack/plugins/cases/server/client/cases/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
GetCategoriesResponseRt,
} from '../../../common/api';
import { createCaseError } from '../../common/error';
import { countAlertsForID, flattenCaseSavedObject } from '../../common/utils';
import { countAlertsForID, flattenCaseSavedObject, countUserAttachments } from '../../common/utils';
import type { CasesClientArgs } from '..';
import { Operations } from '../../authorization';
import { combineAuthorizedAndOwnerFilter } from '../utils';
Expand Down Expand Up @@ -207,7 +207,7 @@ export const get = async (
const res = flattenCaseSavedObject({
savedObject: theCase,
comments: theComments.saved_objects,
totalComment: theComments.total,
totalComment: countUserAttachments(theComments.saved_objects),
totalAlerts: countAlertsForID({ comments: theComments, id }),
});

Expand Down
20 changes: 20 additions & 0 deletions x-pack/plugins/cases/server/common/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ import {
getApplicationRoute,
getCaseViewPath,
isSOError,
countUserAttachments,
} from './utils';
import { newCase } from '../routes/api/__mocks__/request_responses';
import { CASE_VIEW_PAGE_TABS } from '../../common/types';
import { mockCases, mockCaseComments } from '../mocks';
import { createAlertAttachment, createUserAttachment } from '../services/attachments/test_utils';

interface CommentReference {
ids: string[];
Expand Down Expand Up @@ -1332,4 +1334,22 @@ describe('common utils', () => {
expect(isSOError({})).toBe(false);
});
});

describe('countUserAttachments', () => {
it('returns 0 for an empty array', () => {
expect(countUserAttachments([])).toBe(0);
});

it('returns 1 when there is only 1 user attachment', () => {
const attachments = [createUserAttachment(), createAlertAttachment()];

expect(countUserAttachments(attachments)).toBe(1);
});

it('returns 0 when there is only alert attachments', () => {
const attachments = [createAlertAttachment()];

expect(countUserAttachments(attachments)).toBe(0);
});
});
});
14 changes: 14 additions & 0 deletions x-pack/plugins/cases/server/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,17 @@ export const getCaseViewPath = (params: {
};

export const isSOError = <T>(so: { error?: unknown }): so is SOWithErrors<T> => so.error != null;

export const countUserAttachments = (
attachments: Array<SavedObject<CommentAttributes>>
): number => {
let total = 0;

for (const attachment of attachments) {
if (attachment.attributes.type === CommentType.user) {
total += 1;
}
}

return total;
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
postCaseResp,
postCommentUserReq,
getPostCaseRequest,
postCommentAlertReq,
} from '../../../../common/lib/mock';
import {
deleteCasesByESQuery,
Expand All @@ -25,6 +26,7 @@ import {
removeServerGeneratedPropertiesFromCase,
removeServerGeneratedPropertiesFromSavedObject,
extractWarningValueFromWarningHeader,
bulkCreateAttachments,
} from '../../../../common/lib/api';
import {
secOnly,
Expand Down Expand Up @@ -81,6 +83,34 @@ export default ({ getService }: FtrProviderContext): void => {
});
});

it('should set totalComment to 0 when all attachments are alerts', async () => {
const postedCase = await createCase(supertest, postCaseReq);

await bulkCreateAttachments({
supertest,
caseId: postedCase.id,
params: [postCommentAlertReq, postCommentAlertReq],
});

const theCase = await getCase({ supertest, caseId: postedCase.id, includeComments: true });

expect(theCase.totalComment).to.be(0);
});

it('should set totalComment to 1 when there is one user attachment and one alert', async () => {
const postedCase = await createCase(supertest, postCaseReq);

await bulkCreateAttachments({
supertest,
caseId: postedCase.id,
params: [postCommentAlertReq, postCommentUserReq],
});

const theCase = await getCase({ supertest, caseId: postedCase.id, includeComments: true });

expect(theCase.totalComment).to.be(1);
});

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

0 comments on commit c5d330b

Please sign in to comment.