diff options
3 files changed, 61 insertions, 13 deletions
diff --git a/server/sonar-web/src/main/js/app/components/nav/global/GlobalNavPlus.tsx b/server/sonar-web/src/main/js/app/components/nav/global/GlobalNavPlus.tsx index fca27d93214..0b58cdc9491 100644 --- a/server/sonar-web/src/main/js/app/components/nav/global/GlobalNavPlus.tsx +++ b/server/sonar-web/src/main/js/app/components/nav/global/GlobalNavPlus.tsx @@ -33,7 +33,7 @@ import { ComponentQualifier } from '../../../../types/component'; import GlobalNavPlusMenu from './GlobalNavPlusMenu'; interface Props { - appState: Pick<T.AppState, 'qualifiers'>; + appState: Pick<T.AppState, 'branchesEnabled' | 'qualifiers'>; currentUser: T.LoggedInUser; router: Router; } @@ -56,9 +56,7 @@ export class GlobalNavPlus extends React.PureComponent<Props, State> { componentDidMount() { this.mounted = true; - if (hasGlobalPermission(this.props.currentUser, 'provisioning')) { - this.fetchAlmBindings(); - } + this.fetchAlmBindings(); if (this.props.appState.qualifiers.includes('VW')) { getExtensionStart('governance/console').then( @@ -81,6 +79,17 @@ export class GlobalNavPlus extends React.PureComponent<Props, State> { }; fetchAlmBindings = async () => { + const { + appState: { branchesEnabled }, + currentUser + } = this.props; + const canCreateProject = hasGlobalPermission(currentUser, 'provisioning'); + + // getAlmSettings requires branchesEnabled + if (!canCreateProject || !branchesEnabled) { + return; + } + const almSettings = await getAlmSettings(); // Import is only available if exactly one binding is configured @@ -134,7 +143,7 @@ export class GlobalNavPlus extends React.PureComponent<Props, State> { return ( <> <Dropdown - onOpen={canCreateProject ? this.fetchAlmBindings : undefined} + onOpen={this.fetchAlmBindings} overlay={ <GlobalNavPlusMenu canCreateApplication={canCreateApplication} diff --git a/server/sonar-web/src/main/js/app/components/nav/global/__tests__/GlobalNavPlus-test.tsx b/server/sonar-web/src/main/js/app/components/nav/global/__tests__/GlobalNavPlus-test.tsx index 6b1990beef8..8d74d544510 100644 --- a/server/sonar-web/src/main/js/app/components/nav/global/__tests__/GlobalNavPlus-test.tsx +++ b/server/sonar-web/src/main/js/app/components/nav/global/__tests__/GlobalNavPlus-test.tsx @@ -51,20 +51,27 @@ beforeEach(() => { }); it('should render correctly when no rights', async () => { - const wrapper = shallowRender(); + const wrapper = shallowRender([], {}); expect(wrapper.type()).toBeNull(); await waitAndUpdate(wrapper); expect(getAlmSettings).not.toBeCalled(); }); +it('should render correctly if branches not enabled', async () => { + const wrapper = shallowRender([PROJECT_CREATION_RIGHT], { branchesEnabled: false }); + await waitAndUpdate(wrapper); + expect(wrapper).toMatchSnapshot(); + expect(getAlmSettings).not.toBeCalled(); +}); + it('should render correctly', () => { expect( - shallowRender([APP_CREATION_RIGHT, PORTFOLIO_CREATION_RIGHT, PROJECT_CREATION_RIGHT]) + shallowRender([APP_CREATION_RIGHT, PORTFOLIO_CREATION_RIGHT, PROJECT_CREATION_RIGHT], {}) ).toMatchSnapshot('no governance'); const wrapper = shallowRender( [APP_CREATION_RIGHT, PORTFOLIO_CREATION_RIGHT, PROJECT_CREATION_RIGHT], - true + { enableGovernance: true } ); wrapper.setState({ boundAlms: ['bitbucket'] }); expect(wrapper).toMatchSnapshot('full rights and alms'); @@ -77,7 +84,7 @@ it('should load correctly', async () => { { alm: AlmKeys.GitHub, key: 'GH1' } ]); - const wrapper = shallowRender([PROJECT_CREATION_RIGHT]); + const wrapper = shallowRender([PROJECT_CREATION_RIGHT], {}); await waitAndUpdate(wrapper); @@ -86,7 +93,7 @@ it('should load correctly', async () => { }); it('should display component creation form', () => { - const wrapper = shallowRender([PORTFOLIO_CREATION_RIGHT], true); + const wrapper = shallowRender([PORTFOLIO_CREATION_RIGHT], { enableGovernance: true }); wrapper.instance().handleComponentCreationClick(ComponentQualifier.Portfolio); wrapper.setState({ governanceReady: true }); @@ -103,7 +110,7 @@ describe('handleComponentCreate', () => { const portfolio = { key: 'portfolio', qualifier: ComponentQualifier.Portfolio }; - const wrapper = shallowRender([], true); + const wrapper = shallowRender([], { enableGovernance: true }); it('should redirect to admin', async () => { wrapper.instance().handleComponentCreate(portfolio); @@ -120,10 +127,16 @@ describe('handleComponentCreate', () => { }); }); -function shallowRender(permissions: string[] = [], enableGovernance = false) { +function shallowRender( + permissions: string[] = [], + { enableGovernance = false, branchesEnabled = true } +) { return shallow<GlobalNavPlus>( <GlobalNavPlus - appState={{ qualifiers: enableGovernance ? [ComponentQualifier.Portfolio] : [] }} + appState={{ + branchesEnabled, + qualifiers: enableGovernance ? [ComponentQualifier.Portfolio] : [] + }} currentUser={mockLoggedInUser({ permissions: { global: permissions } })} router={mockRouter()} /> diff --git a/server/sonar-web/src/main/js/app/components/nav/global/__tests__/__snapshots__/GlobalNavPlus-test.tsx.snap b/server/sonar-web/src/main/js/app/components/nav/global/__tests__/__snapshots__/GlobalNavPlus-test.tsx.snap index 658c06d2ab9..1d95d08cb45 100644 --- a/server/sonar-web/src/main/js/app/components/nav/global/__tests__/__snapshots__/GlobalNavPlus-test.tsx.snap +++ b/server/sonar-web/src/main/js/app/components/nav/global/__tests__/__snapshots__/GlobalNavPlus-test.tsx.snap @@ -1,5 +1,31 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`should render correctly if branches not enabled 1`] = ` +<Fragment> + <Dropdown + onOpen={[Function]} + overlay={ + <GlobalNavPlusMenu + canCreateApplication={false} + canCreatePortfolio={false} + canCreateProject={true} + compatibleAlms={Array []} + onComponentCreationClick={[Function]} + /> + } + tagName="li" + > + <a + className="navbar-icon navbar-plus" + href="#" + title="my_account.create_new_project_portfolio_or_application" + > + <PlusIcon /> + </a> + </Dropdown> +</Fragment> +`; + exports[`should render correctly: full rights and alms 1`] = ` <Fragment> <Dropdown |