Skip to content

Commit db357a2

Browse files
[Security Solution][lists] Adds tests for exception lists and items part 2 (#74815)
## Summary This is the basics of end to end tests, so there could be a lot more, but this ties to cover the basics of the tests. Test with: ```ts node scripts/functional_tests --config x-pack/test/lists_api_integration/security_and_spaces/config.ts ``` Adds these tests for the route counter parts: * create_exception_list_items.ts * create_exception_lists.ts * delete_exception_list_items.ts * delete_exception_lists.ts * find_exception_list_items.ts * find_exception_lists.ts * read_exception_list_items.ts * read_exception_lists.ts * update_exception_list_items.ts * update_exception_lists.ts Fixes a few minor strings, other tests, but no large bugs found with these tests ### Checklist - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios
1 parent e2ef219 commit db357a2

26 files changed

+1444
-46
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
COMMENTS,
99
DESCRIPTION,
1010
ENTRIES,
11+
ITEM_ID,
1112
ITEM_TYPE,
1213
LIST_ID,
1314
META,
@@ -32,3 +33,26 @@ export const getCreateExceptionListItemSchemaMock = (): CreateExceptionListItemS
3233
tags: TAGS,
3334
type: ITEM_TYPE,
3435
});
36+
37+
/**
38+
* Useful for end to end testing
39+
*/
40+
export const getCreateExceptionListItemMinimalSchemaMock = (): CreateExceptionListItemSchema => ({
41+
description: DESCRIPTION,
42+
entries: ENTRIES,
43+
item_id: ITEM_ID,
44+
list_id: LIST_ID,
45+
name: NAME,
46+
type: ITEM_TYPE,
47+
});
48+
49+
/**
50+
* Useful for end to end testing
51+
*/
52+
export const getCreateExceptionListItemMinimalSchemaMockWithoutId = (): CreateExceptionListItemSchema => ({
53+
description: DESCRIPTION,
54+
entries: ENTRIES,
55+
list_id: LIST_ID,
56+
name: NAME,
57+
type: ITEM_TYPE,
58+
});

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import {
88
DESCRIPTION,
99
ENDPOINT_TYPE,
10+
LIST_ID,
1011
META,
1112
NAME,
1213
NAMESPACE_TYPE,
@@ -26,3 +27,22 @@ export const getCreateExceptionListSchemaMock = (): CreateExceptionListSchema =>
2627
type: ENDPOINT_TYPE,
2728
version: VERSION,
2829
});
30+
31+
/**
32+
* Useful for end to end testing
33+
*/
34+
export const getCreateExceptionListMinimalSchemaMock = (): CreateExceptionListSchema => ({
35+
description: DESCRIPTION,
36+
list_id: LIST_ID,
37+
name: NAME,
38+
type: ENDPOINT_TYPE,
39+
});
40+
41+
/**
42+
* Useful for end to end testing
43+
*/
44+
export const getCreateExceptionListMinimalSchemaMockWithoutId = (): CreateExceptionListSchema => ({
45+
description: DESCRIPTION,
46+
name: NAME,
47+
type: ENDPOINT_TYPE,
48+
});

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
DESCRIPTION,
1010
ENTRIES,
1111
ID,
12+
ITEM_ID,
1213
ITEM_TYPE,
1314
LIST_ITEM_ID,
1415
META,
@@ -34,3 +35,15 @@ export const getUpdateExceptionListItemSchemaMock = (): UpdateExceptionListItemS
3435
tags: TAGS,
3536
type: ITEM_TYPE,
3637
});
38+
39+
/**
40+
* Useful for end to end tests and other mechanisms which want to fill in the values
41+
* after doing a get of the structure.
42+
*/
43+
export const getUpdateMinimalExceptionListItemSchemaMock = (): UpdateExceptionListItemSchema => ({
44+
description: DESCRIPTION,
45+
entries: ENTRIES,
46+
item_id: ITEM_ID,
47+
name: NAME,
48+
type: ITEM_TYPE,
49+
});

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,14 @@ export const getUpdateExceptionListSchemaMock = (): UpdateExceptionListSchema =>
2020
tags: ['malware'],
2121
type: 'endpoint',
2222
});
23+
24+
/**
25+
* Useful for end to end tests and other mechanisms which want to fill in the values
26+
* after doing a get of the structure.
27+
*/
28+
export const getUpdateMinimalExceptionListSchemaMock = (): UpdateExceptionListSchema => ({
29+
description: DESCRIPTION,
30+
list_id: LIST_ID,
31+
name: NAME,
32+
type: 'endpoint',
33+
});

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ import {
77
COMMENTS,
88
DATE_NOW,
99
DESCRIPTION,
10+
ELASTIC_USER,
1011
ENTRIES,
12+
ITEM_ID,
1113
ITEM_TYPE,
14+
LIST_ID,
1215
META,
1316
NAME,
1417
NAMESPACE_TYPE,
@@ -38,3 +41,24 @@ export const getExceptionListItemSchemaMock = (): ExceptionListItemSchema => ({
3841
updated_at: DATE_NOW,
3942
updated_by: USER,
4043
});
44+
45+
/**
46+
* This is useful for end to end tests where we remove the auto generated parts for comparisons
47+
* such as created_at, updated_at, and id.
48+
*/
49+
export const getExceptionListItemResponseMockWithoutAutoGeneratedValues = (): Partial<
50+
ExceptionListItemSchema
51+
> => ({
52+
_tags: [],
53+
comments: [],
54+
created_by: ELASTIC_USER,
55+
description: DESCRIPTION,
56+
entries: ENTRIES,
57+
item_id: ITEM_ID,
58+
list_id: LIST_ID,
59+
name: NAME,
60+
namespace_type: 'single',
61+
tags: [],
62+
type: ITEM_TYPE,
63+
updated_by: ELASTIC_USER,
64+
});

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
import {
88
DATE_NOW,
99
DESCRIPTION,
10+
ELASTIC_USER,
1011
ENDPOINT_TYPE,
1112
IMMUTABLE,
13+
LIST_ID,
1214
META,
15+
NAME,
1316
TIE_BREAKER,
1417
USER,
1518
VERSION,
@@ -18,6 +21,7 @@ import {
1821
import { ENDPOINT_LIST_ID } from '../..';
1922

2023
import { ExceptionListSchema } from './exception_list_schema';
24+
2125
export const getExceptionListSchemaMock = (): ExceptionListSchema => ({
2226
_tags: ['endpoint', 'process', 'malware', 'os:linux'],
2327
_version: _VERSION,
@@ -37,3 +41,23 @@ export const getExceptionListSchemaMock = (): ExceptionListSchema => ({
3741
updated_by: 'user_name',
3842
version: VERSION,
3943
});
44+
45+
/**
46+
* This is useful for end to end tests where we remove the auto generated parts for comparisons
47+
* such as created_at, updated_at, and id.
48+
*/
49+
export const getExceptionResponseMockWithoutAutoGeneratedValues = (): Partial<
50+
ExceptionListSchema
51+
> => ({
52+
_tags: [],
53+
created_by: ELASTIC_USER,
54+
description: DESCRIPTION,
55+
immutable: IMMUTABLE,
56+
list_id: LIST_ID,
57+
name: NAME,
58+
namespace_type: 'single',
59+
tags: [],
60+
type: ENDPOINT_TYPE,
61+
updated_by: ELASTIC_USER,
62+
version: VERSION,
63+
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export const createExceptionListItemRoute = (router: IRouter): void => {
5757
});
5858
if (exceptionList == null) {
5959
return siemResponse.error({
60-
body: `list id: "${listId}" does not exist`,
60+
body: `exception list id: "${listId}" does not exist`,
6161
statusCode: 404,
6262
});
6363
} else {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export const findExceptionListItemRoute = (router: IRouter): void => {
6262
});
6363
if (exceptionListItems == null) {
6464
return siemResponse.error({
65-
body: `list id: "${listId}" does not exist`,
65+
body: `exception list id: "${listId}" does not exist`,
6666
statusCode: 404,
6767
});
6868
}

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

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -54,39 +54,46 @@ export const updateExceptionListItemRoute = (router: IRouter): void => {
5454
namespace_type: namespaceType,
5555
tags,
5656
} = request.body;
57-
const exceptionLists = getExceptionListClient(context);
58-
const exceptionListItem = await exceptionLists.updateExceptionListItem({
59-
_tags,
60-
_version,
61-
comments,
62-
description,
63-
entries,
64-
id,
65-
itemId,
66-
meta,
67-
name,
68-
namespaceType,
69-
tags,
70-
type,
71-
});
72-
if (exceptionListItem == null) {
73-
if (id != null) {
74-
return siemResponse.error({
75-
body: `list item id: "${id}" not found`,
76-
statusCode: 404,
77-
});
78-
} else {
79-
return siemResponse.error({
80-
body: `list item item_id: "${itemId}" not found`,
81-
statusCode: 404,
82-
});
83-
}
57+
if (id == null && itemId == null) {
58+
return siemResponse.error({
59+
body: 'either id or item_id need to be defined',
60+
statusCode: 404,
61+
});
8462
} else {
85-
const [validated, errors] = validate(exceptionListItem, exceptionListItemSchema);
86-
if (errors != null) {
87-
return siemResponse.error({ body: errors, statusCode: 500 });
63+
const exceptionLists = getExceptionListClient(context);
64+
const exceptionListItem = await exceptionLists.updateExceptionListItem({
65+
_tags,
66+
_version,
67+
comments,
68+
description,
69+
entries,
70+
id,
71+
itemId,
72+
meta,
73+
name,
74+
namespaceType,
75+
tags,
76+
type,
77+
});
78+
if (exceptionListItem == null) {
79+
if (id != null) {
80+
return siemResponse.error({
81+
body: `exception list item id: "${id}" does not exist`,
82+
statusCode: 404,
83+
});
84+
} else {
85+
return siemResponse.error({
86+
body: `exception list item item_id: "${itemId}" does not exist`,
87+
statusCode: 404,
88+
});
89+
}
8890
} else {
89-
return response.ok({ body: validated ?? {} });
91+
const [validated, errors] = validate(exceptionListItem, exceptionListItemSchema);
92+
if (errors != null) {
93+
return siemResponse.error({ body: errors, statusCode: 500 });
94+
} else {
95+
return response.ok({ body: validated ?? {} });
96+
}
9097
}
9198
}
9299
} catch (err) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
updateExceptionListSchema,
1616
} from '../../common/schemas';
1717

18-
import { getExceptionListClient } from './utils';
18+
import { getErrorMessageExceptionList, getExceptionListClient } from './utils';
1919

2020
export const updateExceptionListRoute = (router: IRouter): void => {
2121
router.put(
@@ -50,7 +50,7 @@ export const updateExceptionListRoute = (router: IRouter): void => {
5050
const exceptionLists = getExceptionListClient(context);
5151
if (id == null && listId == null) {
5252
return siemResponse.error({
53-
body: `either id or list_id need to be defined`,
53+
body: 'either id or list_id need to be defined',
5454
statusCode: 404,
5555
});
5656
} else {
@@ -69,7 +69,7 @@ export const updateExceptionListRoute = (router: IRouter): void => {
6969
});
7070
if (list == null) {
7171
return siemResponse.error({
72-
body: `exception list id: "${id}" not found`,
72+
body: getErrorMessageExceptionList({ id, listId }),
7373
statusCode: 404,
7474
});
7575
} else {

0 commit comments

Comments
 (0)