Skip to content

Commit

Permalink
[license] Allow usage of charts an tree view pro package for old prem…
Browse files Browse the repository at this point in the history
…ium licenses (mui#13619)

Signed-off-by: Flavien DELANGLE <flaviendelangle@gmail.com>
Co-authored-by: Alexandre Fauquette <45398769+alexfauquette@users.noreply.github.com>
  • Loading branch information
2 people authored and thomasmoon committed Sep 6, 2024
1 parent d4f09ce commit 59ba23d
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 88 deletions.
3 changes: 2 additions & 1 deletion packages/x-license/src/Watermark/Watermark.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';
import { MuiCommercialPackageName, useLicenseVerifier } from '../useLicenseVerifier';
import { useLicenseVerifier } from '../useLicenseVerifier';
import { LICENSE_STATUS, LicenseStatus } from '../utils/licenseStatus';
import { MuiCommercialPackageName } from '../utils/commercialPackages';

function getLicenseErrorMessage(licenseStatus: LicenseStatus) {
switch (licenseStatus) {
Expand Down
1 change: 0 additions & 1 deletion packages/x-license/src/useLicenseVerifier/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export { useLicenseVerifier } from './useLicenseVerifier';
export type { MuiCommercialPackageName } from './useLicenseVerifier';
15 changes: 2 additions & 13 deletions packages/x-license/src/useLicenseVerifier/useLicenseVerifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,8 @@ import {
showNotAvailableInInitialProPlanError,
} from '../utils/licenseErrorMessageUtils';
import { LICENSE_STATUS, LicenseStatus } from '../utils/licenseStatus';
import { extractAcceptedScopes, extractProductScope } from '../utils/licenseScope';
import MuiLicenseInfoContext from '../Unstable_LicenseInfoProvider/MuiLicenseInfoContext';

export type MuiCommercialPackageName =
| 'x-data-grid-pro'
| 'x-data-grid-premium'
| 'x-date-pickers-pro'
| 'x-tree-view-pro'
| 'x-charts-pro';
import { MuiCommercialPackageName } from '../utils/commercialPackages';

export const sharedLicenseStatuses: {
[packageName in MuiCommercialPackageName]?: {
Expand Down Expand Up @@ -48,15 +41,11 @@ export function useLicenseVerifier(
return sharedLicenseStatuses[packageName]!.licenseVerifier;
}

const acceptedScopes = extractAcceptedScopes(packageName);
const productScope = extractProductScope(packageName);

const plan = packageName.includes('premium') ? 'Premium' : 'Pro';
const licenseStatus = verifyLicense({
releaseInfo,
licenseKey,
acceptedScopes,
productScope,
packageName,
});

const fullPackageName = `@mui/${packageName}`;
Expand Down
6 changes: 6 additions & 0 deletions packages/x-license/src/utils/commercialPackages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type MuiCommercialPackageName =
| 'x-data-grid-pro'
| 'x-data-grid-premium'
| 'x-date-pickers-pro'
| 'x-tree-view-pro'
| 'x-charts-pro';
1 change: 1 addition & 0 deletions packages/x-license/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './licenseInfo';
export * from './licenseStatus';
export type { LicenseScope } from './licenseScope';
export type { LicensingModel } from './licensingModel';
export type { MuiCommercialPackageName } from './commercialPackages';
15 changes: 0 additions & 15 deletions packages/x-license/src/utils/licenseScope.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
export const LICENSE_SCOPES = ['pro', 'premium'] as const;
export const PRODUCT_SCOPES = ['data-grid', 'date-pickers', 'charts', 'tree-view'] as const;
export const PLAN_VERSIONS = ['initial', 'Q3-2024'] as const;

export type LicenseScope = (typeof LICENSE_SCOPES)[number];
export type ProductScope = (typeof PRODUCT_SCOPES)[number];
export type PlanVersion = (typeof PLAN_VERSIONS)[number];

export const extractProductScope = (packageName: string): ProductScope => {
// extract the part between "x-" and "-pro"/"-premium"
const regex = /x-(.*?)(-pro|-premium)?$/;
const match = packageName.match(regex);
return match![1] as ProductScope;
};

export const extractAcceptedScopes = (packageName: string): readonly LicenseScope[] => {
return packageName.includes('premium')
? LICENSE_SCOPES.filter((scope) => scope.includes('premium'))
: LICENSE_SCOPES;
};
104 changes: 58 additions & 46 deletions packages/x-license/src/verifyLicense/verifyLicense.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ describe('License: verifyLicense', () => {
verifyLicense({
releaseInfo: '__RELEASE_INFO__',
licenseKey,
acceptedScopes: ['pro', 'premium'],
productScope: 'data-grid',
packageName: 'x-data-grid-pro',
}).status,
).to.throw('MUI X: The release information is invalid. Not able to validate license.');
});
Expand All @@ -42,8 +41,7 @@ describe('License: verifyLicense', () => {
verifyLicense({
releaseInfo: RELEASE_INFO,
licenseKey,
acceptedScopes: ['pro', 'premium'],
productScope: 'data-grid',
packageName: 'x-data-grid-pro',
}).status,
).to.equal(LICENSE_STATUS.Valid);
});
Expand All @@ -62,8 +60,7 @@ describe('License: verifyLicense', () => {
verifyLicense({
releaseInfo: RELEASE_INFO,
licenseKey: expiredLicenseKey,
acceptedScopes: ['pro', 'premium'],
productScope: 'data-grid',
packageName: 'x-data-grid-pro',
}).status,
).to.equal(LICENSE_STATUS.ExpiredVersion);
});
Expand All @@ -75,8 +72,7 @@ describe('License: verifyLicense', () => {
releaseInfo: RELEASE_INFO,
licenseKey:
'b43ff5f9ac93f021855ff59ff0ba5220TkFNRTpNYC1VSSBTQVMsREVWRUxPUEVSX0NPVU5UPTEwLEVYUElSWT0xNTkxNzIzMDY3MDQyLFZFUlNJT049MS4yLjM',
acceptedScopes: ['pro', 'premium'],
productScope: 'data-grid',
packageName: 'x-data-grid-pro',
}).status,
).to.equal(LICENSE_STATUS.Invalid);
});
Expand Down Expand Up @@ -106,8 +102,7 @@ describe('License: verifyLicense', () => {
verifyLicense({
releaseInfo: '__RELEASE_INFO__',
licenseKey: licenseKeyPro,
acceptedScopes: ['pro', 'premium'],
productScope: 'data-grid',
packageName: 'x-data-grid-pro',
}).status,
).to.throw('MUI X: The release information is invalid. Not able to validate license.');
});
Expand All @@ -119,8 +114,7 @@ describe('License: verifyLicense', () => {
verifyLicense({
releaseInfo: RELEASE_INFO,
licenseKey: licenseKeyPro,
acceptedScopes: ['pro', 'premium'],
productScope: 'data-grid',
packageName: 'x-data-grid-pro',
}).status,
).to.equal(LICENSE_STATUS.Valid);
});
Expand All @@ -131,8 +125,7 @@ describe('License: verifyLicense', () => {
verifyLicense({
releaseInfo: RELEASE_INFO,
licenseKey: licenseKeyPremium,
acceptedScopes: ['premium'],
productScope: 'data-grid',
packageName: 'x-data-grid-premium',
}).status,
).to.equal(LICENSE_STATUS.Valid);
});
Expand All @@ -143,8 +136,7 @@ describe('License: verifyLicense', () => {
verifyLicense({
releaseInfo: RELEASE_INFO,
licenseKey: licenseKeyPro,
acceptedScopes: ['premium'],
productScope: 'data-grid',
packageName: 'x-data-grid-premium',
}).status,
).to.equal(LICENSE_STATUS.OutOfScope);
});
Expand All @@ -165,8 +157,7 @@ describe('License: verifyLicense', () => {
verifyLicense({
releaseInfo: RELEASE_INFO,
licenseKey: expiredLicenseKey,
acceptedScopes: ['pro', 'premium'],
productScope: 'data-grid',
packageName: 'x-data-grid-pro',
}).status,
).to.equal(LICENSE_STATUS.Valid);
});
Expand All @@ -184,8 +175,7 @@ describe('License: verifyLicense', () => {
verifyLicense({
releaseInfo: RELEASE_INFO,
licenseKey: expiredLicenseKey,
acceptedScopes: ['pro', 'premium'],
productScope: 'data-grid',
packageName: 'x-data-grid-pro',
}).status,
).to.equal(LICENSE_STATUS.ExpiredAnnualGrace);
});
Expand All @@ -204,8 +194,7 @@ describe('License: verifyLicense', () => {
verifyLicense({
releaseInfo: RELEASE_INFO,
licenseKey: expiredLicenseKey,
acceptedScopes: ['pro', 'premium'],
productScope: 'data-grid',
packageName: 'x-data-grid-pro',
}).status,
).to.equal(LICENSE_STATUS.ExpiredAnnual);
});
Expand All @@ -223,8 +212,7 @@ describe('License: verifyLicense', () => {
verifyLicense({
releaseInfo: RELEASE_INFO,
licenseKey: expiredLicenseKey,
acceptedScopes: ['pro', 'premium'],
productScope: 'data-grid',
packageName: 'x-data-grid-pro',
}).status,
).to.equal(LICENSE_STATUS.Valid);
});
Expand All @@ -237,8 +225,7 @@ describe('License: verifyLicense', () => {
releaseInfo: RELEASE_INFO,
licenseKey:
'b43ff5f9ac93f021855ff59ff0ba5220TkFNRTpNYC1VSSBTQVMsREVWRUxPUEVSX0NPVU5UPTEwLEVYUElSWT0xNTkxNzIzMDY3MDQyLFZFUlNJT049MS4yLjM',
acceptedScopes: ['pro', 'premium'],
productScope: 'data-grid',
packageName: 'x-data-grid-pro',
}).status,
).to.equal(LICENSE_STATUS.Invalid);
});
Expand All @@ -259,74 +246,99 @@ describe('License: verifyLicense', () => {
verifyLicense({
releaseInfo: RELEASE_INFO,
licenseKey: licenseKeyPro,
acceptedScopes: ['pro', 'premium'],
productScope: 'data-grid',
packageName: 'x-data-grid-pro',
}).status,
).to.equal(LICENSE_STATUS.Valid);
});
});

