Skip to content

Commit

Permalink
supply space id to tutorial context (#22760)
Browse files Browse the repository at this point in the history
  • Loading branch information
legrego authored Sep 6, 2018
1 parent 896a89c commit 125cc36
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 4 deletions.
5 changes: 5 additions & 0 deletions x-pack/plugins/spaces/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { createDefaultSpace } from './server/lib/create_default_space';
import { createSpacesService } from './server/lib/create_spaces_service';
import { getActiveSpace } from './server/lib/get_active_space';
import { getSpacesUsageCollector } from './server/lib/get_spaces_usage_collector';
import { createSpacesTutorialContextFactory } from './server/lib/spaces_tutorial_context_factory';
import { wrapError } from './server/lib/errors';
import mappings from './mappings.json';
import { spacesSavedObjectsClientWrapperFactory } from './server/lib/saved_objects_client/saved_objects_client_wrapper_factory';
Expand Down Expand Up @@ -94,6 +95,10 @@ export const spaces = (kibana) => new kibana.Plugin({
spacesSavedObjectsClientWrapperFactory(spacesService)
);

server.addScopedTutorialContextFactory(
createSpacesTutorialContextFactory(spacesService)
);

initPrivateApis(server);
initPublicSpacesApi(server);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@

import { getSpaceIdFromPath } from './spaces_url_parser';

export function createSpacesService(server) {
export interface SpacesService {
getSpaceId: (req: any) => string;
}

export function createSpacesService(server: any): SpacesService {
const serverBasePath = server.config().get('server.basePath');

const contextCache = new WeakMap();

function getSpaceId(request) {
function getSpaceId(request: any) {
if (!contextCache.has(request)) {
populateCache(request);
}
Expand All @@ -21,11 +24,11 @@ export function createSpacesService(server) {
return spaceId;
}

function populateCache(request) {
function populateCache(request: any) {
const spaceId = getSpaceIdFromPath(request.getBasePath(), serverBasePath);

contextCache.set(request, {
spaceId
spaceId,
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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 { DEFAULT_SPACE_ID } from '../../common/constants';
import { createSpacesService } from './create_spaces_service';
import { createSpacesTutorialContextFactory } from './spaces_tutorial_context_factory';

const server = {
config: () => {
return {
get: (key: string) => {
if (key === 'server.basePath') {
return '/foo';
}
throw new Error('unexpected key ' + key);
},
};
},
};

describe('createSpacesTutorialContextFactory', () => {
it('should create a valid context factory', () => {
const spacesService = createSpacesService(server);
expect(typeof createSpacesTutorialContextFactory(spacesService)).toEqual('function');
});

it('should create context with the current space id for space my-space-id', () => {
const spacesService = createSpacesService(server);
const contextFactory = createSpacesTutorialContextFactory(spacesService);

const request = {
getBasePath: () => '/foo/s/my-space-id',
};

expect(contextFactory(request)).toEqual({
spaceId: 'my-space-id',
});
});

it('should create context with the current space id for the default space', () => {
const spacesService = createSpacesService(server);
const contextFactory = createSpacesTutorialContextFactory(spacesService);

const request = {
getBasePath: () => '/foo',
};

expect(contextFactory(request)).toEqual({
spaceId: DEFAULT_SPACE_ID,
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* 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 { SpacesService } from './create_spaces_service';

export function createSpacesTutorialContextFactory(spacesService: SpacesService) {
return function spacesTutorialContextFactory(request: any) {
return {
spaceId: spacesService.getSpaceId(request),
};
};
}

0 comments on commit 125cc36

Please sign in to comment.