Skip to content

Commit

Permalink
Final tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
adcoelho committed Jul 20, 2023
1 parent 5461ebb commit c0e0d82
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
12 changes: 11 additions & 1 deletion x-pack/plugins/cases/server/client/cases/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { CaseUserActionsDeprecatedResponse } from '../../../common/types/ap
import { ConnectorTypes, UserActionActions } from '../../../common/types/domain';
import type { Comment, CommentResponseAlertsType } from '../../../common/api';
import { CommentType, ExternalReferenceStorageType } from '../../../common/api';
import { SECURITY_SOLUTION_OWNER } from '../../../common/constants';
import { FILE_ATTACHMENT_TYPE, SECURITY_SOLUTION_OWNER } from '../../../common/constants';

export const updateUser = {
updated_at: '2020-03-13T08:34:53.450Z',
Expand Down Expand Up @@ -228,6 +228,16 @@ export const commentPersistableState: Comment = {
version: 'WzEsMV0=',
};

export const commentFilePersistableState: Comment = {
...commentExternalReference,
externalReferenceAttachmentTypeId: FILE_ATTACHMENT_TYPE,
externalReferenceMetadata: { files: [{ name: '', extension: '', mimeType: '', created: '' }] },
externalReferenceStorage: {
type: ExternalReferenceStorageType.savedObject as const,
soType: 'iuhu',
},
};

export const basicParams = {
description: 'a description',
title: 'a title',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import type { SavedObject } from '@kbn/core-saved-objects-api-server';
import { createCasesClientMockArgs } from '../../client/mocks';
import { alertComment, comment, mockCaseComments, mockCases, multipleAlert } from '../../mocks';
import { CaseCommentModel } from './case_with_comments';
import { MAX_PERSISTABLE_STATE_AND_EXTERNAL_REFERENCES } from '../../../common/constants';
import {
commentExternalReference,
commentFilePersistableState,
commentPersistableState,
} from '../../client/cases/mock';

describe('CaseCommentModel', () => {
const theCase = mockCases[0];
Expand Down Expand Up @@ -267,6 +273,52 @@ describe('CaseCommentModel', () => {

expect(clientArgs.services.attachmentService.create).not.toHaveBeenCalled();
});

describe('validation', () => {
clientArgs.services.attachmentService.countPersistableStateAndExternalReferenceAttachments.mockResolvedValue(
MAX_PERSISTABLE_STATE_AND_EXTERNAL_REFERENCES
);

afterAll(() => {
jest.clearAllMocks();
});

it('throws if limit is reached when creating persistable state attachment', async () => {
await expect(
model.createComment({
id: 'comment-1',
commentReq: commentPersistableState,
createdDate,
})
).rejects.toThrow(
`Case has reached the maximum allowed number (${MAX_PERSISTABLE_STATE_AND_EXTERNAL_REFERENCES}) of attached persistable state and external reference attachments.`
);
});

it('throws if limit is reached when creating external reference', async () => {
await expect(
model.createComment({
id: 'comment-1',
commentReq: commentExternalReference,
createdDate,
})
).rejects.toThrow(
`Case has reached the maximum allowed number (${MAX_PERSISTABLE_STATE_AND_EXTERNAL_REFERENCES}) of attached persistable state and external reference attachments.`
);
});

it('does not throw if creating a file external reference and the limit is reached', async () => {
clientArgs.fileService.find.mockResolvedValue({ total: 0, files: [] });

await expect(
model.createComment({
id: 'comment-1',
commentReq: commentFilePersistableState,
createdDate,
})
).resolves.not.toThrow();
});
});
});

describe('bulkCreate', () => {
Expand Down Expand Up @@ -526,5 +578,45 @@ describe('CaseCommentModel', () => {
expect(multipleAlertsCall.attributes.alertId).toEqual(['test-id-3', 'test-id-5']);
expect(multipleAlertsCall.attributes.index).toEqual(['test-index-3', 'test-index-5']);
});

describe('validation', () => {
clientArgs.services.attachmentService.countPersistableStateAndExternalReferenceAttachments.mockResolvedValue(
MAX_PERSISTABLE_STATE_AND_EXTERNAL_REFERENCES
);

afterAll(() => {
jest.clearAllMocks();
});

it('throws if limit is reached when creating persistable state attachment', async () => {
await expect(
model.bulkCreate({
attachments: [commentPersistableState],
})
).rejects.toThrow(
`Case has reached the maximum allowed number (${MAX_PERSISTABLE_STATE_AND_EXTERNAL_REFERENCES}) of attached persistable state and external reference attachments.`
);
});

it('throws if limit is reached when creating external reference', async () => {
await expect(
model.bulkCreate({
attachments: [commentExternalReference],
})
).rejects.toThrow(
`Case has reached the maximum allowed number (${MAX_PERSISTABLE_STATE_AND_EXTERNAL_REFERENCES}) of attached persistable state and external reference attachments.`
);
});

it('does not throw if creating a file external reference and the limit is reached', async () => {
clientArgs.fileService.find.mockResolvedValue({ total: 0, files: [] });

await expect(
model.bulkCreate({
attachments: [commentFilePersistableState],
})
).resolves.not.toThrow();
});
});
});
});

0 comments on commit c0e0d82

Please sign in to comment.