describe('key version: 2.2', () => {
const licenseKeyInitial = generateLicense({
const proLicenseKeyInitial = generateLicense({
expiryDate: new Date(releaseDate.getTime() + oneDayInMS),
orderNumber: 'MUI-123',
scope: 'pro',
licensingModel: 'annual',
planVersion: 'initial',
});

const licenseKey2 = generateLicense({
const premiumLicenseKeyInitial = generateLicense({
expiryDate: new Date(releaseDate.getTime() + oneDayInMS),
orderNumber: 'MUI-123',
scope: 'premium',
licensingModel: 'annual',
planVersion: 'initial',
});

const proLicenseKeyQ32024 = generateLicense({
expiryDate: new Date(releaseDate.getTime() + oneDayInMS),
orderNumber: 'MUI-123',
scope: 'pro',
licensingModel: 'annual',
planVersion: 'Q3-2024',
});

it('PlanVersion "initial" should not accept charts', () => {
it('PlanVersion "initial" should not accept x-charts-pro', () => {
process.env.NODE_ENV = 'production';
expect(
verifyLicense({
releaseInfo: RELEASE_INFO,
licenseKey: licenseKeyInitial,
acceptedScopes: ['pro', 'premium'],
productScope: 'charts',
licenseKey: proLicenseKeyInitial,
packageName: 'x-charts-pro',
}).status,
).to.equal(LICENSE_STATUS.NotAvailableInInitialProPlan);
});

it('PlanVersion "initial" should not accept tree-view', () => {
it('PlanVersion "initial" should not accept x-tree-view-pro', () => {
process.env.NODE_ENV = 'production';
expect(
verifyLicense({
releaseInfo: RELEASE_INFO,
licenseKey: licenseKeyInitial,
acceptedScopes: ['pro', 'premium'],
productScope: 'tree-view',
licenseKey: proLicenseKeyInitial,
packageName: 'x-tree-view-pro',
}).status,
).to.equal(LICENSE_STATUS.NotAvailableInInitialProPlan);
});

it('PlanVersion "Q3-2024" should accept charts', () => {
it('PlanVersion "Q3-2024" should accept x-charts-pro', () => {
process.env.NODE_ENV = 'production';
expect(
verifyLicense({
releaseInfo: RELEASE_INFO,
licenseKey: proLicenseKeyQ32024,
packageName: 'x-charts-pro',
}).status,
).to.equal(LICENSE_STATUS.Valid);
});

it('PlanVersion "Q3-2024" should accept x-tree-view-pro', () => {
process.env.NODE_ENV = 'production';
expect(
verifyLicense({
releaseInfo: RELEASE_INFO,
licenseKey: proLicenseKeyQ32024,
packageName: 'x-tree-view-pro',
}).status,
).to.equal(LICENSE_STATUS.Valid);
});

it('Premium with planVersion "initial" should accept x-tree-view-pro', () => {
process.env.NODE_ENV = 'production';
expect(
verifyLicense({
releaseInfo: RELEASE_INFO,
licenseKey: licenseKey2,
acceptedScopes: ['pro', 'premium'],
productScope: 'charts',
licenseKey: premiumLicenseKeyInitial,
packageName: 'x-tree-view-pro',
}).status,
).to.equal(LICENSE_STATUS.Valid);
});

it('PlanVersion "Q3-2024" should accept tree-view', () => {
it('Premium with planVersion "initial" should accept x-charts-pro', () => {
process.env.NODE_ENV = 'production';
expect(
verifyLicense({
releaseInfo: RELEASE_INFO,
licenseKey: licenseKey2,
acceptedScopes: ['pro', 'premium'],
productScope: 'tree-view',
licenseKey: premiumLicenseKeyInitial,
packageName: 'x-charts-pro',
}).status,
).to.equal(LICENSE_STATUS.Valid);
});
Expand Down
Loading

0 comments on commit 59ba23d

Please sign in to comment.