From 64d23a91989c00fc4be8d70010f31c04303f6b04 Mon Sep 17 00:00:00 2001 From: nnamdifrankie Date: Thu, 9 Jan 2020 21:33:54 -0500 Subject: [PATCH 1/2] EMT-65:always return accurate endpoint count --- .../endpoint/server/routes/endpoints.ts | 8 +++- .../apis/endpoint/endpoints.ts | 45 +++++++++++++++++-- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/x-pack/plugins/endpoint/server/routes/endpoints.ts b/x-pack/plugins/endpoint/server/routes/endpoints.ts index 59430947d97da7..fb0b20ead38d37 100644 --- a/x-pack/plugins/endpoint/server/routes/endpoints.ts +++ b/x-pack/plugins/endpoint/server/routes/endpoints.ts @@ -66,6 +66,10 @@ function mapToEndpointResultList( queryParams: Record, searchResponse: SearchResponse ): EndpointResultList { + let totalNumberOfEndpoints = 0; + if (searchResponse?.aggregations?.total?.value) { + totalNumberOfEndpoints = searchResponse.aggregations.total.value; + } if (searchResponse.hits.hits.length > 0) { return { request_page_size: queryParams.size, @@ -74,13 +78,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: [], }; } diff --git a/x-pack/test/api_integration/apis/endpoint/endpoints.ts b/x-pack/test/api_integration/apis/endpoint/endpoints.ts index 95c3678672da30..32864489d37867 100644 --- a/x-pack/test/api_integration/apis/endpoint/endpoints.ts +++ b/x-pack/test/api_integration/apis/endpoint/endpoints.ts @@ -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'); + 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') @@ -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') From ed13c0cabb357cfac9b61140c75e1ee90c7cb062 Mon Sep 17 00:00:00 2001 From: nnamdifrankie Date: Fri, 10 Jan 2020 09:48:44 -0500 Subject: [PATCH 2/2] EMT-65:use shortened form --- x-pack/plugins/endpoint/server/routes/endpoints.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/x-pack/plugins/endpoint/server/routes/endpoints.ts b/x-pack/plugins/endpoint/server/routes/endpoints.ts index fb0b20ead38d37..9d2babc61f11f9 100644 --- a/x-pack/plugins/endpoint/server/routes/endpoints.ts +++ b/x-pack/plugins/endpoint/server/routes/endpoints.ts @@ -66,10 +66,7 @@ function mapToEndpointResultList( queryParams: Record, searchResponse: SearchResponse ): EndpointResultList { - let totalNumberOfEndpoints = 0; - if (searchResponse?.aggregations?.total?.value) { - totalNumberOfEndpoints = searchResponse.aggregations.total.value; - } + const totalNumberOfEndpoints = searchResponse?.aggregations?.total?.value || 0; if (searchResponse.hits.hits.length > 0) { return { request_page_size: queryParams.size,