Skip to content

Commit

Permalink
fix type check, fix api variable case
Browse files Browse the repository at this point in the history
  • Loading branch information
nnamdifrankie committed Jan 6, 2020
1 parent ac81bae commit 0686689
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 48 deletions.
14 changes: 7 additions & 7 deletions x-pack/plugins/endpoint/server/routes/endpoints.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,19 @@ describe('test endpoint route', () => {
const endpointResultList = mockResponse.ok.mock.calls[0][0]?.body as EndpointResultList;
expect(endpointResultList.endpoints.length).toEqual(3);
expect(endpointResultList.total).toEqual(3);
expect(endpointResultList.requestIndex).toEqual(0);
expect(endpointResultList.requestPageSize).toEqual(10);
expect(endpointResultList.request_index).toEqual(0);
expect(endpointResultList.request_page_size).toEqual(10);
});

it('test find the latest of all endpoints with params', async () => {
const mockRequest = httpServerMock.createKibanaRequest({
body: {
pagingProperties: [
paging_properties: [
{
pageSize: 10,
page_size: 10,
},
{
pageIndex: 1,
page_index: 1,
},
],
},
Expand Down Expand Up @@ -117,7 +117,7 @@ describe('test endpoint route', () => {
const endpointResultList = mockResponse.ok.mock.calls[0][0]?.body as EndpointResultList;
expect(endpointResultList.endpoints.length).toEqual(3);
expect(endpointResultList.total).toEqual(3);
expect(endpointResultList.requestIndex).toEqual(10);
expect(endpointResultList.requestPageSize).toEqual(10);
expect(endpointResultList.request_index).toEqual(10);
expect(endpointResultList.request_page_size).toEqual(10);
});
});
48 changes: 22 additions & 26 deletions x-pack/plugins/endpoint/server/routes/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import { SearchResponse } from 'elasticsearch';
import { schema } from '@kbn/config-schema';
import { EndpointAppContext, EndpointData } from '../types';
import { kibanaRequestToEndpointListQuery } from '../services/endpoint/endpoint_query_builders';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { RouteSchemas } from '../../../../../src/core/server/http/router/route';

interface HitSource {
_source: EndpointData;
Expand All @@ -22,33 +20,31 @@ export interface EndpointResultList {
// the total number of unique endpoints in the index
total: number;
// the page size requested
requestPageSize: number;
request_page_size: number;
// the index requested
requestIndex: number;
request_index: number;
}

const endpointListRequestSchema: RouteSchemas<any, any, any> = {
body: schema.nullable(
schema.object({
pagingProperties: schema.arrayOf(
schema.oneOf([
// the number of results to return for this request per page
schema.object({
pageSize: schema.number({ defaultValue: 10, min: 1, max: 10000 }),
}),
// the index of the page to return
schema.object({ pageIndex: schema.number({ defaultValue: 0, min: 0 }) }),
])
),
})
),
};

export function registerEndpointRoutes(router: IRouter, endpointAppContext: EndpointAppContext) {
router.post(
{
path: '/api/endpoint/endpoints',
validate: endpointListRequestSchema,
validate: {
body: schema.nullable(
schema.object({
paging_properties: schema.arrayOf(
schema.oneOf([
// the number of results to return for this request per page
schema.object({
page_size: schema.number({ defaultValue: 10, min: 1, max: 10000 }),
}),
// the index of the page to return
schema.object({ page_index: schema.number({ defaultValue: 0, min: 0 }) }),
])
),
})
),
},
options: { authRequired: true },
},
async (context, req, res) => {
Expand All @@ -72,8 +68,8 @@ function mapToEndpointResultList(
): EndpointResultList {
if (searchResponse.hits.hits.length > 0) {
return {
requestPageSize: queryParams.size,
requestIndex: queryParams.from,
request_page_size: queryParams.size,
request_index: queryParams.from,
endpoints: searchResponse.hits.hits
.map(response => response.inner_hits.most_recent.hits.hits)
.flatMap(data => data as HitSource)
Expand All @@ -82,8 +78,8 @@ function mapToEndpointResultList(
};
} else {
return {
requestPageSize: queryParams.size,
requestIndex: queryParams.from,
request_page_size: queryParams.size,
request_index: queryParams.from,
total: 0,
endpoints: [],
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ async function getPagingProperties(
endpointAppContext: EndpointAppContext
) {
const config = await endpointAppContext.config();
const pagingProperties: { pageSize?: number; pageIndex?: number } = {};
if (request?.body?.pagingProperties) {
for (const property of request.body.pagingProperties) {
const pagingProperties: { page_size?: number; page_index?: number } = {};
if (request?.body?.paging_properties) {
for (const property of request.body.paging_properties) {
Object.assign(
pagingProperties,
...Object.keys(property).map(key => ({ [key]: property[key] }))
);
}
}
return {
pageSize: pagingProperties.pageSize || config.endpointResultListDefaultPageSize,
pageIndex: pagingProperties.pageIndex || config.endpointResultListDefaultFirstPageIndex,
pageSize: pagingProperties.page_size || config.endpointResultListDefaultPageSize,
pageIndex: pagingProperties.page_index || config.endpointResultListDefaultFirstPageIndex,
};
}
20 changes: 10 additions & 10 deletions x-pack/test/api_integration/apis/endpoint/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,42 +21,42 @@ export default function({ getService }: FtrProviderContext) {
.expect(200);
expect(body.total).to.eql(3);
expect(body.endpoints.length).to.eql(3);
expect(body.requestPageSize).to.eql(10);
expect(body.requestIndex).to.eql(0);
expect(body.request_page_size).to.eql(10);
expect(body.request_index).to.eql(0);
});

it('endpoints api should return page based on params passed.', async () => {
const { body } = await supertest
.post('/api/endpoint/endpoints')
.set('kbn-xsrf', 'xxx')
.send({
pagingProperties: [
paging_properties: [
{
pageSize: 1,
page_size: 1,
},
{
pageIndex: 1,
page_index: 1,
},
],
})
.expect(200);
expect(body.total).to.eql(3);
expect(body.endpoints.length).to.eql(1);
expect(body.requestPageSize).to.eql(1);
expect(body.requestIndex).to.eql(1);
expect(body.request_page_size).to.eql(1);
expect(body.request_index).to.eql(1);
});

it('endpoints api should return 400 when pagingProperties is below boundaries.', async () => {
const { body } = await supertest
.post('/api/endpoint/endpoints')
.set('kbn-xsrf', 'xxx')
.send({
pagingProperties: [
paging_properties: [
{
pageSize: 0,
page_size: 0,
},
{
pageIndex: 1,
page_index: 1,
},
],
})
Expand Down

0 comments on commit 0686689

Please sign in to comment.