From 69d437b38903a23958cdaf80ecf7e981626395d5 Mon Sep 17 00:00:00 2001 From: adcoelho Date: Mon, 24 Jul 2023 15:11:19 +0200 Subject: [PATCH] Integration tests. --- .../server/common/limiter_checker/index.ts | 2 +- .../common/limiter_checker/test_utils.ts | 6 +- .../tests/common/comments/post_comment.ts | 72 ++++++ .../internal/bulk_create_attachments.ts | 242 ++++++++++++------ 4 files changed, 234 insertions(+), 88 deletions(-) diff --git a/x-pack/plugins/cases/server/common/limiter_checker/index.ts b/x-pack/plugins/cases/server/common/limiter_checker/index.ts index 31a78cc7526d9..1ef34d70e7e25 100644 --- a/x-pack/plugins/cases/server/common/limiter_checker/index.ts +++ b/x-pack/plugins/cases/server/common/limiter_checker/index.ts @@ -13,7 +13,7 @@ import type { AttachmentService } from '../../services'; import type { Limiter } from './types'; import { AlertLimiter } from './limiters/alerts'; import { FileLimiter } from './limiters/files'; -import { PersistableStateAndExternalReferencesLimiter } from './limiters/persistableStateAndExternalReferences'; +import { PersistableStateAndExternalReferencesLimiter } from './limiters/persistable_state_and_external_references'; export class AttachmentLimitChecker { private readonly limiters: Limiter[]; diff --git a/x-pack/plugins/cases/server/common/limiter_checker/test_utils.ts b/x-pack/plugins/cases/server/common/limiter_checker/test_utils.ts index bddb686609044..fc9515c133069 100644 --- a/x-pack/plugins/cases/server/common/limiter_checker/test_utils.ts +++ b/x-pack/plugins/cases/server/common/limiter_checker/test_utils.ts @@ -31,9 +31,9 @@ export const createUserRequests = (num: number): CommentRequestUserType[] => { export const createPersistableStateRequests = ( num: number ): CommentRequestPersistableStateType[] => { - return [...Array(num).keys()].map((value) => { + return [...Array(num).keys()].map(() => { return { - persistableStateAttachmentTypeId: 'some-id', + persistableStateAttachmentTypeId: '.test', persistableStateAttachmentState: {}, type: CommentType.persistableState as const, owner: 'test', @@ -48,7 +48,7 @@ export const createExternalReferenceRequests = ( return { type: CommentType.externalReference as const, owner: 'test', - externalReferenceAttachmentTypeId: 'doesnt-matter', + externalReferenceAttachmentTypeId: '.test', externalReferenceId: 'so-id', externalReferenceMetadata: {}, externalReferenceStorage: { diff --git a/x-pack/test/cases_api_integration/security_and_spaces/tests/common/comments/post_comment.ts b/x-pack/test/cases_api_integration/security_and_spaces/tests/common/comments/post_comment.ts index 8851a95c6ebc3..0bcde310a224b 100644 --- a/x-pack/test/cases_api_integration/security_and_spaces/tests/common/comments/post_comment.ts +++ b/x-pack/test/cases_api_integration/security_and_spaces/tests/common/comments/post_comment.ts @@ -16,6 +16,7 @@ import { CaseStatuses, CommentRequestExternalReferenceSOType, CommentRequestAlertType, + ExternalReferenceStorageType, } from '@kbn/cases-plugin/common/api'; import { FtrProviderContext } from '../../../../common/ftr_provider_context'; import { @@ -42,6 +43,7 @@ import { getCaseUserActions, removeServerGeneratedPropertiesFromUserAction, getAllComments, + bulkCreateAttachments, } from '../../../../common/lib/api'; import { createSignalsIndex, @@ -468,6 +470,76 @@ export default ({ getService }: FtrProviderContext): void => { expectedHttpCode: 400, }); }); + + it('400s when attempting to add a persistable state to a case that already has 100', async () => { + const postedCase = await createCase(supertest, postCaseReq); + + const attachments = Array(100).fill({ + type: CommentType.externalReference as const, + owner: 'securitySolutionFixture', + externalReferenceAttachmentTypeId: '.test', + externalReferenceId: 'so-id', + externalReferenceMetadata: {}, + externalReferenceStorage: { + soType: 'external-ref', + type: ExternalReferenceStorageType.savedObject as const, + }, + }); + + await bulkCreateAttachments({ + supertest, + caseId: postedCase.id, + params: attachments, + expectedHttpCode: 200, + }); + + await createComment({ + supertest, + caseId: postedCase.id, + params: { + persistableStateAttachmentTypeId: '.test', + persistableStateAttachmentState: {}, + type: CommentType.persistableState as const, + owner: 'securitySolutionFixture', + }, + expectedHttpCode: 400, + }); + }); + + it('400s when attempting to add an external reference to a case that already has 100', async () => { + const postedCase = await createCase(supertest, postCaseReq); + + const attachments = Array(100).fill({ + persistableStateAttachmentTypeId: '.test', + persistableStateAttachmentState: {}, + type: CommentType.persistableState as const, + owner: 'securitySolutionFixture', + }); + + await bulkCreateAttachments({ + supertest, + caseId: postedCase.id, + params: attachments, + expectedHttpCode: 200, + }); + + await createComment({ + supertest, + caseId: postedCase.id, + params: { + type: CommentType.externalReference as const, + owner: 'securitySolutionFixture', + externalReferenceAttachmentTypeId: '.test', + externalReferenceId: 'so-id', + externalReferenceMetadata: {}, + externalReferenceStorage: { + soType: 'external-ref', + type: ExternalReferenceStorageType.savedObject as const, + }, + }, + expectedHttpCode: 400, + }); + }); }); describe('alerts', () => { diff --git a/x-pack/test/cases_api_integration/security_and_spaces/tests/common/internal/bulk_create_attachments.ts b/x-pack/test/cases_api_integration/security_and_spaces/tests/common/internal/bulk_create_attachments.ts index a0b626482f473..e5eaedb6dae8c 100644 --- a/x-pack/test/cases_api_integration/security_and_spaces/tests/common/internal/bulk_create_attachments.ts +++ b/x-pack/test/cases_api_integration/security_and_spaces/tests/common/internal/bulk_create_attachments.ts @@ -15,6 +15,7 @@ import { CaseStatuses, CommentRequestExternalReferenceSOType, CommentType, + ExternalReferenceStorageType, } from '@kbn/cases-plugin/common/api'; import { FtrProviderContext } from '../../../../common/ftr_provider_context'; import { @@ -42,6 +43,7 @@ import { createAndUploadFile, deleteAllFiles, getAllComments, + createComment, } from '../../../../common/lib/api'; import { createSignalsIndex, @@ -619,102 +621,174 @@ export default ({ getService }: FtrProviderContext): void => { await createCaseAndBulkCreateAttachments({ supertest, expectedHttpCode: 400 }); }); - it('400s when attempting to add more than 1K alerts to a case', async () => { - const alerts = [...Array(1001).keys()].map((num) => `test-${num}`); - const postedCase = await createCase(supertest, postCaseReq); - await bulkCreateAttachments({ - supertest, - caseId: postedCase.id, - params: [ - { - ...postCommentAlertReq, - alertId: alerts, - index: alerts, - }, - ], - expectedHttpCode: 400, + describe('validation', () => { + it('400s when attempting to add more than 1K alerts to a case', async () => { + const alerts = [...Array(1001).keys()].map((num) => `test-${num}`); + const postedCase = await createCase(supertest, postCaseReq); + await bulkCreateAttachments({ + supertest, + caseId: postedCase.id, + params: [ + { + ...postCommentAlertReq, + alertId: alerts, + index: alerts, + }, + ], + expectedHttpCode: 400, + }); }); - }); - it('400s when attempting to add more than 1K alerts to a case in the same request', async () => { - const alerts = [...Array(1001).keys()].map((num) => `test-${num}`); - const postedCase = await createCase(supertest, postCaseReq); - await bulkCreateAttachments({ - supertest, - caseId: postedCase.id, - params: [ - { - ...postCommentAlertReq, - alertId: alerts.slice(0, 500), - index: alerts.slice(0, 500), - }, - { - ...postCommentAlertReq, - alertId: alerts.slice(500, alerts.length), - index: alerts.slice(500, alerts.length), - }, - postCommentAlertReq, - ], - expectedHttpCode: 400, + it('400s when attempting to add more than 1K alerts to a case in the same request', async () => { + const alerts = [...Array(1001).keys()].map((num) => `test-${num}`); + const postedCase = await createCase(supertest, postCaseReq); + await bulkCreateAttachments({ + supertest, + caseId: postedCase.id, + params: [ + { + ...postCommentAlertReq, + alertId: alerts.slice(0, 500), + index: alerts.slice(0, 500), + }, + { + ...postCommentAlertReq, + alertId: alerts.slice(500, alerts.length), + index: alerts.slice(500, alerts.length), + }, + postCommentAlertReq, + ], + expectedHttpCode: 400, + }); }); - }); - it('400s when attempting to add an alert to a case that already has 1K alerts', async () => { - const alerts = [...Array(1000).keys()].map((num) => `test-${num}`); - const postedCase = await createCase(supertest, postCaseReq); - await bulkCreateAttachments({ - supertest, - caseId: postedCase.id, - params: [ - { - ...postCommentAlertReq, - alertId: alerts, - index: alerts, - }, - ], + it('400s when attempting to add an alert to a case that already has 1K alerts', async () => { + const alerts = [...Array(1000).keys()].map((num) => `test-${num}`); + const postedCase = await createCase(supertest, postCaseReq); + await bulkCreateAttachments({ + supertest, + caseId: postedCase.id, + params: [ + { + ...postCommentAlertReq, + alertId: alerts, + index: alerts, + }, + ], + }); + + await bulkCreateAttachments({ + supertest, + caseId: postedCase.id, + params: [ + { + ...postCommentAlertReq, + alertId: 'test-id', + index: 'test-index', + }, + ], + expectedHttpCode: 400, + }); }); - await bulkCreateAttachments({ - supertest, - caseId: postedCase.id, - params: [ - { - ...postCommentAlertReq, - alertId: 'test-id', - index: 'test-index', - }, - ], - expectedHttpCode: 400, + it('400s when the case already has alerts and the sum of existing and new alerts exceed 1k', async () => { + const alerts = [...Array(1200).keys()].map((num) => `test-${num}`); + const postedCase = await createCase(supertest, postCaseReq); + await bulkCreateAttachments({ + supertest, + caseId: postedCase.id, + params: [ + { + ...postCommentAlertReq, + alertId: alerts.slice(0, 500), + index: alerts.slice(0, 500), + }, + ], + }); + + await bulkCreateAttachments({ + supertest, + caseId: postedCase.id, + params: [ + { + ...postCommentAlertReq, + alertId: alerts.slice(500), + index: alerts.slice(500), + }, + postCommentAlertReq, + ], + expectedHttpCode: 400, + }); }); - }); - it('400s when the case already has alerts and the sum of existing and new alerts exceed 1k', async () => { - const alerts = [...Array(1200).keys()].map((num) => `test-${num}`); - const postedCase = await createCase(supertest, postCaseReq); - await bulkCreateAttachments({ - supertest, - caseId: postedCase.id, - params: [ - { - ...postCommentAlertReq, - alertId: alerts.slice(0, 500), - index: alerts.slice(0, 500), + it('400s when attempting to bulk create persistable state attachments reaching the 100 limit', async () => { + const postedCase = await createCase(supertest, postCaseReq); + + await createComment({ + supertest, + caseId: postedCase.id, + params: { + type: CommentType.externalReference as const, + owner: 'securitySolutionFixture', + externalReferenceAttachmentTypeId: '.test', + externalReferenceId: 'so-id', + externalReferenceMetadata: {}, + externalReferenceStorage: { + soType: 'external-ref', + type: ExternalReferenceStorageType.savedObject as const, + }, }, - ], + expectedHttpCode: 200, + }); + + const persistableStateAttachments = Array(100).fill({ + persistableStateAttachmentTypeId: '.test', + persistableStateAttachmentState: {}, + type: CommentType.persistableState as const, + owner: 'securitySolutionFixture', + }); + + await bulkCreateAttachments({ + supertest, + caseId: postedCase.id, + params: persistableStateAttachments, + expectedHttpCode: 400, + }); }); - await bulkCreateAttachments({ - supertest, - caseId: postedCase.id, - params: [ - { - ...postCommentAlertReq, - alertId: alerts.slice(500), - index: alerts.slice(500), + it('400s when attempting to bulk create >100 external reference attachments reaching the 100 limit', async () => { + const postedCase = await createCase(supertest, postCaseReq); + + await createComment({ + supertest, + caseId: postedCase.id, + params: { + persistableStateAttachmentTypeId: '.test', + persistableStateAttachmentState: {}, + type: CommentType.persistableState as const, + owner: 'securitySolutionFixture', }, - postCommentAlertReq, - ], - expectedHttpCode: 400, + expectedHttpCode: 200, + }); + + const externalRequestAttachments = Array(100).fill({ + type: CommentType.externalReference as const, + owner: 'securitySolutionFixture', + externalReferenceAttachmentTypeId: '.test', + externalReferenceId: 'so-id', + externalReferenceMetadata: {}, + externalReferenceStorage: { + soType: 'external-ref', + type: ExternalReferenceStorageType.savedObject as const, + }, + }); + + await bulkCreateAttachments({ + supertest, + caseId: postedCase.id, + params: externalRequestAttachments, + expectedHttpCode: 400, + }); }); }); });