From 4f21b1bdf9872e9e56343fc01a4de790131a84a6 Mon Sep 17 00:00:00 2001 From: Jason Stoltzfus Date: Fri, 30 Oct 2020 12:29:47 -0400 Subject: [PATCH] Added App Search log settings routes --- .../enterprise_search/server/plugin.ts | 2 + .../routes/app_search/log_settings.test.ts | 82 +++++++++++++++++++ .../server/routes/app_search/log_settings.ts | 53 ++++++++++++ 3 files changed, 137 insertions(+) create mode 100644 x-pack/plugins/enterprise_search/server/routes/app_search/log_settings.test.ts create mode 100644 x-pack/plugins/enterprise_search/server/routes/app_search/log_settings.ts diff --git a/x-pack/plugins/enterprise_search/server/plugin.ts b/x-pack/plugins/enterprise_search/server/plugin.ts index 4b1040de52f66..27d32e9138318 100644 --- a/x-pack/plugins/enterprise_search/server/plugin.ts +++ b/x-pack/plugins/enterprise_search/server/plugin.ts @@ -41,6 +41,7 @@ import { appSearchTelemetryType } from './saved_objects/app_search/telemetry'; import { registerTelemetryUsageCollector as registerASTelemetryUsageCollector } from './collectors/app_search/telemetry'; import { registerEnginesRoute } from './routes/app_search/engines'; import { registerCredentialsRoutes } from './routes/app_search/credentials'; +import { registerLogSettingsRoutes } from './routes/app_search/log_settings'; import { workplaceSearchTelemetryType } from './saved_objects/workplace_search/telemetry'; import { registerTelemetryUsageCollector as registerWSTelemetryUsageCollector } from './collectors/workplace_search/telemetry'; @@ -128,6 +129,7 @@ export class EnterpriseSearchPlugin implements Plugin { registerConfigDataRoute(dependencies); registerEnginesRoute(dependencies); registerCredentialsRoutes(dependencies); + registerLogSettingsRoutes(dependencies); registerWSOverviewRoute(dependencies); registerWSGroupRoutes(dependencies); diff --git a/x-pack/plugins/enterprise_search/server/routes/app_search/log_settings.test.ts b/x-pack/plugins/enterprise_search/server/routes/app_search/log_settings.test.ts new file mode 100644 index 0000000000000..74ad8faec6d5f --- /dev/null +++ b/x-pack/plugins/enterprise_search/server/routes/app_search/log_settings.test.ts @@ -0,0 +1,82 @@ +/* + * 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 { registerLogSettingsRoutes } from './log_settings'; + +describe('log settings routes', () => { + describe('PUT /api/app_search/log_settings', () => { + let mockRouter: MockRouter; + + beforeEach(() => { + jest.clearAllMocks(); + mockRouter = new MockRouter({ method: 'put', payload: 'body' }); + + registerLogSettingsRoutes({ + ...mockDependencies, + router: mockRouter.router, + }); + }); + + it('creates a request to enterprise search', () => { + const mockRequest = { + body: { + analytics: true, + }, + }; + + mockRouter.callRoute(mockRequest); + + expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({ + path: '/as/log_settings', + }); + }); + + describe('validates', () => { + it('validates good data', () => { + const request = { + body: { + analytics: { enabled: true }, + api: { enabled: true }, + }, + }; + mockRouter.shouldValidate(request); + }); + + it('rejects bad data', () => { + const request = { + body: { + foo: 'bar', + }, + }; + mockRouter.shouldThrow(request); + }); + }); + }); + + describe('GET /api/app_search/log_settings', () => { + let mockRouter: MockRouter; + + beforeEach(() => { + jest.clearAllMocks(); + mockRouter = new MockRouter({ method: 'get' }); + + registerLogSettingsRoutes({ + ...mockDependencies, + router: mockRouter.router, + }); + }); + + it('creates a request to enterprise search', () => { + mockRouter.callRoute({}); + + expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({ + path: '/as/log_settings', + }); + }); + }); +}); diff --git a/x-pack/plugins/enterprise_search/server/routes/app_search/log_settings.ts b/x-pack/plugins/enterprise_search/server/routes/app_search/log_settings.ts new file mode 100644 index 0000000000000..84b8d3fa66e1d --- /dev/null +++ b/x-pack/plugins/enterprise_search/server/routes/app_search/log_settings.ts @@ -0,0 +1,53 @@ +/* + * 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 { IRouteDependencies } from '../../plugin'; + +const logSettingsSchema = schema.object({ + api: schema.maybe( + schema.object({ + enabled: schema.boolean(), + }) + ), + analytics: schema.maybe( + schema.object({ + enabled: schema.boolean(), + }) + ), +}); + +export function registerLogSettingsRoutes({ + router, + enterpriseSearchRequestHandler, +}: IRouteDependencies) { + router.get( + { + path: '/api/app_search/log_settings', + validate: false, + }, + async (context, request, response) => { + return enterpriseSearchRequestHandler.createRequest({ + path: '/as/log_settings', + })(context, request, response); + } + ); + + router.put( + { + path: '/api/app_search/log_settings', + validate: { + body: logSettingsSchema, + }, + }, + async (context, request, response) => { + return enterpriseSearchRequestHandler.createRequest({ + path: '/as/log_settings', + })(context, request, response); + } + ); +}