Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Endpoint] EMT-65:always return accurate endpoint count #54423

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions x-pack/plugins/endpoint/server/routes/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ function mapToEndpointResultList(
queryParams: Record<string, any>,
searchResponse: SearchResponse<EndpointData>
): EndpointResultList {
const totalNumberOfEndpoints = searchResponse?.aggregations?.total?.value || 0;
if (searchResponse.hits.hits.length > 0) {
return {
request_page_size: queryParams.size,
Expand All @@ -74,13 +75,13 @@ function mapToEndpointResultList(
.map(response => response.inner_hits.most_recent.hits.hits)
.flatMap(data => data as HitSource)
.map(entry => entry._source),
total: searchResponse.aggregations.total.value,
total: totalNumberOfEndpoints,
};
} else {
return {
request_page_size: queryParams.size,
request_index: queryParams.from,
total: 0,
total: totalNumberOfEndpoints,
endpoints: [],
};
}
Expand Down
45 changes: 42 additions & 3 deletions x-pack/test/api_integration/apis/endpoint/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,24 @@ export default function({ getService }: FtrProviderContext) {
const esArchiver = getService('esArchiver');
const supertest = getService('supertest');
describe('test endpoints api', () => {
before(() => esArchiver.load('endpoint/endpoints'));
after(() => esArchiver.unload('endpoint/endpoints'));
describe('GET /api/endpoint/endpoints', () => {
describe('POST /api/endpoint/endpoints when index is empty', () => {
it('endpoints api should return empty result when index is empty', async () => {
await esArchiver.unload('endpoint/endpoints');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

const { body } = await supertest
.post('/api/endpoint/endpoints')
.set('kbn-xsrf', 'xxx')
.send()
.expect(200);
expect(body.total).to.eql(0);
expect(body.endpoints.length).to.eql(0);
expect(body.request_page_size).to.eql(10);
expect(body.request_index).to.eql(0);
});
});

describe('POST /api/endpoint/endpoints when index is not empty', () => {
before(() => esArchiver.load('endpoint/endpoints'));
after(() => esArchiver.unload('endpoint/endpoints'));
it('endpoints api should return one entry for each endpoint with default paging', async () => {
const { body } = await supertest
.post('/api/endpoint/endpoints')
Expand Down Expand Up @@ -46,6 +61,30 @@ export default function({ getService }: FtrProviderContext) {
expect(body.request_index).to.eql(1);
});

/* test that when paging properties produces no result, the total should reflect the actual number of endpoints
in the index.
*/
it('endpoints api should return accurate total endpoints if page index produces no result', async () => {
const { body } = await supertest
.post('/api/endpoint/endpoints')
.set('kbn-xsrf', 'xxx')
.send({
paging_properties: [
{
page_size: 10,
},
{
page_index: 3,
},
],
})
.expect(200);
expect(body.total).to.eql(3);
expect(body.endpoints.length).to.eql(0);
expect(body.request_page_size).to.eql(10);
expect(body.request_index).to.eql(30);
});

it('endpoints api should return 400 when pagingProperties is below boundaries.', async () => {
const { body } = await supertest
.post('/api/endpoint/endpoints')
Expand Down