-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Cases] Total external references and persistable state attachments p…
…er case (#162071) Connected to #146945 ## Summary | Description | Limit | Done? | Documented? | ------------- | ---- | :---: | ---- | | Total number of attachments (external references and persistable state) per case | 100 | ✅ | No | ### Checklist Delete any items that are not applicable to this PR. - [x] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios ### Release Notes A case can now only have 100 external references and persistable state(excluding files) attachments combined.
- Loading branch information
Showing
15 changed files
with
693 additions
and
89 deletions.
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.