2022-11-21 06:41:24 -08:00
|
|
|
import { LicenseManager } from '@n8n_io/license-sdk';
|
2023-10-23 04:39:35 -07:00
|
|
|
import { InstanceSettings } from 'n8n-core';
|
2023-11-10 06:04:26 -08:00
|
|
|
import { mock } from 'jest-mock-extended';
|
2022-11-21 06:41:24 -08:00
|
|
|
import config from '@/config';
|
|
|
|
import { License } from '@/License';
|
2023-10-25 07:35:22 -07:00
|
|
|
import { Logger } from '@/Logger';
|
2023-01-04 02:38:48 -08:00
|
|
|
import { N8N_VERSION } from '@/constants';
|
2023-11-10 06:04:26 -08:00
|
|
|
import { mockInstance } from '../shared/mocking';
|
2024-01-22 02:16:29 -08:00
|
|
|
import { OrchestrationService } from '@/services/orchestration.service';
|
2022-11-21 06:41:24 -08:00
|
|
|
|
|
|
|
jest.mock('@n8n_io/license-sdk');
|
|
|
|
|
|
|
|
const MOCK_SERVER_URL = 'https://server.com/v1';
|
|
|
|
const MOCK_RENEW_OFFSET = 259200;
|
|
|
|
const MOCK_INSTANCE_ID = 'instance-id';
|
|
|
|
const MOCK_ACTIVATION_KEY = 'activation-key';
|
2023-07-12 05:11:46 -07:00
|
|
|
const MOCK_FEATURE_FLAG = 'feat:sharing';
|
2023-05-03 01:43:13 -07:00
|
|
|
const MOCK_MAIN_PLAN_ID = '1b765dc4-d39d-4ffe-9885-c56dd67c4b26';
|
2022-11-21 06:41:24 -08:00
|
|
|
|
|
|
|
describe('License', () => {
|
|
|
|
beforeAll(() => {
|
|
|
|
config.set('license.serverUrl', MOCK_SERVER_URL);
|
|
|
|
config.set('license.autoRenewEnabled', true);
|
|
|
|
config.set('license.autoRenewOffset', MOCK_RENEW_OFFSET);
|
test: Fix license tests when `tenantId` is not default (no-changelog) (#6977)
License tests fail if the env has a non-default `tenantId`:
<details>
<summary>Stack trace</summary>
```
nt -- license
> n8n@1.3.0 test /Users/ivov/Development/n8n/packages/cli
> pnpm test:sqlite "--" "license"
> n8n@1.3.0 test:sqlite /Users/ivov/Development/n8n/packages/cli
> N8N_LOG_LEVEL=silent DB_TYPE=sqlite jest "--" "license"
FAIL test/unit/License.test.ts
License
✕ initializes license manager (8 ms)
✓ attempts to activate license with provided key
✓ renews license (1 ms)
✓ check if feature is enabled (2 ms)
✓ check if sharing feature is enabled (1 ms)
✓ check fetching entitlements
✓ check fetching feature values (1 ms)
✓ check management jwt
✓ getMainPlan() returns the right entitlement (3 ms)
✓ getMainPlan() returns undefined if there is no main plan
● License › initializes license manager
expect(jest.fn()).toHaveBeenCalledWith(...expected)
- Expected
+ Received
Object {
"autoRenewEnabled": true,
"autoRenewOffset": 259200,
- "deviceFingerprint": Any<Function>,
- "loadCertStr": Any<Function>,
- "logger": Anything,
+ "deviceFingerprint": [Function deviceFingerprint],
+ "loadCertStr": [Function loadCertStr],
+ "logger": Logger {
+ "logger": DerivedLogger {
+ "_events": Object {
+ "data": [Function ondata],
+ "end": [Function bound onceWrapper],
+ "prefinish": [Function prefinish],
+ },
+ "_eventsCount": 3,
+ "_maxListeners": undefined,
+ "_readableState": ReadableState {
+ "autoDestroy": false,
+ "awaitDrain": 0,
+ "buffer": BufferList {
+ "head": null,
+ "length": 0,
+ "tail": null,
+ },
+ "decoder": null,
+ "defaultEncoding": "utf8",
+ "destroyed": false,
+ "emitClose": true,
+ "emittedReadable": false,
+ "encoding": null,
+ "endEmitted": false,
+ "ended": false,
+ "flowing": true,
+ "highWaterMark": 16,
+ "length": 0,
+ "needReadable": true,
+ "objectMode": true,
+ "paused": false,
+ "pipes": Console {
+ "__winstonerror": [Function bound transportEvent],
+ "__winstonwarn": [Function bound transportEvent],
+ "_events": Object {
+ "close": [Function bound onceWrapper],
+ "drain": [Function pipeOnDrainFunctionResult],
+ "error": Array [
+ [Function onerror],
+ [Function bound transportEvent],
+ ],
+ "finish": [Function bound onceWrapper],
+ "unpipe": Array [
+ [Function bound onceWrapper],
+ [Function onunpipe],
+ ],
+ "warn": [Function bound transportEvent],
+ },
+ "_eventsCount": 6,
+ "_maxListeners": 30,
+ "_writableState": WritableState {
+ "autoDestroy": false,
+ "bufferProcessing": false,
+ "bufferedRequest": null,
+ "bufferedRequestCount": 0,
+ "corked": 0,
+ "corkedRequestsFree": CorkedRequest {
+ "entry": null,
+ "finish": [Function anonymous],
+ "next": null,
+ },
+ "decodeStrings": true,
+ "defaultEncoding": "utf8",
+ "destroyed": false,
+ "emitClose": true,
+ "ended": false,
+ "ending": false,
+ "errorEmitted": false,
+ "finalCalled": false,
+ "finished": false,
+ "highWaterMark": 16,
+ "lastBufferedRequest": null,
+ "length": 0,
+ "needDrain": false,
+ "objectMode": true,
+ "onwrite": [Function anonymous],
+ "pendingcb": 0,
+ "prefinished": false,
+ "sync": true,
+ "writecb": null,
+ "writelen": 0,
+ "writing": false,
+ },
+ "consoleWarnLevels": Object {},
+ "eol": "
+ ",
+ "format": Printf {
+ "template": [Function anonymous],
+ },
+ "handleExceptions": undefined,
+ "handleRejections": undefined,
+ "level": undefined,
+ "levels": Object {
+ "debug": 5,
+ "error": 0,
+ "http": 3,
+ "info": 2,
+ "silly": 6,
+ "verbose": 4,
+ "warn": 1,
+ },
+ "name": "console",
+ "parent": [Circular],
+ "silent": undefined,
+ "stderrLevels": Object {},
+ "writable": true,
+ Symbol(kCapture): false,
+ },
+ "pipesCount": 1,
+ "readableListening": false,
+ "reading": false,
+ "readingMore": false,
+ "resumeScheduled": true,
+ "sync": false,
+ },
+ "_transformState": Object {
+ "afterTransform": [Function bound afterTransform],
+ "needTransform": false,
+ "transforming": false,
+ "writecb": null,
+ "writechunk": null,
+ "writeencoding": null,
+ },
+ "_writableState": WritableState {
+ "autoDestroy": false,
+ "bufferProcessing": false,
+ "bufferedRequest": null,
+ "bufferedRequestCount": 0,
+ "corked": 0,
+ "corkedRequestsFree": CorkedRequest {
+ "entry": null,
+ "finish": [Function anonymous],
+ "next": null,
+ },
+ "decodeStrings": true,
+ "defaultEncoding": "utf8",
+ "destroyed": false,
+ "emitClose": true,
+ "ended": false,
+ "ending": false,
+ "errorEmitted": false,
+ "finalCalled": false,
+ "finished": false,
+ "highWaterMark": 16,
+ "lastBufferedRequest": null,
+ "length": 0,
+ "needDrain": false,
+ "objectMode": true,
+ "onwrite": [Function anonymous],
+ "pendingcb": 0,
+ "prefinished": false,
+ "sync": true,
+ "writecb": null,
+ "writelen": 0,
+ "writing": false,
+ },
+ "allowHalfOpen": true,
+ "defaultMeta": null,
+ "exceptions": ExceptionHandler {
+ "handlers": Map {},
+ "logger": [Circular],
+ },
+ "exitOnError": true,
+ "format": Format {
+ "options": Object {},
+ },
+ "level": "silent",
+ "levels": Object {
+ "debug": 5,
+ "error": 0,
+ "http": 3,
+ "info": 2,
+ "silly": 6,
+ "verbose": 4,
+ "warn": 1,
+ },
+ "profilers": Object {},
+ "readable": true,
+ "rejections": RejectionHandler {
+ "handlers": Map {},
+ "logger": [Circular],
+ },
+ "silent": true,
+ "writable": true,
+ Symbol(kCapture): false,
+ },
+ },
"productIdentifier": "n8n-1.3.0",
- "saveCertStr": Any<Function>,
+ "saveCertStr": [Function saveCertStr],
"server": "https://server.com/v1",
- "tenantId": 1,
+ "tenantId": 1001,
},
Number of calls: 1
29 |
30 | test('initializes license manager', async () => {
> 31 | expect(LicenseManager).toHaveBeenCalledWith({
| ^
32 | autoRenewEnabled: true,
33 | autoRenewOffset: MOCK_RENEW_OFFSET,
34 | deviceFingerprint: expect.any(Function),
at Object.<anonymous> (test/unit/License.test.ts:31:26)
RUNS test/integration/license.api.test.ts
Test Suites: 1 failed, 1 of 2 total
Tests: 1 failed, 9 passed, 10 total
Snapshots: 0 total
Time: 2 s, estimated 4 s
████████████████████████████████████████^C ELIFECYCLE Command failed.
ELIFECYCLE Test failed. See above for more details.
```
</details>
2023-08-21 01:29:21 -07:00
|
|
|
config.set('license.tenantId', 1);
|
2022-11-21 06:41:24 -08:00
|
|
|
});
|
|
|
|
|
2022-12-22 01:14:15 -08:00
|
|
|
let license: License;
|
2023-10-25 07:35:22 -07:00
|
|
|
const logger = mockInstance(Logger);
|
2023-10-23 04:39:35 -07:00
|
|
|
const instanceSettings = mockInstance(InstanceSettings, { instanceId: MOCK_INSTANCE_ID });
|
2024-01-22 02:16:29 -08:00
|
|
|
mockInstance(OrchestrationService);
|
2022-11-21 06:41:24 -08:00
|
|
|
|
|
|
|
beforeEach(async () => {
|
2023-11-17 06:58:50 -08:00
|
|
|
license = new License(logger, instanceSettings, mock(), mock(), mock());
|
2023-10-23 04:39:35 -07:00
|
|
|
await license.init();
|
2022-11-21 06:41:24 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('initializes license manager', async () => {
|
|
|
|
expect(LicenseManager).toHaveBeenCalledWith({
|
|
|
|
autoRenewEnabled: true,
|
|
|
|
autoRenewOffset: MOCK_RENEW_OFFSET,
|
2023-09-17 02:05:54 -07:00
|
|
|
offlineMode: false,
|
2023-09-19 03:10:23 -07:00
|
|
|
renewOnInit: true,
|
2023-09-17 02:05:54 -07:00
|
|
|
deviceFingerprint: expect.any(Function),
|
|
|
|
productIdentifier: `n8n-${N8N_VERSION}`,
|
2023-10-25 07:35:22 -07:00
|
|
|
logger,
|
2023-09-17 02:05:54 -07:00
|
|
|
loadCertStr: expect.any(Function),
|
|
|
|
saveCertStr: expect.any(Function),
|
2023-09-26 04:58:06 -07:00
|
|
|
onFeatureChange: expect.any(Function),
|
2023-10-23 12:39:29 -07:00
|
|
|
collectUsageMetrics: expect.any(Function),
|
2023-09-17 02:05:54 -07:00
|
|
|
server: MOCK_SERVER_URL,
|
|
|
|
tenantId: 1,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('initializes license manager for worker', async () => {
|
2023-11-17 06:58:50 -08:00
|
|
|
license = new License(logger, instanceSettings, mock(), mock(), mock());
|
2023-10-23 04:39:35 -07:00
|
|
|
await license.init('worker');
|
2023-09-17 02:05:54 -07:00
|
|
|
expect(LicenseManager).toHaveBeenCalledWith({
|
|
|
|
autoRenewEnabled: false,
|
|
|
|
autoRenewOffset: MOCK_RENEW_OFFSET,
|
|
|
|
offlineMode: true,
|
2023-09-19 03:10:23 -07:00
|
|
|
renewOnInit: false,
|
2022-11-21 06:41:24 -08:00
|
|
|
deviceFingerprint: expect.any(Function),
|
2023-01-04 02:38:48 -08:00
|
|
|
productIdentifier: `n8n-${N8N_VERSION}`,
|
2023-10-25 07:35:22 -07:00
|
|
|
logger,
|
2022-11-21 06:41:24 -08:00
|
|
|
loadCertStr: expect.any(Function),
|
|
|
|
saveCertStr: expect.any(Function),
|
2023-09-26 04:58:06 -07:00
|
|
|
onFeatureChange: expect.any(Function),
|
2023-10-23 12:39:29 -07:00
|
|
|
collectUsageMetrics: expect.any(Function),
|
2022-11-21 06:41:24 -08:00
|
|
|
server: MOCK_SERVER_URL,
|
|
|
|
tenantId: 1,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-20 01:52:01 -08:00
|
|
|
test('attempts to activate license with provided key', async () => {
|
2022-11-21 06:41:24 -08:00
|
|
|
await license.activate(MOCK_ACTIVATION_KEY);
|
|
|
|
|
|
|
|
expect(LicenseManager.prototype.activate).toHaveBeenCalledWith(MOCK_ACTIVATION_KEY);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('renews license', async () => {
|
|
|
|
await license.renew();
|
|
|
|
|
|
|
|
expect(LicenseManager.prototype.renew).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2023-12-19 03:13:19 -08:00
|
|
|
test('check if feature is enabled', () => {
|
|
|
|
license.isFeatureEnabled(MOCK_FEATURE_FLAG);
|
2022-11-21 06:41:24 -08:00
|
|
|
|
|
|
|
expect(LicenseManager.prototype.hasFeatureEnabled).toHaveBeenCalledWith(MOCK_FEATURE_FLAG);
|
|
|
|
});
|
|
|
|
|
2023-12-19 03:13:19 -08:00
|
|
|
test('check if sharing feature is enabled', () => {
|
|
|
|
license.isFeatureEnabled(MOCK_FEATURE_FLAG);
|
2022-11-21 06:41:24 -08:00
|
|
|
|
|
|
|
expect(LicenseManager.prototype.hasFeatureEnabled).toHaveBeenCalledWith(MOCK_FEATURE_FLAG);
|
|
|
|
});
|
2022-12-20 01:52:01 -08:00
|
|
|
|
2023-12-19 03:13:19 -08:00
|
|
|
test('check fetching entitlements', () => {
|
|
|
|
license.getCurrentEntitlements();
|
2022-12-20 01:52:01 -08:00
|
|
|
|
|
|
|
expect(LicenseManager.prototype.getCurrentEntitlements).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('check fetching feature values', async () => {
|
2023-07-12 05:11:46 -07:00
|
|
|
license.getFeatureValue(MOCK_FEATURE_FLAG);
|
2022-12-20 01:52:01 -08:00
|
|
|
|
2023-07-12 05:11:46 -07:00
|
|
|
expect(LicenseManager.prototype.getFeatureValue).toHaveBeenCalledWith(MOCK_FEATURE_FLAG);
|
2022-12-20 01:52:01 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('check management jwt', async () => {
|
2023-12-19 03:13:19 -08:00
|
|
|
license.getManagementJwt();
|
2022-12-20 01:52:01 -08:00
|
|
|
|
|
|
|
expect(LicenseManager.prototype.getManagementJwt).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2023-05-03 01:43:13 -07:00
|
|
|
test('getMainPlan() returns the right entitlement', async () => {
|
2022-12-20 01:52:01 -08:00
|
|
|
// mock entitlements response
|
|
|
|
License.prototype.getCurrentEntitlements = jest.fn().mockReturnValue([
|
2023-05-03 01:43:13 -07:00
|
|
|
{
|
|
|
|
id: '84a9c852-1349-478d-9ad1-b3f55510e477',
|
|
|
|
productId: '670650f2-72d8-4397-898c-c249906e2cc2',
|
|
|
|
productMetadata: {},
|
|
|
|
features: {},
|
|
|
|
featureOverrides: {},
|
|
|
|
validFrom: new Date(),
|
|
|
|
validTo: new Date(),
|
|
|
|
},
|
2022-12-20 01:52:01 -08:00
|
|
|
{
|
|
|
|
id: MOCK_MAIN_PLAN_ID,
|
2023-05-03 01:43:13 -07:00
|
|
|
productId: '670650f2-72d8-4397-898c-c249906e2cc2',
|
2022-12-20 01:52:01 -08:00
|
|
|
productMetadata: {
|
|
|
|
terms: {
|
|
|
|
isMainPlan: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
features: {},
|
|
|
|
featureOverrides: {},
|
|
|
|
validFrom: new Date(),
|
|
|
|
validTo: new Date(),
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
jest.fn(license.getMainPlan).mockReset();
|
|
|
|
|
|
|
|
const mainPlan = license.getMainPlan();
|
2022-12-22 01:14:15 -08:00
|
|
|
expect(mainPlan?.id).toBe(MOCK_MAIN_PLAN_ID);
|
2022-12-20 01:52:01 -08:00
|
|
|
});
|
2023-05-03 01:43:13 -07:00
|
|
|
|
|
|
|
test('getMainPlan() returns undefined if there is no main plan', async () => {
|
|
|
|
// mock entitlements response
|
|
|
|
License.prototype.getCurrentEntitlements = jest.fn().mockReturnValue([
|
|
|
|
{
|
|
|
|
id: '84a9c852-1349-478d-9ad1-b3f55510e477',
|
|
|
|
productId: '670650f2-72d8-4397-898c-c249906e2cc2',
|
|
|
|
productMetadata: {}, // has no `productMetadata.terms.isMainPlan`!
|
|
|
|
features: {},
|
|
|
|
featureOverrides: {},
|
|
|
|
validFrom: new Date(),
|
|
|
|
validTo: new Date(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'c1aae471-c24e-4874-ad88-b97107de486c',
|
|
|
|
productId: '670650f2-72d8-4397-898c-c249906e2cc2',
|
|
|
|
productMetadata: {}, // has no `productMetadata.terms.isMainPlan`!
|
|
|
|
features: {},
|
|
|
|
featureOverrides: {},
|
|
|
|
validFrom: new Date(),
|
|
|
|
validTo: new Date(),
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
jest.fn(license.getMainPlan).mockReset();
|
|
|
|
|
|
|
|
const mainPlan = license.getMainPlan();
|
|
|
|
expect(mainPlan).toBeUndefined();
|
|
|
|
});
|
2022-11-21 06:41:24 -08:00
|
|
|
});
|