From ef66ee089dbbda4441dab0d262a1b34727b1b445 Mon Sep 17 00:00:00 2001 From: Grégoire Aubert Date: Thu, 28 Jun 2018 17:21:09 +0200 Subject: SONAR-10937 Change ‘invalid license’ message after updating license MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/sonar-web/src/main/js/api/marketplace.ts | 4 + .../nav/component/ComponentNavBgTaskNotif.tsx | 20 +--- .../nav/component/ComponentNavLicenseNotif.tsx | 102 +++++++++++++++++++++ .../__tests__/ComponentNavBgTaskNotif-test.tsx | 11 +-- .../__tests__/ComponentNavLicenseNotif-test.tsx | 82 +++++++++++++++++ .../ComponentNavBgTaskNotif-test.tsx.snap | 39 ++------ .../ComponentNavLicenseNotif-test.tsx.snap | 66 +++++++++++++ 7 files changed, 267 insertions(+), 57 deletions(-) create mode 100644 server/sonar-web/src/main/js/app/components/nav/component/ComponentNavLicenseNotif.tsx create mode 100644 server/sonar-web/src/main/js/app/components/nav/component/__tests__/ComponentNavLicenseNotif-test.tsx create mode 100644 server/sonar-web/src/main/js/app/components/nav/component/__tests__/__snapshots__/ComponentNavLicenseNotif-test.tsx.snap (limited to 'server') 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 { 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 { - 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 } else if (currentTask && currentTask.status === STATUSES.FAILED) { if ( currentTask.errorType && - currentTask.errorType.includes('LICENSING') && hasMessage('license.component_navigation.button', currentTask.errorType) ) { - return ( - - {currentTask.errorMessage} - {this.context.canAdmin ? ( - - {translate('license.component_navigation.button', currentTask.errorType)}. - - ) : ( - translate('please_contact_administrator') - )} - - ); + return ; } 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 { + 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 ( + + + {translate('component_navigation.status.last_blocked_due_to_bad_license')} + + + ); + } + + return ( + + {currentTask.errorMessage} + {this.context.canAdmin ? ( + + {translate('license.component_navigation.button', currentTask.errorType)}. + + ) : ( + translate('please_contact_administrator') + )} + + ); + } +} 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( , - { 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).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).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).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( + , + { 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`] = ` - - - Foo - - - license.component_navigation.button.LICENSING - . - - -`; - -exports[`renders background task license info correctly 2`] = ` - - - Foo - - please_contact_administrator - + `; 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`] = ` + + + component_navigation.status.last_blocked_due_to_bad_license + + +`; + +exports[`renders background task license info correctly 1`] = ` + + + Foo + + + license.component_navigation.button.LICENSING + . + + +`; + +exports[`renders background task license info correctly 2`] = ` + + + Foo + + please_contact_administrator + +`; + +exports[`renders correctly for LICENSING_LOC error 1`] = ` + + + Foo + + + license.component_navigation.button.LICENSING_LOC + . + + +`; -- cgit v1.2.3