Skip to content

Commit

Permalink
add Endpoint Details tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pzl committed Jan 29, 2020
1 parent 332a3c6 commit 93a0d4b
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 3 deletions.
78 changes: 78 additions & 0 deletions x-pack/plugins/endpoint/server/routes/endpoints.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,82 @@ describe('test endpoint route', () => {
expect(endpointResultList.request_page_index).toEqual(10);
expect(endpointResultList.request_page_size).toEqual(10);
});

describe('Endpoint Details route', () => {
it('should return 404 on no results', async () => {
const mockRequest = httpServerMock.createKibanaRequest({ params: { id: 'BADID' } });
mockScopedClient.callAsCurrentUser.mockImplementationOnce(() =>
Promise.resolve({
took: 3,
timed_out: false,
_shards: {
total: 1,
successful: 1,
skipped: 0,
failed: 0,
},
hits: {
total: {
value: 9,
relation: 'eq',
},
max_score: null,
hits: [],
},
})
);
[routeConfig, routeHandler] = routerMock.get.mock.calls.find(([{ path }]) =>
path.startsWith('/api/endpoint/endpoints')
)!;

await routeHandler(
({
core: {
elasticsearch: {
dataClient: mockScopedClient,
},
},
} as unknown) as RequestHandlerContext,
mockRequest,
mockResponse
);

expect(mockScopedClient.callAsCurrentUser).toBeCalled();
expect(routeConfig.options).toEqual({ authRequired: true });
expect(mockResponse.notFound).toBeCalled();
const message = mockResponse.notFound.mock.calls[0][0]?.body;
expect(message).toEqual('Endpoint Not Found');
});

it('should return a single endpoint', async () => {
const mockRequest = httpServerMock.createKibanaRequest({
params: { id: data.hits.hits[0]._id },
});
const response: SearchResponse<EndpointMetadata> = (data as unknown) as SearchResponse<
EndpointMetadata
>;
mockScopedClient.callAsCurrentUser.mockImplementationOnce(() => Promise.resolve(response));
[routeConfig, routeHandler] = routerMock.get.mock.calls.find(([{ path }]) =>
path.startsWith('/api/endpoint/endpoints')
)!;

await routeHandler(
({
core: {
elasticsearch: {
dataClient: mockScopedClient,
},
},
} as unknown) as RequestHandlerContext,
mockRequest,
mockResponse
);

expect(mockScopedClient.callAsCurrentUser).toBeCalled();
expect(routeConfig.options).toEqual({ authRequired: true });
expect(mockResponse.ok).toBeCalled();
const result = mockResponse.ok.mock.calls[0][0]?.body as EndpointMetadata;
expect(result).toHaveProperty('endpoint');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
*/
import { httpServerMock, loggingServiceMock } from '../../../../../../src/core/server/mocks';
import { EndpointConfigSchema } from '../../config';
import { kibanaRequestToEndpointListQuery } from './endpoint_query_builders';
import {
kibanaRequestToEndpointListQuery,
kibanaRequestToEndpointFetchQuery,
} from './endpoint_query_builders';

describe('test query builder', () => {
describe('test query builder request processing', () => {
describe('query builder', () => {
describe('EndpointListQuery', () => {
it('test default query params for all endpoints when no params or body is provided', async () => {
const mockRequest = httpServerMock.createKibanaRequest({
body: {},
Expand Down Expand Up @@ -51,4 +54,27 @@ describe('test query builder', () => {
} as Record<string, any>);
});
});

describe('EndpointFetchQuery', () => {
it('searches for the correct ID', () => {
const mockID = 'AABBCCDD-0011-2233-AA44-DEADBEEF8899';
const mockRequest = httpServerMock.createKibanaRequest({
params: {
id: mockID,
},
});
const query = kibanaRequestToEndpointFetchQuery(mockRequest, {
logFactory: loggingServiceMock.create(),
config: () => Promise.resolve(EndpointConfigSchema.validate({})),
});
expect(query).toEqual({
body: {
query: { match: { machine_id: mockID } },
sort: [{ created_at: { order: 'desc' } }],
size: 1,
},
index: 'endpoint-agent*',
});
});
});
});

0 comments on commit 93a0d4b

Please sign in to comment.