From 9180ca17de5c2b496908a86d8d500d2c1554d74c Mon Sep 17 00:00:00 2001 From: FrankHassanabad Date: Tue, 11 Aug 2020 20:07:06 -0600 Subject: [PATCH] Added tests for exception lists and fixed one flaky test found --- .../create_exception_list_schema.mock.ts | 20 ++ .../update_exception_list_item_schema.mock.ts | 13 ++ .../update_exception_list_schema.mock.ts | 11 ++ .../response/exception_list_schema.mock.ts | 24 +++ .../routes/update_exception_list_route.ts | 6 +- .../tests/create_exception_lists.ts | 77 ++++++++ .../tests/delete_exception_lists.ts | 90 +++++++++ .../tests/export_list_items.ts | 5 +- .../tests/find_exception_lists.ts | 67 +++++++ .../security_and_spaces/tests/index.ts | 5 + .../tests/read_exception_lists.ts | 112 +++++++++++ .../tests/update_exception_lists.ts | 182 ++++++++++++++++++ x-pack/test/lists_api_integration/utils.ts | 44 ++++- 13 files changed, 650 insertions(+), 6 deletions(-) create mode 100644 x-pack/test/lists_api_integration/security_and_spaces/tests/create_exception_lists.ts create mode 100644 x-pack/test/lists_api_integration/security_and_spaces/tests/delete_exception_lists.ts create mode 100644 x-pack/test/lists_api_integration/security_and_spaces/tests/find_exception_lists.ts create mode 100644 x-pack/test/lists_api_integration/security_and_spaces/tests/read_exception_lists.ts create mode 100644 x-pack/test/lists_api_integration/security_and_spaces/tests/update_exception_lists.ts diff --git a/x-pack/plugins/lists/common/schemas/request/create_exception_list_schema.mock.ts b/x-pack/plugins/lists/common/schemas/request/create_exception_list_schema.mock.ts index d9c04746103690..f8431fcce1bf76 100644 --- a/x-pack/plugins/lists/common/schemas/request/create_exception_list_schema.mock.ts +++ b/x-pack/plugins/lists/common/schemas/request/create_exception_list_schema.mock.ts @@ -7,6 +7,7 @@ import { DESCRIPTION, ENDPOINT_TYPE, + LIST_ID, META, NAME, NAMESPACE_TYPE, @@ -26,3 +27,22 @@ export const getCreateExceptionListSchemaMock = (): CreateExceptionListSchema => type: ENDPOINT_TYPE, version: VERSION, }); + +/** + * Useful for end to end testing + */ +export const getCreateExceptionListMinimalSchemaMock = (): CreateExceptionListSchema => ({ + description: DESCRIPTION, + list_id: LIST_ID, + name: NAME, + type: ENDPOINT_TYPE, +}); + +/** + * Useful for end to end testing + */ +export const getCreateExceptionListMinimalSchemaMockWithoutId = (): CreateExceptionListSchema => ({ + description: DESCRIPTION, + name: NAME, + type: ENDPOINT_TYPE, +}); diff --git a/x-pack/plugins/lists/common/schemas/request/update_exception_list_item_schema.mock.ts b/x-pack/plugins/lists/common/schemas/request/update_exception_list_item_schema.mock.ts index 90d70c273f490e..4b91799c950c1a 100644 --- a/x-pack/plugins/lists/common/schemas/request/update_exception_list_item_schema.mock.ts +++ b/x-pack/plugins/lists/common/schemas/request/update_exception_list_item_schema.mock.ts @@ -34,3 +34,16 @@ export const getUpdateExceptionListItemSchemaMock = (): UpdateExceptionListItemS tags: TAGS, type: ITEM_TYPE, }); + +/** + * Useful for end to end tests and other mechanisms which want to fill in the values + * after doing a get of the structure. + */ +export const getUpdateMinimalExceptionListItemSchemaMock = (): UpdateExceptionListItemSchema => ({ + description: DESCRIPTION, + entries: ENTRIES, + id: ID, + item_id: LIST_ITEM_ID, + name: NAME, + type: ITEM_TYPE, +}); diff --git a/x-pack/plugins/lists/common/schemas/request/update_exception_list_schema.mock.ts b/x-pack/plugins/lists/common/schemas/request/update_exception_list_schema.mock.ts index 22af29e6af0b78..b7dc2d9e0c9487 100644 --- a/x-pack/plugins/lists/common/schemas/request/update_exception_list_schema.mock.ts +++ b/x-pack/plugins/lists/common/schemas/request/update_exception_list_schema.mock.ts @@ -20,3 +20,14 @@ export const getUpdateExceptionListSchemaMock = (): UpdateExceptionListSchema => tags: ['malware'], type: 'endpoint', }); + +/** + * Useful for end to end tests and other mechanisms which want to fill in the values + * after doing a get of the structure. + */ +export const getUpdateMinimalExceptionListSchemaMock = (): UpdateExceptionListSchema => ({ + description: DESCRIPTION, + list_id: LIST_ID, + name: NAME, + type: 'endpoint', +}); diff --git a/x-pack/plugins/lists/common/schemas/response/exception_list_schema.mock.ts b/x-pack/plugins/lists/common/schemas/response/exception_list_schema.mock.ts index 2655b09631b23c..e2f0a7c06b4004 100644 --- a/x-pack/plugins/lists/common/schemas/response/exception_list_schema.mock.ts +++ b/x-pack/plugins/lists/common/schemas/response/exception_list_schema.mock.ts @@ -7,9 +7,12 @@ import { DATE_NOW, DESCRIPTION, + ELASTIC_USER, ENDPOINT_TYPE, IMMUTABLE, + LIST_ID, META, + NAME, TIE_BREAKER, USER, VERSION, @@ -18,6 +21,7 @@ import { import { ENDPOINT_LIST_ID } from '../..'; import { ExceptionListSchema } from './exception_list_schema'; + export const getExceptionListSchemaMock = (): ExceptionListSchema => ({ _tags: ['endpoint', 'process', 'malware', 'os:linux'], _version: _VERSION, @@ -37,3 +41,23 @@ export const getExceptionListSchemaMock = (): ExceptionListSchema => ({ updated_by: 'user_name', version: VERSION, }); + +/** + * This is useful for end to end tests where we remove the auto generated parts for comparisons + * such as created_at, updated_at, and id. + */ +export const getExceptionResponseMockWithoutAutoGeneratedValues = (): Partial< + ExceptionListSchema +> => ({ + _tags: [], + created_by: ELASTIC_USER, + description: DESCRIPTION, + immutable: IMMUTABLE, + list_id: LIST_ID, + name: NAME, + namespace_type: 'single', + tags: [], + type: ENDPOINT_TYPE, + updated_by: ELASTIC_USER, + version: VERSION, +}); diff --git a/x-pack/plugins/lists/server/routes/update_exception_list_route.ts b/x-pack/plugins/lists/server/routes/update_exception_list_route.ts index 8102210b8430d7..1903d0f601d1d1 100644 --- a/x-pack/plugins/lists/server/routes/update_exception_list_route.ts +++ b/x-pack/plugins/lists/server/routes/update_exception_list_route.ts @@ -15,7 +15,7 @@ import { updateExceptionListSchema, } from '../../common/schemas'; -import { getExceptionListClient } from './utils'; +import { getErrorMessageExceptionList, getExceptionListClient } from './utils'; export const updateExceptionListRoute = (router: IRouter): void => { router.put( @@ -50,7 +50,7 @@ export const updateExceptionListRoute = (router: IRouter): void => { const exceptionLists = getExceptionListClient(context); if (id == null && listId == null) { return siemResponse.error({ - body: `either id or list_id need to be defined`, + body: 'either id or list_id need to be defined', statusCode: 404, }); } else { @@ -69,7 +69,7 @@ export const updateExceptionListRoute = (router: IRouter): void => { }); if (list == null) { return siemResponse.error({ - body: `exception list id: "${id}" not found`, + body: getErrorMessageExceptionList({ id, listId }), statusCode: 404, }); } else { diff --git a/x-pack/test/lists_api_integration/security_and_spaces/tests/create_exception_lists.ts b/x-pack/test/lists_api_integration/security_and_spaces/tests/create_exception_lists.ts new file mode 100644 index 00000000000000..2b654c72ae282e --- /dev/null +++ b/x-pack/test/lists_api_integration/security_and_spaces/tests/create_exception_lists.ts @@ -0,0 +1,77 @@ +/* + * 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 { ExceptionListSchema } from '../../../../plugins/lists/common'; +import { EXCEPTION_LIST_URL } from '../../../../plugins/lists/common/constants'; +import { FtrProviderContext } from '../../common/ftr_provider_context'; +import { getExceptionResponseMockWithoutAutoGeneratedValues } from '../../../../plugins/lists/common/schemas/response/exception_list_schema.mock'; +import { + getCreateExceptionListMinimalSchemaMock, + getCreateExceptionListMinimalSchemaMockWithoutId, +} from '../../../../plugins/lists/common/schemas/request/create_exception_list_schema.mock'; + +import { deleteAllExceptions, removeExceptionListServerGeneratedProperties } from '../../utils'; + +// eslint-disable-next-line import/no-default-export +export default ({ getService }: FtrProviderContext) => { + const supertest = getService('supertest'); + const es = getService('es'); + + describe('create_exception_lists', () => { + describe('creating exception lists', () => { + afterEach(async () => { + await deleteAllExceptions(es); + }); + + it('should create a simple exception list', async () => { + const { body } = await supertest + .post(EXCEPTION_LIST_URL) + .set('kbn-xsrf', 'true') + .send(getCreateExceptionListMinimalSchemaMock()) + .expect(200); + + const bodyToCompare = removeExceptionListServerGeneratedProperties(body); + expect(bodyToCompare).to.eql(getExceptionResponseMockWithoutAutoGeneratedValues()); + }); + + it('should create a simple exception list without a list_id', async () => { + const { body } = await supertest + .post(EXCEPTION_LIST_URL) + .set('kbn-xsrf', 'true') + .send(getCreateExceptionListMinimalSchemaMockWithoutId()) + .expect(200); + + const bodyToCompare = removeExceptionListServerGeneratedProperties(body); + const outputtedList: Partial = { + ...getExceptionResponseMockWithoutAutoGeneratedValues(), + list_id: bodyToCompare.list_id, + }; + expect(bodyToCompare).to.eql(outputtedList); + }); + + it('should cause a 409 conflict if we attempt to create the same list_id twice', async () => { + await supertest + .post(EXCEPTION_LIST_URL) + .set('kbn-xsrf', 'true') + .send(getCreateExceptionListMinimalSchemaMock()) + .expect(200); + + const { body } = await supertest + .post(EXCEPTION_LIST_URL) + .set('kbn-xsrf', 'true') + .send(getCreateExceptionListMinimalSchemaMock()) + .expect(409); + + expect(body).to.eql({ + message: 'exception list id: "some-list-id" already exists', + status_code: 409, + }); + }); + }); + }); +}; diff --git a/x-pack/test/lists_api_integration/security_and_spaces/tests/delete_exception_lists.ts b/x-pack/test/lists_api_integration/security_and_spaces/tests/delete_exception_lists.ts new file mode 100644 index 00000000000000..e4eb8992789590 --- /dev/null +++ b/x-pack/test/lists_api_integration/security_and_spaces/tests/delete_exception_lists.ts @@ -0,0 +1,90 @@ +/* + * 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 { getExceptionResponseMockWithoutAutoGeneratedValues } from '../../../../plugins/lists/common/schemas/response/exception_list_schema.mock'; +import { getCreateExceptionListMinimalSchemaMock } from '../../../../plugins/lists/common/schemas/request/create_exception_list_schema.mock'; +import { FtrProviderContext } from '../../common/ftr_provider_context'; +import { EXCEPTION_LIST_URL } from '../../../../plugins/lists/common/constants'; + +import { deleteAllExceptions, removeExceptionListServerGeneratedProperties } from '../../utils'; + +// eslint-disable-next-line import/no-default-export +export default ({ getService }: FtrProviderContext) => { + const supertest = getService('supertest'); + const es = getService('es'); + + describe('delete_exception_lists', () => { + describe('delete exception lists', () => { + afterEach(async () => { + await deleteAllExceptions(es); + }); + + it('should delete a single exception list by its list_id', async () => { + // create an exception list + await supertest + .post(EXCEPTION_LIST_URL) + .set('kbn-xsrf', 'true') + .send(getCreateExceptionListMinimalSchemaMock()) + .expect(200); + + // delete the exception list by its list id + const { body } = await supertest + .delete( + `${EXCEPTION_LIST_URL}?list_id=${getCreateExceptionListMinimalSchemaMock().list_id}` + ) + .set('kbn-xsrf', 'true') + .expect(200); + + const bodyToCompare = removeExceptionListServerGeneratedProperties(body); + expect(bodyToCompare).to.eql(getExceptionResponseMockWithoutAutoGeneratedValues()); + }); + + it('should delete a single exception list using an auto generated id', async () => { + // create an exception list + const { body: bodyWithCreatedList } = await supertest + .post(EXCEPTION_LIST_URL) + .set('kbn-xsrf', 'true') + .send(getCreateExceptionListMinimalSchemaMock()) + .expect(200); + + // delete that list by its auto-generated id + const { body } = await supertest + .delete(`${EXCEPTION_LIST_URL}?id=${bodyWithCreatedList.id}`) + .set('kbn-xsrf', 'true') + .expect(200); + + const bodyToCompare = removeExceptionListServerGeneratedProperties(body); + expect(bodyToCompare).to.eql(getExceptionResponseMockWithoutAutoGeneratedValues()); + }); + + it('should return an error if the id does not exist when trying to delete it', async () => { + const { body } = await supertest + .delete(`${EXCEPTION_LIST_URL}?id=c1e1b359-7ac1-4e96-bc81-c683c092436f`) + .set('kbn-xsrf', 'true') + .expect(404); + + expect(body).to.eql({ + message: 'Exception list id: "c1e1b359-7ac1-4e96-bc81-c683c092436f" does not exist', + status_code: 404, + }); + }); + + it('should return an error if the list_id does not exist when trying to delete it', async () => { + const { body } = await supertest + .delete(`${EXCEPTION_LIST_URL}?list_id=c1e1b359-7ac1-4e96-bc81-c683c092436f`) + .set('kbn-xsrf', 'true') + .expect(404); + + expect(body).to.eql({ + message: 'Exception list list_id: "c1e1b359-7ac1-4e96-bc81-c683c092436f" does not exist', + status_code: 404, + }); + }); + }); + }); +}; 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 index 6fe783fc497f2c..74c28f5abdfc1d 100644 --- 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 @@ -96,8 +96,9 @@ export default ({ getService }: FtrProviderContext): void => { .set('kbn-xsrf', 'true') .expect(200) .parse(binaryToString); - - expect(body.toString()).to.eql('127.0.0.2\n127.0.0.1\n'); + const bodyString = body.toString(); + expect(bodyString.includes('127.0.0.1')).to.be(true); + expect(bodyString.includes('127.0.0.2')).to.be(true); }); }); }); diff --git a/x-pack/test/lists_api_integration/security_and_spaces/tests/find_exception_lists.ts b/x-pack/test/lists_api_integration/security_and_spaces/tests/find_exception_lists.ts new file mode 100644 index 00000000000000..c2328a7d112f43 --- /dev/null +++ b/x-pack/test/lists_api_integration/security_and_spaces/tests/find_exception_lists.ts @@ -0,0 +1,67 @@ +/* + * 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 { getExceptionResponseMockWithoutAutoGeneratedValues } from '../../../../plugins/lists/common/schemas/response/exception_list_schema.mock'; +import { getCreateExceptionListMinimalSchemaMock } from '../../../../plugins/lists/common/schemas/request/create_exception_list_schema.mock'; +import { FtrProviderContext } from '../../common/ftr_provider_context'; +import { EXCEPTION_LIST_URL } from '../../../../plugins/lists/common/constants'; + +import { deleteAllExceptions, removeExceptionListServerGeneratedProperties } from '../../utils'; + +// eslint-disable-next-line import/no-default-export +export default ({ getService }: FtrProviderContext): void => { + const supertest = getService('supertest'); + const es = getService('es'); + + describe('find_exception_lists', () => { + describe('find exception lists', () => { + afterEach(async () => { + await deleteAllExceptions(es); + }); + + it('should return an empty find body correctly if no exception lists are loaded', async () => { + const { body } = await supertest + .get(`${EXCEPTION_LIST_URL}/_find`) + .set('kbn-xsrf', 'true') + .send() + .expect(200); + + expect(body).to.eql({ + data: [], + page: 1, + per_page: 20, + total: 0, + }); + }); + + it('should return a single exception list when a single exception list is loaded from a find with defaults added', async () => { + // add a single exception list + await supertest + .post(EXCEPTION_LIST_URL) + .set('kbn-xsrf', 'true') + .send(getCreateExceptionListMinimalSchemaMock()) + .expect(200); + + // query the single exception list from _find + const { body } = await supertest + .get(`${EXCEPTION_LIST_URL}/_find`) + .set('kbn-xsrf', 'true') + .send() + .expect(200); + + body.data = [removeExceptionListServerGeneratedProperties(body.data[0])]; + expect(body).to.eql({ + data: [getExceptionResponseMockWithoutAutoGeneratedValues()], + page: 1, + per_page: 20, + total: 1, + }); + }); + }); + }); +}; 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 302877a680aa66..4817c272de5afa 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 @@ -23,5 +23,10 @@ export default ({ loadTestFile }: FtrProviderContext): void => { loadTestFile(require.resolve('./find_list_items')); loadTestFile(require.resolve('./import_list_items')); loadTestFile(require.resolve('./export_list_items')); + loadTestFile(require.resolve('./create_exception_lists')); + loadTestFile(require.resolve('./read_exception_lists')); + loadTestFile(require.resolve('./update_exception_lists')); + loadTestFile(require.resolve('./delete_exception_lists')); + loadTestFile(require.resolve('./find_exception_lists')); }); }; diff --git a/x-pack/test/lists_api_integration/security_and_spaces/tests/read_exception_lists.ts b/x-pack/test/lists_api_integration/security_and_spaces/tests/read_exception_lists.ts new file mode 100644 index 00000000000000..6646f6b230d2c3 --- /dev/null +++ b/x-pack/test/lists_api_integration/security_and_spaces/tests/read_exception_lists.ts @@ -0,0 +1,112 @@ +/* + * 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 { ExceptionListSchema } from '../../../../plugins/lists/common'; +import { getExceptionResponseMockWithoutAutoGeneratedValues } from '../../../../plugins/lists/common/schemas/response/exception_list_schema.mock'; +import { + getCreateExceptionListMinimalSchemaMock, + getCreateExceptionListMinimalSchemaMockWithoutId, +} from '../../../../plugins/lists/common/schemas/request/create_exception_list_schema.mock'; +import { FtrProviderContext } from '../../common/ftr_provider_context'; +import { EXCEPTION_LIST_URL } from '../../../../plugins/lists/common/constants'; + +import { deleteAllExceptions, removeExceptionListServerGeneratedProperties } from '../../utils'; + +// eslint-disable-next-line import/no-default-export +export default ({ getService }: FtrProviderContext) => { + const supertest = getService('supertest'); + const es = getService('es'); + + describe('read_exception_lists', () => { + describe('reading exception lists', () => { + afterEach(async () => { + await deleteAllExceptions(es); + }); + + it('should be able to read a single exception list using list_id', async () => { + // create a simple exception list to read + await supertest + .post(EXCEPTION_LIST_URL) + .set('kbn-xsrf', 'true') + .send(getCreateExceptionListMinimalSchemaMock()) + .expect(200); + + const { body } = await supertest + .get(`${EXCEPTION_LIST_URL}?list_id=${getCreateExceptionListMinimalSchemaMock().list_id}`) + .set('kbn-xsrf', 'true') + .expect(200); + + const bodyToCompare = removeExceptionListServerGeneratedProperties(body); + expect(bodyToCompare).to.eql(getExceptionResponseMockWithoutAutoGeneratedValues()); + }); + + it('should be able to read a single exception list using id', async () => { + // create a simple exception list to read + const { body: createListBody } = await supertest + .post(EXCEPTION_LIST_URL) + .set('kbn-xsrf', 'true') + .send(getCreateExceptionListMinimalSchemaMock()) + .expect(200); + + const { body } = await supertest + .get(`${EXCEPTION_LIST_URL}?id=${createListBody.id}`) + .set('kbn-xsrf', 'true') + .expect(200); + + const bodyToCompare = removeExceptionListServerGeneratedProperties(body); + expect(bodyToCompare).to.eql(getExceptionResponseMockWithoutAutoGeneratedValues()); + }); + + it('should be able to read a single list with an auto-generated list_id', async () => { + // create a simple exception list to read + const { body: createListBody } = await supertest + .post(EXCEPTION_LIST_URL) + .set('kbn-xsrf', 'true') + .send(getCreateExceptionListMinimalSchemaMockWithoutId()) + .expect(200); + + const { body } = await supertest + .get(`${EXCEPTION_LIST_URL}?list_id=${createListBody.list_id}`) + .set('kbn-xsrf', 'true') + .expect(200); + + const outputtedList: Partial = { + ...getExceptionResponseMockWithoutAutoGeneratedValues(), + list_id: body.list_id, + }; + + const bodyToCompare = removeExceptionListServerGeneratedProperties(body); + expect(bodyToCompare).to.eql(outputtedList); + }); + + it('should return 404 if given a fake id', async () => { + const { body } = await supertest + .get(`${EXCEPTION_LIST_URL}?id=c1e1b359-7ac1-4e96-bc81-c683c092436f`) + .set('kbn-xsrf', 'true') + .expect(404); + + expect(body).to.eql({ + status_code: 404, + message: 'Exception list id: "c1e1b359-7ac1-4e96-bc81-c683c092436f" does not exist', + }); + }); + + it('should return 404 if given a fake list_id', async () => { + const { body } = await supertest + .get(`${EXCEPTION_LIST_URL}?list_id=c1e1b359-7ac1-4e96-bc81-c683c092436f`) + .set('kbn-xsrf', 'true') + .expect(404); + + expect(body).to.eql({ + status_code: 404, + message: 'Exception list list_id: "c1e1b359-7ac1-4e96-bc81-c683c092436f" does not exist', + }); + }); + }); + }); +}; diff --git a/x-pack/test/lists_api_integration/security_and_spaces/tests/update_exception_lists.ts b/x-pack/test/lists_api_integration/security_and_spaces/tests/update_exception_lists.ts new file mode 100644 index 00000000000000..05f916cf1ce755 --- /dev/null +++ b/x-pack/test/lists_api_integration/security_and_spaces/tests/update_exception_lists.ts @@ -0,0 +1,182 @@ +/* + * 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 { getExceptionResponseMockWithoutAutoGeneratedValues } from '../../../../plugins/lists/common/schemas/response/exception_list_schema.mock'; +import { getCreateExceptionListMinimalSchemaMock } from '../../../../plugins/lists/common/schemas/request/create_exception_list_schema.mock'; +import { FtrProviderContext } from '../../common/ftr_provider_context'; +import { EXCEPTION_LIST_URL } from '../../../../plugins/lists/common/constants'; + +import { deleteAllExceptions, removeExceptionListServerGeneratedProperties } from '../../utils'; +import { + UpdateExceptionListSchema, + ExceptionListSchema, +} from '../../../../plugins/lists/common/schemas'; + +import { getUpdateMinimalExceptionListSchemaMock } from '../../../../plugins/lists/common/schemas/request/update_exception_list_schema.mock'; + +// eslint-disable-next-line import/no-default-export +export default ({ getService }: FtrProviderContext) => { + const supertest = getService('supertest'); + const es = getService('es'); + + describe('update_exception_lists', () => { + describe('update exception lists', () => { + afterEach(async () => { + await deleteAllExceptions(es); + }); + + it('should update a single exception list property of name using an id', async () => { + // create a simple exception list + await supertest + .post(EXCEPTION_LIST_URL) + .set('kbn-xsrf', 'true') + .send(getCreateExceptionListMinimalSchemaMock()) + .expect(200); + + // update a exception list's name + const updatedList: UpdateExceptionListSchema = { + ...getUpdateMinimalExceptionListSchemaMock(), + name: 'some other name', + }; + + const { body } = await supertest + .put(EXCEPTION_LIST_URL) + .set('kbn-xsrf', 'true') + .send(updatedList) + .expect(200); + + const outputList: Partial = { + ...getExceptionResponseMockWithoutAutoGeneratedValues(), + name: 'some other name', + version: 2, + }; + const bodyToCompare = removeExceptionListServerGeneratedProperties(body); + expect(bodyToCompare).to.eql(outputList); + }); + + it('should update a single exception list property of name using an auto-generated list_id', async () => { + // eslint-disable-next-line @typescript-eslint/naming-convention + const { list_id, ...listNoId } = getCreateExceptionListMinimalSchemaMock(); + + // create a simple exception list + const { body: createListBody } = await supertest + .post(EXCEPTION_LIST_URL) + .set('kbn-xsrf', 'true') + .send(listNoId) + .expect(200); + + // update a exception list's name + const updatedList: UpdateExceptionListSchema = { + ...getUpdateMinimalExceptionListSchemaMock(), + id: createListBody.id, + name: 'some other name', + }; + + const { body } = await supertest + .put(EXCEPTION_LIST_URL) + .set('kbn-xsrf', 'true') + .send(updatedList) + .expect(200); + + const outputList: Partial = { + ...getExceptionResponseMockWithoutAutoGeneratedValues(), + name: 'some other name', + list_id: body.list_id, + version: 2, + }; + const bodyToCompare = removeExceptionListServerGeneratedProperties(body); + expect(bodyToCompare).to.eql(outputList); + }); + + it('should change the version of a list when it updates two properties', async () => { + // create a simple exception list + await supertest + .post(EXCEPTION_LIST_URL) + .set('kbn-xsrf', 'true') + .send(getCreateExceptionListMinimalSchemaMock()) + .expect(200); + + // update a simple exception list property of name and description + // update a exception list's name + const updatedList: UpdateExceptionListSchema = { + ...getUpdateMinimalExceptionListSchemaMock(), + name: 'some other name', + description: 'some other description', + }; + + const { body } = await supertest + .put(EXCEPTION_LIST_URL) + .set('kbn-xsrf', 'true') + .send(updatedList) + .expect(200); + + const outputList: Partial = { + ...getExceptionResponseMockWithoutAutoGeneratedValues(), + name: 'some other name', + description: 'some other description', + version: 2, + }; + const bodyToCompare = removeExceptionListServerGeneratedProperties(body); + expect(bodyToCompare).to.eql(outputList); + }); + + it('should give a 404 if it is given a fake id', async () => { + const updatedList: UpdateExceptionListSchema = { + ...getUpdateMinimalExceptionListSchemaMock(), + id: '5096dec6-b6b9-4d8d-8f93-6c2602079d9d', + }; + delete updatedList.list_id; + + const { body } = await supertest + .put(EXCEPTION_LIST_URL) + .set('kbn-xsrf', 'true') + .send(updatedList) + .expect(404); + + expect(body).to.eql({ + status_code: 404, + message: 'Exception list id: "5096dec6-b6b9-4d8d-8f93-6c2602079d9d" does not exist', + }); + }); + + it('should give a 404 if it is given a fake list_id', async () => { + const updatedList: UpdateExceptionListSchema = { + ...getUpdateMinimalExceptionListSchemaMock(), + list_id: '5096dec6-b6b9-4d8d-8f93-6c2602079d9d', + }; + + const { body } = await supertest + .put(EXCEPTION_LIST_URL) + .set('kbn-xsrf', 'true') + .send(updatedList) + .expect(404); + + expect(body).to.eql({ + status_code: 404, + message: 'Exception list list_id: "5096dec6-b6b9-4d8d-8f93-6c2602079d9d" does not exist', + }); + }); + + it('should give a 404 if both id and list_id is null', async () => { + // eslint-disable-next-line @typescript-eslint/naming-convention + const { list_id, ...listNoId } = getUpdateMinimalExceptionListSchemaMock(); + + const { body } = await supertest + .put(EXCEPTION_LIST_URL) + .set('kbn-xsrf', 'true') + .send(listNoId) + .expect(404); + + expect(body).to.eql({ + status_code: 404, + message: 'either id or list_id need to be defined', + }); + }); + }); + }); +}; diff --git a/x-pack/test/lists_api_integration/utils.ts b/x-pack/test/lists_api_integration/utils.ts index 272768fdf50b30..71c7497db98a03 100644 --- a/x-pack/test/lists_api_integration/utils.ts +++ b/x-pack/test/lists_api_integration/utils.ts @@ -6,8 +6,9 @@ import { SuperTest } from 'supertest'; import supertestAsPromised from 'supertest-as-promised'; +import { Client } from '@elastic/elasticsearch'; -import { ListItemSchema } from '../../plugins/lists/common/schemas'; +import { ListItemSchema, ExceptionListSchema } from '../../plugins/lists/common/schemas'; import { ListSchema } from '../../plugins/lists/common'; import { LIST_INDEX } from '../../plugins/lists/common/constants'; @@ -83,6 +84,18 @@ export const removeListItemServerGeneratedProperties = ( return removedProperties; }; +/** + * This will remove server generated properties such as date times, etc... + * @param list List to pass in to remove typical server generated properties + */ +export const removeExceptionListServerGeneratedProperties = ( + list: Partial +): Partial => { + /* eslint-disable-next-line @typescript-eslint/naming-convention */ + const { created_at, updated_at, id, tie_breaker_id, _version, ...removedProperties } = list; + return removedProperties; +}; + // Similar to ReactJs's waitFor from here: https://testing-library.com/docs/dom-testing-library/api-async#waitfor export const waitFor = async ( functionToTest: () => Promise, @@ -124,3 +137,32 @@ export const binaryToString = (res: any, callback: any): void => { callback(null, Buffer.from(res.data)); }); }; + +/** + * Remove all exceptions from the .kibana index + * This will retry 20 times before giving up and hopefully still not interfere with other tests + * @param es The ElasticSearch handle + */ +export const deleteAllExceptions = async (es: Client, retryCount = 20): Promise => { + if (retryCount > 0) { + try { + await es.deleteByQuery({ + index: '.kibana', + q: 'type:exception-list or type:exception-list-agnostic', + wait_for_completion: true, + refresh: true, + body: {}, + }); + } catch (err) { + // eslint-disable-next-line no-console + console.log( + `Failure trying to deleteAllExceptions, retries left are: ${retryCount - 1}`, + err + ); + await deleteAllExceptions(es, retryCount - 1); + } + } else { + // eslint-disable-next-line no-console + console.log('Could not deleteAllExceptions, no retries are left'); + } +};