Skip to content

Commit

Permalink
EMT-65: update test data and datastructure based on schema
Browse files Browse the repository at this point in the history
  • Loading branch information
nnamdifrankie committed Jan 24, 2020
1 parent 5127ff5 commit 1128056
Show file tree
Hide file tree
Showing 9 changed files with 635 additions and 388 deletions.
48 changes: 18 additions & 30 deletions x-pack/plugins/endpoint/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class EndpointAppConstants {

export interface EndpointResultList {
// the endpoint restricted by the page size
endpoints: EndpointData[];
endpoints: EndpointMetadata[];
// the total number of unique endpoints in the index
total: number;
// the page size requested
Expand All @@ -19,40 +19,28 @@ export interface EndpointResultList {
request_page_index: number;
}

export interface EndpointData {
machine_id: string;
created_at: Date;
host: {
name: string;
hostname: string;
ip: string;
mac_address: string;
os: {
name: string;
full: string;
};
export interface EndpointMetadata {
event: {
created: string;
};
endpoint: {
domain: string;
is_base_image: boolean;
active_directory_distinguished_name: string;
active_directory_hostname: string;
upgrade: {
status?: string;
updated_at?: Date;
};
isolation: {
status: boolean;
request_status?: string | boolean;
updated_at?: Date;
};
policy: {
name: string;
id: string;
};
sensor: {
persistence: boolean;
status: object;
};
agent: {
version: string;
id: string;
};
host: {
id: string;
hostname: string;
ip: string[];
mac: string[];
os: {
name: string;
full: string;
version: string;
};
};
}
16 changes: 8 additions & 8 deletions x-pack/plugins/endpoint/server/routes/endpoints.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
httpServiceMock,
loggingServiceMock,
} from '../../../../../src/core/server/mocks';
import { EndpointData, EndpointResultList } from '../../common/types';
import { EndpointMetadata, EndpointResultList } from '../../common/types';
import { SearchResponse } from 'elasticsearch';
import { registerEndpointRoutes } from './endpoints';
import { EndpointConfigSchema } from '../config';
Expand Down Expand Up @@ -49,8 +49,8 @@ describe('test endpoint route', () => {
it('test find the latest of all endpoints', async () => {
const mockRequest = httpServerMock.createKibanaRequest({});

const response: SearchResponse<EndpointData> = (data as unknown) as SearchResponse<
EndpointData
const response: SearchResponse<EndpointMetadata> = (data as unknown) as SearchResponse<
EndpointMetadata
>;
mockScopedClient.callAsCurrentUser.mockImplementationOnce(() => Promise.resolve(response));
[routeConfig, routeHandler] = routerMock.post.mock.calls.find(([{ path }]) =>
Expand All @@ -73,8 +73,8 @@ describe('test endpoint route', () => {
expect(routeConfig.options).toEqual({ authRequired: true });
expect(mockResponse.ok).toBeCalled();
const endpointResultList = mockResponse.ok.mock.calls[0][0]?.body as EndpointResultList;
expect(endpointResultList.endpoints.length).toEqual(3);
expect(endpointResultList.total).toEqual(3);
expect(endpointResultList.endpoints.length).toEqual(2);
expect(endpointResultList.total).toEqual(2);
expect(endpointResultList.request_page_index).toEqual(0);
expect(endpointResultList.request_page_size).toEqual(10);
});
Expand All @@ -93,7 +93,7 @@ describe('test endpoint route', () => {
},
});
mockScopedClient.callAsCurrentUser.mockImplementationOnce(() =>
Promise.resolve((data as unknown) as SearchResponse<EndpointData>)
Promise.resolve((data as unknown) as SearchResponse<EndpointMetadata>)
);
[routeConfig, routeHandler] = routerMock.post.mock.calls.find(([{ path }]) =>
path.startsWith('/api/endpoint/endpoints')
Expand All @@ -115,8 +115,8 @@ describe('test endpoint route', () => {
expect(routeConfig.options).toEqual({ authRequired: true });
expect(mockResponse.ok).toBeCalled();
const endpointResultList = mockResponse.ok.mock.calls[0][0]?.body as EndpointResultList;
expect(endpointResultList.endpoints.length).toEqual(3);
expect(endpointResultList.total).toEqual(3);
expect(endpointResultList.endpoints.length).toEqual(2);
expect(endpointResultList.total).toEqual(2);
expect(endpointResultList.request_page_index).toEqual(10);
expect(endpointResultList.request_page_size).toEqual(10);
});
Expand Down
8 changes: 4 additions & 4 deletions x-pack/plugins/endpoint/server/routes/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import { SearchResponse } from 'elasticsearch';
import { schema } from '@kbn/config-schema';

import { kibanaRequestToEndpointListQuery } from '../services/endpoint/endpoint_query_builders';
import { EndpointData, EndpointResultList } from '../../common/types';
import { EndpointMetadata, EndpointResultList } from '../../common/types';
import { EndpointAppContext } from '../types';

interface HitSource {
_source: EndpointData;
_source: EndpointMetadata;
}

export function registerEndpointRoutes(router: IRouter, endpointAppContext: EndpointAppContext) {
Expand Down Expand Up @@ -44,7 +44,7 @@ export function registerEndpointRoutes(router: IRouter, endpointAppContext: Endp
const response = (await context.core.elasticsearch.dataClient.callAsCurrentUser(
'search',
queryParams
)) as SearchResponse<EndpointData>;
)) as SearchResponse<EndpointMetadata>;
return res.ok({ body: mapToEndpointResultList(queryParams, response) });
} catch (err) {
return res.internalError({ body: err });
Expand All @@ -55,7 +55,7 @@ export function registerEndpointRoutes(router: IRouter, endpointAppContext: Endp

function mapToEndpointResultList(
queryParams: Record<string, any>,
searchResponse: SearchResponse<EndpointData>
searchResponse: SearchResponse<EndpointMetadata>
): EndpointResultList {
const totalNumberOfEndpoints = searchResponse?.aggregations?.total?.value || 0;
if (searchResponse.hits.hits.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ describe('test query builder', () => {
match_all: {},
},
collapse: {
field: 'machine_id',
field: 'host.id.keyword',
inner_hits: {
name: 'most_recent',
size: 1,
sort: [{ created_at: 'desc' }],
sort: [{ 'event.created': 'desc' }],
},
},
aggs: {
total: {
cardinality: {
field: 'machine_id',
field: 'host.id.keyword',
},
},
},
sort: [
{
created_at: {
'event.created': {
order: 'desc',
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@ export const kibanaRequestToEndpointListQuery = async (
match_all: {},
},
collapse: {
field: 'machine_id',
field: 'host.id.keyword',
inner_hits: {
name: 'most_recent',
size: 1,
sort: [{ created_at: 'desc' }],
sort: [{ 'event.created': 'desc' }],
},
},
aggs: {
total: {
cardinality: {
field: 'machine_id',
field: 'host.id.keyword',
},
},
},
sort: [
{
created_at: {
'event.created': {
order: 'desc',
},
},
Expand Down
Loading

0 comments on commit 1128056

Please sign in to comment.