Skip to content

Commit

Permalink
[8.x] [Spaces] Filtering out features that do not have space scope (#…
Browse files Browse the repository at this point in the history
…194157) (#194212)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[Spaces] Filtering out features that do not have space scope
(#194157)](#194157)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Elena
Shostak","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-09-26T18:44:11Z","message":"[Spaces]
Filtering out features that do not have space scope (#194157)\n\n##
Summary\r\n\r\nFiltering out features that do not have space scope
in\r\n`create_space_page`\r\n\r\n\r\n### Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n\r\n__Fixes:
https://github.com/elastic/kibana/issues/194134__\r\n\r\n## Release
Note\r\nFiltering out features that do not have space scope
in\r\n`create_space_page`","sha":"f8416b892744ced1864404670a51e501237446c3","branchLabelMapping":{"^v9.0.0$":"main","^v8.16.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Team:Security","Feature:Security/Spaces","v9.0.0","backport:prev-minor"],"title":"[Spaces]
Filtering out features that do not have space
scope","number":194157,"url":"https://github.com/elastic/kibana/pull/194157","mergeCommit":{"message":"[Spaces]
Filtering out features that do not have space scope (#194157)\n\n##
Summary\r\n\r\nFiltering out features that do not have space scope
in\r\n`create_space_page`\r\n\r\n\r\n### Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n\r\n__Fixes:
https://github.com/elastic/kibana/issues/194134__\r\n\r\n## Release
Note\r\nFiltering out features that do not have space scope
in\r\n`create_space_page`","sha":"f8416b892744ced1864404670a51e501237446c3"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/194157","number":194157,"mergeCommit":{"message":"[Spaces]
Filtering out features that do not have space scope (#194157)\n\n##
Summary\r\n\r\nFiltering out features that do not have space scope
in\r\n`create_space_page`\r\n\r\n\r\n### Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n\r\n__Fixes:
https://github.com/elastic/kibana/issues/194134__\r\n\r\n## Release
Note\r\nFiltering out features that do not have space scope
in\r\n`create_space_page`","sha":"f8416b892744ced1864404670a51e501237446c3"}}]}]
BACKPORT-->

Co-authored-by: Elena Shostak <[email protected]>
  • Loading branch information
kibanamachine and elena-shostak authored Sep 26, 2024
1 parent fea2be2 commit c51859c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { act } from 'react-dom/test-utils';

import { DEFAULT_APP_CATEGORIES } from '@kbn/core/public';
import { notificationServiceMock, scopedHistoryMock } from '@kbn/core/public/mocks';
import { KibanaFeatureScope } from '@kbn/features-plugin/common';
import { KibanaFeature } from '@kbn/features-plugin/public';
import { featuresPluginMock } from '@kbn/features-plugin/public/mocks';
import { findTestSubject, mountWithIntl } from '@kbn/test-jest-helpers';
Expand Down Expand Up @@ -48,6 +49,15 @@ featuresStart.getFeatures.mockResolvedValue([
app: [],
category: DEFAULT_APP_CATEGORIES.kibana,
privileges: null,
scope: [KibanaFeatureScope.Spaces, KibanaFeatureScope.Security],
}),
new KibanaFeature({
id: 'feature-2',
name: 'feature 2',
app: [],
category: DEFAULT_APP_CATEGORIES.kibana,
privileges: null,
scope: [KibanaFeatureScope.Security],
}),
]);

Expand Down Expand Up @@ -641,6 +651,54 @@ describe('ManageSpacePage', () => {

expect(spacesManager.updateSpace).toHaveBeenCalledTimes(1);
});

it('shows only features with space scope', async () => {
const spacesManager = spacesManagerMock.create();
spacesManager.getSpace = jest.fn().mockResolvedValue({
id: 'my-space',
name: 'Existing Space',
description: 'hey an existing space',
color: '#aabbcc',
initials: 'AB',
disabledFeatures: [],
});
spacesManager.getActiveSpace = jest.fn().mockResolvedValue(space);

const wrapper = mountWithIntl(
<CreateSpacePage
spaceId={'my-space'}
spacesManager={spacesManager as unknown as SpacesManager}
getFeatures={featuresStart.getFeatures}
notifications={notificationServiceMock.createStartContract()}
history={history}
capabilities={{
navLinks: {},
management: {},
catalogue: {},
spaces: { manage: true },
}}
eventTracker={eventTracker}
allowFeatureVisibility
allowSolutionVisibility
/>
);

await waitFor(() => {
wrapper.update();
expect(spacesManager.getSpace).toHaveBeenCalledWith('my-space');
});

expect(wrapper.state('features')).toEqual([
new KibanaFeature({
id: 'feature-1',
name: 'feature 1',
app: [],
category: DEFAULT_APP_CATEGORIES.kibana,
privileges: null,
scope: [KibanaFeatureScope.Spaces, KibanaFeatureScope.Security],
}),
]);
});
});

function updateSpace(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import React, { Component } from 'react';

import type { Capabilities, NotificationsStart, ScopedHistory } from '@kbn/core/public';
import { SectionLoading } from '@kbn/es-ui-shared-plugin/public';
import { KibanaFeatureScope } from '@kbn/features-plugin/common';
import type { FeaturesPluginStart, KibanaFeature } from '@kbn/features-plugin/public';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';
Expand Down Expand Up @@ -91,6 +92,10 @@ export class CreateSpacePage extends Component<Props, State> {
};
}

private filterSpaceFeatures = (features: KibanaFeature[]) => {
return features.filter((feature) => feature.scope?.includes(KibanaFeatureScope.Spaces));
};

public async componentDidMount() {
if (!this.props.capabilities.spaces.manage) {
return;
Expand All @@ -103,7 +108,8 @@ export class CreateSpacePage extends Component<Props, State> {
await this.loadSpace(spaceId, getFeatures());
} else {
const features = await getFeatures();
this.setState({ isLoading: false, features });

this.setState({ isLoading: false, features: this.filterSpaceFeatures(features) });
}
} catch (e) {
notifications.toasts.addError(e, {
Expand Down Expand Up @@ -410,6 +416,7 @@ export class CreateSpacePage extends Component<Props, State> {
spacesManager.getSpace(spaceId),
featuresPromise,
]);

if (space) {
if (onLoadSpace) {
onLoadSpace(space);
Expand All @@ -426,7 +433,7 @@ export class CreateSpacePage extends Component<Props, State> {
!!space.initials && getSpaceInitials({ name: space.name }) !== space.initials,
customAvatarColor: !!space.color && getSpaceColor({ name: space.name }) !== space.color,
},
features,
features: this.filterSpaceFeatures(features),
originalSpace: space,
isLoading: false,
});
Expand Down

0 comments on commit c51859c

Please sign in to comment.