Skip to content

Commit

Permalink
fix the total on summary api
Browse files Browse the repository at this point in the history
review suggestions
  • Loading branch information
ashokaditya committed Jan 26, 2022
1 parent e24628d commit bdc0c97
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('get_exception_list_summary', () => {
page: 1,
per_page: 0,
saved_objects: [],
total: 7,
total: 10,
};
savedObjectsClient.find.mockResolvedValue(savedObject);

Expand Down Expand Up @@ -68,7 +68,7 @@ describe('get_exception_list_summary', () => {
page: 1,
per_page: 0,
saved_objects: [],
total: 7,
total: 3,
};
savedObjectsClient.find.mockResolvedValue(savedObject);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* 2.0.
*/

import { ENDPOINT_HOST_ISOLATION_EXCEPTIONS_LIST_ID } from '@kbn/securitysolution-list-constants';
import type {
ExceptionListSummarySchema,
FilterOrUndefined,
Expand Down Expand Up @@ -63,6 +62,10 @@ export const getExceptionListSummary = async ({
}
}

// only pick the items in the list and not the list definition
const itemTypeFilter = `${savedObjectType}.attributes.type: "simple"`;
const adjustedFilter = filter ? `(${filter}) AND ${itemTypeFilter}` : itemTypeFilter;

const savedObject = await savedObjectsClient.find<ExceptionListSoSchema, ByOsAggType>({
aggs: {
by_os: {
Expand All @@ -71,7 +74,7 @@ export const getExceptionListSummary = async ({
},
},
},
filter,
filter: adjustedFilter,
perPage: 0,
search: finalListId,
searchFields: ['list_id'],
Expand All @@ -88,12 +91,7 @@ export const getExceptionListSummary = async ({
(acc, item: ByOsAggBucketType) => ({
...acc,
[item.key]: item.doc_count,
total:
// Do not add up the items by OS if host isolation exception
// As each host exception entry applies to all OSs
listId === ENDPOINT_HOST_ISOLATION_EXCEPTIONS_LIST_ID
? item.doc_count
: acc.total + item.doc_count,
total: savedObject.total,
}),
{ linux: 0, macos: 0, total: 0, windows: 0 }
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,40 @@ export default ({ getService }: FtrProviderContext) => {
};
expect(body).to.eql(expected);
});

it('should not sum up the items by OS for summary total', async () => {
await supertest
.post(EXCEPTION_LIST_URL)
.set('kbn-xsrf', 'true')
.send(getCreateExceptionListMinimalSchemaMock())
.expect(200);

const item = getCreateExceptionListItemMinimalSchemaMock();

await supertest
.post(EXCEPTION_LIST_ITEM_URL)
.set('kbn-xsrf', 'true')
.send({
...item,
os_types: ['windows', 'linux', 'macos'],
item_id: `${item.item_id}-some_item_id`,
})
.expect(200);

const { body }: SummaryResponseType = await supertest
.get(`${EXCEPTION_LIST_URL}/summary?list_id=${LIST_ID}`)
.set('kbn-xsrf', 'true')
.send()
.expect(200);

const expected: ExceptionListSummarySchema = {
linux: 1,
macos: 1,
total: 1,
windows: 1,
};
expect(body).to.eql(expected);
});
});
});
};

0 comments on commit bdc0c97

Please sign in to comment.