Skip to content

Commit

Permalink
[license] Fix error terminology (#9614)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari authored Jul 12, 2023
1 parent aa9186b commit aa05cd9
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('<DataGridPremium /> - License', () => {
generateLicense({
expiryDate: addYears(new Date(), 1),
orderNumber: 'Test',
licensingModel: 'subscription',
scope: 'pro',
}),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,6 @@ import { expect } from 'chai';
import { generateLicense } from './generateLicense';

describe('License: generateLicense', () => {
// TODO: Remove
it('should generate pro license properly when "scope" is not provided', () => {
expect(
generateLicense({
expiryDate: new Date(1591723879062),
orderNumber: 'MUI-123',
licensingModel: 'subscription',
}),
).to.equal(
'b2b2ea9c6fd846e11770da3c795d6f63Tz1NVUktMTIzLEU9MTU5MTcyMzg3OTA2MixTPXBybyxMTT1zdWJzY3JpcHRpb24sS1Y9Mg==',
);
});

it('should generate pro license properly when `scope: "pro"`', () => {
expect(
generateLicense({
Expand All @@ -41,20 +28,7 @@ describe('License: generateLicense', () => {
);
});

// TODO: Remove
it('should generate perpetual license when "licensingModel" is not provided', () => {
expect(
generateLicense({
expiryDate: new Date(1591723879062),
orderNumber: 'MUI-123',
scope: 'pro',
}),
).to.equal(
'b16edd8e6bc83293a723779a259f520cTz1NVUktMTIzLEU9MTU5MTcyMzg3OTA2MixTPXBybyxMTT1wZXJwZXR1YWwsS1Y9Mg==',
);
});

it('should generate subscription license when `licensingModel: "subscription"`', () => {
it('should generate annual license when `licensingModel: "subscription"`', () => {
expect(
generateLicense({
expiryDate: new Date(1591723879062),
Expand Down
14 changes: 6 additions & 8 deletions packages/x-license-pro/src/generateLicense/generateLicense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ const licenseVersion = '2';
export interface LicenseDetails {
orderNumber: string;
expiryDate: Date;
// TODO: to be made required once the store is updated
scope?: LicenseScope;
// TODO: to be made required once the store is updated
licensingModel?: LicensingModel;
scope: LicenseScope;
licensingModel: LicensingModel;
}

function getClearLicenseString(details: LicenseDetails) {
Expand All @@ -20,12 +18,12 @@ function getClearLicenseString(details: LicenseDetails) {
}

if (details.licensingModel && !LICENSING_MODELS.includes(details.licensingModel)) {
throw new Error('MUI: Invalid sales model');
throw new Error('MUI: Invalid licensing model');
}

return `O=${details.orderNumber},E=${details.expiryDate.getTime()},S=${
details.scope ?? 'pro'
},LM=${details.licensingModel ?? 'perpetual'},KV=${licenseVersion}`;
return `O=${details.orderNumber},E=${details.expiryDate.getTime()},S=${details.scope},LM=${
details.licensingModel
},KV=${licenseVersion}`;
}

export function generateLicense(details: LicenseDetails) {
Expand Down
4 changes: 4 additions & 0 deletions packages/x-license-pro/src/utils/licensingModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export const LICENSING_MODELS = [
* On development, a license is outdated if the expiry date has been reached
* On production, a license is outdated if the current version of the software was released after the expiry date of the license (see "perpetual")
*/
'annual',
/**
* TODO 2025 remove, legacy name of annual.
*/
'subscription',
] as const;

Expand Down
25 changes: 25 additions & 0 deletions packages/x-license-pro/src/verifyLicense/verifyLicense.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ describe('License: verifyLicense', () => {
it('should check expired license properly', () => {
const expiredLicenseKey = generateLicense({
expiryDate: new Date(releaseDate.getTime() - oneDayInMS),
scope: 'pro',
licensingModel: 'perpetual',
orderNumber: 'MUI-123',
});

Expand Down Expand Up @@ -129,6 +131,7 @@ describe('License: verifyLicense', () => {
const expiredLicenseKey = generateLicense({
expiryDate: new Date(releaseDate.getTime() + oneDayInMS),
orderNumber: 'MUI-123',
scope: 'pro',
licensingModel: 'subscription',
});

Expand All @@ -146,6 +149,7 @@ describe('License: verifyLicense', () => {
const expiredLicenseKey = generateLicense({
expiryDate: new Date(releaseDate.getTime() + oneDayInMS),
orderNumber: 'MUI-123',
scope: 'pro',
licensingModel: 'subscription',
});

Expand All @@ -163,6 +167,7 @@ describe('License: verifyLicense', () => {
const expiredLicenseKey = generateLicense({
expiryDate: new Date(releaseDate.getTime() + oneDayInMS),
orderNumber: 'MUI-123',
scope: 'pro',
licensingModel: 'perpetual',
});

Expand All @@ -189,4 +194,24 @@ describe('License: verifyLicense', () => {
).to.equal(LicenseStatus.Invalid);
});
});

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

it('should accept licensingModel="annual"', () => {
expect(
verifyLicense({
releaseInfo: RELEASE_INFO,
licenseKey: licenseKeyPro,
acceptedScopes: ['pro', 'premium'],
isProduction: true,
}),
).to.equal(LicenseStatus.Valid);
});
});
});
4 changes: 2 additions & 2 deletions packages/x-license-pro/src/verifyLicense/verifyLicense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export function verifyLicense({
}

if (license.licensingModel == null || !LICENSING_MODELS.includes(license.licensingModel)) {
console.error('Error checking license. Sales model not found or invalid!');
console.error('Error checking license. Licensing model not found or invalid!');
return LicenseStatus.Invalid;
}

Expand All @@ -147,7 +147,7 @@ export function verifyLicense({
if (license.expiryTimestamp < pkgTimestamp) {
return LicenseStatus.ExpiredVersion;
}
} else if (license.licensingModel === 'subscription') {
} else if (license.licensingModel === 'subscription' || license.licensingModel === 'annual') {
if (license.expiryTimestamp < new Date().getTime()) {
return LicenseStatus.ExpiredAnnual;
}
Expand Down

0 comments on commit aa05cd9

Please sign in to comment.