Skip to content

Commit

Permalink
Integration tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
adcoelho committed Jul 24, 2023
1 parent 3fa3b39 commit 69d437b
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
CaseStatuses,
CommentRequestExternalReferenceSOType,
CommentRequestAlertType,
ExternalReferenceStorageType,
} from '@kbn/cases-plugin/common/api';
import { FtrProviderContext } from '../../../../common/ftr_provider_context';
import {
Expand All @@ -42,6 +43,7 @@ import {
getCaseUserActions,
removeServerGeneratedPropertiesFromUserAction,
getAllComments,
bulkCreateAttachments,
} from '../../../../common/lib/api';
import {
createSignalsIndex,
Expand Down Expand Up @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
CaseStatuses,
CommentRequestExternalReferenceSOType,
CommentType,
ExternalReferenceStorageType,
} from '@kbn/cases-plugin/common/api';
import { FtrProviderContext } from '../../../../common/ftr_provider_context';
import {
Expand Down Expand Up @@ -42,6 +43,7 @@ import {
createAndUploadFile,
deleteAllFiles,
getAllComments,
createComment,
} from '../../../../common/lib/api';
import {
createSignalsIndex,
Expand Down Expand Up @@ -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,
});
});
});
});
Expand Down

0 comments on commit 69d437b

Please sign in to comment.