From 940d5829ababf3910150b33b25fc0e077a5c1da8 Mon Sep 17 00:00:00 2001 From: FrankHassanabad Date: Mon, 10 Aug 2020 22:54:09 -0600 Subject: [PATCH] Added export_list_items end to end tests --- .../tests/export_list_items.ts | 104 ++++++++++++++++++ .../security_and_spaces/tests/index.ts | 1 + x-pack/test/lists_api_integration/utils.ts | 16 +++ 3 files changed, 121 insertions(+) create mode 100644 x-pack/test/lists_api_integration/security_and_spaces/tests/export_list_items.ts diff --git a/x-pack/test/lists_api_integration/security_and_spaces/tests/export_list_items.ts b/x-pack/test/lists_api_integration/security_and_spaces/tests/export_list_items.ts new file mode 100644 index 00000000000000..6fe783fc497f2c --- /dev/null +++ b/x-pack/test/lists_api_integration/security_and_spaces/tests/export_list_items.ts @@ -0,0 +1,104 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import expect from '@kbn/expect'; +import { getCreateMinimalListItemSchemaMock } from '../../../../plugins/lists/common/schemas/request/create_list_item_schema.mock'; +import { getCreateMinimalListSchemaMock } from '../../../../plugins/lists/common/schemas/request/create_list_schema.mock'; +import { LIST_ID, NAME } from '../../../../plugins/lists/common/constants.mock'; +import { CreateListItemSchema } from '../../../../plugins/lists/common/schemas'; +import { FtrProviderContext } from '../../common/ftr_provider_context'; + +import { LIST_ITEM_URL, LIST_URL } from '../../../../plugins/lists/common/constants'; + +import { createListsIndex, deleteListsIndex, binaryToString } from '../../utils'; + +// eslint-disable-next-line import/no-default-export +export default ({ getService }: FtrProviderContext): void => { + const supertest = getService('supertest'); + + describe('export_list_items', () => { + describe('exporting lists', () => { + beforeEach(async () => { + await createListsIndex(supertest); + }); + + afterEach(async () => { + await deleteListsIndex(supertest); + }); + + it('should set the response content types to be expected', async () => { + await supertest + .post(LIST_URL) + .set('kbn-xsrf', 'true') + .send(getCreateMinimalListSchemaMock()) + .expect(200); + + await supertest + .post(LIST_ITEM_URL) + .set('kbn-xsrf', 'true') + .send(getCreateMinimalListItemSchemaMock()) + .expect(200); + + await supertest + .post(`${LIST_ITEM_URL}/_export?list_id=${LIST_ID}`) + .set('kbn-xsrf', 'true') + .expect('Content-Disposition', `attachment; filename="${NAME}"`) + .expect(200); + }); + + it('should export a single list item with a list id', async () => { + await supertest + .post(LIST_URL) + .set('kbn-xsrf', 'true') + .send(getCreateMinimalListSchemaMock()) + .expect(200); + + await supertest + .post(LIST_ITEM_URL) + .set('kbn-xsrf', 'true') + .send(getCreateMinimalListItemSchemaMock()) + .expect(200); + + const { body } = await supertest + .post(`${LIST_ITEM_URL}/_export?list_id=${LIST_ID}`) + .set('kbn-xsrf', 'true') + .expect(200) + .parse(binaryToString); + + expect(body.toString()).to.eql('127.0.0.1\n'); + }); + + it('should export two list items with a list id', async () => { + await supertest + .post(LIST_URL) + .set('kbn-xsrf', 'true') + .send(getCreateMinimalListSchemaMock()) + .expect(200); + + await supertest + .post(LIST_ITEM_URL) + .set('kbn-xsrf', 'true') + .send(getCreateMinimalListItemSchemaMock()) + .expect(200); + + const secondList: CreateListItemSchema = { + ...getCreateMinimalListItemSchemaMock(), + id: 'list-item-2', + value: '127.0.0.2', + }; + await supertest.post(LIST_ITEM_URL).set('kbn-xsrf', 'true').send(secondList).expect(200); + + const { body } = await supertest + .post(`${LIST_ITEM_URL}/_export?list_id=${LIST_ID}`) + .set('kbn-xsrf', 'true') + .expect(200) + .parse(binaryToString); + + expect(body.toString()).to.eql('127.0.0.2\n127.0.0.1\n'); + }); + }); + }); +}; diff --git a/x-pack/test/lists_api_integration/security_and_spaces/tests/index.ts b/x-pack/test/lists_api_integration/security_and_spaces/tests/index.ts index f131541ce439d7..302877a680aa66 100644 --- a/x-pack/test/lists_api_integration/security_and_spaces/tests/index.ts +++ b/x-pack/test/lists_api_integration/security_and_spaces/tests/index.ts @@ -22,5 +22,6 @@ export default ({ loadTestFile }: FtrProviderContext): void => { loadTestFile(require.resolve('./find_lists')); loadTestFile(require.resolve('./find_list_items')); loadTestFile(require.resolve('./import_list_items')); + loadTestFile(require.resolve('./export_list_items')); }); }; diff --git a/x-pack/test/lists_api_integration/utils.ts b/x-pack/test/lists_api_integration/utils.ts index 4f231ee9350047..272768fdf50b30 100644 --- a/x-pack/test/lists_api_integration/utils.ts +++ b/x-pack/test/lists_api_integration/utils.ts @@ -108,3 +108,19 @@ export const waitFor = async ( } }); }; + +/** + * Useful for export_api testing to convert from a multi-part binary back to a string + * @param res Response + * @param callback Callback + */ +export const binaryToString = (res: any, callback: any): void => { + res.setEncoding('binary'); + res.data = ''; + res.on('data', (chunk: any) => { + res.data += chunk; + }); + res.on('end', () => { + callback(null, Buffer.from(res.data)); + }); +};