Skip to content

Commit

Permalink
[Cases] Validate max user actions on update comment. (#163150)
Browse files Browse the repository at this point in the history
## Summary

Updating a comment in a case creates a user action. We want to validate
that updating a comment will not make the total user actions of a case
exceed 10000.

## Release Notes

Updating a case comment is now included in the 10000 user actions
restriction.
  • Loading branch information
adcoelho authored and pull[bot] committed Dec 13, 2023
1 parent 7b5ada3 commit c805855
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
47 changes: 25 additions & 22 deletions x-pack/plugins/cases/server/client/attachments/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@

import { comment, actionComment } from '../../mocks';
import { createCasesClientMockArgs } from '../mocks';
import { MAX_COMMENT_LENGTH } from '../../../common/constants';
import { MAX_COMMENT_LENGTH, MAX_USER_ACTIONS_PER_CASE } from '../../../common/constants';
import { update } from './update';
import { createUserActionServiceMock } from '../../services/mocks';

describe('update', () => {
const caseID = 'test-case';

const clientArgs = createCasesClientMockArgs();
const userActionService = createUserActionServiceMock();

clientArgs.services.userActionService = userActionService;

beforeEach(() => {
jest.clearAllMocks();
Expand All @@ -25,36 +31,39 @@ describe('update', () => {
.toString();

await expect(
update(
{ updateRequest: { ...updateComment, comment: longComment }, caseID: 'test-case' },
clientArgs
)
update({ updateRequest: { ...updateComment, comment: longComment }, caseID }, clientArgs)
).rejects.toThrow(
`Failed to patch comment case id: test-case: Error: The length of the comment is too long. The maximum length is ${MAX_COMMENT_LENGTH}.`
);
});

it('should throw an error if the comment is an empty string', async () => {
await expect(
update(
{ updateRequest: { ...updateComment, comment: '' }, caseID: 'test-case' },
clientArgs
)
update({ updateRequest: { ...updateComment, comment: '' }, caseID }, clientArgs)
).rejects.toThrow(
'Failed to patch comment case id: test-case: Error: The comment field cannot be an empty string.'
);
});

it('should throw an error if the description is a string with empty characters', async () => {
await expect(
update(
{ updateRequest: { ...updateComment, comment: ' ' }, caseID: 'test-case' },
clientArgs
)
update({ updateRequest: { ...updateComment, comment: ' ' }, caseID }, clientArgs)
).rejects.toThrow(
'Failed to patch comment case id: test-case: Error: The comment field cannot be an empty string.'
);
});

it(`throws error when the case user actions become > ${MAX_USER_ACTIONS_PER_CASE}`, async () => {
userActionService.getMultipleCasesUserActionsTotal.mockResolvedValue({
[caseID]: MAX_USER_ACTIONS_PER_CASE,
});

await expect(
update({ updateRequest: { ...updateComment }, caseID }, clientArgs)
).rejects.toThrow(
`The case with id ${caseID} has reached the limit of ${MAX_USER_ACTIONS_PER_CASE} user actions.`
);
});
});

describe('actions', () => {
Expand All @@ -67,7 +76,7 @@ describe('update', () => {

await expect(
update(
{ updateRequest: { ...updateActionComment, comment: longComment }, caseID: 'test-case' },
{ updateRequest: { ...updateActionComment, comment: longComment }, caseID },
clientArgs
)
).rejects.toThrow(
Expand All @@ -77,21 +86,15 @@ describe('update', () => {

it('should throw an error if the comment is an empty string', async () => {
await expect(
update(
{ updateRequest: { ...updateActionComment, comment: '' }, caseID: 'test-case' },
clientArgs
)
update({ updateRequest: { ...updateActionComment, comment: '' }, caseID }, clientArgs)
).rejects.toThrow(
'Failed to patch comment case id: test-case: Error: The comment field cannot be an empty string.'
);
});

it('should throw an error if the description is a string with empty characters', async () => {
await expect(
update(
{ updateRequest: { ...updateActionComment, comment: ' ' }, caseID: 'test-case' },
clientArgs
)
update({ updateRequest: { ...updateActionComment, comment: ' ' }, caseID }, clientArgs)
).rejects.toThrow(
'Failed to patch comment case id: test-case: Error: The comment field cannot be an empty string.'
);
Expand Down
8 changes: 7 additions & 1 deletion x-pack/plugins/cases/server/client/attachments/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import Boom from '@hapi/boom';

import { validateMaxUserActions } from '../../../common/utils/validators';
import { AttachmentPatchRequestRt } from '../../../common/types/api';
import { CaseCommentModel } from '../../common/models';
import { createCaseError } from '../../common/error';
Expand All @@ -29,7 +30,7 @@ export async function update(
clientArgs: CasesClientArgs
): Promise<Case> {
const {
services: { attachmentService },
services: { attachmentService, userActionService },
logger,
authorization,
externalReferenceAttachmentTypeRegistry,
Expand All @@ -41,6 +42,11 @@ export async function update(
version: queryCommentVersion,
...queryRestAttributes
} = decodeWithExcessOrThrow(AttachmentPatchRequestRt)(queryParams);
await validateMaxUserActions({
caseId: caseID,
userActionService,
userActionsToAdd: 1,
});

decodeCommentRequest(queryRestAttributes, externalReferenceAttachmentTypeRegistry);

Expand Down

0 comments on commit c805855

Please sign in to comment.