Skip to content

Commit 3841ab3

Browse files
Added other end to end tests to complete the exception list items
1 parent 30e39a2 commit 3841ab3

File tree

10 files changed

+608
-38
lines changed

10 files changed

+608
-38
lines changed

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

Lines changed: 2 additions & 2 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,
@@ -42,8 +43,7 @@ export const getUpdateExceptionListItemSchemaMock = (): UpdateExceptionListItemS
4243
export const getUpdateMinimalExceptionListItemSchemaMock = (): UpdateExceptionListItemSchema => ({
4344
description: DESCRIPTION,
4445
entries: ENTRIES,
45-
id: ID,
46-
item_id: LIST_ITEM_ID,
46+
item_id: ITEM_ID,
4747
name: NAME,
4848
type: ITEM_TYPE,
4949
});

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/utils/get_error_message_exception_list_item.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const getErrorMessageExceptionListItem = ({
1414
if (id != null) {
1515
return `exception list item id: "${id}" does not exist`;
1616
} else if (itemId != null) {
17-
return `exception list item list_id: "${itemId}" does not exist`;
17+
return `exception list item item_id: "${itemId}" does not exist`;
1818
} else {
1919
return 'exception list item does not exist';
2020
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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 expect from '@kbn/expect';
8+
9+
import { ExceptionListItemSchema } from '../../../../plugins/lists/common';
10+
import { getExceptionListItemResponseMockWithoutAutoGeneratedValues } from '../../../../plugins/lists/common/schemas/response/exception_list_item_schema.mock';
11+
import {
12+
getCreateExceptionListItemMinimalSchemaMock,
13+
getCreateExceptionListItemMinimalSchemaMockWithoutId,
14+
} from '../../../../plugins/lists/common/schemas/request/create_exception_list_item_schema.mock';
15+
import { getCreateExceptionListMinimalSchemaMock } from '../../../../plugins/lists/common/schemas/request/create_exception_list_schema.mock';
16+
import { FtrProviderContext } from '../../common/ftr_provider_context';
17+
import {
18+
EXCEPTION_LIST_URL,
19+
EXCEPTION_LIST_ITEM_URL,
20+
} from '../../../../plugins/lists/common/constants';
21+
22+
import { deleteAllExceptions, removeExceptionListItemServerGeneratedProperties } from '../../utils';
23+
24+
// eslint-disable-next-line import/no-default-export
25+
export default ({ getService }: FtrProviderContext) => {
26+
const supertest = getService('supertest');
27+
const es = getService('es');
28+
29+
describe('delete_exception_list_items', () => {
30+
describe('delete exception list items', () => {
31+
afterEach(async () => {
32+
await deleteAllExceptions(es);
33+
});
34+
35+
it('should delete a single exception list item by its item_id', async () => {
36+
// create an exception list
37+
await supertest
38+
.post(EXCEPTION_LIST_URL)
39+
.set('kbn-xsrf', 'true')
40+
.send(getCreateExceptionListMinimalSchemaMock())
41+
.expect(200);
42+
43+
// create an exception list item
44+
await supertest
45+
.post(EXCEPTION_LIST_ITEM_URL)
46+
.set('kbn-xsrf', 'true')
47+
.send(getCreateExceptionListItemMinimalSchemaMock())
48+
.expect(200);
49+
50+
// delete the exception list item by its item_id
51+
const { body } = await supertest
52+
.delete(
53+
`${EXCEPTION_LIST_ITEM_URL}?item_id=${
54+
getCreateExceptionListItemMinimalSchemaMock().item_id
55+
}`
56+
)
57+
.set('kbn-xsrf', 'true')
58+
.expect(200);
59+
60+
const bodyToCompare = removeExceptionListItemServerGeneratedProperties(body);
61+
expect(bodyToCompare).to.eql(getExceptionListItemResponseMockWithoutAutoGeneratedValues());
62+
});
63+
64+
it('should delete a single exception list item using an auto generated id', async () => {
65+
// create an exception list
66+
await supertest
67+
.post(EXCEPTION_LIST_URL)
68+
.set('kbn-xsrf', 'true')
69+
.send(getCreateExceptionListMinimalSchemaMock())
70+
.expect(200);
71+
72+
// create an exception list item
73+
const { body: bodyWithCreatedList } = await supertest
74+
.post(EXCEPTION_LIST_ITEM_URL)
75+
.set('kbn-xsrf', 'true')
76+
.send(getCreateExceptionListItemMinimalSchemaMockWithoutId())
77+
.expect(200);
78+
79+
// delete that exception list item by its auto-generated id
80+
const { body } = await supertest
81+
.delete(`${EXCEPTION_LIST_ITEM_URL}?id=${bodyWithCreatedList.id}`)
82+
.set('kbn-xsrf', 'true')
83+
.expect(200);
84+
const outputtedList: Partial<ExceptionListItemSchema> = {
85+
...getExceptionListItemResponseMockWithoutAutoGeneratedValues(),
86+
item_id: body.item_id,
87+
};
88+
89+
const bodyToCompare = removeExceptionListItemServerGeneratedProperties(body);
90+
expect(bodyToCompare).to.eql(outputtedList);
91+
});
92+
93+
it('should return an error if the id does not exist when trying to delete it', async () => {
94+
const { body } = await supertest
95+
.delete(`${EXCEPTION_LIST_ITEM_URL}?id=c1e1b359-7ac1-4e96-bc81-c683c092436f`)
96+
.set('kbn-xsrf', 'true')
97+
.expect(404);
98+
99+
expect(body).to.eql({
100+
message: 'exception list item id: "c1e1b359-7ac1-4e96-bc81-c683c092436f" does not exist',
101+
status_code: 404,
102+
});
103+
});
104+
105+
it('should return an error if the item_id does not exist when trying to delete it', async () => {
106+
const { body } = await supertest
107+
.delete(`${EXCEPTION_LIST_ITEM_URL}?item_id=c1e1b359-7ac1-4e96-bc81-c683c092436f`)
108+
.set('kbn-xsrf', 'true')
109+
.expect(404);
110+
111+
expect(body).to.eql({
112+
message:
113+
'exception list item item_id: "c1e1b359-7ac1-4e96-bc81-c683c092436f" does not exist',
114+
status_code: 404,
115+
});
116+
});
117+
});
118+
});
119+
};

x-pack/test/lists_api_integration/security_and_spaces/tests/delete_exception_lists.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66

77
import expect from '@kbn/expect';
88

9+
import { ExceptionListSchema } from '../../../../plugins/lists/common';
910
import { getExceptionResponseMockWithoutAutoGeneratedValues } from '../../../../plugins/lists/common/schemas/response/exception_list_schema.mock';
10-
import { getCreateExceptionListMinimalSchemaMock } from '../../../../plugins/lists/common/schemas/request/create_exception_list_schema.mock';
11+
import {
12+
getCreateExceptionListMinimalSchemaMock,
13+
getCreateExceptionListMinimalSchemaMockWithoutId,
14+
} from '../../../../plugins/lists/common/schemas/request/create_exception_list_schema.mock';
1115
import { FtrProviderContext } from '../../common/ftr_provider_context';
1216
import { EXCEPTION_LIST_URL } from '../../../../plugins/lists/common/constants';
1317

@@ -49,7 +53,7 @@ export default ({ getService }: FtrProviderContext) => {
4953
const { body: bodyWithCreatedList } = await supertest
5054
.post(EXCEPTION_LIST_URL)
5155
.set('kbn-xsrf', 'true')
52-
.send(getCreateExceptionListMinimalSchemaMock())
56+
.send(getCreateExceptionListMinimalSchemaMockWithoutId())
5357
.expect(200);
5458

5559
// delete that list by its auto-generated id
@@ -58,8 +62,12 @@ export default ({ getService }: FtrProviderContext) => {
5862
.set('kbn-xsrf', 'true')
5963
.expect(200);
6064

65+
const outputtedList: Partial<ExceptionListSchema> = {
66+
...getExceptionResponseMockWithoutAutoGeneratedValues(),
67+
list_id: body.list_id,
68+
};
6169
const bodyToCompare = removeExceptionListServerGeneratedProperties(body);
62-
expect(bodyToCompare).to.eql(getExceptionResponseMockWithoutAutoGeneratedValues());
70+
expect(bodyToCompare).to.eql(outputtedList);
6371
});
6472

6573
it('should return an error if the id does not exist when trying to delete it', async () => {
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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 expect from '@kbn/expect';
8+
9+
import { getExceptionListItemResponseMockWithoutAutoGeneratedValues } from '../../../../plugins/lists/common/schemas/response/exception_list_item_schema.mock';
10+
import { getCreateExceptionListItemMinimalSchemaMock } from '../../../../plugins/lists/common/schemas/request/create_exception_list_item_schema.mock';
11+
import { getCreateExceptionListMinimalSchemaMock } from '../../../../plugins/lists/common/schemas/request/create_exception_list_schema.mock';
12+
import { FtrProviderContext } from '../../common/ftr_provider_context';
13+
import {
14+
EXCEPTION_LIST_URL,
15+
EXCEPTION_LIST_ITEM_URL,
16+
} from '../../../../plugins/lists/common/constants';
17+
18+
import { deleteAllExceptions, removeExceptionListItemServerGeneratedProperties } from '../../utils';
19+
20+
// eslint-disable-next-line import/no-default-export
21+
export default ({ getService }: FtrProviderContext): void => {
22+
const supertest = getService('supertest');
23+
const es = getService('es');
24+
25+
describe('find_exception_list_items', () => {
26+
describe('find exception list items', () => {
27+
afterEach(async () => {
28+
await deleteAllExceptions(es);
29+
});
30+
31+
it('should return an empty find body correctly if no exception list items are loaded', async () => {
32+
await supertest
33+
.post(EXCEPTION_LIST_URL)
34+
.set('kbn-xsrf', 'true')
35+
.send(getCreateExceptionListMinimalSchemaMock())
36+
.expect(200);
37+
38+
const { body } = await supertest
39+
.get(
40+
`${EXCEPTION_LIST_ITEM_URL}/_find?list_id=${
41+
getCreateExceptionListMinimalSchemaMock().list_id
42+
}`
43+
)
44+
.set('kbn-xsrf', 'true')
45+
.send()
46+
.expect(200);
47+
48+
expect(body).to.eql({
49+
data: [],
50+
page: 1,
51+
per_page: 20,
52+
total: 0,
53+
});
54+
});
55+
56+
it('should return 404 if given a list_id that does not exist', async () => {
57+
const { body } = await supertest
58+
.get(`${EXCEPTION_LIST_ITEM_URL}/_find?list_id=non_exist`)
59+
.set('kbn-xsrf', 'true')
60+
.send()
61+
.expect(404);
62+
63+
expect(body).to.eql({
64+
message: 'exception list id: "non_exist" does not exist',
65+
status_code: 404,
66+
});
67+
});
68+
69+
it('should return a single exception list item when a single exception list item is loaded from a find with defaults added', async () => {
70+
// add the exception list
71+
await supertest
72+
.post(EXCEPTION_LIST_URL)
73+
.set('kbn-xsrf', 'true')
74+
.send(getCreateExceptionListMinimalSchemaMock())
75+
.expect(200);
76+
77+
// add a single exception list item
78+
await supertest
79+
.post(EXCEPTION_LIST_ITEM_URL)
80+
.set('kbn-xsrf', 'true')
81+
.send(getCreateExceptionListItemMinimalSchemaMock())
82+
.expect(200);
83+
84+
// query the single exception list from _find
85+
const { body } = await supertest
86+
.get(
87+
`${EXCEPTION_LIST_ITEM_URL}/_find?list_id=${
88+
getCreateExceptionListMinimalSchemaMock().list_id
89+
}`
90+
)
91+
.set('kbn-xsrf', 'true')
92+
.send()
93+
.expect(200);
94+
95+
body.data = [removeExceptionListItemServerGeneratedProperties(body.data[0])];
96+
expect(body).to.eql({
97+
data: [getExceptionListItemResponseMockWithoutAutoGeneratedValues()],
98+
page: 1,
99+
per_page: 20,
100+
total: 1,
101+
});
102+
});
103+
});
104+
});
105+
};

x-pack/test/lists_api_integration/security_and_spaces/tests/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ export default ({ loadTestFile }: FtrProviderContext): void => {
2626
loadTestFile(require.resolve('./create_exception_lists'));
2727
loadTestFile(require.resolve('./create_exception_list_items'));
2828
loadTestFile(require.resolve('./read_exception_lists'));
29+
loadTestFile(require.resolve('./read_exception_list_items'));
2930
loadTestFile(require.resolve('./update_exception_lists'));
31+
loadTestFile(require.resolve('./update_exception_list_items'));
3032
loadTestFile(require.resolve('./delete_exception_lists'));
33+
loadTestFile(require.resolve('./delete_exception_list_items'));
3134
loadTestFile(require.resolve('./find_exception_lists'));
35+
loadTestFile(require.resolve('./find_exception_list_items'));
3236
});
3337
};

0 commit comments

Comments
 (0)