From 44cd067c856e7eb8ac1d3d1ace4925c43032ec46 Mon Sep 17 00:00:00 2001 From: Constance Chen Date: Thu, 25 Jun 2020 13:42:31 -0700 Subject: [PATCH] Update server routes to account for new changes - Remove login redirect catch from routes, since the access helper should now handle that for most users by disabling the plugin (superusers will see a generic cannot connect/error screen) - Refactor out new config values to a shared mock --- .../server/routes/__mocks__/config.mock.ts | 11 ++++++ .../server/routes/__mocks__/index.ts | 8 +++++ .../server/routes/app_search/engines.test.ts | 36 ++----------------- .../server/routes/app_search/engines.ts | 6 ---- 4 files changed, 21 insertions(+), 40 deletions(-) create mode 100644 x-pack/plugins/enterprise_search/server/routes/__mocks__/config.mock.ts create mode 100644 x-pack/plugins/enterprise_search/server/routes/__mocks__/index.ts diff --git a/x-pack/plugins/enterprise_search/server/routes/__mocks__/config.mock.ts b/x-pack/plugins/enterprise_search/server/routes/__mocks__/config.mock.ts new file mode 100644 index 00000000000000..e4d560a2be63cc --- /dev/null +++ b/x-pack/plugins/enterprise_search/server/routes/__mocks__/config.mock.ts @@ -0,0 +1,11 @@ +/* + * 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. + */ + +export const mockConfig = { + enabled: true, + host: 'http://localhost:3002', + accessCheckTimeout: 200, +}; diff --git a/x-pack/plugins/enterprise_search/server/routes/__mocks__/index.ts b/x-pack/plugins/enterprise_search/server/routes/__mocks__/index.ts new file mode 100644 index 00000000000000..8545d65b6f78d9 --- /dev/null +++ b/x-pack/plugins/enterprise_search/server/routes/__mocks__/index.ts @@ -0,0 +1,8 @@ +/* + * 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. + */ + +export { MockRouter } from './router.mock'; +export { mockConfig } from './config.mock'; diff --git a/x-pack/plugins/enterprise_search/server/routes/app_search/engines.test.ts b/x-pack/plugins/enterprise_search/server/routes/app_search/engines.test.ts index c45514ae537fe9..77289810049f5f 100644 --- a/x-pack/plugins/enterprise_search/server/routes/app_search/engines.test.ts +++ b/x-pack/plugins/enterprise_search/server/routes/app_search/engines.test.ts @@ -5,7 +5,7 @@ */ import { loggingSystemMock } from 'src/core/server/mocks'; -import { MockRouter } from '../__mocks__/router.mock'; +import { MockRouter, mockConfig } from '../__mocks__'; import { registerEnginesRoute } from './engines'; @@ -37,9 +37,7 @@ describe('engine routes', () => { registerEnginesRoute({ router: mockRouter.router, log: mockLogger, - config: { - host: 'http://localhost:3002', - }, + config: mockConfig, }); }); @@ -64,24 +62,6 @@ describe('engine routes', () => { }); }); - describe('when the underlying App Search API redirects to /login', () => { - beforeEach(() => { - AppSearchAPI.shouldBeCalledWith( - `http://localhost:3002/as/engines/collection?type=indexed&page%5Bcurrent%5D=1&page%5Bsize%5D=10`, - { headers: { Authorization: AUTH_HEADER } } - ).andReturnRedirect(); - }); - - it('should return 403 with a message', async () => { - await mockRouter.callRoute(mockRequest); - - expect(mockRouter.response.forbidden).toHaveBeenCalledWith({ - body: 'no-as-account', - }); - expect(mockLogger.info).toHaveBeenCalledWith('No corresponding App Search account found'); - }); - }); - describe('when the App Search URL is invalid', () => { beforeEach(() => { AppSearchAPI.shouldBeCalledWith( @@ -152,18 +132,6 @@ describe('engine routes', () => { const AppSearchAPI = { shouldBeCalledWith(expectedUrl: string, expectedParams: object) { return { - andReturnRedirect() { - fetchMock.mockImplementation((url: string, params: object) => { - expect(url).toEqual(expectedUrl); - expect(params).toEqual(expectedParams); - - return Promise.resolve( - new Response('{}', { - url: '/login', - }) - ); - }); - }, andReturn(response: object) { fetchMock.mockImplementation((url: string, params: object) => { expect(url).toEqual(expectedUrl); diff --git a/x-pack/plugins/enterprise_search/server/routes/app_search/engines.ts b/x-pack/plugins/enterprise_search/server/routes/app_search/engines.ts index ffc7a0228454ff..b86555ca54a166 100644 --- a/x-pack/plugins/enterprise_search/server/routes/app_search/engines.ts +++ b/x-pack/plugins/enterprise_search/server/routes/app_search/engines.ts @@ -38,12 +38,6 @@ export function registerEnginesRoute({ router, config, log }: IRouteDependencies headers: { Authorization: request.headers.authorization as string }, }); - if (enginesResponse.url.endsWith('/login')) { - log.info('No corresponding App Search account found'); - // Note: Can't use response.unauthorized, Kibana will auto-log out the user - return response.forbidden({ body: 'no-as-account' }); - } - const engines = await enginesResponse.json(); const hasValidData = Array.isArray(engines?.results) && typeof engines?.meta?.page?.total_results === 'number';