-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Cases] Add guardrails for add and update comment API #161200
Changes from 6 commits
dd10222
f87deb5
c719484
74868c0
a3795f6
28004e9
480adef
75eb73a
3ca8625
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* 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 { comment, actionComment } from '../../mocks'; | ||
import { createCasesClientMockArgs } from '../mocks'; | ||
import { MAX_COMMENT_LENGTH } from '../../../common/constants'; | ||
import { update } from './update'; | ||
|
||
describe('update', () => { | ||
const clientArgs = createCasesClientMockArgs(); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('comments', () => { | ||
const updateComment = { ...comment, id: 'comment-id', version: 'WzAsMV0=' }; | ||
it('should throw an error if the comment length is too long', async () => { | ||
const longComment = Array(MAX_COMMENT_LENGTH + 1) | ||
.fill('x') | ||
.toString(); | ||
|
||
await expect( | ||
update( | ||
{ updateRequest: { ...updateComment, comment: longComment }, caseID: 'test-case' }, | ||
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 | ||
) | ||
).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 | ||
) | ||
).rejects.toThrow( | ||
'Failed to patch comment case id: test-case: Error: The comment field cannot be an empty string.' | ||
); | ||
}); | ||
}); | ||
|
||
describe('actions', () => { | ||
const updateActionComment = { ...actionComment, id: 'comment-id', version: 'WzAsMV0=' }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is the difference between Edit: I know it is the type 😅 but more specifically, how are they different in practice? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think @cnasikas can answer it 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If a user isolates a host from the alerts flyout the security solution team adds a comment to a case + some information about the host etc. At the time we did not have the attachment framework so this became a core attachment type. |
||
|
||
it('should throw an error if the comment length is too long', async () => { | ||
const longComment = Array(MAX_COMMENT_LENGTH + 1) | ||
.fill('x') | ||
.toString(); | ||
|
||
await expect( | ||
update( | ||
{ updateRequest: { ...updateActionComment, comment: longComment }, caseID: 'test-case' }, | ||
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: { ...updateActionComment, comment: '' }, caseID: 'test-case' }, | ||
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 | ||
) | ||
).rejects.toThrow( | ||
'Failed to patch comment case id: test-case: Error: The comment field cannot be an empty string.' | ||
); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add some tests for the
CommentRequestRt
to verify the validation. In general, I think it is nice if we created a strong linked chain of tests. For example, we test theCommentRequestRt
with all possible testing scenarios. Then we test theaddComment
andbulkCreate
but there there is no need to check all possible testing scenarios (unless there is new logic on top of it). One is enough to ensure the integration between the function and the schema. The chain now isaddComment
->CommentRequestRt
. If the tests inCommentRequestRt
pass we are sure thataddComment
will work correctly because we have at least one test that tests the integration between the two. Lastly, with integration tests, we check the integration between theaddComment
and the route.This article explains the concept in detail: https://www.jamesshore.com/v2/projects/nullables/testing-without-mocks#sociable-tests
cc @adcoelho
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wondered about this but we need to guarantee somehow that (in this case)
CommentRequestRt
is being used byaddComment
andbulkCreate
.Is this what you mean with
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh okay, so just checking if I understood it correctly,
long comment, empty string
etc should be part ofCommentRequestRt
tests inx-pack/plugins/cases/common/api/cases/comment/index.test.ts
addComment, update and bulk create
just tests the integration withCommentRequestRt
with one testroute
andpost_comment, patch_comment, bulk_create_attachments
integration with one test?