From 3fa3b398ec8eb9bd13a450f559aec2f340d80b3f Mon Sep 17 00:00:00 2001 From: adcoelho Date: Mon, 24 Jul 2023 14:25:49 +0200 Subject: [PATCH] Addressing PR comments. --- .../plugins/cases/server/client/cases/mock.ts | 4 +-- ...ble_state_and_external_references.test.ts} | 17 ++++----- ...sistable_state_and_external_references.ts} | 0 .../common/models/case_with_comments.test.ts | 6 ++-- .../server/services/attachments/index.test.ts | 35 +++++++++++++++++++ 5 files changed, 49 insertions(+), 13 deletions(-) rename x-pack/plugins/cases/server/common/limiter_checker/limiters/{persistableStateAndExternalReferences.test.ts => persistable_state_and_external_references.test.ts} (89%) rename x-pack/plugins/cases/server/common/limiter_checker/limiters/{persistableStateAndExternalReferences.ts => persistable_state_and_external_references.ts} (100%) diff --git a/x-pack/plugins/cases/server/client/cases/mock.ts b/x-pack/plugins/cases/server/client/cases/mock.ts index 7e92e8c2e1beb..ca96d20bad570 100644 --- a/x-pack/plugins/cases/server/client/cases/mock.ts +++ b/x-pack/plugins/cases/server/client/cases/mock.ts @@ -228,13 +228,13 @@ export const commentPersistableState: Comment = { version: 'WzEsMV0=', }; -export const commentFilePersistableState: Comment = { +export const commentFileExternalReference: Comment = { ...commentExternalReference, externalReferenceAttachmentTypeId: FILE_ATTACHMENT_TYPE, externalReferenceMetadata: { files: [{ name: '', extension: '', mimeType: '', created: '' }] }, externalReferenceStorage: { type: ExternalReferenceStorageType.savedObject as const, - soType: 'iuhu', + soType: 'file', }, }; diff --git a/x-pack/plugins/cases/server/common/limiter_checker/limiters/persistableStateAndExternalReferences.test.ts b/x-pack/plugins/cases/server/common/limiter_checker/limiters/persistable_state_and_external_references.test.ts similarity index 89% rename from x-pack/plugins/cases/server/common/limiter_checker/limiters/persistableStateAndExternalReferences.test.ts rename to x-pack/plugins/cases/server/common/limiter_checker/limiters/persistable_state_and_external_references.test.ts index 8f64ff87564ae..df40e3841f1d7 100644 --- a/x-pack/plugins/cases/server/common/limiter_checker/limiters/persistableStateAndExternalReferences.test.ts +++ b/x-pack/plugins/cases/server/common/limiter_checker/limiters/persistable_state_and_external_references.test.ts @@ -6,7 +6,7 @@ */ import { createAttachmentServiceMock } from '../../../services/mocks'; -import { PersistableStateAndExternalReferencesLimiter } from './persistableStateAndExternalReferences'; +import { PersistableStateAndExternalReferencesLimiter } from './persistable_state_and_external_references'; import { createExternalReferenceRequests, createFileRequests, @@ -18,11 +18,7 @@ import { MAX_PERSISTABLE_STATE_AND_EXTERNAL_REFERENCES } from '../../../../commo describe('PersistableStateAndExternalReferencesLimiter', () => { const caseId = 'test-id'; const attachmentService = createAttachmentServiceMock(); - attachmentService.countPersistableStateAndExternalReferenceAttachments.mockImplementation( - async () => { - return 1; - } - ); + attachmentService.countPersistableStateAndExternalReferenceAttachments.mockResolvedValue(1); const limiter = new PersistableStateAndExternalReferencesLimiter(attachmentService); @@ -31,13 +27,13 @@ describe('PersistableStateAndExternalReferencesLimiter', () => { }); describe('public fields', () => { - it('sets the errorMessage to the 1k limit', () => { + 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 1k', () => { + it('sets the limit to 100', () => { expect(limiter.limit).toBe(MAX_PERSISTABLE_STATE_AND_EXTERNAL_REFERENCES); }); }); @@ -66,6 +62,11 @@ describe('PersistableStateAndExternalReferencesLimiter', () => { limiter.countOfItemsInRequest([ createPersistableStateRequests(1)[0], createExternalReferenceRequests(1)[0], + createUserRequests(1)[0], + createFileRequests({ + numRequests: 1, + numFiles: 1, + })[0], ]) ).toBe(2); }); diff --git a/x-pack/plugins/cases/server/common/limiter_checker/limiters/persistableStateAndExternalReferences.ts b/x-pack/plugins/cases/server/common/limiter_checker/limiters/persistable_state_and_external_references.ts similarity index 100% rename from x-pack/plugins/cases/server/common/limiter_checker/limiters/persistableStateAndExternalReferences.ts rename to x-pack/plugins/cases/server/common/limiter_checker/limiters/persistable_state_and_external_references.ts diff --git a/x-pack/plugins/cases/server/common/models/case_with_comments.test.ts b/x-pack/plugins/cases/server/common/models/case_with_comments.test.ts index f5684969797d4..779ed8767a9b3 100644 --- a/x-pack/plugins/cases/server/common/models/case_with_comments.test.ts +++ b/x-pack/plugins/cases/server/common/models/case_with_comments.test.ts @@ -13,7 +13,7 @@ import { CaseCommentModel } from './case_with_comments'; import { MAX_PERSISTABLE_STATE_AND_EXTERNAL_REFERENCES } from '../../../common/constants'; import { commentExternalReference, - commentFilePersistableState, + commentFileExternalReference, commentPersistableState, } from '../../client/cases/mock'; @@ -313,7 +313,7 @@ describe('CaseCommentModel', () => { await expect( model.createComment({ id: 'comment-1', - commentReq: commentFilePersistableState, + commentReq: commentFileExternalReference, createdDate, }) ).resolves.not.toThrow(); @@ -613,7 +613,7 @@ describe('CaseCommentModel', () => { await expect( model.bulkCreate({ - attachments: [commentFilePersistableState], + attachments: [commentFileExternalReference], }) ).resolves.not.toThrow(); }); diff --git a/x-pack/plugins/cases/server/services/attachments/index.test.ts b/x-pack/plugins/cases/server/services/attachments/index.test.ts index 54a48f0a53c67..5967cb6fb2600 100644 --- a/x-pack/plugins/cases/server/services/attachments/index.test.ts +++ b/x-pack/plugins/cases/server/services/attachments/index.test.ts @@ -537,4 +537,39 @@ describe('AttachmentService', () => { }); }); }); + + describe('countPersistableStateAndExternalReferenceAttachments', () => { + it('does not throw and calls unsecuredSavedObjectsClient.find with the right parameters', async () => { + unsecuredSavedObjectsClient.find.mockResolvedValue( + createSOFindResponse([{ ...createUserAttachment(), score: 0 }]) + ); + + await expect( + service.countPersistableStateAndExternalReferenceAttachments({ caseId: 'test-id' }) + ).resolves.not.toThrow(); + + await expect(unsecuredSavedObjectsClient.find).toHaveBeenCalledWith( + expect.objectContaining({ + hasReference: { id: 'test-id', type: 'cases' }, + type: 'cases-comments', + }) + ); + }); + + it('returns the expected total', async () => { + const total = 3; + + unsecuredSavedObjectsClient.find.mockResolvedValue( + createSOFindResponse( + Array(total).fill({ ...createUserAttachment({ foo: 'bar' }), score: 0 }) + ) + ); + + const res = await service.countPersistableStateAndExternalReferenceAttachments({ + caseId: 'test-id', + }); + + expect(res).toBe(total); + }); + }); });