Skip to content

Commit

Permalink
Adding error check and test
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-buttner committed May 26, 2023
1 parent 8136935 commit 0fddd61
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
19 changes: 19 additions & 0 deletions x-pack/plugins/cases/server/services/attachments/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,25 @@ describe('AttachmentService', () => {
).resolves.not.toThrow();
});

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

const errorResponseObj = createErrorSO();

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

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

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

it('strips excess fields', async () => {
unsecuredSavedObjectsClient.bulkCreate.mockResolvedValue({
saved_objects: [createUserAttachment({ foo: 'bar' })],
Expand Down
20 changes: 12 additions & 8 deletions x-pack/plugins/cases/server/services/attachments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,16 +240,20 @@ export class AttachmentService {
const validatedAttachments: AttachmentSavedObjectTransformed[] = [];

for (const so of res.saved_objects) {
const transformedAttachment = injectAttachmentSOAttributesFromRefs(
so,
this.context.persistableStateAttachmentTypeRegistry
);
if (isSOError(so)) {
validatedAttachments.push(so as AttachmentSavedObjectTransformed);
} else {
const transformedAttachment = injectAttachmentSOAttributesFromRefs(
so,
this.context.persistableStateAttachmentTypeRegistry
);

const validatedAttributes = decodeOrThrow(AttachmentTransformedAttributesRt)(
transformedAttachment.attributes
);
const validatedAttributes = decodeOrThrow(AttachmentTransformedAttributesRt)(
transformedAttachment.attributes
);

validatedAttachments.push(Object.assign(so, { attributes: validatedAttributes }));
validatedAttachments.push(Object.assign(so, { attributes: validatedAttributes }));
}
}

return Object.assign(res, { saved_objects: validatedAttachments });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,6 @@ export class AttachmentGetter {
response: SavedObjectsFindResponse<AttachmentPersistedAttributes>
): Array<SavedObject<AttributesTypeAlerts>> {
return response.saved_objects.map((so) => {
// We need a cast here because the limited attachment type conflicts with the expected result even though they
// should be the same
const validatedAttributes = decodeOrThrow(AttributesTypeAlertsRt)(so.attributes);

return Object.assign(so, { attributes: validatedAttributes });
Expand Down

0 comments on commit 0fddd61

Please sign in to comment.