Skip to content

Commit

Permalink
improve handling when navigating to the root of a non-default space
Browse files Browse the repository at this point in the history
  • Loading branch information
legrego committed Sep 16, 2019
1 parent 0a8ba3f commit 48b0091
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,73 @@ describe('onPostAuthInterceptor', () => {
);
});

it('redirects to the space selector when accessing the root of the default space', async () => {
const spaces = [
{
id: 'default',
type: 'space',
attributes: {
name: 'Default space',
_reserved: true,
},
},
{
id: 'a-space',
type: 'space',
attributes: {
name: 'a space',
},
},
];

const { response, spacesService } = await request('/', spaces);

expect(response.status).toEqual(302);
expect(response.header.location).toEqual(`/spaces/space_selector`);

expect(spacesService.scopedClient).toHaveBeenCalledWith(
expect.objectContaining({
headers: expect.objectContaining({
authorization: headers.authorization,
}),
})
);
}, 30000);

it('allows the request to continue when accessing the root of a non-default space', async () => {
const spaces = [
{
id: 'default',
type: 'space',
attributes: {
name: 'Default space',
_reserved: true,
},
},
{
id: 'a-space',
type: 'space',
attributes: {
name: 'a space',
},
},
];

const { response, spacesService } = await request('/s/a-space', spaces);

// OSS handles this redirection for us
expect(response.status).toEqual(302);
expect(response.header.location).toEqual(`/s/a-space${defaultRoute}`);

expect(spacesService.scopedClient).toHaveBeenCalledWith(
expect.objectContaining({
headers: expect.objectContaining({
authorization: headers.authorization,
}),
})
);
}, 30000);

describe('with a single available space', () => {
it('it redirects to the defaultRoute within the context of the single Space when navigating to Kibana root', async () => {
const spaces = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import { Logger, CoreSetup } from 'src/core/server';
import { Space } from '../../../common/model/space';
import { wrapError } from '../errors';
import { addSpaceIdToPath, getSpaceIdFromPath } from '../spaces_url_parser';
import { addSpaceIdToPath } from '../spaces_url_parser';
import { XPackMainPlugin } from '../../../../xpack_main/xpack_main';
import { SpacesServiceSetup } from '../../new_platform/spaces_service/spaces_service';
import { LegacyAPI } from '../../new_platform/plugin';
import { getSpaceSelectorUrl } from '../get_space_selector_url';
import { DEFAULT_SPACE_ID } from '../../../common/constants';

export interface OnPostAuthInterceptorDeps {
getLegacyAPI(): LegacyAPI;
Expand All @@ -32,7 +33,11 @@ export function initSpacesOnPostAuthRequestInterceptor({
http.registerOnPostAuth(async (request, response, toolkit) => {
const path = request.url.pathname!;

const isRequestingKibanaRoot = path === '/';
const spaceId = spacesService.getSpaceId(request);

// The root of kibana is also the root of the defaut space,
// since the default space does not have a URL Identifier (i.e., `/s/foo`).
const isRequestingKibanaRoot = path === '/' && spaceId === DEFAULT_SPACE_ID;
const isRequestingApplication = path.startsWith('/app');

const spacesClient = await spacesService.scopedClient(request);
Expand Down Expand Up @@ -71,11 +76,8 @@ export function initSpacesOnPostAuthRequestInterceptor({
// This condition should only happen after selecting a space, or when transitioning from one application to another
// e.g.: Navigating from Dashboard to Timelion
if (isRequestingApplication) {
let spaceId: string = '';
let space: Space;
try {
spaceId = getSpaceIdFromPath(http.basePath.get(request), serverBasePath);

log.debug(`Verifying access to space "${spaceId}"`);

space = await spacesClient.get(spaceId);
Expand Down

0 comments on commit 48b0091

Please sign in to comment.