From e7e46cbe87bd2c5658877fdc1cb5b6e173b0e494 Mon Sep 17 00:00:00 2001 From: Wouter Admiraal <45544358+wouter-admiraal-sonarsource@users.noreply.github.com> Date: Tue, 24 Aug 2021 13:25:20 +0200 Subject: [PATCH] SONAR-15279 Enable bulk change for profile editors --- .../js/apps/coding-rules/components/App.tsx | 5 +- .../components/__tests__/App-test.tsx | 31 ++++++-- .../__tests__/__snapshots__/App-test.tsx.snap | 79 ++++++++++++++++++- 3 files changed, 106 insertions(+), 9 deletions(-) diff --git a/server/sonar-web/src/main/js/apps/coding-rules/components/App.tsx b/server/sonar-web/src/main/js/apps/coding-rules/components/App.tsx index db8c6cbb9e5..b1326f51341 100644 --- a/server/sonar-web/src/main/js/apps/coding-rules/components/App.tsx +++ b/server/sonar-web/src/main/js/apps/coding-rules/components/App.tsx @@ -526,9 +526,10 @@ export class App extends React.PureComponent { renderBulkButton = () => { const { currentUser, languages } = this.props; const { canWrite, paging, query, referencedProfiles } = this.state; + const canUpdate = canWrite || Object.values(referencedProfiles).some(p => p.actions?.edit); - if (!isLoggedIn(currentUser) || !canWrite) { - return null; + if (!isLoggedIn(currentUser) || !canUpdate) { + return
; } return ( diff --git a/server/sonar-web/src/main/js/apps/coding-rules/components/__tests__/App-test.tsx b/server/sonar-web/src/main/js/apps/coding-rules/components/__tests__/App-test.tsx index c0299fee5f1..78d7af61097 100644 --- a/server/sonar-web/src/main/js/apps/coding-rules/components/__tests__/App-test.tsx +++ b/server/sonar-web/src/main/js/apps/coding-rules/components/__tests__/App-test.tsx @@ -19,9 +19,16 @@ */ import { shallow } from 'enzyme'; import * as React from 'react'; +import { searchQualityProfiles } from '../../../../api/quality-profiles'; import { getRulesApp } from '../../../../api/rules'; import ScreenPositionHelper from '../../../../components/common/ScreenPositionHelper'; -import { mockCurrentUser, mockLocation, mockRouter, mockRule } from '../../../../helpers/testMocks'; +import { + mockCurrentUser, + mockLocation, + mockQualityProfile, + mockRouter, + mockRule +} from '../../../../helpers/testMocks'; import { waitAndUpdate } from '../../../../helpers/testUtils'; import { App } from '../App'; @@ -78,18 +85,30 @@ describe('renderBulkButton', () => { const wrapper = shallowRender({ currentUser: mockCurrentUser() }); - expect(wrapper.instance().renderBulkButton()).toBeNull(); + expect(wrapper.instance().renderBulkButton()).toMatchSnapshot(); }); it('should be null when the user does not have the sufficient permission', () => { - (getRulesApp as jest.Mock).mockReturnValue({ canWrite: false, repositories: [] }); + (getRulesApp as jest.Mock).mockReturnValueOnce({ canWrite: false, repositories: [] }); + + const wrapper = shallowRender(); + expect(wrapper.instance().renderBulkButton()).toMatchSnapshot(); + }); + it('should show bulk change button when user has global admin rights on quality profiles', async () => { + (getRulesApp as jest.Mock).mockReturnValueOnce({ canWrite: true, repositories: [] }); const wrapper = shallowRender(); - expect(wrapper.instance().renderBulkButton()).toBeNull(); + await waitAndUpdate(wrapper); + + expect(wrapper.instance().renderBulkButton()).toMatchSnapshot(); }); - it('should show bulk change button when everything is fine', async () => { - (getRulesApp as jest.Mock).mockReturnValue({ canWrite: true, repositories: [] }); + it('should show bulk change button when user has edit rights on specific quality profile', async () => { + (getRulesApp as jest.Mock).mockReturnValueOnce({ canWrite: false, repositories: [] }); + (searchQualityProfiles as jest.Mock).mockReturnValueOnce({ + profiles: [mockQualityProfile({ key: 'foo', actions: { edit: true } }), mockQualityProfile()] + }); + const wrapper = shallowRender(); await waitAndUpdate(wrapper); diff --git a/server/sonar-web/src/main/js/apps/coding-rules/components/__tests__/__snapshots__/App-test.tsx.snap b/server/sonar-web/src/main/js/apps/coding-rules/components/__tests__/__snapshots__/App-test.tsx.snap index 032ad554504..09a0e14f6fb 100644 --- a/server/sonar-web/src/main/js/apps/coding-rules/components/__tests__/__snapshots__/App-test.tsx.snap +++ b/server/sonar-web/src/main/js/apps/coding-rules/components/__tests__/__snapshots__/App-test.tsx.snap @@ -1,6 +1,82 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`renderBulkButton should show bulk change button when everything is fine 1`] = ` +exports[`renderBulkButton should be null when the user does not have the sufficient permission 1`] = `
`; + +exports[`renderBulkButton should be null when the user is not logged in 1`] = `
`; + +exports[`renderBulkButton should show bulk change button when user has edit rights on specific quality profile 1`] = ` + +`; + +exports[`renderBulkButton should show bulk change button when user has global admin rights on quality profiles 1`] = ` +