Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cases] Add decode to attachment functionality #158424

Merged
merged 6 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions x-pack/plugins/cases/common/api/cases/comment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,25 @@ export const PersistableStateAttachmentRt = rt.strict({
});

const AttributesTypeUserRt = rt.intersection([ContextTypeUserRt, CommentAttributesBasicRt]);
const AttributesTypeAlertsRt = rt.intersection([AlertCommentRequestRt, CommentAttributesBasicRt]);
export const AttributesTypeAlertsRt = rt.intersection([
AlertCommentRequestRt,
CommentAttributesBasicRt,
]);
const AttributesTypeActionsRt = rt.intersection([
ActionsCommentRequestRt,
CommentAttributesBasicRt,
]);

const AttributesTypeExternalReferenceRt = rt.intersection([
export const AttributesTypeExternalReferenceRt = rt.intersection([
ExternalReferenceRt,
CommentAttributesBasicRt,
]);

const AttributesTypeExternalReferenceWithoutRefsRt = rt.intersection([
ExternalReferenceWithoutRefsRt,
CommentAttributesBasicRt,
]);

const AttributesTypeExternalReferenceNoSORt = rt.intersection([
ExternalReferenceNoSORt,
CommentAttributesBasicRt,
Expand All @@ -152,7 +160,7 @@ const AttributesTypePersistableStateRt = rt.intersection([
CommentAttributesBasicRt,
]);

const CommentAttributesRt = rt.union([
export const CommentAttributesRt = rt.union([
AttributesTypeUserRt,
AttributesTypeAlertsRt,
AttributesTypeActionsRt,
Expand All @@ -172,7 +180,7 @@ const CommentAttributesWithoutRefsRt = rt.union([
AttributesTypeUserRt,
AttributesTypeAlertsRt,
AttributesTypeActionsRt,
ExternalReferenceWithoutRefsRt,
AttributesTypeExternalReferenceWithoutRefsRt,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this was a bug before. ExternalReferenceWithoutRefsRt. Doesn't include the common attributes like created_by etc.

AttributesTypePersistableStateRt,
]);

Expand Down Expand Up @@ -326,6 +334,7 @@ export type AttributesTypeExternalReferenceSO = rt.TypeOf<
export type AttributesTypeExternalReferenceNoSO = rt.TypeOf<
typeof AttributesTypeExternalReferenceNoSORt
>;
export type ExternalReferenceWithoutRefs = rt.TypeOf<typeof ExternalReferenceWithoutRefsRt>;
export type AttributesTypePersistableState = rt.TypeOf<typeof AttributesTypePersistableStateRt>;
export type CommentAttributes = rt.TypeOf<typeof CommentAttributesRt>;
export type CommentAttributesNoSO = rt.TypeOf<typeof CommentAttributesNoSORt>;
Expand Down
4 changes: 4 additions & 0 deletions x-pack/plugins/cases/server/common/types/attachments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import type { SavedObject } from '@kbn/core/server';
import type { JsonValue } from '@kbn/utility-types';
import type { CommentAttributes } from '../../../common/api';
import { CommentAttributesRt, CommentPatchAttributesRt } from '../../../common/api';
import type { User } from './user';

interface AttachmentCommonPersistedAttributes {
Expand Down Expand Up @@ -51,3 +52,6 @@ export type AttachmentPersistedAttributes = AttachmentRequestAttributes &

export type AttachmentTransformedAttributes = CommentAttributes;
export type AttachmentSavedObjectTransformed = SavedObject<AttachmentTransformedAttributes>;

export const AttachmentTransformedAttributesRt = CommentAttributesRt;
export const AttachmentPartialAttributesRt = CommentPatchAttributesRt;
221 changes: 220 additions & 1 deletion x-pack/plugins/cases/server/services/attachments/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* 2.0.
*/

import { unset } from 'lodash';

import { savedObjectsClientMock } from '@kbn/core/server/mocks';
import { loggerMock } from '@kbn/logging-mocks';
import { AttachmentService } from '.';
Expand All @@ -19,8 +21,11 @@ import {
persistableStateAttachmentAttributes,
persistableStateAttachmentAttributesWithoutInjectedId,
} from '../../attachment_framework/mocks';
import { createAlertAttachment, createUserAttachment } from './test_utils';
import { CommentType } from '../../../common';
import { createSOFindResponse } from '../test_utils';

describe('CasesService', () => {
describe('AttachmentService', () => {
const unsecuredSavedObjectsClient = savedObjectsClientMock.create();
const mockLogger = loggerMock.create();
const persistableStateAttachmentTypeRegistry = createPersistableStateAttachmentTypeRegistryMock();
Expand All @@ -35,6 +40,100 @@ describe('CasesService', () => {
});
});

describe('create', () => {
describe('Decoding', () => {
it('does not throw when the response has the required fields', async () => {
unsecuredSavedObjectsClient.create.mockResolvedValue(createUserAttachment());

await expect(
service.create({
attributes: createUserAttachment().attributes,
references: [],
id: '1',
})
).resolves.not.toThrow();
});

it('strips excess fields', async () => {
unsecuredSavedObjectsClient.create.mockResolvedValue(createUserAttachment({ foo: 'bar' }));

const res = await service.create({
attributes: createUserAttachment().attributes,
references: [],
id: '1',
});

expect(res).toStrictEqual(createUserAttachment());
});

it('throws when the response is missing the attributes.comment', async () => {
const invalidAttachment = createUserAttachment();
unset(invalidAttachment, 'attributes.comment');

unsecuredSavedObjectsClient.create.mockResolvedValue(invalidAttachment);

await expect(
service.create({
attributes: createUserAttachment().attributes,
references: [],
id: '1',
})
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Invalid value \\"undefined\\" supplied to \\"comment\\",Invalid value \\"user\\" supplied to \\"type\\",Invalid value \\"undefined\\" supplied to \\"alertId\\",Invalid value \\"undefined\\" supplied to \\"index\\",Invalid value \\"undefined\\" supplied to \\"rule\\",Invalid value \\"undefined\\" supplied to \\"actions\\",Invalid value \\"undefined\\" supplied to \\"externalReferenceAttachmentTypeId\\",Invalid value \\"undefined\\" supplied to \\"externalReferenceMetadata\\",Invalid value \\"undefined\\" supplied to \\"externalReferenceId\\",Invalid value \\"undefined\\" supplied to \\"externalReferenceStorage\\",Invalid value \\"undefined\\" supplied to \\"persistableStateAttachmentTypeId\\",Invalid value \\"undefined\\" supplied to \\"persistableStateAttachmentState\\""`
);
});
});
});

describe('bulkCreate', () => {
describe('Decoding', () => {
it('does not throw when the response has the required fields', async () => {
unsecuredSavedObjectsClient.bulkCreate.mockResolvedValue({
saved_objects: [createUserAttachment()],
});

await expect(
service.bulkCreate({
attachments: [
{ attributes: createUserAttachment().attributes, references: [], id: '1' },
],
})
).resolves.not.toThrow();
});

it('strips excess fields', async () => {
unsecuredSavedObjectsClient.bulkCreate.mockResolvedValue({
saved_objects: [createUserAttachment({ foo: 'bar' })],
});

const res = await service.bulkCreate({
attachments: [{ attributes: createUserAttachment().attributes, references: [], id: '1' }],
});

expect(res).toStrictEqual({ saved_objects: [createUserAttachment()] });
});

it('throws when the response is missing the attributes.comment field', async () => {
const invalidAttachment = createUserAttachment();
unset(invalidAttachment, 'attributes.comment');

unsecuredSavedObjectsClient.bulkCreate.mockResolvedValue({
saved_objects: [invalidAttachment],
});

await expect(
service.bulkCreate({
attachments: [
{ attributes: createUserAttachment().attributes, references: [], id: '1' },
],
})
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Invalid value \\"undefined\\" supplied to \\"comment\\",Invalid value \\"user\\" supplied to \\"type\\",Invalid value \\"undefined\\" supplied to \\"alertId\\",Invalid value \\"undefined\\" supplied to \\"index\\",Invalid value \\"undefined\\" supplied to \\"rule\\",Invalid value \\"undefined\\" supplied to \\"actions\\",Invalid value \\"undefined\\" supplied to \\"externalReferenceAttachmentTypeId\\",Invalid value \\"undefined\\" supplied to \\"externalReferenceMetadata\\",Invalid value \\"undefined\\" supplied to \\"externalReferenceId\\",Invalid value \\"undefined\\" supplied to \\"externalReferenceStorage\\",Invalid value \\"undefined\\" supplied to \\"persistableStateAttachmentTypeId\\",Invalid value \\"undefined\\" supplied to \\"persistableStateAttachmentState\\""`
);
});
});
});

describe('update', () => {
const soClientRes = {
id: '1',
Expand Down Expand Up @@ -85,6 +184,46 @@ describe('CasesService', () => {

expect(res).toEqual({ ...soClientRes, attributes: externalReferenceAttachmentESAttributes });
});

describe('Decoding', () => {
it('does not throw when the response has the required fields', async () => {
unsecuredSavedObjectsClient.update.mockResolvedValue(createUserAttachment());

await expect(
service.update({
updatedAttributes: { comment: 'yes', type: CommentType.user, owner: 'hi' },
attachmentId: '1',
})
).resolves.not.toThrow();
});

it('strips excess fields', async () => {
unsecuredSavedObjectsClient.update.mockResolvedValue(createUserAttachment({ foo: 'bar' }));

const res = await service.update({
updatedAttributes: { comment: 'yes', type: CommentType.user, owner: 'hi' },
attachmentId: '1',
});

expect(res).toStrictEqual(createUserAttachment());
});

it('throws when the response is missing the attributes.rule.name', async () => {
const invalidAttachment = createAlertAttachment();
unset(invalidAttachment, 'attributes.rule.name');

unsecuredSavedObjectsClient.update.mockResolvedValue(invalidAttachment);

await expect(
service.update({
updatedAttributes: createUserAttachment().attributes,
attachmentId: '1',
})
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Invalid value \\"alert\\" supplied to \\"type\\",Invalid value \\"undefined\\" supplied to \\"rule,name\\""`
);
});
});
});

describe('bulkUpdate', () => {
Expand Down Expand Up @@ -139,5 +278,85 @@ describe('CasesService', () => {
],
});
});

describe('Decoding', () => {
it('does not throw when the response has the required fields', async () => {
unsecuredSavedObjectsClient.bulkUpdate.mockResolvedValue({
saved_objects: [createUserAttachment()],
});

const updatedAttributes = createUserAttachment().attributes;

await expect(
service.bulkUpdate({ comments: [{ attachmentId: '1', updatedAttributes }] })
).resolves.not.toThrow();
});

it('strips excess fields', async () => {
const updatedAttributes = createUserAttachment().attributes;

unsecuredSavedObjectsClient.bulkUpdate.mockResolvedValue({
saved_objects: [createUserAttachment({ foo: 'bar' })],
});

const res = await service.bulkUpdate({
comments: [{ attachmentId: '1', updatedAttributes }],
});

expect(res).toStrictEqual({ saved_objects: [createUserAttachment()] });
});

it('throws when the response is missing the attributes.rule.name field', async () => {
const invalidAttachment = createAlertAttachment();
unset(invalidAttachment, 'attributes.rule.name');

unsecuredSavedObjectsClient.bulkUpdate.mockResolvedValue({
saved_objects: [invalidAttachment],
});

const updatedAttributes = createAlertAttachment().attributes;

await expect(
service.bulkUpdate({ comments: [{ attachmentId: '1', updatedAttributes }] })
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Invalid value \\"alert\\" supplied to \\"type\\",Invalid value \\"undefined\\" supplied to \\"rule,name\\""`
);
});
});
});

describe('find', () => {
describe('Decoding', () => {
it('does not throw when the response has the required fields', async () => {
unsecuredSavedObjectsClient.find.mockResolvedValue(
createSOFindResponse([{ ...createUserAttachment(), score: 0 }])
);

await expect(service.find({})).resolves.not.toThrow();
});

it('strips excess fields', async () => {
unsecuredSavedObjectsClient.find.mockResolvedValue(
createSOFindResponse([{ ...createUserAttachment({ foo: 'bar' }), score: 0 }])
);

const res = await service.find({});

expect(res).toStrictEqual(createSOFindResponse([{ ...createUserAttachment(), score: 0 }]));
});

it('throws when the response is missing the attributes.rule.name field', async () => {
const invalidAttachment = createUserAttachment();
unset(invalidAttachment, 'attributes.comment');

unsecuredSavedObjectsClient.find.mockResolvedValue(
createSOFindResponse([{ ...invalidAttachment, score: 0 }])
);

await expect(service.find({})).rejects.toThrowErrorMatchingInlineSnapshot(
`"Invalid value \\"undefined\\" supplied to \\"comment\\",Invalid value \\"user\\" supplied to \\"type\\",Invalid value \\"undefined\\" supplied to \\"alertId\\",Invalid value \\"undefined\\" supplied to \\"index\\",Invalid value \\"undefined\\" supplied to \\"rule\\",Invalid value \\"undefined\\" supplied to \\"actions\\",Invalid value \\"undefined\\" supplied to \\"externalReferenceAttachmentTypeId\\",Invalid value \\"undefined\\" supplied to \\"externalReferenceMetadata\\",Invalid value \\"undefined\\" supplied to \\"externalReferenceId\\",Invalid value \\"undefined\\" supplied to \\"externalReferenceStorage\\",Invalid value \\"undefined\\" supplied to \\"persistableStateAttachmentTypeId\\",Invalid value \\"undefined\\" supplied to \\"persistableStateAttachmentState\\""`
);
});
});
});
});
Loading