kibana/x-pack/plugins/spaces/server/lib/toggle_ui_capabilities.test.ts
Larry Gregory 9c2fa892ca
[7.x] Spaces - Client NP Migration, Phase 1 (#40856) (#53113)
* Spaces - Client NP Migration, Phase 1 (#40856)

* shimming NP for spaces client-side plugin

* refresh active space in nav control when updated

* fix advanced settings screen

* allow npStart from unauthed routes

* use NP for deriving space management url

* remove security's usage of SpacesManager

* remove usages of ui/capabilities

* fix tests

* implement NP plugin interface

* remove hack in favor of convention in migration guide

* shim feature catalogue registration

* streamline nav control, and handle async loading more gracefully

* adding opaqueId

* fixes from merge

* fix merge from master

* fixing merge from master

* move _active_space route to NP

* moving to the NP feature catalogue registry

* moving setup to setup phase

* optimizing active space retrieval

* reverting test isolation change

* Apply suggestions from code review

Co-Authored-By: Aleh Zasypkin <aleh.zasypkin@gmail.com>

* removing unnecessary PluginInitializerContext

* updating advanced settings subtitle

* using NP anonymousPaths service

* additional nav_control_popover cleanup

* additional cleanup

* testing out onActiveSpaceChange$ property

* make the linter happy

* make the type checker happy

* fixing types

* fix merge from master

* spaces LP init should run on all pages, not just the kibana app

* address nits

* fix infra/logs, and the spaces disabled scenario

* fix typescript errors

* revert changes to infra plugin

* reintroducing activeSpace injected var for legacy plugins

* fixing react deprecation warning and unhandled promise rejection

* restore activeSpace default var

* spaces does not need to check its own enabled status

* fix from merge

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>

* fix backport merge
2019-12-16 12:13:05 -05:00

169 lines
4.2 KiB
TypeScript

/*
* 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 { Feature } from '../../../../plugins/features/server';
import { Space } from '../../common/model/space';
import { toggleUICapabilities } from './toggle_ui_capabilities';
import { Capabilities } from 'src/core/public';
const features: Feature[] = [
{
id: 'feature_1',
name: 'Feature 1',
app: [],
privileges: {},
},
{
id: 'feature_2',
name: 'Feature 2',
navLinkId: 'feature2',
app: [],
catalogue: ['feature2Entry'],
management: {
kibana: ['somethingElse'],
},
privileges: {
all: {
app: [],
ui: [],
savedObject: {
all: [],
read: [],
},
},
},
},
{
id: 'feature_3',
name: 'Feature 3',
navLinkId: 'feature3',
app: [],
catalogue: ['feature3Entry'],
management: {
kibana: ['indices'],
},
privileges: {
all: {
app: [],
ui: [],
savedObject: {
all: [],
read: [],
},
},
},
},
];
const buildCapabilities = () =>
Object.freeze({
navLinks: {
feature1: true,
feature2: true,
feature3: true,
unknownFeature: true,
},
catalogue: {
discover: true,
visualize: false,
},
management: {
kibana: {
settings: false,
indices: true,
somethingElse: true,
},
},
feature_1: {
foo: true,
bar: true,
},
feature_2: {
foo: true,
bar: true,
},
feature_3: {
foo: true,
bar: true,
},
}) as Capabilities;
describe('toggleUiCapabilities', () => {
it('does not toggle capabilities when the space has no disabled features', () => {
const space: Space = {
id: 'space',
name: '',
disabledFeatures: [],
};
const capabilities = buildCapabilities();
const result = toggleUICapabilities(features, capabilities, space);
expect(result).toEqual(buildCapabilities());
});
it('ignores unknown disabledFeatures', () => {
const space: Space = {
id: 'space',
name: '',
disabledFeatures: ['i-do-not-exist'],
};
const capabilities = buildCapabilities();
const result = toggleUICapabilities(features, capabilities, space);
expect(result).toEqual(buildCapabilities());
});
it('disables the corresponding navLink, catalogue, management sections, and all capability flags for disabled features', () => {
const space: Space = {
id: 'space',
name: '',
disabledFeatures: ['feature_2'],
};
const capabilities = buildCapabilities();
const result = toggleUICapabilities(features, capabilities, space);
const expectedCapabilities = buildCapabilities();
expectedCapabilities.navLinks.feature2 = false;
expectedCapabilities.catalogue.feature2Entry = false;
expectedCapabilities.management.kibana.somethingElse = false;
expectedCapabilities.feature_2.bar = false;
expectedCapabilities.feature_2.foo = false;
expect(result).toEqual(expectedCapabilities);
});
it('can disable everything', () => {
const space: Space = {
id: 'space',
name: '',
disabledFeatures: ['feature_1', 'feature_2', 'feature_3'],
};
const capabilities = buildCapabilities();
const result = toggleUICapabilities(features, capabilities, space);
const expectedCapabilities = buildCapabilities();
expectedCapabilities.feature_1.bar = false;
expectedCapabilities.feature_1.foo = false;
expectedCapabilities.navLinks.feature2 = false;
expectedCapabilities.catalogue.feature2Entry = false;
expectedCapabilities.management.kibana.somethingElse = false;
expectedCapabilities.feature_2.bar = false;
expectedCapabilities.feature_2.foo = false;
expectedCapabilities.navLinks.feature3 = false;
expectedCapabilities.catalogue.feature3Entry = false;
expectedCapabilities.management.kibana.indices = false;
expectedCapabilities.feature_3.bar = false;
expectedCapabilities.feature_3.foo = false;
expect(result).toEqual(expectedCapabilities);
});
});