Skip to content

Commit

Permalink
More tests for alert list functionality + cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
madirey committed Feb 5, 2020
1 parent 28f7baa commit e6cac13
Show file tree
Hide file tree
Showing 7 changed files with 1,599 additions and 143,594 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const initialState = (): AlertListState => {
return {
alerts: [],
request_page_size: 10,
request_index: 0,
request_page_index: 0,
result_from_index: 0,
total: 0,
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { AlertData } from '../../../../../common/types';
import { AlertResultList } from '../../../../../common/types';

export interface AlertListData {
alerts: AlertData[];
request_page_size: number;
request_index: number;
total: number;
}

export type AlertListState = AlertListData;
export type AlertListData = AlertResultList;
export type AlertListState = AlertResultList;
47 changes: 25 additions & 22 deletions x-pack/plugins/endpoint/server/routes/alerts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
} from '../../../../../src/core/server/mocks';
import { AlertData, AlertResultList } from '../../common/types';
import { SearchResponse } from 'elasticsearch';
import { registerAlertRoutes } from './alerts';
import { reqSchema, registerAlertRoutes } from './alerts';
import { EndpointConfigSchema } from '../config';
import * as data from '../test_data/all_alerts_data.json';
import * as dataLegacy from '../test_data/all_alerts_data_legacy.json';
Expand Down Expand Up @@ -72,7 +72,7 @@ describe('test alerts route', () => {
expect(routeConfig.options).toEqual({ authRequired: true });
expect(mockResponse.ok).toBeCalled();
const alertResultList = mockResponse.ok.mock.calls[0][0]?.body as AlertResultList;
expect(alertResultList.total).toEqual(132);
expect(alertResultList.total).toEqual(21);
expect(alertResultList.request_page_index).toEqual(0);
expect(alertResultList.result_from_index).toEqual(0);
expect(alertResultList.request_page_size).toEqual(10);
Expand Down Expand Up @@ -103,7 +103,7 @@ describe('test alerts route', () => {
expect(routeConfig.options).toEqual({ authRequired: true });
expect(mockResponse.ok).toBeCalled();
const alertResultList = mockResponse.ok.mock.calls[0][0]?.body as AlertResultList;
expect(alertResultList.total).toEqual(132);
expect(alertResultList.total).toEqual(21);
expect(alertResultList.request_page_index).toEqual(0);
expect(alertResultList.result_from_index).toEqual(0);
expect(alertResultList.request_page_size).toEqual(10);
Expand All @@ -113,13 +113,11 @@ describe('test alerts route', () => {
const mockRequest = httpServerMock.createKibanaRequest({
method: 'post',
body: {
page_size: 20,
page_index: 2,
page_size: 6,
page_index: 3,
},
});
mockScopedClient.callAsCurrentUser.mockImplementationOnce(() =>
Promise.resolve((data as unknown) as SearchResponse<AlertData>)
);
mockScopedClient.callAsCurrentUser.mockImplementationOnce(() => Promise.resolve(data));
[routeConfig, routeHandler] = routerMock.post.mock.calls.find(([{ path }]) =>
path.startsWith('/api/endpoint/alerts')
)!;
Expand All @@ -140,24 +138,21 @@ describe('test alerts route', () => {
expect(routeConfig.options).toEqual({ authRequired: true });
expect(mockResponse.ok).toBeCalled();
const alertResultList = mockResponse.ok.mock.calls[0][0]?.body as AlertResultList;
expect(alertResultList.alerts.length).toEqual(20);
expect(alertResultList.total).toEqual(132);
expect(alertResultList.request_page_index).toEqual(2);
expect(alertResultList.result_from_index).toEqual(40);
expect(alertResultList.request_page_size).toEqual(20);
expect(alertResultList.total).toEqual(21);
expect(alertResultList.request_page_index).toEqual(3);
expect(alertResultList.result_from_index).toEqual(18);
expect(alertResultList.request_page_size).toEqual(6);
});

it('should return alert results according to pagination params -- GET', async () => {
const mockRequest = httpServerMock.createKibanaRequest({
path: '/api/endpoint/alerts',
query: {
page_size: 20,
page_size: 3,
page_index: 2,
},
});
mockScopedClient.callAsCurrentUser.mockImplementationOnce(() =>
Promise.resolve((data as unknown) as SearchResponse<AlertData>)
);
mockScopedClient.callAsCurrentUser.mockImplementationOnce(() => Promise.resolve(data));
[routeConfig, routeHandler] = routerMock.get.mock.calls.find(([{ path }]) =>
path.startsWith('/api/endpoint/alerts')
)!;
Expand All @@ -178,11 +173,19 @@ describe('test alerts route', () => {
expect(routeConfig.options).toEqual({ authRequired: true });
expect(mockResponse.ok).toBeCalled();
const alertResultList = mockResponse.ok.mock.calls[0][0]?.body as AlertResultList;
expect(alertResultList.alerts.length).toEqual(20);
expect(alertResultList.total).toEqual(132);
expect(alertResultList.total).toEqual(21);
expect(alertResultList.request_page_index).toEqual(2);
expect(alertResultList.result_from_index).toEqual(40);
expect(alertResultList.request_page_size).toEqual(20);
expect(alertResultList.result_from_index).toEqual(6);
expect(alertResultList.request_page_size).toEqual(3);
});

it('should correctly validate params', async () => {
const validate = () => {
reqSchema.validate({
page_size: 'abc',
page_index: 0,
});
};
expect(validate).toThrow();
});
// TODO: add tests for track_total_hits
});
2 changes: 1 addition & 1 deletion x-pack/plugins/endpoint/server/routes/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { EndpointAppContext } from '../types';

const ALERTS_ROUTE = '/api/endpoint/alerts';

const reqSchema = schema.object({
export const reqSchema = schema.object({
page_size: schema.number({ defaultValue: 10, min: 1, max: 10000 }),
page_index: schema.number({ defaultValue: 0, min: 0 }),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { httpServerMock, loggingServiceMock } from '../../../../../../src/core/server/mocks';
import { httpServerMock, loggingServiceMock } from 'src/core/server/mocks';
import { EndpointConfigSchema } from '../../config';
import { getPagingProperties, kibanaRequestToAlertListQuery } from './alert_query_builders';

describe('test query builder', () => {
describe('test query builder request processing', () => {
it('test default query params for all alerts when no params or body is provided', async () => {
it('should execute the correct Elasticsearch query for a default request', async () => {
const mockRequest = httpServerMock.createKibanaRequest({
body: {},
});
Expand Down Expand Up @@ -39,6 +39,38 @@ describe('test query builder', () => {
index: 'my-index',
} as Record<string, any>);
});
// TODO: add tests for calculating track_total_hits
it('should adjust track_total_hits for deep pagination', async () => {
const mockRequest = httpServerMock.createKibanaRequest({
query: {
page_index: 10,
page_size: 1000,
},
});
const mockCtx = {
logFactory: loggingServiceMock.create(),
config: () => Promise.resolve(EndpointConfigSchema.validate({})),
};
const queryParams = await getPagingProperties(mockRequest, mockCtx);
const query = await kibanaRequestToAlertListQuery(queryParams, mockCtx);

expect(query).toEqual({
body: {
query: {
match_all: {},
},
sort: [
{
'@timestamp': {
order: 'desc',
},
},
],
track_total_hits: 12000,
},
from: 10000,
size: 1000,
index: 'my-index',
} as Record<string, any>);
});
});
});
Loading

0 comments on commit e6cac13

Please sign in to comment.