From: Grégoire Aubert
Date: Thu, 22 Feb 2018 16:35:02 +0000 (+0100)
Subject: SONAR-10053 Display error message when entering license of unknown edition in marketplace
X-Git-Tag: 7.5~1631
X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=2c7c8d8b0c1575f7e2235f5a7868d70bf914f3ba;p=sonarqube.git
SONAR-10053 Display error message when entering license of unknown edition in marketplace
---
diff --git a/server/sonar-web/src/main/js/apps/marketplace/components/LicenseEditionSet.tsx b/server/sonar-web/src/main/js/apps/marketplace/components/LicenseEditionSet.tsx
index 6be68c1aa31..b8099a65079 100644
--- a/server/sonar-web/src/main/js/apps/marketplace/components/LicenseEditionSet.tsx
+++ b/server/sonar-web/src/main/js/apps/marketplace/components/LicenseEditionSet.tsx
@@ -37,14 +37,15 @@ export interface Props {
interface State {
acceptTerms: boolean;
- license: string;
- licenseEdition?: Edition;
- loading: boolean;
- previewStatus?: string;
formData?: {
serverId?: string;
ncloc?: number;
};
+ license: string;
+ licenseEdition?: Edition;
+ loading: boolean;
+ previewStatus?: string;
+ wrongEdition: boolean;
}
export default class LicenseEditionSet extends React.PureComponent {
@@ -52,7 +53,7 @@ export default class LicenseEditionSet extends React.PureComponent
constructor(props: Props) {
super(props);
- this.state = { acceptTerms: false, license: '', loading: false };
+ this.state = { acceptTerms: false, license: '', loading: false, wrongEdition: false };
this.fetchLicensePreview = debounce(this.fetchLicensePreview, 100);
}
@@ -71,16 +72,18 @@ export default class LicenseEditionSet extends React.PureComponent
({ previewStatus, nextEditionKey }) => {
if (this.mounted) {
const { edition } = this.props;
- this.updateLicense(
- license,
- this.props.editions.find(edition => edition.key === nextEditionKey),
- edition && edition.key !== nextEditionKey ? undefined : previewStatus
+ const licenseEdition = this.props.editions.find(
+ edition => edition.key === nextEditionKey
+ );
+ const wrongEdition = Boolean(
+ !licenseEdition || (edition && edition.key !== nextEditionKey)
);
+ this.setLicense({ license, loading: false, licenseEdition, previewStatus, wrongEdition });
}
},
() => {
if (this.mounted) {
- this.updateLicense(license, undefined, undefined);
+ this.resetLicense({ license, loading: false });
}
}
);
@@ -114,42 +117,59 @@ export default class LicenseEditionSet extends React.PureComponent
this.fetchLicensePreview(license);
this.setState({ license });
} else {
- this.updateLicense(license, undefined, undefined);
+ this.resetLicense({});
}
};
- handleTermsCheck = (checked: boolean) =>
- this.setState({ acceptTerms: checked }, () =>
- this.updateLicense(this.state.license, this.state.licenseEdition, this.state.previewStatus)
+ handleTermsCheck = (checked: boolean) => {
+ this.setLicense({ acceptTerms: checked });
+ };
+
+ resetLicense(state: Pick) {
+ this.setLicense(
+ Object.assign(
+ {
+ license: '',
+ licenseEdition: undefined,
+ previewStatus: undefined,
+ wrongEdition: false
+ },
+ state
+ )
);
+ }
+
+ setLicense(state: Pick) {
+ this.setState(state, this.updateParentLicense);
+ }
- updateLicense = (license: string, licenseEdition?: Edition, previewStatus?: string) => {
- this.setState({ license, licenseEdition, loading: false, previewStatus });
+ updateParentLicense = () => {
+ const { acceptTerms, license, previewStatus, wrongEdition } = this.state;
this.props.updateLicense(
- previewStatus !== 'NO_INSTALL' && !this.state.acceptTerms ? undefined : license,
- previewStatus
+ previewStatus !== 'NO_INSTALL' && !acceptTerms ? undefined : license,
+ wrongEdition ? undefined : previewStatus
);
};
renderAlert() {
- const { licenseEdition, previewStatus } = this.state;
- if (!previewStatus) {
+ const { licenseEdition, previewStatus, wrongEdition } = this.state;
+ if (!previewStatus || wrongEdition) {
const { edition } = this.props;
- if (!edition) {
- return undefined;
- }
return (
);
}
@@ -177,6 +197,7 @@ export default class LicenseEditionSet extends React.PureComponent
url: (
{licenseEdition.name}
@@ -199,6 +220,7 @@ export default class LicenseEditionSet extends React.PureComponent
{translate('marketplace.terms_and_conditions')}
@@ -222,8 +244,8 @@ export default class LicenseEditionSet extends React.PureComponent
)}
- }>
+ }
+ loading={loading}>
{this.renderAlert()}
diff --git a/server/sonar-web/src/main/js/apps/marketplace/components/__tests__/LicenseEditionSet-test.tsx b/server/sonar-web/src/main/js/apps/marketplace/components/__tests__/LicenseEditionSet-test.tsx
index 158a52befed..691f9c3613f 100644
--- a/server/sonar-web/src/main/js/apps/marketplace/components/__tests__/LicenseEditionSet-test.tsx
+++ b/server/sonar-web/src/main/js/apps/marketplace/components/__tests__/LicenseEditionSet-test.tsx
@@ -64,11 +64,15 @@ it('should display the get license link with parameters', async () => {
});
it('should correctly display status message after checking license', async () => {
- let wrapper = await testLicenseStatus('NO_INSTALL', jest.fn());
+ let wrapper = await testLicenseStatus('NO_INSTALL');
expect(wrapper.find('p.alert')).toMatchSnapshot();
- wrapper = await testLicenseStatus('AUTOMATIC_INSTALL', jest.fn());
+ wrapper = await testLicenseStatus('AUTOMATIC_INSTALL');
expect(wrapper.find('p.alert')).toMatchSnapshot();
- wrapper = await testLicenseStatus('MANUAL_INSTALL', jest.fn());
+ wrapper = await testLicenseStatus('MANUAL_INSTALL');
+ expect(wrapper.find('p.alert')).toMatchSnapshot();
+ wrapper = await testLicenseStatus('AUTOMATIC_INSTALL', jest.fn(), 'bar');
+ expect(wrapper.find('p.alert')).toMatchSnapshot();
+ wrapper = await testLicenseStatus('AUTOMATIC_INSTALL', jest.fn(), 'bar', { edition: undefined });
expect(wrapper.find('p.alert')).toMatchSnapshot();
});
@@ -103,11 +107,16 @@ function getWrapper(props = {}) {
);
}
-async function testLicenseStatus(status: string, updateLicense: jest.Mock) {
+async function testLicenseStatus(
+ status: string,
+ updateLicense = jest.fn(),
+ nextEditionKey = 'foo',
+ props = {}
+) {
getLicensePreview.mockImplementation(() =>
- Promise.resolve({ nextEditionKey: 'foo', previewStatus: status })
+ Promise.resolve({ nextEditionKey, previewStatus: status })
);
- const wrapper = getWrapper({ updateLicense });
+ const wrapper = getWrapper({ updateLicense, ...props });
change(wrapper.find('textarea'), 'mylicense');
expect(getLicensePreview).toHaveBeenCalled();
await new Promise(setImmediate);
diff --git a/server/sonar-web/src/main/js/apps/marketplace/components/__tests__/__snapshots__/LicenseEditionSet-test.tsx.snap b/server/sonar-web/src/main/js/apps/marketplace/components/__tests__/__snapshots__/LicenseEditionSet-test.tsx.snap
index 8631ff5d7d4..5ff0d2f7d37 100644
--- a/server/sonar-web/src/main/js/apps/marketplace/components/__tests__/__snapshots__/LicenseEditionSet-test.tsx.snap
+++ b/server/sonar-web/src/main/js/apps/marketplace/components/__tests__/__snapshots__/LicenseEditionSet-test.tsx.snap
@@ -24,6 +24,22 @@ exports[`should correctly display status message after checking license 3`] = `
`;
+exports[`should correctly display status message after checking license 4`] = `
+
+ marketplace.wrong_license_type_x.Foo
+
+`;
+
+exports[`should correctly display status message after checking license 5`] = `
+
+ marketplace.wrong_license_type
+
+`;
+
exports[`should display correctly 1`] = `