diff options
author | Grégoire Aubert <gregoire.aubert@sonarsource.com> | 2018-06-28 17:21:09 +0200 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2018-07-05 20:21:54 +0200 |
commit | ef66ee089dbbda4441dab0d262a1b34727b1b445 (patch) | |
tree | 433be5b3c901da4a58721f9fe45242ff483ce603 /server | |
parent | 76ff58d995313f600ec7ae5341a52e0a4150b70f (diff) | |
download | sonarqube-ef66ee089dbbda4441dab0d262a1b34727b1b445.tar.gz sonarqube-ef66ee089dbbda4441dab0d262a1b34727b1b445.zip |
SONAR-10937 Change ‘invalid license’ message after updating license
Diffstat (limited to 'server')
7 files changed, 267 insertions, 57 deletions
diff --git a/server/sonar-web/src/main/js/api/marketplace.ts b/server/sonar-web/src/main/js/api/marketplace.ts index 8448d9fbfcb..5077a51d301 100644 --- a/server/sonar-web/src/main/js/api/marketplace.ts +++ b/server/sonar-web/src/main/js/api/marketplace.ts @@ -37,6 +37,10 @@ export interface License { type: string; } +export function isValidLicense(): Promise<{ isValidLicense: boolean }> { + return getJSON('/api/editions/is_valid_license'); +} + export function showLicense(): Promise<License> { return getJSON('/api/editions/show_license').catch((e: { response: Response }) => { if (e.response && e.response.status === 404) { diff --git a/server/sonar-web/src/main/js/app/components/nav/component/ComponentNavBgTaskNotif.tsx b/server/sonar-web/src/main/js/app/components/nav/component/ComponentNavBgTaskNotif.tsx index b5bb9a7a3dc..3ec72350517 100644 --- a/server/sonar-web/src/main/js/app/components/nav/component/ComponentNavBgTaskNotif.tsx +++ b/server/sonar-web/src/main/js/app/components/nav/component/ComponentNavBgTaskNotif.tsx @@ -20,7 +20,7 @@ import * as React from 'react'; import { Link } from 'react-router'; import { FormattedMessage } from 'react-intl'; -import * as PropTypes from 'prop-types'; +import ComponentNavLicenseNotif from './ComponentNavLicenseNotif'; import NavBarNotif from '../../../../components/nav/NavBarNotif'; import PendingIcon from '../../../../components/icons-components/PendingIcon'; import { Component } from '../../../types'; @@ -37,10 +37,6 @@ interface Props { } export default class ComponentNavBgTaskNotif extends React.PureComponent<Props> { - static contextTypes = { - canAdmin: PropTypes.bool.isRequired - }; - renderMessage(messageKey: string, status?: string) { const { component } = this.props; const canSeeBackgroundTasks = @@ -82,21 +78,9 @@ export default class ComponentNavBgTaskNotif extends React.PureComponent<Props> } else if (currentTask && currentTask.status === STATUSES.FAILED) { if ( currentTask.errorType && - currentTask.errorType.includes('LICENSING') && hasMessage('license.component_navigation.button', currentTask.errorType) ) { - return ( - <NavBarNotif className="alert alert-danger"> - <span className="little-spacer-right">{currentTask.errorMessage}</span> - {this.context.canAdmin ? ( - <Link to="/admin/extension/license/app"> - {translate('license.component_navigation.button', currentTask.errorType)}. - </Link> - ) : ( - translate('please_contact_administrator') - )} - </NavBarNotif> - ); + return <ComponentNavLicenseNotif currentTask={currentTask} />; } return ( diff --git a/server/sonar-web/src/main/js/app/components/nav/component/ComponentNavLicenseNotif.tsx b/server/sonar-web/src/main/js/app/components/nav/component/ComponentNavLicenseNotif.tsx new file mode 100644 index 00000000000..9a251ce316d --- /dev/null +++ b/server/sonar-web/src/main/js/app/components/nav/component/ComponentNavLicenseNotif.tsx @@ -0,0 +1,102 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +import * as React from 'react'; +import { Link } from 'react-router'; +import * as PropTypes from 'prop-types'; +import NavBarNotif from '../../../../components/nav/NavBarNotif'; +import { translate } from '../../../../helpers/l10n'; +import { Task } from '../../../../api/ce'; +import { isValidLicense } from '../../../../api/marketplace'; + +interface Props { + currentTask?: Task; +} + +interface State { + isValidLicense?: boolean; + loading: boolean; +} + +export default class ComponentNavLicenseNotif extends React.PureComponent<Props, State> { + mounted = false; + + static contextTypes = { + canAdmin: PropTypes.bool.isRequired + }; + + state: State = { loading: false }; + + componentDidMount() { + this.mounted = true; + this.fetchIsValidLicense(); + } + + componentWillUnmount() { + this.mounted = false; + } + + fetchIsValidLicense = () => { + this.setState({ loading: true }); + isValidLicense().then( + ({ isValidLicense }) => { + if (this.mounted) { + this.setState({ isValidLicense, loading: false }); + } + }, + () => { + if (this.mounted) { + this.setState({ loading: false }); + } + } + ); + }; + + render() { + const { currentTask } = this.props; + const { isValidLicense, loading } = this.state; + + if (loading || !currentTask || !currentTask.errorType) { + return null; + } + + if (isValidLicense && currentTask.errorType !== 'LICENSING_LOC') { + return ( + <NavBarNotif className="alert alert-danger"> + <span className="little-spacer-right"> + {translate('component_navigation.status.last_blocked_due_to_bad_license')} + </span> + </NavBarNotif> + ); + } + + return ( + <NavBarNotif className="alert alert-danger"> + <span className="little-spacer-right">{currentTask.errorMessage}</span> + {this.context.canAdmin ? ( + <Link to="/admin/extension/license/app"> + {translate('license.component_navigation.button', currentTask.errorType)}. + </Link> + ) : ( + translate('please_contact_administrator') + )} + </NavBarNotif> + ); + } +} diff --git a/server/sonar-web/src/main/js/app/components/nav/component/__tests__/ComponentNavBgTaskNotif-test.tsx b/server/sonar-web/src/main/js/app/components/nav/component/__tests__/ComponentNavBgTaskNotif-test.tsx index c404f6ee227..b027ea1fcb9 100644 --- a/server/sonar-web/src/main/js/app/components/nav/component/__tests__/ComponentNavBgTaskNotif-test.tsx +++ b/server/sonar-web/src/main/js/app/components/nav/component/__tests__/ComponentNavBgTaskNotif-test.tsx @@ -64,21 +64,14 @@ it('renders background task license info correctly', () => { expect( getWrapper({ currentTask: { status: 'FAILED', errorType: 'LICENSING', errorMessage: 'Foo' } }) ).toMatchSnapshot(); - expect( - getWrapper( - { currentTask: { status: 'FAILED', errorType: 'LICENSING', errorMessage: 'Foo' } }, - { canAdmin: false } - ) - ).toMatchSnapshot(); }); -function getWrapper(props = {}, context = {}) { +function getWrapper(props = {}) { return shallow( <ComponentNavBgTaskNotif component={component} currentTask={{ status: 'FAILED' } as Task} {...props} - />, - { context: { canAdmin: true, ...context } } + /> ); } diff --git a/server/sonar-web/src/main/js/app/components/nav/component/__tests__/ComponentNavLicenseNotif-test.tsx b/server/sonar-web/src/main/js/app/components/nav/component/__tests__/ComponentNavLicenseNotif-test.tsx new file mode 100644 index 00000000000..4fb9cb4214b --- /dev/null +++ b/server/sonar-web/src/main/js/app/components/nav/component/__tests__/ComponentNavLicenseNotif-test.tsx @@ -0,0 +1,82 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +import * as React from 'react'; +import { shallow } from 'enzyme'; +import ComponentNavLicenseNotif from '../ComponentNavLicenseNotif'; +import { Task } from '../../../../../api/ce'; +import { isValidLicense } from '../../../../../api/marketplace'; +import { waitAndUpdate } from '../../../../../helpers/testUtils'; + +jest.mock('../../../../../helpers/l10n', () => { + const l10n = require.requireActual('../../../../../helpers/l10n'); + l10n.hasMessage = jest.fn(() => true); + return l10n; +}); + +jest.mock('../../../../../api/marketplace', () => ({ + isValidLicense: jest.fn().mockResolvedValue({ isValidLicense: false }) +})); + +beforeEach(() => { + (isValidLicense as jest.Mock<any>).mockClear(); +}); + +it('renders background task license info correctly', async () => { + let wrapper = getWrapper({ + currentTask: { status: 'FAILED', errorType: 'LICENSING', errorMessage: 'Foo' } + }); + await waitAndUpdate(wrapper); + expect(wrapper).toMatchSnapshot(); + + wrapper = getWrapper( + { currentTask: { status: 'FAILED', errorType: 'LICENSING', errorMessage: 'Foo' } }, + { canAdmin: false } + ); + await waitAndUpdate(wrapper); + expect(wrapper).toMatchSnapshot(); +}); + +it('renders a different message if the license is valid', async () => { + (isValidLicense as jest.Mock<any>).mockResolvedValueOnce({ isValidLicense: true }); + const wrapper = getWrapper({ + currentTask: { status: 'FAILED', errorType: 'LICENSING', errorMessage: 'Foo' } + }); + await waitAndUpdate(wrapper); + expect(wrapper).toMatchSnapshot(); +}); + +it('renders correctly for LICENSING_LOC error', async () => { + (isValidLicense as jest.Mock<any>).mockResolvedValueOnce({ isValidLicense: true }); + const wrapper = getWrapper({ + currentTask: { status: 'FAILED', errorType: 'LICENSING_LOC', errorMessage: 'Foo' } + }); + await waitAndUpdate(wrapper); + expect(wrapper).toMatchSnapshot(); +}); + +function getWrapper(props = {}, context = {}) { + return shallow( + <ComponentNavLicenseNotif + currentTask={{ errorMessage: 'Foo', errorType: 'LICENSING' } as Task} + {...props} + />, + { context: { canAdmin: true, ...context } } + ); +} diff --git a/server/sonar-web/src/main/js/app/components/nav/component/__tests__/__snapshots__/ComponentNavBgTaskNotif-test.tsx.snap b/server/sonar-web/src/main/js/app/components/nav/component/__tests__/__snapshots__/ComponentNavBgTaskNotif-test.tsx.snap index 2c7d92f06fb..1521b4bcd93 100644 --- a/server/sonar-web/src/main/js/app/components/nav/component/__tests__/__snapshots__/ComponentNavBgTaskNotif-test.tsx.snap +++ b/server/sonar-web/src/main/js/app/components/nav/component/__tests__/__snapshots__/ComponentNavBgTaskNotif-test.tsx.snap @@ -24,36 +24,15 @@ exports[`renders background task in progress info correctly 1`] = ` `; exports[`renders background task license info correctly 1`] = ` -<NavBarNotif - className="alert alert-danger" -> - <span - className="little-spacer-right" - > - Foo - </span> - <Link - onlyActiveOnIndex={false} - style={Object {}} - to="/admin/extension/license/app" - > - license.component_navigation.button.LICENSING - . - </Link> -</NavBarNotif> -`; - -exports[`renders background task license info correctly 2`] = ` -<NavBarNotif - className="alert alert-danger" -> - <span - className="little-spacer-right" - > - Foo - </span> - please_contact_administrator -</NavBarNotif> +<ComponentNavLicenseNotif + currentTask={ + Object { + "errorMessage": "Foo", + "errorType": "LICENSING", + "status": "FAILED", + } + } +/> `; exports[`renders background task pending info correctly 1`] = ` diff --git a/server/sonar-web/src/main/js/app/components/nav/component/__tests__/__snapshots__/ComponentNavLicenseNotif-test.tsx.snap b/server/sonar-web/src/main/js/app/components/nav/component/__tests__/__snapshots__/ComponentNavLicenseNotif-test.tsx.snap new file mode 100644 index 00000000000..b9da1de4908 --- /dev/null +++ b/server/sonar-web/src/main/js/app/components/nav/component/__tests__/__snapshots__/ComponentNavLicenseNotif-test.tsx.snap @@ -0,0 +1,66 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`renders a different message if the license is valid 1`] = ` +<NavBarNotif + className="alert alert-danger" +> + <span + className="little-spacer-right" + > + component_navigation.status.last_blocked_due_to_bad_license + </span> +</NavBarNotif> +`; + +exports[`renders background task license info correctly 1`] = ` +<NavBarNotif + className="alert alert-danger" +> + <span + className="little-spacer-right" + > + Foo + </span> + <Link + onlyActiveOnIndex={false} + style={Object {}} + to="/admin/extension/license/app" + > + license.component_navigation.button.LICENSING + . + </Link> +</NavBarNotif> +`; + +exports[`renders background task license info correctly 2`] = ` +<NavBarNotif + className="alert alert-danger" +> + <span + className="little-spacer-right" + > + Foo + </span> + please_contact_administrator +</NavBarNotif> +`; + +exports[`renders correctly for LICENSING_LOC error 1`] = ` +<NavBarNotif + className="alert alert-danger" +> + <span + className="little-spacer-right" + > + Foo + </span> + <Link + onlyActiveOnIndex={false} + style={Object {}} + to="/admin/extension/license/app" + > + license.component_navigation.button.LICENSING_LOC + . + </Link> +</NavBarNotif> +`; |