Skip to content

Commit

Permalink
Decode file name on upload value lists and fix bug with removing valu…
Browse files Browse the repository at this point in the history
…e list (#111838)

* Decode fileName when creating a list

* Return wait_for for delete list item

* Return back import

* Update x-pack/plugins/lists/server/services/items/write_lines_to_bulk_list_items.test.ts

Co-authored-by: Ryland Herrick <[email protected]>

* Use i18n for message

Co-authored-by: Kibana Machine <[email protected]>
Co-authored-by: Ryland Herrick <[email protected]>
  • Loading branch information
3 people authored Sep 14, 2021
1 parent d38d756 commit 43ea293
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { getListItemResponseMock } from '../../../common/schemas/response/list_item_schema.mock';
import { createListIfItDoesNotExist } from '../lists/create_list_if_it_does_not_exist';

import {
LinesResult,
Expand All @@ -23,6 +24,10 @@ jest.mock('./create_list_items_bulk', () => ({
createListItemsBulk: jest.fn(),
}));

jest.mock('../lists/create_list_if_it_does_not_exist', () => ({
createListIfItDoesNotExist: jest.fn(),
}));

describe('write_lines_to_bulk_list_items', () => {
beforeEach(() => {
jest.clearAllMocks();
Expand Down Expand Up @@ -61,6 +66,17 @@ describe('write_lines_to_bulk_list_items', () => {
expect.objectContaining({ value: ['127.0.0.1', '127.0.0.2'] })
);
});

it('creates a list with a decoded file name', async () => {
const options = getImportListItemsToStreamOptionsMock();
const promise = importListItemsToStream({ ...options, listId: undefined });
options.stream.push(`--\nContent-Disposition: attachment; filename="%22Filename%22.txt"`);
options.stream.push(null);
await promise;
expect(createListIfItDoesNotExist).toBeCalledWith(
expect.objectContaining({ id: `"Filename".txt`, name: `"Filename".txt` })
);
});
});

describe('writeBufferToItems', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type {
Type,
} from '@kbn/securitysolution-io-ts-list-types';
import { Version } from '@kbn/securitysolution-io-ts-types';
import { i18n } from '@kbn/i18n';

import { createListIfItDoesNotExist } from '../lists/create_list_if_it_does_not_exist';
import { ConfigType } from '../../config';
Expand Down Expand Up @@ -59,17 +60,20 @@ export const importListItemsToStream = ({
let list: ListSchema | null = null;
readBuffer.on('fileName', async (fileNameEmitted: string) => {
readBuffer.pause();
fileName = fileNameEmitted;
fileName = decodeURIComponent(fileNameEmitted);
if (listId == null) {
list = await createListIfItDoesNotExist({
description: `File uploaded from file system of ${fileNameEmitted}`,
description: i18n.translate('xpack.lists.services.items.fileUploadFromFileSystem', {
defaultMessage: 'File uploaded from file system of {fileName}',
values: { fileName },
}),
deserializer,
esClient,
id: fileNameEmitted,
id: fileName,
immutable: false,
listIndex,
meta,
name: fileNameEmitted,
name: fileName,
serializer,
type,
user,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('delete_list', () => {
const deleteQuery = {
id: LIST_ID,
index: LIST_INDEX,
refresh: false,
refresh: 'wait_for',
};
expect(options.esClient.delete).toHaveBeenNthCalledWith(1, deleteQuery);
});
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/lists/server/services/lists/delete_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const deleteList = async ({
await esClient.delete({
id,
index: listIndex,
refresh: false,
refresh: 'wait_for',
});
return list;
}
Expand Down

0 comments on commit 43ea293

Please sign in to comment.