-
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] Total external references and persistable state attachments per case #162071
Merged
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e6cfc4d
Validate persistableState and externalReferences.
adcoelho b057087
Merge remote-tracking branch 'upstream/main' into validate-total-atta…
adcoelho ac58ed4
Merge remote-tracking branch 'upstream/main' into validate-total-atta…
adcoelho e33fdd9
Tests.
adcoelho 5461ebb
Merge remote-tracking branch 'upstream/main' into validate-total-atta…
adcoelho c0e0d82
Final tests.
adcoelho 882243b
Merge remote-tracking branch 'upstream/main' into validate-total-atta…
adcoelho 3fa3b39
Addressing PR comments.
adcoelho 69d437b
Integration tests.
adcoelho 555c2eb
Merge remote-tracking branch 'upstream/main' into validate-total-atta…
adcoelho 23ddd38
Merge remote-tracking branch 'upstream/main' into validate-total-atta…
adcoelho c66c1c8
Last PR comments.
adcoelho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
.../server/common/limiter_checker/limiters/persistable_state_and_external_references.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* 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 { createAttachmentServiceMock } from '../../../services/mocks'; | ||
import { PersistableStateAndExternalReferencesLimiter } from './persistable_state_and_external_references'; | ||
import { | ||
createExternalReferenceRequests, | ||
createFileRequests, | ||
createPersistableStateRequests, | ||
createUserRequests, | ||
} from '../test_utils'; | ||
import { MAX_PERSISTABLE_STATE_AND_EXTERNAL_REFERENCES } from '../../../../common/constants'; | ||
|
||
describe('PersistableStateAndExternalReferencesLimiter', () => { | ||
const caseId = 'test-id'; | ||
const attachmentService = createAttachmentServiceMock(); | ||
attachmentService.countPersistableStateAndExternalReferenceAttachments.mockResolvedValue(1); | ||
|
||
const limiter = new PersistableStateAndExternalReferencesLimiter(attachmentService); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('public fields', () => { | ||
it('sets the errorMessage to the 100 limit', () => { | ||
expect(limiter.errorMessage).toMatchInlineSnapshot( | ||
`"Case has reached the maximum allowed number (100) of attached persistable state and external reference attachments."` | ||
); | ||
}); | ||
|
||
it('sets the limit to 100', () => { | ||
expect(limiter.limit).toBe(MAX_PERSISTABLE_STATE_AND_EXTERNAL_REFERENCES); | ||
}); | ||
}); | ||
|
||
describe('countOfItemsWithinCase', () => { | ||
it('calls the attachment service with the right params', () => { | ||
limiter.countOfItemsWithinCase(caseId); | ||
|
||
expect( | ||
attachmentService.countPersistableStateAndExternalReferenceAttachments | ||
).toHaveBeenCalledWith({ caseId }); | ||
}); | ||
}); | ||
|
||
describe('countOfItemsInRequest', () => { | ||
it('returns 0 when passed an empty array', () => { | ||
expect(limiter.countOfItemsInRequest([])).toBe(0); | ||
}); | ||
|
||
it('returns 0 when the requests are not for persistable state attachments or external references', () => { | ||
expect(limiter.countOfItemsInRequest(createUserRequests(2))).toBe(0); | ||
}); | ||
|
||
it('counts persistable state attachments or external references correctly', () => { | ||
expect( | ||
limiter.countOfItemsInRequest([ | ||
createPersistableStateRequests(1)[0], | ||
createExternalReferenceRequests(1)[0], | ||
createUserRequests(1)[0], | ||
createFileRequests({ | ||
numRequests: 1, | ||
numFiles: 1, | ||
})[0], | ||
]) | ||
).toBe(2); | ||
}); | ||
|
||
it('excludes fileAttachmentsRequests from the count', () => { | ||
expect( | ||
limiter.countOfItemsInRequest( | ||
createFileRequests({ | ||
numRequests: 1, | ||
numFiles: 1, | ||
}) | ||
) | ||
).toBe(0); | ||
}); | ||
}); | ||
}); |
37 changes: 37 additions & 0 deletions
37
...cases/server/common/limiter_checker/limiters/persistable_state_and_external_references.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* 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 type { AttachmentService } from '../../../services'; | ||
import { CommentType } from '../../../../common/api'; | ||
import type { CommentRequest } from '../../../../common/api'; | ||
import { MAX_PERSISTABLE_STATE_AND_EXTERNAL_REFERENCES } from '../../../../common/constants'; | ||
import { isFileAttachmentRequest, isPersistableStateOrExternalReference } from '../../utils'; | ||
import { BaseLimiter } from '../base_limiter'; | ||
|
||
export class PersistableStateAndExternalReferencesLimiter extends BaseLimiter { | ||
constructor(private readonly attachmentService: AttachmentService) { | ||
super({ | ||
limit: MAX_PERSISTABLE_STATE_AND_EXTERNAL_REFERENCES, | ||
attachmentType: [CommentType.persistableState, CommentType.externalReference], | ||
attachmentNoun: 'persistable state and external reference attachments', | ||
}); | ||
} | ||
|
||
public async countOfItemsWithinCase(caseId: string): Promise<number> { | ||
return this.attachmentService.countPersistableStateAndExternalReferenceAttachments({ | ||
caseId, | ||
}); | ||
} | ||
|
||
public countOfItemsInRequest(requests: CommentRequest[]): number { | ||
const totalReferences = requests | ||
.filter(isPersistableStateOrExternalReference) | ||
.filter((request) => !isFileAttachmentRequest(request)); | ||
|
||
return totalReferences.length; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit: There are the same mocks in
x-pack/plugins/cases/server/mocks.ts
orx-pack/test/cases_api_integration/common/lib/mock.ts
. Otherwise, we can extract these mock functions tox-pack/plugins/cases/server/mocks.ts
.