Skip to content

Commit 461d684

Browse files
[security solutions][lists] Adds end to end tests (#74473)
## Summary Adds initial set of end to end tests for lists You can run all of these with the command from kibana root: ```ts node scripts/functional_tests --config x-pack/test/lists_api_integration/security_and_spaces/config.ts ``` Fixes a few minor bugs found such as... * Validation for importing lists was not checking if the indexes were created first * Some wording for the error messages had duplicate words within them ### Checklist - [x] [Unit or functional tests](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#cross-browser-compatibility) were updated or added to match the most common scenarios
1 parent f621b0e commit 461d684

34 files changed

+1709
-6
lines changed

x-pack/plugins/lists/common/constants.mock.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { EntriesArray } from './schemas/types';
99
export const DATE_NOW = '2020-04-20T15:25:31.830Z';
1010
export const OLD_DATE_RELATIVE_TO_DATE_NOW = '2020-04-19T15:25:31.830Z';
1111
export const USER = 'some user';
12+
export const ELASTIC_USER = 'elastic';
1213
export const LIST_INDEX = '.lists';
1314
export const LIST_ITEM_INDEX = '.items';
1415
export const NAME = 'some name';

x-pack/plugins/lists/common/schemas/request/create_list_item_schema.mock.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,20 @@ export const getCreateListItemSchemaMock = (): CreateListItemSchema => ({
1414
meta: META,
1515
value: VALUE,
1616
});
17+
18+
/**
19+
* Useful for end to end testing
20+
*/
21+
export const getCreateMinimalListItemSchemaMock = (): CreateListItemSchema => ({
22+
id: LIST_ITEM_ID,
23+
list_id: LIST_ID,
24+
value: VALUE,
25+
});
26+
27+
/**
28+
* Useful for end to end testing
29+
*/
30+
export const getCreateMinimalListItemSchemaMockWithoutId = (): CreateListItemSchema => ({
31+
list_id: LIST_ID,
32+
value: VALUE,
33+
});

x-pack/plugins/lists/common/schemas/request/create_list_schema.mock.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,22 @@ export const getCreateListSchemaMock = (): CreateListSchema => ({
1818
type: TYPE,
1919
version: VERSION,
2020
});
21+
22+
/**
23+
* Useful for end to end tests and other mechanisms which want to fill in the values
24+
*/
25+
export const getCreateMinimalListSchemaMock = (): CreateListSchema => ({
26+
description: DESCRIPTION,
27+
id: LIST_ID,
28+
name: NAME,
29+
type: TYPE,
30+
});
31+
32+
/**
33+
* Useful for end to end tests and other mechanisms which want to fill in the values
34+
*/
35+
export const getCreateMinimalListSchemaMockWithoutId = (): CreateListSchema => ({
36+
description: DESCRIPTION,
37+
name: NAME,
38+
type: TYPE,
39+
});

x-pack/plugins/lists/common/schemas/request/import_list_item_schema.mock.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,12 @@ import { ImportListItemSchema } from './import_list_item_schema';
99
export const getImportListItemSchemaMock = (): ImportListItemSchema => ({
1010
file: {},
1111
});
12+
13+
/**
14+
* This is useful for end to end tests, it will return a buffer given a string array
15+
* of things to import.
16+
* @param input Array of strings of things to import
17+
*/
18+
export const getImportListItemAsBuffer = (input: string[]): Buffer => {
19+
return Buffer.from(input.join('\r\n'));
20+
};

x-pack/plugins/lists/common/schemas/request/update_list_item_schema.mock.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import { ID, META, VALUE } from '../../constants.mock';
7+
import { ID, LIST_ITEM_ID, META, VALUE } from '../../constants.mock';
88

99
import { UpdateListItemSchema } from './update_list_item_schema';
1010

@@ -13,3 +13,11 @@ export const getUpdateListItemSchemaMock = (): UpdateListItemSchema => ({
1313
meta: META,
1414
value: VALUE,
1515
});
16+
17+
/**
18+
* Useful for end to end testing
19+
*/
20+
export const getUpdateMinimalListItemSchemaMock = (): UpdateListItemSchema => ({
21+
id: LIST_ITEM_ID,
22+
value: VALUE,
23+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import { DESCRIPTION, LIST_ID, META, NAME, _VERSION } from '../../constants.mock';
8+
9+
import { UpdateListSchema } from './update_list_schema';
10+
11+
export const getUpdateListSchemaMock = (): UpdateListSchema => ({
12+
_version: _VERSION,
13+
description: DESCRIPTION,
14+
id: LIST_ID,
15+
meta: META,
16+
name: NAME,
17+
});
18+
19+
/**
20+
* Useful for end to end tests and other mechanisms which want to fill in the values
21+
* after doing a get of the structure.
22+
*/
23+
export const getUpdateMinimalListSchemaMock = (): UpdateListSchema => ({
24+
description: DESCRIPTION,
25+
id: LIST_ID,
26+
name: NAME,
27+
});
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import { left } from 'fp-ts/lib/Either';
8+
import { pipe } from 'fp-ts/lib/pipeable';
9+
10+
import { exactCheck, foldLeftRight, getPaths } from '../../shared_imports';
11+
12+
import { UpdateListSchema, updateListSchema } from './update_list_schema';
13+
import { getUpdateListSchemaMock } from './update_list_schema.mock';
14+
15+
describe('update_list_schema', () => {
16+
test('it should validate a typical list request', () => {
17+
const payload = getUpdateListSchemaMock();
18+
const decoded = updateListSchema.decode(payload);
19+
const checked = exactCheck(payload, decoded);
20+
const message = pipe(checked, foldLeftRight);
21+
expect(getPaths(left(message.errors))).toEqual([]);
22+
expect(message.schema).toEqual(payload);
23+
});
24+
25+
test('it should accept an undefined for "meta" but strip it out', () => {
26+
const payload = getUpdateListSchemaMock();
27+
const outputPayload = getUpdateListSchemaMock();
28+
delete payload.meta;
29+
const decoded = updateListSchema.decode(payload);
30+
const checked = exactCheck(payload, decoded);
31+
const message = pipe(checked, foldLeftRight);
32+
delete outputPayload.meta;
33+
expect(getPaths(left(message.errors))).toEqual([]);
34+
expect(message.schema).toEqual(outputPayload);
35+
});
36+
37+
test('it should not allow an extra key to be sent in', () => {
38+
const payload: UpdateListSchema & {
39+
extraKey?: string;
40+
} = getUpdateListSchemaMock();
41+
payload.extraKey = 'some new value';
42+
const decoded = updateListSchema.decode(payload);
43+
const checked = exactCheck(payload, decoded);
44+
const message = pipe(checked, foldLeftRight);
45+
expect(getPaths(left(message.errors))).toEqual(['invalid keys "extraKey"']);
46+
expect(message.schema).toEqual({});
47+
});
48+
});

x-pack/plugins/lists/common/schemas/response/list_item_schema.mock.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import { ListItemSchema } from '../../../common/schemas';
88
import {
99
DATE_NOW,
10+
ELASTIC_USER,
1011
LIST_ID,
1112
LIST_ITEM_ID,
1213
META,
@@ -31,3 +32,15 @@ export const getListItemResponseMock = (): ListItemSchema => ({
3132
updated_by: USER,
3233
value: VALUE,
3334
});
35+
36+
/**
37+
* This is useful for end to end tests where we remove the auto generated parts for comparisons
38+
* such as created_at, updated_at, and id.
39+
*/
40+
export const getListItemResponseMockWithoutAutoGeneratedValues = (): Partial<ListItemSchema> => ({
41+
created_by: ELASTIC_USER,
42+
list_id: LIST_ID,
43+
type: TYPE,
44+
updated_by: ELASTIC_USER,
45+
value: VALUE,
46+
});

x-pack/plugins/lists/common/schemas/response/list_schema.mock.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { ListSchema } from '../../../common/schemas';
88
import {
99
DATE_NOW,
1010
DESCRIPTION,
11+
ELASTIC_USER,
1112
IMMUTABLE,
1213
LIST_ID,
1314
META,
@@ -35,3 +36,17 @@ export const getListResponseMock = (): ListSchema => ({
3536
updated_by: USER,
3637
version: VERSION,
3738
});
39+
40+
/**
41+
* This is useful for end to end tests where we remove the auto generated parts for comparisons
42+
* such as created_at, updated_at, and id.
43+
*/
44+
export const getListResponseMockWithoutAutoGeneratedValues = (): Partial<ListSchema> => ({
45+
created_by: ELASTIC_USER,
46+
description: DESCRIPTION,
47+
immutable: IMMUTABLE,
48+
name: NAME,
49+
type: TYPE,
50+
updated_by: ELASTIC_USER,
51+
version: VERSION,
52+
});

x-pack/plugins/lists/server/routes/create_list_item_route.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ export const createListItemRoute = (router: IRouter): void => {
3636
statusCode: 404,
3737
});
3838
} else {
39+
if (id != null) {
40+
const listItem = await lists.getListItem({ id });
41+
if (listItem != null) {
42+
return siemResponse.error({
43+
body: `list item id: "${id}" already exists`,
44+
statusCode: 409,
45+
});
46+
}
47+
}
3948
const createdListItem = await lists.createListItem({
4049
deserializer: list.deserializer,
4150
id,

0 commit comments

Comments
 (0)