Skip to content

Commit

Permalink
Adding test and fixing 404 issue
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-buttner committed Mar 31, 2023
1 parent 69ec214 commit 4e5efcf
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 5 deletions.
10 changes: 9 additions & 1 deletion x-pack/plugins/cases/server/client/attachments/bulk_delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import pMap from 'p-map';
import { partition } from 'lodash';
import type { File } from '@kbn/files-plugin/common';
import type { FileServiceStart } from '@kbn/files-plugin/server';
import { FileNotFoundError } from '@kbn/files-plugin/server/file_service/errors';
import { BulkDeleteFileAttachmentsRequestRt, excess, throwErrors } from '../../../common/api';
import { MAX_CONCURRENT_SEARCHES } from '../../../common/constants';
import type { CasesClientArgs } from '../types';
Expand Down Expand Up @@ -85,9 +86,16 @@ export const bulkDeleteFileAttachments = async (
user,
});
} catch (error) {
let errorToTrack = error;

// if it's an error from the file service let's put it in a boom so we don't loose the status code of a 404
if (error instanceof FileNotFoundError) {
errorToTrack = Boom.notFound(error.message);
}

throw createCaseError({
message: `Failed to delete file attachments for case: ${caseId}: ${error}`,
error,
error: errorToTrack,
logger,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"browser": false,
"requiredPlugins": [
"features",
"cases"
"cases",
"files",
],
"optionalPlugins": [
"security",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { HttpApiTagOperation } from '@kbn/cases-plugin/common/constants/types';
import type { FileKind } from '@kbn/files-plugin/common';
import type { FilesSetup } from '@kbn/files-plugin/server';

export const CASES_TEST_FIXTURE_OWNER = 'casesTestFixtureOwner';
export const CASES_TEST_FIXTURE_FILE_KIND_ID = `${CASES_TEST_FIXTURE_OWNER}_file_kind_id`;

const buildFileKind = (): FileKind => {
return {
id: CASES_TEST_FIXTURE_FILE_KIND_ID,
http: fileKindHttpTags(),
allowedMimeTypes: ['image/png'],
};
};

const fileKindHttpTags = (): FileKind['http'] => {
return {
create: buildTag(HttpApiTagOperation.Create),
download: buildTag(HttpApiTagOperation.Read),
getById: buildTag(HttpApiTagOperation.Read),
list: buildTag(HttpApiTagOperation.Read),
};
};

const access = 'access:';

const buildTag = (operation: HttpApiTagOperation) => {
return {
tags: [`${access}${CASES_TEST_FIXTURE_OWNER}${operation}`],
};
};

export const registerCaseFixtureFileKinds = (filesSetupPlugin: FilesSetup) => {
const fileKind = buildFileKind();
filesSetupPlugin.registerFileKind(fileKind);
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ import { PluginSetupContract as FeaturesPluginSetup } from '@kbn/features-plugin
import { SpacesPluginStart } from '@kbn/spaces-plugin/server';
import { SecurityPluginStart } from '@kbn/security-plugin/server';
import type { CasesStart, CasesSetup } from '@kbn/cases-plugin/server';
import { FilesSetup } from '@kbn/files-plugin/server';
import { getPersistableStateAttachment } from './attachments/persistable_state';
import { getExternalReferenceAttachment } from './attachments/external_reference';
import { registerRoutes } from './routes';
import { registerCaseFixtureFileKinds } from './files';

export interface FixtureSetupDeps {
features: FeaturesPluginSetup;
cases: CasesSetup;
files: FilesSetup;
}

export interface FixtureStartDeps {
Expand All @@ -36,6 +39,7 @@ export class FixturePlugin implements Plugin<void, void, FixtureSetupDeps, Fixtu
deps.cases.attachmentFramework.registerPersistableState(getPersistableStateAttachment());

registerRoutes(core, this.log);
registerCaseFixtureFileKinds(deps.files);
}

public start(core: CoreStart, plugins: FixtureStartDeps) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import expect from '@kbn/expect';
import { CaseResponse } from '@kbn/cases-plugin/common';
import { constructFileKindIdByOwner } from '@kbn/cases-plugin/common/files';
import { Owner } from '@kbn/cases-plugin/common/constants/types';
import { CASES_TEST_FIXTURE_FILE_KIND_ID } from '@kbn/cases-api-integration-test-plugin/server/files';
import { getFilesAttachmentReq, getPostCaseRequest } from '../../../../common/lib/mock';
import { FtrProviderContext } from '../../../../common/ftr_provider_context';
import {
Expand Down Expand Up @@ -74,12 +75,58 @@ export default ({ getService }: FtrProviderContext): void => {
await deleteAllCaseItems(es);
});

it('returns a 400 when attempting to delete a file with a file kind that is not within a case plugin', async () => {
const postedSecCase = await createCase(
supertestWithoutAuth,
getPostCaseRequest({ owner: 'securitySolution' }),
200,
{
user: superUser,
space: 'space1',
}
);

const create = await createFile({
supertest: supertestWithoutAuth,
params: {
name: 'testfile',
kind: CASES_TEST_FIXTURE_FILE_KIND_ID,
mimeType: 'text/plain',
meta: {
caseIds: [postedSecCase.id],
owner: ['securitySolution'],
},
},
auth: { user: superUser, space: 'space1' },
});

await bulkCreateAttachments({
supertest: supertestWithoutAuth,
caseId: postedSecCase.id,
params: [
getFilesAttachmentReq({
externalReferenceId: create.file.id,
owner: 'securitySolution',
}),
],
auth: { user: superUser, space: 'space1' },
});

await bulkDeleteFileAttachments({
supertest: supertestWithoutAuth,
caseId: postedSecCase.id,
fileIds: [create.file.id],
auth: { user: superUser, space: 'space1' },
expectedHttpCode: 400,
});
});

it('fails to delete a file when the file does not exist', async () => {
await bulkDeleteFileAttachments({
supertest,
caseId: postedCase.id,
fileIds: ['abc'],
expectedHttpCode: 500,
expectedHttpCode: 404,
});
});

Expand All @@ -101,8 +148,8 @@ export default ({ getService }: FtrProviderContext): void => {
});
});

it('returns a 400 when there are 501 ids being deleted', async () => {
const ids = Array.from(Array(501).keys()).map((item) => item.toString());
it('returns a 400 when there are 51 ids being deleted', async () => {
const ids = Array.from(Array(51).keys()).map((item) => item.toString());
await bulkDeleteFileAttachments({
supertest,
caseId: postedCase.id,
Expand Down

0 comments on commit 4e5efcf

Please sign in to comment.