Skip to content

Commit

Permalink
Adding error tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-buttner committed May 25, 2023
1 parent 5a294c5 commit 4921389
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
2 changes: 1 addition & 1 deletion x-pack/plugins/cases/common/api/cases/comment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ const AttributesTypeActionsRt = rt.intersection([
CommentAttributesBasicRt,
]);

export const AttributesTypeExternalReferenceRt = rt.intersection([
const AttributesTypeExternalReferenceRt = rt.intersection([
ExternalReferenceRt,
CommentAttributesBasicRt,
]);
Expand Down
21 changes: 20 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 @@ -21,7 +21,7 @@ import {
persistableStateAttachmentAttributes,
persistableStateAttachmentAttributesWithoutInjectedId,
} from '../../attachment_framework/mocks';
import { createAlertAttachment, createUserAttachment } from './test_utils';
import { createAlertAttachment, createErrorSO, createUserAttachment } from './test_utils';
import { CommentType } from '../../../common';
import { createSOFindResponse } from '../test_utils';

Expand Down Expand Up @@ -292,6 +292,25 @@ describe('AttachmentService', () => {
).resolves.not.toThrow();
});

it('returns error objects unmodified', async () => {
const userAttachment = createUserAttachment({ foo: 'bar' });

const errorResponseObj = createErrorSO();

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

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/cases/server/services/attachments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ export class AttachmentService {
const attachment = res.saved_objects[i];

if (isSOError(attachment)) {
// Forcing the type here even though it is an error. The caller is responsible for
// Forcing the type here even though it is an error. The client is responsible for
// determining what to do with the errors
// TODO: we should fix the return type of this function so that it can return errors
validatedAttachments.push(attachment as AttachmentSavedObjectTransformed);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import type { SavedObjectsFindResponse } from '@kbn/core/server';
import { loggerMock } from '@kbn/logging-mocks';
import { createPersistableStateAttachmentTypeRegistryMock } from '../../../attachment_framework/mocks';
import { AttachmentGetter } from './get';
import { createAlertAttachment, createFileAttachment, createUserAttachment } from '../test_utils';
import {
createAlertAttachment,
createErrorSO,
createFileAttachment,
createUserAttachment,
} from '../test_utils';
import { mockPointInTimeFinder, createSOFindResponse } from '../../test_utils';

describe('AttachmentService getter', () => {
Expand Down Expand Up @@ -43,6 +48,16 @@ describe('AttachmentService getter', () => {
await expect(attachmentGetter.bulkGet(['1'])).resolves.not.toThrow();
});

it('does not modified the error saved objects', async () => {
unsecuredSavedObjectsClient.bulkGet.mockResolvedValue({
saved_objects: [createUserAttachment(), createErrorSO()],
});

const res = await attachmentGetter.bulkGet(['1', '2']);

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

it('strips excess fields', async () => {
unsecuredSavedObjectsClient.bulkGet.mockResolvedValue({
saved_objects: [{ ...createUserAttachment({ foo: 'bar' }) }],
Expand Down
13 changes: 13 additions & 0 deletions x-pack/plugins/cases/server/services/attachments/test_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ import {
} from '../../../common/constants';
import { CASE_REF_NAME, EXTERNAL_REFERENCE_REF_NAME } from '../../common/constants';

export const createErrorSO = () =>
({
id: '1',
type: CASE_COMMENT_SAVED_OBJECT,
error: {
error: 'error',
message: 'message',
statusCode: 500,
},
references: [],
// casting because this complains about attributes not being there
} as unknown as SavedObject<AttributesTypeUser>);

export const createUserAttachment = (attributes?: object): SavedObject<AttributesTypeUser> => {
return {
id: '1',
Expand Down

0 comments on commit 4921389

Please sign in to comment.