Skip to content

Commit

Permalink
Update server routes to account for new changes
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
cee-chen committed Jun 29, 2020
1 parent 015a6c9 commit 44cd067
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -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,
};
Original file line number Diff line number Diff line change
@@ -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';
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -37,9 +37,7 @@ describe('engine routes', () => {
registerEnginesRoute({
router: mockRouter.router,
log: mockLogger,
config: {
host: 'http://localhost:3002',
},
config: mockConfig,
});
});

Expand All @@ -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(
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 44cd067

Please sign in to comment.