Skip to content

Commit

Permalink
Added server routes
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonStoltz committed Jan 26, 2021
1 parent 3121e47 commit 9d22cbd
Show file tree
Hide file tree
Showing 4 changed files with 313 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,14 @@ export class MockRouter {
public validateRoute = (request: MockRouterRequest) => {
if (!this.payload) throw new Error('Cannot validate wihout a payload type specified.');

const [config] = this.router[this.method].mock.calls[0];
// @ts-ignore
const configForMethodAndPath = this.router[this.method].mock.calls.find(
// @ts-ignore
([config]) => config.path === this.path
);
if (!configForMethodAndPath)
throw new Error(`No route registered for ${this.method} & ${this.path}`);
const [config] = configForMethodAndPath;
const validate = config.validate as RouteValidatorConfig<{}, {}, {}>;

const payloadValidation = validate[this.payload] as { validate(request: KibanaRequest): void };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { registerCredentialsRoutes } from './credentials';
import { registerSettingsRoutes } from './settings';
import { registerAnalyticsRoutes } from './analytics';
import { registerDocumentsRoutes, registerDocumentRoutes } from './documents';
import { registerSearchSettingsRoutes } from './search_settings';

export const registerAppSearchRoutes = (dependencies: RouteDependencies) => {
registerEnginesRoutes(dependencies);
Expand All @@ -19,4 +20,5 @@ export const registerAppSearchRoutes = (dependencies: RouteDependencies) => {
registerAnalyticsRoutes(dependencies);
registerDocumentsRoutes(dependencies);
registerDocumentRoutes(dependencies);
registerSearchSettingsRoutes(dependencies);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { MockRouter, mockRequestHandler, mockDependencies } from '../../__mocks__';

import { registerSearchSettingsRoutes } from './search_settings';

describe('search settings routes', () => {
const boosts = { foo: [{}] };
const resultFields = { foo: {} };
const searchFields = { foo: {} };
const searchSettings = {
boosts,
result_fields: resultFields,
search_fields: searchFields,
};

beforeEach(() => {
jest.clearAllMocks();
});

describe('GET /api/app_search/engines/{name}/search_settings/details', () => {
let mockRouter: MockRouter;

beforeEach(() => {
jest.clearAllMocks();
mockRouter = new MockRouter({
method: 'get',
path: '/api/app_search/engines/{engineName}/search_settings/details',
});

registerSearchSettingsRoutes({
...mockDependencies,
router: mockRouter.router,
});
});

it('creates a request to enterprise search', () => {
mockRouter.callRoute({
params: { engineName: 'some-engine' },
});

expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({
path: '/as/engines/some-engine/search_settings/details',
});
});
});

describe('PUT /api/app_search/engines/{name}/search_settings', () => {
let mockRouter: MockRouter;

beforeEach(() => {
jest.clearAllMocks();
mockRouter = new MockRouter({
method: 'put',
path: '/api/app_search/engines/{engineName}/search_settings',
payload: 'body',
});

registerSearchSettingsRoutes({
...mockDependencies,
router: mockRouter.router,
});
});

it('creates a request to enterprise search', () => {
mockRouter.callRoute({
params: { engineName: 'some-engine' },
body: searchSettings,
});

expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({
path: '/as/engines/some-engine/search_settings',
});
});

describe('validates', () => {
it('correctly', () => {
const request = { body: searchSettings };
mockRouter.shouldValidate(request);
});

it('missing required fields', () => {
const request = { body: {} };
mockRouter.shouldThrow(request);
});
});
});

describe('POST /api/app_search/engines/{name}/search_settings/reset', () => {
let mockRouter: MockRouter;

beforeEach(() => {
jest.clearAllMocks();
mockRouter = new MockRouter({
method: 'post',
path: '/api/app_search/engines/{engineName}/search_settings/reset',
});

registerSearchSettingsRoutes({
...mockDependencies,
router: mockRouter.router,
});
});

it('creates a request to enterprise search', () => {
mockRouter.callRoute({
params: { engineName: 'some-engine' },
});

expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({
path: '/as/engines/some-engine/search_settings/reset',
});
});
});

describe('POST /api/app_search/engines/{name}/search_settings_search', () => {
let mockRouter: MockRouter;

it('creates a request to enterprise search', () => {
mockRouter = new MockRouter({
method: 'post',
path: '/api/app_search/engines/{engineName}/search_settings_search',
payload: 'body',
});

registerSearchSettingsRoutes({
...mockDependencies,
router: mockRouter.router,
});

mockRouter.callRoute({
params: { engineName: 'some-engine' },
body: searchSettings,
});

expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({
path: '/as/engines/some-engine/search_settings_search',
});
});

describe('validates body', () => {
beforeEach(() => {
mockRouter = new MockRouter({
method: 'post',
path: '/api/app_search/engines/{engineName}/search_settings_search',
payload: 'body',
});

registerSearchSettingsRoutes({
...mockDependencies,
router: mockRouter.router,
});
});

it('correctly', () => {
const request = {
body: {
boosts,
search_fields: searchFields,
},
};
mockRouter.shouldValidate(request);
});

it('missing required fields', () => {
const request = { body: {} };
mockRouter.shouldThrow(request);
});
});

describe('validates query', () => {
it('correctly', () => {
mockRouter = new MockRouter({
method: 'post',
path: '/api/app_search/engines/{engineName}/search_settings_search',
payload: 'query',
});

registerSearchSettingsRoutes({
...mockDependencies,
router: mockRouter.router,
});

const request = {
query: {
query: 'foo',
},
};
mockRouter.shouldValidate(request);
});

it('missing required fields', () => {
const request = { query: {} };
mockRouter.shouldThrow(request);
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { schema } from '@kbn/config-schema';

import { RouteDependencies } from '../../plugin';

// We only do a very light type check here, and allow unknowns, because the request is validated
// on the ent-search server, so it would be redundant to check it here as well.
const boosts = schema.recordOf(
schema.string(),
schema.arrayOf(schema.object({}, { unknowns: 'allow' }))
);
const resultFields = schema.recordOf(schema.string(), schema.object({}, { unknowns: 'allow' }));
const searchFields = schema.recordOf(schema.string(), schema.object({}, { unknowns: 'allow' }));

const searchSettingsSchema = schema.object({
boosts,
result_fields: resultFields,
search_fields: searchFields,
});

export function registerSearchSettingsRoutes({
router,
enterpriseSearchRequestHandler,
}: RouteDependencies) {
router.get(
{
path: '/api/app_search/engines/{engineName}/search_settings/details',
validate: {
params: schema.object({
engineName: schema.string(),
}),
},
},
async (context, request, response) => {
return enterpriseSearchRequestHandler.createRequest({
path: `/as/engines/${request.params.engineName}/search_settings/details`,
})(context, request, response);
}
);

router.post(
{
path: '/api/app_search/engines/{engineName}/search_settings/reset',
validate: {
params: schema.object({
engineName: schema.string(),
}),
},
},
async (context, request, response) => {
return enterpriseSearchRequestHandler.createRequest({
path: `/as/engines/${request.params.engineName}/search_settings/reset`,
})(context, request, response);
}
);

router.put(
{
path: '/api/app_search/engines/{engineName}/search_settings',
validate: {
params: schema.object({
engineName: schema.string(),
}),
body: searchSettingsSchema,
},
},
async (context, request, response) => {
return enterpriseSearchRequestHandler.createRequest({
path: `/as/engines/${request.params.engineName}/search_settings`,
})(context, request, response);
}
);

router.post(
{
path: '/api/app_search/engines/{engineName}/search_settings_search',
validate: {
params: schema.object({
engineName: schema.string(),
}),
body: schema.object({
boosts,
search_fields: searchFields,
}),
query: schema.object({
query: schema.string(),
}),
},
},
async (context, request, response) => {
return enterpriseSearchRequestHandler.createRequest({
path: `/as/engines/${request.params.engineName}/search_settings_search`,
})(context, request, response);
}
);
}

0 comments on commit 9d22cbd

Please sign in to comment.