Skip to content

Commit

Permalink
Adds more end to end tests, fixes some message bugs in the REST route…
Browse files Browse the repository at this point in the history
…s, and adds some missing unit tests
  • Loading branch information
FrankHassanabad committed Aug 6, 2020
1 parent cc685d0 commit ec64d17
Show file tree
Hide file tree
Showing 12 changed files with 374 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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 { DESCRIPTION, LIST_ID, META, NAME, _VERSION } from '../../constants.mock';

import { UpdateListSchema } from './update_list_schema';

export const getUpdateListSchemaMock = (): UpdateListSchema => ({
_version: _VERSION,
description: DESCRIPTION,
id: LIST_ID,
meta: META,
name: NAME,
});

/**
* 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 getUpdateMinimalListSchemaMock = (): UpdateListSchema => ({
description: DESCRIPTION,
id: LIST_ID,
name: NAME,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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 { left } from 'fp-ts/lib/Either';
import { pipe } from 'fp-ts/lib/pipeable';

import { exactCheck, foldLeftRight, getPaths } from '../../shared_imports';

import { UpdateListSchema, updateListSchema } from './update_list_schema';
import { getUpdateListSchemaMock } from './update_list_schema.mock';

describe('update_list_schema', () => {
test('it should validate a typical list request', () => {
const payload = getUpdateListSchemaMock();
const decoded = updateListSchema.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});

test('it should accept an undefined for "meta" but strip it out', () => {
const payload = getUpdateListSchemaMock();
const outputPayload = getUpdateListSchemaMock();
delete payload.meta;
const decoded = updateListSchema.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);
delete outputPayload.meta;
expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(outputPayload);
});

test('it should not allow an extra key to be sent in', () => {
const payload: UpdateListSchema & {
extraKey?: string;
} = getUpdateListSchemaMock();
payload.extraKey = 'some new value';
const decoded = updateListSchema.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual(['invalid keys "extraKey"']);
expect(message.schema).toEqual({});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export const getListResponseMock = (): ListSchema => ({
* such as created_at, updated_at, and id.
*/
export const getListResponseMockWithoutAutoGeneratedValues = (): Partial<ListSchema> => ({
_version: 'WzAsMV0=',
created_by: ELASTIC_USER,
description: DESCRIPTION,
immutable: IMMUTABLE,
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/lists/server/routes/patch_list_route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const patchListRoute = (router: IRouter): void => {
const list = await lists.updateList({ _version, description, id, meta, name, version });
if (list == null) {
return siemResponse.error({
body: `list id: "${id}" found found`,
body: `list id: "${id}" not found`,
statusCode: 404,
});
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const updateExceptionListRoute = (router: IRouter): void => {
});
if (list == null) {
return siemResponse.error({
body: `exception list id: "${id}" found found`,
body: `exception list id: "${id}" not found`,
statusCode: 404,
});
} else {
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/lists/server/routes/update_list_route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const updateListRoute = (router: IRouter): void => {
const list = await lists.updateList({ _version, description, id, meta, name, version });
if (list == null) {
return siemResponse.error({
body: `list id: "${id}" found found`,
body: `list id: "${id}" not found`,
statusCode: 404,
});
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default ({ getService }: FtrProviderContext) => {

describe('create_lists', () => {
describe('validation errors', () => {
it('should give an error that the index must exist first if it does not exist before creating a rule', async () => {
it('should give an error that the index must exist first if it does not exist before creating a list', async () => {
const { body } = await supertest
.post(LIST_URL)
.set('kbn-xsrf', 'true')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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 { FtrProviderContext } from '../../common/ftr_provider_context';
import { LIST_URL } from '../../../../plugins/lists/common/constants';

import { getCreateMinimalListSchemaMock } from '../../../../plugins/lists/common/schemas/request/create_list_schema.mock';
import { createListsIndex, deleteListsIndex, removeServerGeneratedProperties } from '../../utils';
import { getListResponseMockWithoutAutoGeneratedValues } from '../../../../plugins/lists/common/schemas/response/list_schema.mock';

// eslint-disable-next-line import/no-default-export
export default ({ getService }: FtrProviderContext) => {
const supertest = getService('supertest');

describe('delete_lists', () => {
describe('deleting lists', () => {
beforeEach(async () => {
await createListsIndex(supertest);
});

afterEach(async () => {
await deleteListsIndex(supertest);
});

it('should delete a single list with a list id', async () => {
// create a list
await supertest
.post(LIST_URL)
.set('kbn-xsrf', 'true')
.send(getCreateMinimalListSchemaMock())
.expect(200);

// delete the list by its list id
const { body } = await supertest
.delete(`${LIST_URL}?id=${getCreateMinimalListSchemaMock().id}`)
.set('kbn-xsrf', 'true')
.expect(200);

const bodyToCompare = removeServerGeneratedProperties(body);
expect(bodyToCompare).to.eql(getListResponseMockWithoutAutoGeneratedValues());
});

it('should delete a single list using an auto generated id', async () => {
// add a list
const { body: bodyWithCreatedList } = await supertest
.post(LIST_URL)
.set('kbn-xsrf', 'true')
.send(getCreateMinimalListSchemaMock())
.expect(200);

// delete that list by its auto-generated id
const { body } = await supertest
.delete(`${LIST_URL}?id=${bodyWithCreatedList.id}`)
.set('kbn-xsrf', 'true')
.expect(200);

const bodyToCompare = removeServerGeneratedProperties(body);
expect(bodyToCompare).to.eql(getListResponseMockWithoutAutoGeneratedValues());
});

it('should return an error if the id does not exist when trying to delete it', async () => {
const { body } = await supertest
.delete(`${LIST_URL}?id=c1e1b359-7ac1-4e96-bc81-c683c092436f`)
.set('kbn-xsrf', 'true')
.expect(404);

expect(body).to.eql({
message: 'list id: "c1e1b359-7ac1-4e96-bc81-c683c092436f" was not found',
status_code: 404,
});
});
});
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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 { FtrProviderContext } from '../../common/ftr_provider_context';
import { LIST_URL } from '../../../../plugins/lists/common/constants';

import { getCreateMinimalListSchemaMock } from '../../../../plugins/lists/common/schemas/request/create_list_schema.mock';
import { createListsIndex, deleteListsIndex, removeServerGeneratedProperties } from '../../utils';
import { getListResponseMockWithoutAutoGeneratedValues } from '../../../../plugins/lists/common/schemas/response/list_schema.mock';

// eslint-disable-next-line import/no-default-export
export default ({ getService }: FtrProviderContext): void => {
const supertest = getService('supertest');

describe('find_lists', () => {
describe('find lists', () => {
beforeEach(async () => {
await createListsIndex(supertest);
});

afterEach(async () => {
await deleteListsIndex(supertest);
});

it('should return an empty find body correctly if no lists are loaded', async () => {
const { body } = await supertest
.get(`${LIST_URL}/_find`)
.set('kbn-xsrf', 'true')
.send()
.expect(200);

expect(body).to.eql({
cursor: 'WzBd',
data: [],
page: 1,
per_page: 20,
total: 0,
});
});

it('should return a single list when a single list is loaded from a find with defaults added', async () => {
// add a single list
await supertest
.post(LIST_URL)
.set('kbn-xsrf', 'true')
.send(getCreateMinimalListSchemaMock())
.expect(200);

// query the single list from _find
const { body } = await supertest
.get(`${LIST_URL}/_find`)
.set('kbn-xsrf', 'true')
.send()
.expect(200);

body.data = [removeServerGeneratedProperties(body.data[0])];
// cursor is a constant changing value so we have to delete it as well.
delete body.cursor;
expect(body).to.eql({
data: [getListResponseMockWithoutAutoGeneratedValues()],
page: 1,
per_page: 20,
total: 1,
});
});
});
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ export default ({ loadTestFile }: FtrProviderContext): void => {

loadTestFile(require.resolve('./create_lists'));
loadTestFile(require.resolve('./read_lists'));
loadTestFile(require.resolve('./update_lists'));
loadTestFile(require.resolve('./delete_lists'));
loadTestFile(require.resolve('./find_lists'));
});
};
Loading

0 comments on commit ec64d17

Please sign in to comment.