Skip to content

Commit

Permalink
Added tests for exception lists and fixed one flaky test found
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankHassanabad committed Aug 12, 2020
1 parent 506e953 commit 9180ca1
Show file tree
Hide file tree
Showing 13 changed files with 650 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import {
DESCRIPTION,
ENDPOINT_TYPE,
LIST_ID,
META,
NAME,
NAMESPACE_TYPE,
Expand All @@ -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,
});
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
import {
DATE_NOW,
DESCRIPTION,
ELASTIC_USER,
ENDPOINT_TYPE,
IMMUTABLE,
LIST_ID,
META,
NAME,
TIE_BREAKER,
USER,
VERSION,
Expand All @@ -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,
Expand All @@ -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,
});
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ExceptionListSchema> = {
...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,
});
});
});
});
};
Original file line number Diff line number Diff line change
@@ -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,
});
});
});
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
});
});
});
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
});
};
Loading

0 comments on commit 9180ca1

Please sign in to comment.