From 5733f1160ffd94e6852a0fbabd72fbee7174b8aa Mon Sep 17 00:00:00 2001 From: Alexander Bulychev Date: Thu, 7 Sep 2023 12:53:23 +0400 Subject: [PATCH] Review fix --- .../js/core/__tests__/license.test.ts | 45 ++++++++++++++++ packages/devextreme/js/core/component.js | 53 ++++++++----------- packages/devextreme/js/core/errors.js | 24 ++++++++- .../tests/DevExpress.core/core.licensing.js | 8 +-- 4 files changed, 95 insertions(+), 35 deletions(-) create mode 100644 packages/devextreme/js/core/__tests__/license.test.ts diff --git a/packages/devextreme/js/core/__tests__/license.test.ts b/packages/devextreme/js/core/__tests__/license.test.ts new file mode 100644 index 000000000000..3f5bf926c97e --- /dev/null +++ b/packages/devextreme/js/core/__tests__/license.test.ts @@ -0,0 +1,45 @@ +import { verifyLicense } from '../component'; + +const token231 = 'ewogICJmb3JtYXQiOiAxLAogICJjdXN0b21lcklkIjogImIxMTQwYjQ2LWZkZTEtNDFiZC1hMjgwLTRkYjlmOGU3ZDliZCIsCiAgIm1heFZlcnNpb25BbGxvd2VkIjogMjMxCn0=.DiDceRbil4IzXl5av7pNkKieyqHHhRf+CM477zDu4N9fyrhkQsjRourYvgVfkbSm+EQplkXhlMBc3s8Vm9n+VtPaMbeWXis92cdW/6HiT+Dm54xw5vZ5POGunKRrNYUzd9zTbYcz0bYA/dc/mHFeUdXA0UlKcx1uMaXmtJrkK74='; +const token232 = 'ewogICJmb3JtYXQiOiAxLAogICJjdXN0b21lcklkIjogIjYxMjFmMDIyLTFjMTItNDNjZC04YWE0LTkwNzJkNDU4YjYxNCIsCiAgIm1heFZlcnNpb25BbGxvd2VkIjogMjMyCn0=.RENyZ3Ga5rCB7/XNKYbk2Ffv1n9bUexYNhyOlqcAD02YVnPw6XyQcN+ZORScKDU9gOInJ4o7vPxkgh10KvMZNn+FuBK8UcUR7kchk7z0CHGuOcIn2jD5X2hG6SYJ0UCBG/JDG35AL09T7Uv/pGj4PolRsANxtuMpoqmvX2D2vkU='; +const tokenUnverified = 'ewogICJmb3JtYXQiOiAxLAogICJjdXN0b21lcklkIjogImIxMTQwYjQ2LWZkZTEtNDFiZC1hMjgwLTRkYjlmOGU3ZDliZCIsCiAgIm1heFZlcnNpb25BbGxvd2VkIjogMjMxCn0=.NVsilC5uWlD5QGS6bocLMlsVVK0VpZXYwU2DstUiLRpEI79/onuR8dGWasCLBo4PORDHPkNA/Ej8XeCHzJ0EkXRRZ7E2LrP/xlEfHRXTruvW4IEbZt3LiwJBt6/isLz+wzXtYtjV7tpE07/Y0TFoy+mWpHoU11GVtwKh6weRxkg='; +const tokenInvalidJson = 'YWJj.vjx6wAI9jVkHJAnKcsuYNZ5UvCq3UhypQ+0f+kZ37/Qc1uj4BM6//Kfi4SVsXGOaOTFYWgzesROnHCp3jZRqphJwal4yXHD1sGFi6FEdB4MgdgNZvsZSnxNWLs/7s07CzuHLTpJrAG7sTdHVkQWZNnSCKjzV7909c/Stl9+hkLo='; +const tokenInvalidBase64 = 'ewogICJmb3JtYXQiOiAxLAogICJjdXN0b21lcklkIjogIjM3Yjg4ZjBmLWQ0MmMtNDJiZS05YjhkLTU1ZGMwYzUzYzAxZiIsCiAgIm1heFZlcnNpb25BbGxvd2VkIjogMjIxCn0-.EnP/RDKg0eSyaPU1eDUFll1lqOdYbhN3u73LhN1op8vjNwA0P1vKiT1DfQRmXudlleGWgDkLA2OmJYUER8j7I3LSFf3hLkBAoWoBErgveTb2zkbz8P1i9lE+XmzIXeYHyZBYUt0IPkNfajF9zzbSDDin1CvW7pnADi0vIeZ5ICQ='; +const tokenMissingField1 = 'ewogICJmb3JtYXQiOiAxLAogICJtYXhWZXJzaW9uQWxsb3dlZCI6IDIzMQp9.WH30cajUFcKqw/fwt4jITM/5tzVwPpbdbezhhdBi5oeOvU06zKY0J4M8gQy8GQ++RPYVCAo2md6vI9D80FD2CC4w+hpQLJNJJgNUHYPrgG6CX1yAB3M+NKHsPP9S71bXAgwvignb5uPo0R5emQzr4RKDhWQMKtgqEcRe+yme2mU='; +const tokenMissingField2 = 'ewogICJjdXN0b21lcklkIjogImIxMTQwYjQ2LWZkZTEtNDFiZC1hMjgwLTRkYjlmOGU3ZDliZCIsCiAgIm1heFZlcnNpb25BbGxvd2VkIjogMjMxCn0=.ok32DBaAgf3ijLmNQb+A0kUV2AiSivqvZJADdF607qqlAaduAVnotJtgdwm/Ib3MErfaGrDohCYoFMnKQevkRxFkA7tK3kOBnTZPUnZY0r3wyulMQmr4Qo+Sjf/fyXs4IYpGsC7/uJjgrCos8uzBegfmgfM93XSt6pKl9+c5xvc='; +const tokenMissingField3 = 'ewogICJmb3JtYXQiOiAxLAogICJjdXN0b21lcklkIjogImIxMTQwYjQ2LWZkZTEtNDFiZC1hMjgwLTRkYjlmOGU3ZDliZCIKfQ==.resgTqmazrorRNw7mmtV31XQnmTSw0uLEArsmpzCjWMQJLocBfAjpFvKBf+SAG9q+1iOSFySj64Uv2xBVqHnyeNVBRbouOKOnAB8RpkKvN4sc5SDc8JAG5TkwPVSzK/VLBpQxpqbxlcrRfHwz9gXqQoPt4/ZVATn285iw3DW0CU='; +const tokenUnsupportedVersion = 'ewogICJmb3JtYXQiOiAyLAogICJjdXN0b21lcklkIjogImIxMTQwYjQ2LWZkZTEtNDFiZC1hMjgwLTRkYjlmOGU3ZDliZCIsCiAgIm1heFZlcnNpb25BbGxvd2VkIjogMjMxCn0=.tTBymZMROsYyMiP6ldXFqGurbzqjhSQIu/pjyEUJA3v/57VgToomYl7FVzBj1asgHpadvysyTUiX3nFvPxbp166L3+LB3Jybw9ueMnwePu5vQOO0krqKLBqRq+TqHKn7k76uYRbkCIo5UajNfzetHhlkin3dJf3x2K/fcwbPW5A='; + +describe('verifyLicense', () => { + it('Empty license', () => { + expect(verifyLicense()).toEqual('W0019'); + expect(verifyLicense(null, '1.2.3')).toEqual('W0019'); + expect(verifyLicense(undefined)).toEqual('W0019'); + }); + + it('Valid license', () => { + expect(verifyLicense(token231, '23.1')).toBeUndefined(); + expect(verifyLicense(token231, '12.3')).toBeUndefined(); + expect(verifyLicense(token232, '23.1')).toBeUndefined(); + expect(verifyLicense(token232, '23.2')).toBeUndefined(); + }); + + it('Expired license', () => { + expect(verifyLicense(token231, '23.2')).toEqual('W0020'); + expect(verifyLicense(token232, '42.4')).toEqual('W0020'); + }); + + it('Corrupted token', () => { + expect(verifyLicense(tokenUnverified, '1.2.3')).toEqual('W0021'); + expect(verifyLicense(tokenInvalidJson, '1.2.3')).toEqual('W0021'); + expect(verifyLicense(tokenInvalidBase64, '1.2.3')).toEqual('W0021'); + expect(verifyLicense(tokenMissingField1, '1.2.3')).toEqual('W0021'); + expect(verifyLicense(tokenMissingField2, '1.2.3')).toEqual('W0021'); + expect(verifyLicense(tokenMissingField3, '1.2.3')).toEqual('W0021'); + expect(verifyLicense(tokenUnsupportedVersion, '1.2.3')).toEqual('W0021'); + expect(verifyLicense('Another', '1.2.3')).toEqual('W0021'); + expect(verifyLicense('str@nge')).toEqual('W0021'); + expect(verifyLicense('in.put')).toEqual('W0021'); + expect(verifyLicense('3.2.1', '1.2.3')).toEqual('W0021'); + }); +}); diff --git a/packages/devextreme/js/core/component.js b/packages/devextreme/js/core/component.js index 06970ed95f57..e817e697f19e 100644 --- a/packages/devextreme/js/core/component.js +++ b/packages/devextreme/js/core/component.js @@ -23,19 +23,6 @@ const isInnerOption = (optionName) => { return optionName.indexOf('_', 0) === 0; }; -export const emptyLicenseMessage = 'DevExtreme: Valid license key not found.\n\n' + - 'If you are using a trial version, you must uninstall all copies of DevExtreme once your 30 days trial period expires. For licensing-related information, please refer to the DevExtreme End User License Agreement: https://js.devexpress.com/EULAs/DevExtremeComplete/.\n\n' + - 'To continue using DevExtreme in a project, you must purchase a license. For pricing/licensing options, please visit: https://js.devexpress.com/Buy/.\n\n' + - 'If you have licensing-related questions or need help with a purchase, please email clientservices@devexpress.com. We will be happy to follow-up.'; -export const invalidVersionLicenseMessage = 'DevExtreme: The license key is expired.\n\n' + - 'A mismatch exists between license key/DevExtreme version.\n\n' + - 'To proceed, you can:\n' + - ' ● use a version of DevExtreme linked to your license key https://www.devexpress.com/ClientCenter/DownloadManager/\n' + - ' ● renew your DevExpress Subscription (once you renew your subscription, you will be entitled to product updates and support service https://www.devexpress.com/buy/renew/)\n\n' + - 'If you have licensing-related questions or need help with a renewal, please email clientservices@devexpress.com. We will be happy to follow-up.'; -export const invalidFormatLicenseMessage = 'DevExtreme: License key verification failed.\n\n' + - 'Make certain to specify a correct key in the GlobalConfig. If you continue to encounter an error, please visit https://www.devexpress.com/ClientCenter/DownloadManager/ to obtain a valid key.\n\n' + - 'If you have a valid license and this problem persists, please submit a support ticket via the DevExpress Support Center. We will be happy to follow-up: https://supportcenter.devexpress.com/ticket/create'; let licenseVerified = false; export const Component = Class.inherit({ @@ -102,23 +89,9 @@ export const Component = Class.inherit({ if(!licenseVerified) { licenseVerified = true; - const licenseToken = Config().license; - if(licenseToken) { - const license = parseToken(licenseToken); - - if(license.kind === 'corrupted') { - errors.log(invalidFormatLicenseMessage); - } else { - const [majorStr, minorStr] = version.split('.'); - const major = parseInt(majorStr, 10); - const minor = parseInt(minorStr, 10); - - if(major * 10 + minor > license.payload.maxVersionAllowed) { - errors.log(invalidVersionLicenseMessage); - } - } - } else { - errors.log(emptyLicenseMessage); + const licenseError = verifyLicense(Config().license, version); + if(licenseError) { + errors.log(licenseError); } } }, @@ -425,6 +398,26 @@ export const Component = Class.inherit({ } }); +export function verifyLicense(licenseToken, ver = version) { + if(licenseToken) { + const license = parseToken(licenseToken); + + if(license.kind === 'corrupted') { + return 'W0021'; + } else { + const [majorStr, minorStr] = ver.split('.'); + const major = parseInt(majorStr, 10); + const minor = parseInt(minorStr, 10); + + if(major * 10 + minor > license.payload.maxVersionAllowed) { + return 'W0020'; + } + } + } else { + return 'W0019'; + } +} + ///#DEBUG export function resetLicenseCheckSkipCondition() { licenseVerified = false; diff --git a/packages/devextreme/js/core/errors.js b/packages/devextreme/js/core/errors.js index 3876f0290d04..a5d64b2f3c0c 100644 --- a/packages/devextreme/js/core/errors.js +++ b/packages/devextreme/js/core/errors.js @@ -249,5 +249,27 @@ export default errorUtils({ /** * @name ErrorsCore.W0018 */ - W0018: 'Setting the "position" property with a function is deprecated since v21.2' + W0018: 'Setting the "position" property with a function is deprecated since v21.2', + /** + * @name ErrorsCore.W0019 + */ + W0019: 'DevExtreme: Valid license key not found.\n\n' + + 'If you are using a trial version, you must uninstall all copies of DevExtreme once your 30 days trial period expires. For licensing-related information, please refer to the DevExtreme End User License Agreement: https://js.devexpress.com/EULAs/DevExtremeComplete/.\n\n' + + 'To continue using DevExtreme in a project, you must purchase a license. For pricing/licensing options, please visit: https://js.devexpress.com/Buy/.\n\n' + + 'If you have licensing-related questions or need help with a purchase, please email clientservices@devexpress.com. We will be happy to follow-up.', + /** + * @name ErrorsCore.W0019 + */ + W0020: 'DevExtreme: The license key is expired.\n\n' + + 'A mismatch exists between license key/DevExtreme version.\n\n' + + 'To proceed, you can:\n' + + ' ● use a version of DevExtreme linked to your license key https://www.devexpress.com/ClientCenter/DownloadManager/\n' + + ' ● renew your DevExpress Subscription (once you renew your subscription, you will be entitled to product updates and support service https://www.devexpress.com/buy/renew/)\n\n' + + 'If you have licensing-related questions or need help with a renewal, please email clientservices@devexpress.com. We will be happy to follow-up.', + /** + * @name ErrorsCore.W0019 + */ + W0021: 'DevExtreme: License key verification failed.\n\n' + + 'Make certain to specify a correct key in the GlobalConfig. If you continue to encounter an error, please visit https://www.devexpress.com/ClientCenter/DownloadManager/ to obtain a valid key.\n\n' + + 'If you have a valid license and this problem persists, please submit a support ticket via the DevExpress Support Center. We will be happy to follow-up: https://supportcenter.devexpress.com/ticket/create', }); diff --git a/packages/devextreme/testing/tests/DevExpress.core/core.licensing.js b/packages/devextreme/testing/tests/DevExpress.core/core.licensing.js index 0d3f86c05adf..b4268c6f9166 100644 --- a/packages/devextreme/testing/tests/DevExpress.core/core.licensing.js +++ b/packages/devextreme/testing/tests/DevExpress.core/core.licensing.js @@ -1,6 +1,6 @@ import config from 'core/config'; import errors from 'core/errors'; -import { Component, emptyLicenseMessage, invalidVersionLicenseMessage, invalidFormatLicenseMessage, resetLicenseCheckSkipCondition } from 'core/component'; +import { Component, resetLicenseCheckSkipCondition } from 'core/component'; const { test, module } = QUnit; const validToken_23_2 = 'ewogICJmb3JtYXQiOiAxLAogICJjdXN0b21lcklkIjogIjYxMjFmMDIyLTFjMTItNDNjZC04YWE0LTkwNzJkNDU4YjYxNCIsCiAgIm1heFZlcnNpb25BbGxvd2VkIjogMjMyCn0=.RENyZ3Ga5rCB7/XNKYbk2Ffv1n9bUexYNhyOlqcAD02YVnPw6XyQcN+ZORScKDU9gOInJ4o7vPxkgh10KvMZNn+FuBK8UcUR7kchk7z0CHGuOcIn2jD5X2hG6SYJ0UCBG/JDG35AL09T7Uv/pGj4PolRsANxtuMpoqmvX2D2vkU='; @@ -33,7 +33,7 @@ module('License check', { const instance = new TestComponent(); assert.equal(log.length, 1); - assert.strictEqual(log[0][0], emptyLicenseMessage); + assert.strictEqual(log[0][0], 'W0019'); }); test('token should be verified', function(assert) { @@ -50,7 +50,7 @@ module('License check', { const instance = new TestComponent(); assert.equal(log.length, 1); - assert.strictEqual(log[0][0], invalidVersionLicenseMessage); + assert.strictEqual(log[0][0], 'W0020'); }); test('token check should be failed - wrong format', function(assert) { @@ -59,6 +59,6 @@ module('License check', { const instance = new TestComponent(); assert.equal(log.length, 1); - assert.strictEqual(log[0][0], invalidFormatLicenseMessage); + assert.strictEqual(log[0][0], 'W0021'); }); });