ToolJet/server/test/modules/licensing/unit/LicenseBase.spec.ts

157 lines
5.6 KiB
TypeScript

import LicenseBase from '@modules/licensing/configs/LicenseBase';
import { Terms } from '@modules/licensing/interfaces/terms';
import { LICENSE_TYPE } from '@modules/licensing/constants';
import { BASIC_PLAN_TERMS as CE_BASIC_PLAN_TERMS } from '@modules/licensing/constants/PlanTerms';
const futureExpiry = new Date();
futureExpiry.setFullYear(futureExpiry.getFullYear() + 1);
const EXPIRY_STR = futureExpiry.toISOString().split('T')[0];
const BASIC_TERMS: Partial<Terms> = {
app: {
pages: { enabled: false, count: 5, features: { appHeaderAndLogo: false, addNavGroup: false } },
permissions: { component: false, query: false, pages: false },
features: { promote: false, release: false, history: false },
components: { navigation: false },
},
features: {},
modules: { enabled: false },
permissions: { customGroups: false },
};
const EXPIRED_EXPIRY = new Date('2020-01-01T23:59:59Z');
function makeLicense(licenseData?: Partial<Terms>, expiryDate?: Date): LicenseBase {
// LicenseBase constructor: (BASIC_PLAN_TERMS, licenseData, updatedDate, startDate, expiryDate, plan)
return new (LicenseBase as any)(
BASIC_TERMS,
licenseData,
new Date(),
new Date(),
expiryDate ?? new Date(EXPIRY_STR + ' 23:59:59')
);
}
describe('LicenseBase — appComponents', () => {
it('returns BASIC_PLAN_TERMS components when license is expired', () => {
const license = makeLicense(
{
expiry: '2020-01-01',
type: LICENSE_TYPE.ENTERPRISE,
features: {},
app: {
pages: { enabled: true, count: 10, features: { appHeaderAndLogo: true, addNavGroup: true } },
permissions: { component: true, query: true, pages: true },
components: { navigation: true },
},
},
EXPIRED_EXPIRY
);
// expired → IsBasicPlan = true → returns BASIC_TERMS components (navigation: false)
expect(license.appComponents).toEqual({ navigation: false });
});
it('returns {} when license has no app.components', () => {
const license = makeLicense({
expiry: EXPIRY_STR,
type: LICENSE_TYPE.ENTERPRISE,
features: {},
// app omitted intentionally
});
expect(license.appComponents).toEqual({});
});
it('returns component values from license when app.components present', () => {
const license = makeLicense({
expiry: EXPIRY_STR,
type: LICENSE_TYPE.ENTERPRISE,
features: {},
app: {
pages: { enabled: true, count: 10, features: { appHeaderAndLogo: true, addNavGroup: true } },
permissions: { component: true, query: true, pages: true },
components: { navigation: true },
},
});
expect(license.appComponents).toEqual({ navigation: true });
});
it('defaults undefined component values to false', () => {
const license = makeLicense({
expiry: EXPIRY_STR,
type: LICENSE_TYPE.ENTERPRISE,
features: {},
app: {
pages: { enabled: true, count: 10, features: { appHeaderAndLogo: true, addNavGroup: true } },
permissions: { component: true, query: true, pages: true },
components: { navigation: undefined },
},
});
expect(license.appComponents).toEqual({ navigation: false });
});
});
describe('LicenseBase — features getter includes componentX keys', () => {
it('includes componentNavigation: false when license has navigation: false', () => {
const license = makeLicense({
expiry: EXPIRY_STR,
type: LICENSE_TYPE.ENTERPRISE,
features: {},
app: {
pages: { enabled: true, count: 10, features: { appHeaderAndLogo: true, addNavGroup: true } },
permissions: { component: true, query: true, pages: true },
components: { navigation: false },
},
});
expect((license.features as any).componentNavigation).toBe(false);
});
it('includes componentNavigation: true when license has navigation: true', () => {
const license = makeLicense({
expiry: EXPIRY_STR,
type: LICENSE_TYPE.ENTERPRISE,
features: {},
app: {
pages: { enabled: true, count: 10, features: { appHeaderAndLogo: true, addNavGroup: true } },
permissions: { component: true, query: true, pages: true },
components: { navigation: true },
},
});
expect((license.features as any).componentNavigation).toBe(true);
});
it('includes no componentX keys when app.components absent from license', () => {
const license = makeLicense({
expiry: EXPIRY_STR,
type: LICENSE_TYPE.ENTERPRISE,
features: {},
});
const keys = Object.keys(license.features as object).filter((k) => k.startsWith('component'));
expect(keys).toHaveLength(0);
});
it('includes componentNavigation from BASIC_PLAN_TERMS when license is expired', () => {
const license = makeLicense(
{
expiry: '2020-01-01',
type: LICENSE_TYPE.ENTERPRISE,
features: {},
app: {
pages: { enabled: true, count: 10, features: { appHeaderAndLogo: true, addNavGroup: true } },
permissions: { component: true, query: true, pages: true },
components: { navigation: true },
},
},
EXPIRED_EXPIRY
);
// expired → IsBasicPlan = true → features uses BASIC_TERMS (navigation: false)
expect((license.features as any).componentNavigation).toBe(false);
});
});
describe('LicenseBase — source basic plan terms', () => {
it('exposes componentNavigation from CE BASIC_PLAN_TERMS', () => {
const license = new (LicenseBase as any)(CE_BASIC_PLAN_TERMS);
expect(license.appComponents).toEqual({ navigation: false });
expect((license.features as any).componentNavigation).toBe(false);
});
});