first commit
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
import type { LicenseProvider, LicenseState } from '@n8n/backend-common';
|
||||
import type { BooleanLicenseFeature, NumericLicenseFeature } from '@n8n/constants';
|
||||
|
||||
import type { License } from '@/license';
|
||||
|
||||
export interface LicenseMockDefaults {
|
||||
features?: BooleanLicenseFeature[];
|
||||
quotas?: Partial<{ [K in NumericLicenseFeature]: number }>;
|
||||
}
|
||||
|
||||
export class LicenseMocker {
|
||||
private _enabledFeatures: Set<BooleanLicenseFeature> = new Set();
|
||||
|
||||
private _defaultFeatures: Set<BooleanLicenseFeature> = new Set();
|
||||
|
||||
private _featureQuotas: Map<NumericLicenseFeature, number> = new Map();
|
||||
|
||||
private _defaultQuotas: Map<NumericLicenseFeature, number> = new Map();
|
||||
|
||||
mock(license: License) {
|
||||
license.isLicensed = this.isFeatureEnabled.bind(this);
|
||||
license.getValue = this.getFeatureValue.bind(this);
|
||||
}
|
||||
|
||||
mockLicenseState(licenseState: LicenseState) {
|
||||
const licenseProvider: LicenseProvider = {
|
||||
isLicensed: this.isFeatureEnabled.bind(this),
|
||||
getValue: this.getFeatureValue.bind(this),
|
||||
};
|
||||
|
||||
licenseState.setLicenseProvider(licenseProvider);
|
||||
}
|
||||
|
||||
reset() {
|
||||
this._enabledFeatures = new Set(this._defaultFeatures);
|
||||
this._featureQuotas = new Map(this._defaultQuotas);
|
||||
}
|
||||
|
||||
setDefaults(defaults: LicenseMockDefaults) {
|
||||
this._defaultFeatures = new Set(defaults.features ?? []);
|
||||
this._defaultQuotas = new Map(
|
||||
Object.entries(defaults.quotas ?? {}) as Array<[NumericLicenseFeature, number]>,
|
||||
);
|
||||
}
|
||||
|
||||
isFeatureEnabled(feature: BooleanLicenseFeature): boolean {
|
||||
return this._enabledFeatures.has(feature);
|
||||
}
|
||||
|
||||
getFeatureValue(feature: string): boolean | number | undefined {
|
||||
if (this._featureQuotas.has(feature as NumericLicenseFeature)) {
|
||||
return this._featureQuotas.get(feature as NumericLicenseFeature);
|
||||
} else if (this._enabledFeatures.has(feature as BooleanLicenseFeature)) {
|
||||
return true;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
enable(feature: BooleanLicenseFeature) {
|
||||
this._enabledFeatures.add(feature);
|
||||
}
|
||||
|
||||
disable(feature: BooleanLicenseFeature) {
|
||||
this._enabledFeatures.delete(feature);
|
||||
}
|
||||
|
||||
setQuota(feature: NumericLicenseFeature, quota: number) {
|
||||
this._featureQuotas.set(feature, quota);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user