]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-13589 Restrict query to correct editions
authorJeremy Davis <jeremy.davis@sonarsource.com>
Mon, 6 Jul 2020 13:35:47 +0000 (15:35 +0200)
committersonartech <sonartech@sonarsource.com>
Mon, 6 Jul 2020 20:05:49 +0000 (20:05 +0000)
server/sonar-web/src/main/js/app/components/nav/global/GlobalNavPlus.tsx
server/sonar-web/src/main/js/app/components/nav/global/__tests__/GlobalNavPlus-test.tsx
server/sonar-web/src/main/js/app/components/nav/global/__tests__/__snapshots__/GlobalNavPlus-test.tsx.snap

index fca27d93214812aafdbe6f2e8bc3d3d7fdabbd13..0b58cdc94912e18887f9ec8a08a5c3e859876b11 100644 (file)
@@ -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}
index 6b1990beef85a266e1a1698603408fcc71fd5de0..8d74d544510743c35b29f8f23e112e7038b1149e 100644 (file)
@@ -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()}
     />
index 658c06d2ab923719f4869c08bf9eab3ff5aed995..1d95d08cb4500c541b6192517936f65259359fc0 100644 (file)
@@ -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