test(core): Move unit tests closer to testable components (no-changelog) (#10287)

This commit is contained in:
Tomi Turtiainen
2024-08-05 12:12:25 +03:00
committed by GitHub
parent 8131d66f8c
commit afa43e75f6
80 changed files with 95 additions and 105 deletions

View File

@@ -1,6 +1,6 @@
import { v4 as uuid } from 'uuid';
import { Container } from 'typedi';
import type { INode } from 'n8n-workflow';
import type { INode, INodeTypeData } from 'n8n-workflow';
import { randomInt } from 'n8n-workflow';
import type { User } from '@db/entities/User';
import { WorkflowRepository } from '@db/repositories/workflow.repository';
@@ -14,7 +14,6 @@ import { mockInstance } from '../shared/mocking';
import { randomCredentialPayload as randomCred } from '../integration/shared/random';
import * as testDb from '../integration/shared/testDb';
import type { SaveCredentialFunction } from '../integration/shared/types';
import { mockNodeTypesData } from '../unit/Helpers';
import { affixRoleToSaveCredential } from '../integration/shared/db/credentials';
import { createOwner, createUser } from '../integration/shared/db/users';
import { SharedCredentialsRepository } from '@/databases/repositories/sharedCredentials.repository';
@@ -25,6 +24,36 @@ import { ProjectRepository } from '@/databases/repositories/project.repository';
const ownershipService = mockInstance(OwnershipService);
function mockNodeTypesData(
nodeNames: string[],
options?: {
addTrigger?: boolean;
},
) {
return nodeNames.reduce<INodeTypeData>((acc, nodeName) => {
return (
(acc[`n8n-nodes-base.${nodeName}`] = {
sourcePath: '',
type: {
description: {
displayName: nodeName,
name: nodeName,
group: [],
description: '',
version: 1,
defaults: {},
inputs: [],
outputs: [],
properties: [],
},
trigger: options?.addTrigger ? async () => undefined : undefined,
},
}),
acc
);
}, {});
}
const createWorkflow = async (nodes: INode[], workflowOwner?: User): Promise<WorkflowEntity> => {
const workflowDetails = {
id: randomInt(1, 10).toString(),

View File

@@ -11,7 +11,7 @@ import { WaitingWebhooks } from '@/WaitingWebhooks';
import { WaitingForms } from '@/WaitingForms';
import type { IResponseCallbackData } from '@/Interfaces';
import { mockInstance } from '../shared/mocking';
import { mockInstance } from '@test/mocking';
import { GlobalConfig } from '@n8n/config';
import Container from 'typedi';

View File

@@ -0,0 +1,6 @@
/**
* Ensure all pending promises settle. The promise's `resolve` is placed in
* the macrotask queue and so called at the next iteration of the event loop
* after all promises in the microtask queue have settled first.
*/
export const flushPromises = async () => await new Promise(setImmediate);

View File

@@ -8,7 +8,7 @@ import {
randomEmail,
randomName,
uniqueId,
} from '../../integration/shared/random';
} from '../integration/shared/random';
export const mockCredential = (): CredentialsEntity =>
Object.assign(new CredentialsEntity(), randomCredentialPayload());

View File

@@ -1,163 +0,0 @@
import { ActiveExecutions } from '@/ActiveExecutions';
import PCancelable from 'p-cancelable';
import { v4 as uuid } from 'uuid';
import type { IExecuteResponsePromiseData, IRun } from 'n8n-workflow';
import { createDeferredPromise } from 'n8n-workflow';
import type { IWorkflowExecutionDataProcess } from '@/Interfaces';
import type { ExecutionRepository } from '@db/repositories/execution.repository';
import { mock } from 'jest-mock-extended';
import { ConcurrencyControlService } from '@/concurrency/concurrency-control.service';
import { mockInstance } from '@test/mocking';
const FAKE_EXECUTION_ID = '15';
const FAKE_SECOND_EXECUTION_ID = '20';
const updateExistingExecution = jest.fn();
const createNewExecution = jest.fn(async () => FAKE_EXECUTION_ID);
const executionRepository = mock<ExecutionRepository>({
updateExistingExecution,
createNewExecution,
});
const concurrencyControl = mockInstance(ConcurrencyControlService, {
// @ts-expect-error Private property
isEnabled: false,
});
describe('ActiveExecutions', () => {
let activeExecutions: ActiveExecutions;
beforeEach(() => {
activeExecutions = new ActiveExecutions(mock(), executionRepository, concurrencyControl);
});
afterEach(() => {
jest.clearAllMocks();
});
test('Should initialize activeExecutions with empty list', () => {
expect(activeExecutions.getActiveExecutions().length).toBe(0);
});
test('Should add execution to active execution list', async () => {
const newExecution = mockExecutionData();
const executionId = await activeExecutions.add(newExecution);
expect(executionId).toBe(FAKE_EXECUTION_ID);
expect(activeExecutions.getActiveExecutions().length).toBe(1);
expect(createNewExecution).toHaveBeenCalledTimes(1);
expect(updateExistingExecution).toHaveBeenCalledTimes(0);
});
test('Should update execution if add is called with execution ID', async () => {
const newExecution = mockExecutionData();
const executionId = await activeExecutions.add(newExecution, FAKE_SECOND_EXECUTION_ID);
expect(executionId).toBe(FAKE_SECOND_EXECUTION_ID);
expect(activeExecutions.getActiveExecutions().length).toBe(1);
expect(createNewExecution).toHaveBeenCalledTimes(0);
expect(updateExistingExecution).toHaveBeenCalledTimes(1);
});
test('Should fail attaching execution to invalid executionId', async () => {
const deferredPromise = mockCancelablePromise();
expect(() => {
activeExecutions.attachWorkflowExecution(FAKE_EXECUTION_ID, deferredPromise);
}).toThrow();
});
test('Should successfully attach execution to valid executionId', async () => {
const newExecution = mockExecutionData();
await activeExecutions.add(newExecution, FAKE_EXECUTION_ID);
const deferredPromise = mockCancelablePromise();
expect(() =>
activeExecutions.attachWorkflowExecution(FAKE_EXECUTION_ID, deferredPromise),
).not.toThrow();
});
test('Should attach and resolve response promise to existing execution', async () => {
const newExecution = mockExecutionData();
await activeExecutions.add(newExecution, FAKE_EXECUTION_ID);
const deferredPromise = await mockDeferredPromise();
activeExecutions.attachResponsePromise(FAKE_EXECUTION_ID, deferredPromise);
const fakeResponse = { data: { resultData: { runData: {} } } };
activeExecutions.resolveResponsePromise(FAKE_EXECUTION_ID, fakeResponse);
await expect(deferredPromise.promise()).resolves.toEqual(fakeResponse);
});
test('Should remove an existing execution', async () => {
const newExecution = mockExecutionData();
const executionId = await activeExecutions.add(newExecution);
activeExecutions.remove(executionId);
expect(activeExecutions.getActiveExecutions().length).toBe(0);
});
test('Should resolve post execute promise on removal', async () => {
const newExecution = mockExecutionData();
const executionId = await activeExecutions.add(newExecution);
const postExecutePromise = activeExecutions.getPostExecutePromise(executionId);
// Force the above to be executed since we cannot await it
await new Promise((res) => {
setTimeout(res, 100);
});
const fakeOutput = mockFullRunData();
activeExecutions.remove(executionId, fakeOutput);
await expect(postExecutePromise).resolves.toEqual(fakeOutput);
});
test('Should throw error when trying to create a promise with invalid execution', async () => {
await expect(activeExecutions.getPostExecutePromise(FAKE_EXECUTION_ID)).rejects.toThrow();
});
test('Should call function to cancel execution when asked to stop', async () => {
const newExecution = mockExecutionData();
const executionId = await activeExecutions.add(newExecution);
const cancelExecution = jest.fn();
const cancellablePromise = mockCancelablePromise();
cancellablePromise.cancel = cancelExecution;
activeExecutions.attachWorkflowExecution(executionId, cancellablePromise);
void activeExecutions.stopExecution(executionId);
expect(cancelExecution).toHaveBeenCalledTimes(1);
});
});
function mockExecutionData(): IWorkflowExecutionDataProcess {
return {
executionMode: 'manual',
workflowData: {
id: '123',
name: 'Test workflow 1',
active: false,
createdAt: new Date(),
updatedAt: new Date(),
nodes: [],
connections: {},
},
userId: uuid(),
};
}
function mockFullRunData(): IRun {
return {
data: {
resultData: {
runData: {},
},
},
mode: 'manual',
startedAt: new Date(),
status: 'new',
};
}
// eslint-disable-next-line @typescript-eslint/promise-function-async
const mockCancelablePromise = () => new PCancelable<IRun>((resolve) => resolve());
// eslint-disable-next-line @typescript-eslint/promise-function-async
const mockDeferredPromise = () => createDeferredPromise<IExecuteResponsePromiseData>();

View File

@@ -1,40 +0,0 @@
import { CredentialTypes } from '@/CredentialTypes';
import { Container } from 'typedi';
import { LoadNodesAndCredentials } from '@/LoadNodesAndCredentials';
import { mockInstance } from '../shared/mocking';
describe('CredentialTypes', () => {
const mockNodesAndCredentials = mockInstance(LoadNodesAndCredentials, {
loadedCredentials: {
fakeFirstCredential: {
type: {
name: 'fakeFirstCredential',
displayName: 'Fake First Credential',
properties: [],
},
sourcePath: '',
},
fakeSecondCredential: {
type: {
name: 'fakeSecondCredential',
displayName: 'Fake Second Credential',
properties: [],
},
sourcePath: '',
},
},
});
const credentialTypes = Container.get(CredentialTypes);
test('Should throw error when calling invalid credential name', () => {
expect(() => credentialTypes.getByName('fakeThirdCredential')).toThrowError();
});
test('Should return correct credential type for valid name', () => {
const mockedCredentialTypes = mockNodesAndCredentials.loadedCredentials;
expect(credentialTypes.getByName('fakeFirstCredential')).toStrictEqual(
mockedCredentialTypes.fakeFirstCredential.type,
);
});
});

View File

@@ -1,296 +0,0 @@
import Container from 'typedi';
import type {
IAuthenticateGeneric,
ICredentialDataDecryptedObject,
ICredentialType,
IHttpRequestOptions,
INode,
INodeProperties,
} from 'n8n-workflow';
import { deepCopy } from 'n8n-workflow';
import { Workflow } from 'n8n-workflow';
import { CredentialsHelper } from '@/CredentialsHelper';
import { NodeTypes } from '@/NodeTypes';
import { LoadNodesAndCredentials } from '@/LoadNodesAndCredentials';
import { CredentialsRepository } from '@db/repositories/credentials.repository';
import { SharedCredentialsRepository } from '@db/repositories/sharedCredentials.repository';
import { mockInstance } from '../shared/mocking';
describe('CredentialsHelper', () => {
mockInstance(CredentialsRepository);
mockInstance(SharedCredentialsRepository);
const mockNodesAndCredentials = mockInstance(LoadNodesAndCredentials, {
loadedNodes: {
'test.set': {
sourcePath: '',
type: {
description: {
displayName: 'Set',
name: 'set',
group: ['input'],
version: 1,
description: 'Sets a value',
defaults: {
name: 'Set',
color: '#0000FF',
},
inputs: ['main'],
outputs: ['main'],
properties: [
{
displayName: 'Value1',
name: 'value1',
type: 'string',
default: 'default-value1',
},
{
displayName: 'Value2',
name: 'value2',
type: 'string',
default: 'default-value2',
},
],
},
},
},
},
});
const nodeTypes = mockInstance(NodeTypes);
describe('authenticate', () => {
const tests: Array<{
description: string;
input: {
credentials: ICredentialDataDecryptedObject;
credentialType: ICredentialType;
};
output: IHttpRequestOptions;
}> = [
{
description: 'basicAuth, default property names',
input: {
credentials: {
user: 'user1',
password: 'password1',
},
credentialType: new (class TestApi implements ICredentialType {
name = 'testApi';
displayName = 'Test API';
properties: INodeProperties[] = [
{
displayName: 'User',
name: 'user',
type: 'string',
default: '',
},
{
displayName: 'Password',
name: 'password',
type: 'string',
default: '',
},
];
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
auth: {
username: '={{$credentials.user}}',
password: '={{$credentials.password}}',
},
},
};
})(),
},
output: {
url: '',
headers: {},
auth: { username: 'user1', password: 'password1' },
qs: {},
},
},
{
description: 'headerAuth',
input: {
credentials: {
accessToken: 'test',
},
credentialType: new (class TestApi implements ICredentialType {
name = 'testApi';
displayName = 'Test API';
properties: INodeProperties[] = [
{
displayName: 'Access Token',
name: 'accessToken',
type: 'string',
default: '',
},
];
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {
Authorization: '=Bearer {{$credentials.accessToken}}',
},
},
};
})(),
},
output: { url: '', headers: { Authorization: 'Bearer test' }, qs: {} },
},
{
description: 'headerAuth, key and value expressions',
input: {
credentials: {
accessToken: 'test',
},
credentialType: new (class TestApi implements ICredentialType {
name = 'testApi';
displayName = 'Test API';
properties: INodeProperties[] = [
{
displayName: 'Access Token',
name: 'accessToken',
type: 'string',
default: '',
},
];
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {
'={{$credentials.accessToken}}': '=Bearer {{$credentials.accessToken}}',
},
},
};
})(),
},
output: { url: '', headers: { test: 'Bearer test' }, qs: {} },
},
{
description: 'queryAuth',
input: {
credentials: {
accessToken: 'test',
},
credentialType: new (class TestApi implements ICredentialType {
name = 'testApi';
displayName = 'Test API';
properties: INodeProperties[] = [
{
displayName: 'Access Token',
name: 'accessToken',
type: 'string',
default: '',
},
];
authenticate = {
type: 'generic',
properties: {
qs: {
accessToken: '={{$credentials.accessToken}}',
},
},
} as IAuthenticateGeneric;
})(),
},
output: { url: '', headers: {}, qs: { accessToken: 'test' } },
},
{
description: 'custom authentication',
input: {
credentials: {
accessToken: 'test',
user: 'testUser',
},
credentialType: new (class TestApi implements ICredentialType {
name = 'testApi';
displayName = 'Test API';
properties: INodeProperties[] = [
{
displayName: 'My Token',
name: 'myToken',
type: 'string',
default: '',
},
];
async authenticate(
credentials: ICredentialDataDecryptedObject,
requestOptions: IHttpRequestOptions,
): Promise<IHttpRequestOptions> {
requestOptions.headers!.Authorization = `Bearer ${credentials.accessToken}`;
requestOptions.qs!.user = credentials.user;
return requestOptions;
}
})(),
},
output: {
url: '',
headers: { Authorization: 'Bearer test' },
qs: { user: 'testUser' },
},
},
];
const node: INode = {
id: 'uuid-1',
parameters: {},
name: 'test',
type: 'test.set',
typeVersion: 1,
position: [0, 0],
};
const incomingRequestOptions = {
url: '',
headers: {},
qs: {},
};
const workflow = new Workflow({
nodes: [node],
connections: {},
active: false,
nodeTypes,
});
for (const testData of tests) {
test(testData.description, async () => {
//@ts-expect-error `loadedCredentials` is a getter and we are replacing it here with a property
mockNodesAndCredentials.loadedCredentials = {
[testData.input.credentialType.name]: {
type: testData.input.credentialType,
sourcePath: '',
},
};
const credentialsHelper = Container.get(CredentialsHelper);
const result = await credentialsHelper.authenticate(
testData.input.credentials,
testData.input.credentialType.name,
deepCopy(incomingRequestOptions),
workflow,
node,
);
expect(result).toEqual(testData.output);
});
}
});
});

View File

@@ -1,40 +0,0 @@
import { Container } from 'typedi';
import { ExecutionMetadataRepository } from '@db/repositories/executionMetadata.repository';
import { ExecutionMetadataService } from '@/services/executionMetadata.service';
import { mockInstance } from '../shared/mocking';
describe('ExecutionMetadataService', () => {
const repository = mockInstance(ExecutionMetadataRepository);
test('Execution metadata is saved in a batch', async () => {
const toSave = {
test1: 'value1',
test2: 'value2',
};
const executionId = '1234';
await Container.get(ExecutionMetadataService).save(executionId, toSave);
expect(repository.upsert).toHaveBeenCalledTimes(1);
expect(repository.upsert.mock.calls[0]).toEqual([
[
{
executionId,
key: 'test1',
value: 'value1',
},
{
executionId,
key: 'test2',
value: 'value2',
},
],
{
conflictPaths: {
executionId: true,
key: true,
},
},
]);
});
});

View File

@@ -1,167 +0,0 @@
import { Container } from 'typedi';
import { Cipher } from 'n8n-core';
import { SettingsRepository } from '@db/repositories/settings.repository';
import type { ExternalSecretsSettings } from '@/Interfaces';
import { License } from '@/License';
import { ExternalSecretsManager } from '@/ExternalSecrets/ExternalSecretsManager.ee';
import { ExternalSecretsProviders } from '@/ExternalSecrets/ExternalSecretsProviders.ee';
import { InternalHooks } from '@/InternalHooks';
import { mockInstance } from '../../shared/mocking';
import {
DummyProvider,
ErrorProvider,
FailedProvider,
MockProviders,
} from '../../shared/ExternalSecrets/utils';
import { mock } from 'jest-mock-extended';
describe('External Secrets Manager', () => {
const connectedDate = '2023-08-01T12:32:29.000Z';
let settings: string | null = null;
const mockProvidersInstance = new MockProviders();
const license = mockInstance(License);
const settingsRepo = mockInstance(SettingsRepository);
mockInstance(InternalHooks);
const cipher = Container.get(Cipher);
let providersMock: ExternalSecretsProviders;
let manager: ExternalSecretsManager;
const createMockSettings = (settings: ExternalSecretsSettings): string => {
return cipher.encrypt(settings);
};
const decryptSettings = (settings: string) => {
return JSON.parse(cipher.decrypt(settings));
};
beforeAll(() => {
providersMock = mockInstance(ExternalSecretsProviders, mockProvidersInstance);
settings = createMockSettings({
dummy: { connected: true, connectedAt: new Date(connectedDate), settings: {} },
});
});
beforeEach(() => {
mockProvidersInstance.setProviders({
dummy: DummyProvider,
});
license.isExternalSecretsEnabled.mockReturnValue(true);
settingsRepo.getEncryptedSecretsProviderSettings.mockResolvedValue(settings);
manager = new ExternalSecretsManager(
mock(),
settingsRepo,
license,
providersMock,
cipher,
mock(),
);
});
afterEach(() => {
manager?.shutdown();
jest.useRealTimers();
});
test('should get secret', async () => {
await manager.init();
expect(manager.getSecret('dummy', 'test1')).toBe('value1');
});
test('should not throw errors during init', async () => {
mockProvidersInstance.setProviders({
dummy: ErrorProvider,
});
expect(async () => await manager!.init()).not.toThrow();
});
test('should not throw errors during shutdown', async () => {
mockProvidersInstance.setProviders({
dummy: ErrorProvider,
});
await manager.init();
expect(() => manager!.shutdown()).not.toThrow();
});
test('should save provider settings', async () => {
const settingsSpy = jest.spyOn(settingsRepo, 'saveEncryptedSecretsProviderSettings');
await manager.init();
await manager.setProviderSettings('dummy', {
test: 'value',
});
expect(decryptSettings(settingsSpy.mock.calls[0][0])).toEqual({
dummy: {
connected: true,
connectedAt: connectedDate,
settings: {
test: 'value',
},
},
});
});
test('should call provider update functions on a timer', async () => {
jest.useFakeTimers();
await manager.init();
const updateSpy = jest.spyOn(manager.getProvider('dummy')!, 'update');
expect(updateSpy).toBeCalledTimes(0);
jest.runOnlyPendingTimers();
expect(updateSpy).toBeCalledTimes(1);
});
test('should not call provider update functions if the not licensed', async () => {
jest.useFakeTimers();
license.isExternalSecretsEnabled.mockReturnValue(false);
await manager.init();
const updateSpy = jest.spyOn(manager.getProvider('dummy')!, 'update');
expect(updateSpy).toBeCalledTimes(0);
jest.runOnlyPendingTimers();
expect(updateSpy).toBeCalledTimes(0);
});
test('should not call provider update functions if the provider has an error', async () => {
jest.useFakeTimers();
mockProvidersInstance.setProviders({
dummy: FailedProvider,
});
await manager.init();
const updateSpy = jest.spyOn(manager.getProvider('dummy')!, 'update');
expect(updateSpy).toBeCalledTimes(0);
jest.runOnlyPendingTimers();
expect(updateSpy).toBeCalledTimes(0);
});
test('should reinitialize a provider when save provider settings', async () => {
await manager.init();
const dummyInitSpy = jest.spyOn(DummyProvider.prototype, 'init');
await manager.setProviderSettings('dummy', {
test: 'value',
});
expect(dummyInitSpy).toBeCalledTimes(1);
});
});

View File

@@ -1,39 +0,0 @@
import { SourceControlGitService } from '@/environments/sourceControl/sourceControlGit.service.ee';
import { mock } from 'jest-mock-extended';
import type { SourceControlPreferences } from '@/environments/sourceControl/types/sourceControlPreferences';
import type { User } from '@/databases/entities/User';
import type { SimpleGit } from 'simple-git';
describe('GitService', () => {
describe('initRepository', () => {
describe('when local repo is set up after remote is ready', () => {
it('should track remote', async () => {
/**
* Arrange
*/
const gitService = new SourceControlGitService(mock(), mock(), mock());
const prefs = mock<SourceControlPreferences>({ branchName: 'main' });
const user = mock<User>();
const git = mock<SimpleGit>();
const checkoutSpy = jest.spyOn(git, 'checkout');
const branchSpy = jest.spyOn(git, 'branch');
gitService.git = git;
jest.spyOn(gitService, 'setGitSshCommand').mockResolvedValue();
jest
.spyOn(gitService, 'getBranches')
.mockResolvedValue({ currentBranch: '', branches: ['main'] });
/**
* Act
*/
await gitService.initRepository(prefs, user);
/**
* Assert
*/
expect(checkoutSpy).toHaveBeenCalledWith('main');
expect(branchSpy).toHaveBeenCalledWith(['--set-upstream-to=origin/main', 'main']);
});
});
});
});

View File

@@ -1,38 +0,0 @@
import type { INodeTypeData } from 'n8n-workflow';
/**
* Ensure all pending promises settle. The promise's `resolve` is placed in
* the macrotask queue and so called at the next iteration of the event loop
* after all promises in the microtask queue have settled first.
*/
export const flushPromises = async () => await new Promise(setImmediate);
export function mockNodeTypesData(
nodeNames: string[],
options?: {
addTrigger?: boolean;
},
) {
return nodeNames.reduce<INodeTypeData>((acc, nodeName) => {
return (
(acc[`n8n-nodes-base.${nodeName}`] = {
sourcePath: '',
type: {
description: {
displayName: nodeName,
name: nodeName,
group: [],
description: '',
version: 1,
defaults: {},
inputs: [],
outputs: [],
properties: [],
},
trigger: options?.addTrigger ? async () => undefined : undefined,
},
}),
acc
);
}, {});
}

View File

@@ -1,40 +0,0 @@
import { UserRepository } from '@/databases/repositories/user.repository';
import { mockInstance } from '../../shared/mocking';
import * as helpers from '@/Ldap/helpers.ee';
import { AuthIdentity } from '@/databases/entities/AuthIdentity';
import { User } from '@/databases/entities/User';
import { generateNanoId } from '@/databases/utils/generators';
const userRepository = mockInstance(UserRepository);
describe('Ldap/helpers', () => {
describe('updateLdapUserOnLocalDb', () => {
// We need to use `save` so that that the subscriber in
// packages/cli/src/databases/entities/Project.ts receives the full user.
// With `update` it would only receive the updated fields, e.g. the `id`
// would be missing.
test('does not use `Repository.update`, but `Repository.save` instead', async () => {
//
// ARRANGE
//
const user = Object.assign(new User(), { id: generateNanoId() } as User);
const authIdentity = Object.assign(new AuthIdentity(), {
user: { id: user.id },
} as AuthIdentity);
const data: Partial<User> = { firstName: 'Nathan', lastName: 'Nathaniel' };
userRepository.findOneBy.mockResolvedValueOnce(user);
//
// ACT
//
await helpers.updateLdapUserOnLocalDb(authIdentity, data);
//
// ASSERT
//
expect(userRepository.save).toHaveBeenCalledWith({ ...user, ...data }, { transaction: true });
expect(userRepository.update).not.toHaveBeenCalled();
});
});
});

View File

@@ -1,273 +0,0 @@
import { LicenseManager } from '@n8n_io/license-sdk';
import { InstanceSettings } from 'n8n-core';
import { mock } from 'jest-mock-extended';
import config from '@/config';
import { License } from '@/License';
import { Logger } from '@/Logger';
import { N8N_VERSION } from '@/constants';
import { mockInstance } from '../shared/mocking';
import { OrchestrationService } from '@/services/orchestration.service';
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';
const MOCK_FEATURE_FLAG = 'feat:sharing';
const MOCK_MAIN_PLAN_ID = '1b765dc4-d39d-4ffe-9885-c56dd67c4b26';
describe('License', () => {
beforeAll(() => {
config.set('license.serverUrl', MOCK_SERVER_URL);
config.set('license.autoRenewEnabled', true);
config.set('license.autoRenewOffset', MOCK_RENEW_OFFSET);
config.set('license.tenantId', 1);
});
let license: License;
const logger = mockInstance(Logger);
const instanceSettings = mockInstance(InstanceSettings, { instanceId: MOCK_INSTANCE_ID });
mockInstance(OrchestrationService);
beforeEach(async () => {
license = new License(logger, instanceSettings, mock(), mock(), mock());
await license.init();
});
test('initializes license manager', async () => {
expect(LicenseManager).toHaveBeenCalledWith({
autoRenewEnabled: true,
autoRenewOffset: MOCK_RENEW_OFFSET,
offlineMode: false,
renewOnInit: true,
deviceFingerprint: expect.any(Function),
productIdentifier: `n8n-${N8N_VERSION}`,
logger,
loadCertStr: expect.any(Function),
saveCertStr: expect.any(Function),
onFeatureChange: expect.any(Function),
collectUsageMetrics: expect.any(Function),
collectPassthroughData: expect.any(Function),
server: MOCK_SERVER_URL,
tenantId: 1,
});
});
test('initializes license manager for worker', async () => {
license = new License(logger, instanceSettings, mock(), mock(), mock());
await license.init('worker');
expect(LicenseManager).toHaveBeenCalledWith({
autoRenewEnabled: false,
autoRenewOffset: MOCK_RENEW_OFFSET,
offlineMode: true,
renewOnInit: false,
deviceFingerprint: expect.any(Function),
productIdentifier: `n8n-${N8N_VERSION}`,
logger,
loadCertStr: expect.any(Function),
saveCertStr: expect.any(Function),
onFeatureChange: expect.any(Function),
collectUsageMetrics: expect.any(Function),
collectPassthroughData: expect.any(Function),
server: MOCK_SERVER_URL,
tenantId: 1,
});
});
test('attempts to activate license with provided key', async () => {
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();
});
test('check if feature is enabled', () => {
license.isFeatureEnabled(MOCK_FEATURE_FLAG);
expect(LicenseManager.prototype.hasFeatureEnabled).toHaveBeenCalledWith(MOCK_FEATURE_FLAG);
});
test('check if sharing feature is enabled', () => {
license.isFeatureEnabled(MOCK_FEATURE_FLAG);
expect(LicenseManager.prototype.hasFeatureEnabled).toHaveBeenCalledWith(MOCK_FEATURE_FLAG);
});
test('check fetching entitlements', () => {
license.getCurrentEntitlements();
expect(LicenseManager.prototype.getCurrentEntitlements).toHaveBeenCalled();
});
test('check fetching feature values', async () => {
license.getFeatureValue(MOCK_FEATURE_FLAG);
expect(LicenseManager.prototype.getFeatureValue).toHaveBeenCalledWith(MOCK_FEATURE_FLAG);
});
test('check management jwt', async () => {
license.getManagementJwt();
expect(LicenseManager.prototype.getManagementJwt).toHaveBeenCalled();
});
test('getMainPlan() returns the right entitlement', async () => {
// mock entitlements response
License.prototype.getCurrentEntitlements = jest.fn().mockReturnValue([
{
id: '84a9c852-1349-478d-9ad1-b3f55510e477',
productId: '670650f2-72d8-4397-898c-c249906e2cc2',
productMetadata: {},
features: {},
featureOverrides: {},
validFrom: new Date(),
validTo: new Date(),
},
{
id: MOCK_MAIN_PLAN_ID,
productId: '670650f2-72d8-4397-898c-c249906e2cc2',
productMetadata: {
terms: {
isMainPlan: true,
},
},
features: {},
featureOverrides: {},
validFrom: new Date(),
validTo: new Date(),
},
]);
jest.fn(license.getMainPlan).mockReset();
const mainPlan = license.getMainPlan();
expect(mainPlan?.id).toBe(MOCK_MAIN_PLAN_ID);
});
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();
});
});
describe('License', () => {
beforeEach(() => {
config.load(config.default);
});
describe('init', () => {
describe('in single-main setup', () => {
describe('with `license.autoRenewEnabled` enabled', () => {
it('should enable renewal', async () => {
config.set('multiMainSetup.enabled', false);
await new License(mock(), mock(), mock(), mock(), mock()).init();
expect(LicenseManager).toHaveBeenCalledWith(
expect.objectContaining({ autoRenewEnabled: true, renewOnInit: true }),
);
});
});
describe('with `license.autoRenewEnabled` disabled', () => {
it('should disable renewal', async () => {
config.set('license.autoRenewEnabled', false);
await new License(mock(), mock(), mock(), mock(), mock()).init();
expect(LicenseManager).toHaveBeenCalledWith(
expect.objectContaining({ autoRenewEnabled: false, renewOnInit: false }),
);
});
});
});
describe('in multi-main setup', () => {
describe('with `license.autoRenewEnabled` disabled', () => {
test.each(['unset', 'leader', 'follower'])(
'if %s status, should disable removal',
async (status) => {
config.set('multiMainSetup.enabled', true);
config.set('multiMainSetup.instanceType', status);
config.set('license.autoRenewEnabled', false);
await new License(mock(), mock(), mock(), mock(), mock()).init();
expect(LicenseManager).toHaveBeenCalledWith(
expect.objectContaining({ autoRenewEnabled: false, renewOnInit: false }),
);
},
);
});
describe('with `license.autoRenewEnabled` enabled', () => {
test.each(['unset', 'follower'])('if %s status, should disable removal', async (status) => {
config.set('multiMainSetup.enabled', true);
config.set('multiMainSetup.instanceType', status);
config.set('license.autoRenewEnabled', false);
await new License(mock(), mock(), mock(), mock(), mock()).init();
expect(LicenseManager).toHaveBeenCalledWith(
expect.objectContaining({ autoRenewEnabled: false, renewOnInit: false }),
);
});
it('if leader status, should enable renewal', async () => {
config.set('multiMainSetup.enabled', true);
config.set('multiMainSetup.instanceType', 'leader');
await new License(mock(), mock(), mock(), mock(), mock()).init();
expect(LicenseManager).toHaveBeenCalledWith(
expect.objectContaining({ autoRenewEnabled: true, renewOnInit: true }),
);
});
});
});
});
describe('reinit', () => {
it('should reinitialize license manager', async () => {
const license = new License(mock(), mock(), mock(), mock(), mock());
await license.init();
const initSpy = jest.spyOn(license, 'init');
await license.reinit();
expect(initSpy).toHaveBeenCalledWith('main', true);
expect(LicenseManager.prototype.reset).toHaveBeenCalled();
expect(LicenseManager.prototype.initialize).toHaveBeenCalled();
});
});
});

View File

@@ -1,91 +0,0 @@
import { PostHog } from 'posthog-node';
import { InstanceSettings } from 'n8n-core';
import { PostHogClient } from '@/posthog';
import config from '@/config';
import { mockInstance } from '../shared/mocking';
jest.mock('posthog-node');
describe('PostHog', () => {
const instanceId = 'test-id';
const userId = 'distinct-id';
const apiKey = 'api-key';
const apiHost = 'api-host';
const instanceSettings = mockInstance(InstanceSettings, { instanceId });
beforeAll(() => {
config.set('diagnostics.config.posthog.apiKey', apiKey);
config.set('diagnostics.config.posthog.apiHost', apiHost);
});
beforeEach(() => {
config.set('diagnostics.enabled', true);
jest.resetAllMocks();
});
it('inits PostHog correctly', async () => {
const ph = new PostHogClient(instanceSettings);
await ph.init();
expect(PostHog.prototype.constructor).toHaveBeenCalledWith(apiKey, { host: apiHost });
});
it('does not initialize or track if diagnostics are not enabled', async () => {
config.set('diagnostics.enabled', false);
const ph = new PostHogClient(instanceSettings);
await ph.init();
ph.track({
userId: 'test',
event: 'test',
properties: {},
});
expect(PostHog.prototype.constructor).not.toHaveBeenCalled();
expect(PostHog.prototype.capture).not.toHaveBeenCalled();
});
it('captures PostHog events', async () => {
const event = 'test event';
const properties = {
user_id: 'test',
test: true,
};
const ph = new PostHogClient(instanceSettings);
await ph.init();
ph.track({
userId,
event,
properties,
});
expect(PostHog.prototype.capture).toHaveBeenCalledWith({
distinctId: userId,
event,
userId,
properties,
sendFeatureFlags: true,
});
});
it('gets feature flags', async () => {
const createdAt = new Date();
const ph = new PostHogClient(instanceSettings);
await ph.init();
await ph.getFeatureFlags({
id: userId,
createdAt,
});
expect(PostHog.prototype.getAllFlags).toHaveBeenCalledWith(`${instanceId}#${userId}`, {
personProperties: {
created_at_timestamp: createdAt.getTime().toString(),
},
});
});
});

View File

@@ -1,260 +0,0 @@
import Container from 'typedi';
import {
generateSshKeyPair,
getRepoType,
getTrackingInformationFromPostPushResult,
getTrackingInformationFromPrePushResult,
getTrackingInformationFromPullResult,
sourceControlFoldersExistCheck,
} from '@/environments/sourceControl/sourceControlHelper.ee';
import { License } from '@/License';
import { SourceControlPreferencesService } from '@/environments/sourceControl/sourceControlPreferences.service.ee';
import { InstanceSettings } from 'n8n-core';
import path from 'path';
import {
SOURCE_CONTROL_SSH_FOLDER,
SOURCE_CONTROL_GIT_FOLDER,
} from '@/environments/sourceControl/constants';
import { constants as fsConstants, accessSync } from 'fs';
import type { SourceControlledFile } from '@/environments/sourceControl/types/sourceControlledFile';
import type { SourceControlPreferences } from '@/environments/sourceControl/types/sourceControlPreferences';
import { mockInstance } from '../shared/mocking';
const pushResult: SourceControlledFile[] = [
{
file: 'credential_stubs/kkookWGIeey9K4Kt.json',
id: 'kkookWGIeey9K4Kt',
name: '(deleted)',
type: 'credential',
status: 'deleted',
location: 'local',
conflict: false,
updatedAt: '',
pushed: true,
},
{
file: 'variable_stubs.json',
id: 'variables',
name: 'variables',
type: 'variables',
status: 'modified',
location: 'local',
conflict: false,
updatedAt: '',
pushed: true,
},
{
file: 'workflows/BpFS26gViuGqrIVP.json',
id: 'BpFS26gViuGqrIVP',
name: 'My workflow 5',
type: 'workflow',
status: 'modified',
location: 'remote',
conflict: true,
pushed: true,
updatedAt: '2023-07-10T10:10:59.000Z',
},
{
file: 'workflows/BpFS26gViuGqrIVP.json',
id: 'BpFS26gViuGqrIVP',
name: 'My workflow 5',
type: 'workflow',
status: 'modified',
location: 'local',
conflict: true,
updatedAt: '2023-07-10T10:10:59.000Z',
},
{
file: 'workflows/dAU6dNthm4TR3gXx.json',
id: 'dAU6dNthm4TR3gXx',
name: 'My workflow 7',
type: 'workflow',
status: 'created',
location: 'local',
conflict: false,
pushed: true,
updatedAt: '2023-07-10T10:02:45.186Z',
},
{
file: 'workflows/haQetoXq9GxHSkft.json',
id: 'haQetoXq9GxHSkft',
name: 'My workflow 6',
type: 'workflow',
status: 'created',
location: 'local',
conflict: false,
updatedAt: '2023-07-10T10:02:39.276Z',
},
];
const pullResult: SourceControlledFile[] = [
{
file: 'credential_stubs/kkookWGIeey9K4Kt.json',
id: 'kkookWGIeey9K4Kt',
name: '(deleted)',
type: 'credential',
status: 'deleted',
location: 'local',
conflict: false,
updatedAt: '',
},
{
file: 'credential_stubs/abcdeWGIeey9K4aa.json',
id: 'abcdeWGIeey9K4aa',
name: 'modfied credential',
type: 'credential',
status: 'modified',
location: 'local',
conflict: false,
updatedAt: '',
},
{
file: 'workflows/BpFS26gViuGqrIVP.json',
id: 'BpFS26gViuGqrIVP',
name: '(deleted)',
type: 'workflow',
status: 'deleted',
location: 'local',
conflict: false,
updatedAt: '',
},
{
file: 'variable_stubs.json',
id: 'variables',
name: 'variables',
type: 'variables',
status: 'modified',
location: 'local',
conflict: false,
updatedAt: '',
},
{
file: 'workflows/dAU6dNthm4TR3gXx.json',
id: 'dAU6dNthm4TR3gXx',
name: 'My workflow 7',
type: 'workflow',
status: 'created',
location: 'local',
conflict: false,
updatedAt: '2023-07-10T10:02:45.186Z',
},
{
file: 'workflows/haQetoXq9GxHSkft.json',
id: 'haQetoXq9GxHSkft',
name: 'My workflow 6',
type: 'workflow',
status: 'modified',
location: 'local',
conflict: false,
updatedAt: '2023-07-10T10:02:39.276Z',
},
];
const license = mockInstance(License);
beforeAll(async () => {
jest.resetAllMocks();
license.isSourceControlLicensed.mockReturnValue(true);
Container.get(SourceControlPreferencesService).getPreferences = () => ({
branchName: 'main',
connected: true,
repositoryUrl: 'git@example.com:n8ntest/n8n_testrepo.git',
branchReadOnly: false,
branchColor: '#5296D6',
publicKey:
'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDBSz2nMZAiUBWe6n89aWd5x9QMcIOaznVW3fpuCYC4L n8n deploy key',
});
});
describe('Source Control', () => {
it('should generate an SSH key pair', async () => {
const keyPair = await generateSshKeyPair('ed25519');
expect(keyPair.privateKey).toBeTruthy();
expect(keyPair.privateKey).toContain('BEGIN OPENSSH PRIVATE KEY');
expect(keyPair.publicKey).toBeTruthy();
expect(keyPair.publicKey).toContain('ssh-ed25519');
});
it('should generate an RSA key pair', async () => {
const keyPair = await generateSshKeyPair('rsa');
expect(keyPair.privateKey).toBeTruthy();
expect(keyPair.privateKey).toContain('BEGIN OPENSSH PRIVATE KEY');
expect(keyPair.publicKey).toBeTruthy();
expect(keyPair.publicKey).toContain('ssh-rsa');
});
it('should check for git and ssh folders and create them if required', async () => {
const { n8nFolder } = Container.get(InstanceSettings);
const sshFolder = path.join(n8nFolder, SOURCE_CONTROL_SSH_FOLDER);
const gitFolder = path.join(n8nFolder, SOURCE_CONTROL_GIT_FOLDER);
let hasThrown = false;
try {
accessSync(sshFolder, fsConstants.F_OK);
} catch (error) {
hasThrown = true;
}
expect(hasThrown).toBeTruthy();
hasThrown = false;
try {
accessSync(gitFolder, fsConstants.F_OK);
} catch (error) {
hasThrown = true;
}
expect(hasThrown).toBeTruthy();
// create missing folders
expect(sourceControlFoldersExistCheck([gitFolder, sshFolder], true)).toBe(false);
// find folders this time
expect(sourceControlFoldersExistCheck([gitFolder, sshFolder], true)).toBe(true);
expect(accessSync(sshFolder, fsConstants.F_OK)).toBeUndefined();
expect(accessSync(gitFolder, fsConstants.F_OK)).toBeUndefined();
});
it('should get repo type from url', async () => {
expect(getRepoType('git@github.com:n8ntest/n8n_testrepo.git')).toBe('github');
expect(getRepoType('git@gitlab.com:n8ntest/n8n_testrepo.git')).toBe('gitlab');
expect(getRepoType('git@mygitea.io:n8ntest/n8n_testrepo.git')).toBe('other');
});
it('should get tracking information from pre-push results', () => {
const trackingResult = getTrackingInformationFromPrePushResult(pushResult);
expect(trackingResult).toEqual({
workflowsEligible: 3,
workflowsEligibleWithConflicts: 1,
credsEligible: 1,
credsEligibleWithConflicts: 0,
variablesEligible: 1,
});
});
it('should get tracking information from post-push results', () => {
const trackingResult = getTrackingInformationFromPostPushResult(pushResult);
expect(trackingResult).toEqual({
workflowsPushed: 2,
workflowsEligible: 3,
credsPushed: 1,
variablesPushed: 1,
});
});
it('should get tracking information from pull results', () => {
const trackingResult = getTrackingInformationFromPullResult(pullResult);
expect(trackingResult).toEqual({
credConflicts: 1,
workflowConflicts: 1,
workflowUpdates: 3,
});
});
it('should class validate correct preferences', async () => {
const validPreferences: Partial<SourceControlPreferences> = {
branchName: 'main',
repositoryUrl: 'git@example.com:n8ntest/n8n_testrepo.git',
branchReadOnly: false,
branchColor: '#5296D6',
};
const validationResult = await Container.get(
SourceControlPreferencesService,
).validateSourceControlPreferences(validPreferences);
expect(validationResult).toBeTruthy();
});
});

View File

@@ -1,431 +0,0 @@
import type RudderStack from '@rudderstack/rudder-sdk-node';
import { Telemetry } from '@/telemetry';
import config from '@/config';
import { flushPromises } from './Helpers';
import { PostHogClient } from '@/posthog';
import { mock } from 'jest-mock-extended';
import { InstanceSettings } from 'n8n-core';
import { mockInstance } from '../shared/mocking';
jest.unmock('@/telemetry');
jest.mock('@/posthog');
describe('Telemetry', () => {
let startPulseSpy: jest.SpyInstance;
const spyTrack = jest.spyOn(Telemetry.prototype, 'track').mockName('track');
const mockRudderStack = mock<RudderStack>();
let telemetry: Telemetry;
const instanceId = 'Telemetry unit test';
const testDateTime = new Date('2022-01-01 00:00:00');
const instanceSettings = mockInstance(InstanceSettings, { instanceId });
beforeAll(() => {
// @ts-expect-error Spying on private method
startPulseSpy = jest.spyOn(Telemetry.prototype, 'startPulse').mockImplementation(() => {});
jest.useFakeTimers();
jest.setSystemTime(testDateTime);
config.set('diagnostics.enabled', true);
config.set('deployment.type', 'n8n-testing');
});
afterAll(async () => {
jest.clearAllTimers();
jest.useRealTimers();
startPulseSpy.mockRestore();
await telemetry.trackN8nStop();
});
beforeEach(async () => {
spyTrack.mockClear();
const postHog = new PostHogClient(instanceSettings);
await postHog.init();
telemetry = new Telemetry(mock(), postHog, mock(), instanceSettings, mock());
// @ts-expect-error Assigning to private property
telemetry.rudderStack = mockRudderStack;
});
afterEach(async () => {
await telemetry.trackN8nStop();
});
describe('trackN8nStop', () => {
test('should call track method', async () => {
await telemetry.trackN8nStop();
expect(spyTrack).toHaveBeenCalledTimes(1);
});
});
describe('trackWorkflowExecution', () => {
beforeEach(() => {
jest.setSystemTime(testDateTime);
});
test('should count executions correctly', async () => {
const payload = {
workflow_id: '1',
is_manual: true,
success: true,
error_node_type: 'custom-nodes-base.node-type',
};
payload.is_manual = true;
payload.success = true;
const execTime1 = fakeJestSystemTime('2022-01-01 12:00:00');
telemetry.trackWorkflowExecution(payload);
fakeJestSystemTime('2022-01-01 12:30:00');
telemetry.trackWorkflowExecution(payload);
payload.is_manual = false;
payload.success = true;
const execTime2 = fakeJestSystemTime('2022-01-01 13:00:00');
telemetry.trackWorkflowExecution(payload);
fakeJestSystemTime('2022-01-01 12:30:00');
telemetry.trackWorkflowExecution(payload);
payload.is_manual = true;
payload.success = false;
const execTime3 = fakeJestSystemTime('2022-01-01 14:00:00');
telemetry.trackWorkflowExecution(payload);
fakeJestSystemTime('2022-01-01 12:30:00');
telemetry.trackWorkflowExecution(payload);
payload.is_manual = false;
payload.success = false;
const execTime4 = fakeJestSystemTime('2022-01-01 15:00:00');
telemetry.trackWorkflowExecution(payload);
fakeJestSystemTime('2022-01-01 12:30:00');
telemetry.trackWorkflowExecution(payload);
expect(spyTrack).toHaveBeenCalledTimes(0);
const execBuffer = telemetry.getCountsBuffer();
expect(execBuffer['1'].manual_success?.count).toBe(2);
expect(execBuffer['1'].manual_success?.first).toEqual(execTime1);
expect(execBuffer['1'].prod_success?.count).toBe(2);
expect(execBuffer['1'].prod_success?.first).toEqual(execTime2);
expect(execBuffer['1'].manual_error?.count).toBe(2);
expect(execBuffer['1'].manual_error?.first).toEqual(execTime3);
expect(execBuffer['1'].prod_error?.count).toBe(2);
expect(execBuffer['1'].prod_error?.first).toEqual(execTime4);
});
test('should fire "Workflow execution errored" event for failed executions', async () => {
const payload = {
workflow_id: '1',
is_manual: true,
success: false,
error_node_type: 'custom-nodes-base.node-type',
};
const execTime1 = fakeJestSystemTime('2022-01-01 12:00:00');
telemetry.trackWorkflowExecution(payload);
fakeJestSystemTime('2022-01-01 12:30:00');
telemetry.trackWorkflowExecution(payload);
let execBuffer = telemetry.getCountsBuffer();
// should not fire event for custom nodes
expect(spyTrack).toHaveBeenCalledTimes(0);
expect(execBuffer['1'].manual_error?.count).toBe(2);
expect(execBuffer['1'].manual_error?.first).toEqual(execTime1);
payload.error_node_type = 'n8n-nodes-base.node-type';
fakeJestSystemTime('2022-01-01 13:00:00');
telemetry.trackWorkflowExecution(payload);
fakeJestSystemTime('2022-01-01 12:30:00');
telemetry.trackWorkflowExecution(payload);
execBuffer = telemetry.getCountsBuffer();
// should fire event for custom nodes
expect(spyTrack).toHaveBeenCalledTimes(2);
expect(spyTrack).toHaveBeenCalledWith('Workflow execution errored', payload);
expect(execBuffer['1'].manual_error?.count).toBe(4);
expect(execBuffer['1'].manual_error?.first).toEqual(execTime1);
});
test('should track production executions count correctly', async () => {
const payload = {
workflow_id: '1',
is_manual: false,
success: true,
error_node_type: 'node_type',
};
// successful execution
const execTime1 = fakeJestSystemTime('2022-01-01 12:00:00');
telemetry.trackWorkflowExecution(payload);
expect(spyTrack).toHaveBeenCalledTimes(0);
let execBuffer = telemetry.getCountsBuffer();
expect(execBuffer['1'].manual_error).toBeUndefined();
expect(execBuffer['1'].manual_success).toBeUndefined();
expect(execBuffer['1'].prod_error).toBeUndefined();
expect(execBuffer['1'].prod_success?.count).toBe(1);
expect(execBuffer['1'].prod_success?.first).toEqual(execTime1);
// successful execution n8n node
payload.error_node_type = 'n8n-nodes-base.merge';
payload.workflow_id = '2';
telemetry.trackWorkflowExecution(payload);
expect(spyTrack).toHaveBeenCalledTimes(0);
execBuffer = telemetry.getCountsBuffer();
expect(execBuffer['1'].manual_error).toBeUndefined();
expect(execBuffer['1'].manual_success).toBeUndefined();
expect(execBuffer['1'].prod_error).toBeUndefined();
expect(execBuffer['1'].prod_success?.count).toBe(1);
expect(execBuffer['2'].prod_success?.count).toBe(1);
expect(execBuffer['1'].prod_success?.first).toEqual(execTime1);
expect(execBuffer['2'].prod_success?.first).toEqual(execTime1);
// additional successful execution
payload.error_node_type = 'n8n-nodes-base.merge';
payload.workflow_id = '2';
telemetry.trackWorkflowExecution(payload);
payload.error_node_type = 'n8n-nodes-base.merge';
payload.workflow_id = '1';
telemetry.trackWorkflowExecution(payload);
expect(spyTrack).toHaveBeenCalledTimes(0);
execBuffer = telemetry.getCountsBuffer();
expect(execBuffer['1'].manual_error).toBeUndefined();
expect(execBuffer['1'].manual_success).toBeUndefined();
expect(execBuffer['1'].prod_error).toBeUndefined();
expect(execBuffer['2'].manual_error).toBeUndefined();
expect(execBuffer['2'].manual_success).toBeUndefined();
expect(execBuffer['2'].prod_error).toBeUndefined();
expect(execBuffer['1'].prod_success?.count).toBe(2);
expect(execBuffer['2'].prod_success?.count).toBe(2);
expect(execBuffer['1'].prod_success?.first).toEqual(execTime1);
expect(execBuffer['2'].prod_success?.first).toEqual(execTime1);
// failed execution
const execTime2 = fakeJestSystemTime('2022-01-01 12:00:00');
payload.error_node_type = 'custom-package.custom-node';
payload.success = false;
telemetry.trackWorkflowExecution(payload);
expect(spyTrack).toHaveBeenCalledTimes(0);
execBuffer = telemetry.getCountsBuffer();
expect(execBuffer['1'].manual_error).toBeUndefined();
expect(execBuffer['1'].manual_success).toBeUndefined();
expect(execBuffer['2'].manual_error).toBeUndefined();
expect(execBuffer['2'].manual_success).toBeUndefined();
expect(execBuffer['2'].prod_error).toBeUndefined();
expect(execBuffer['1'].prod_error?.count).toBe(1);
expect(execBuffer['1'].prod_success?.count).toBe(2);
expect(execBuffer['2'].prod_success?.count).toBe(2);
expect(execBuffer['1'].prod_error?.first).toEqual(execTime2);
expect(execBuffer['1'].prod_success?.first).toEqual(execTime1);
expect(execBuffer['2'].prod_success?.first).toEqual(execTime1);
// failed execution n8n node
payload.success = false;
payload.error_node_type = 'n8n-nodes-base.merge';
payload.is_manual = true;
telemetry.trackWorkflowExecution(payload);
expect(spyTrack).toHaveBeenCalledTimes(1);
execBuffer = telemetry.getCountsBuffer();
expect(execBuffer['1'].manual_error?.count).toBe(1);
expect(execBuffer['1'].manual_success).toBeUndefined();
expect(execBuffer['2'].manual_error).toBeUndefined();
expect(execBuffer['2'].manual_success).toBeUndefined();
expect(execBuffer['2'].prod_error).toBeUndefined();
expect(execBuffer['1'].prod_success?.count).toBe(2);
expect(execBuffer['1'].prod_error?.count).toBe(1);
expect(execBuffer['2'].prod_success?.count).toBe(2);
expect(execBuffer['1'].prod_error?.first).toEqual(execTime2);
expect(execBuffer['1'].prod_success?.first).toEqual(execTime1);
expect(execBuffer['2'].prod_success?.first).toEqual(execTime1);
});
});
describe('pulse', () => {
let pulseSpy: jest.SpyInstance;
beforeAll(() => {
startPulseSpy.mockRestore();
});
beforeEach(() => {
fakeJestSystemTime(testDateTime);
pulseSpy = jest.spyOn(Telemetry.prototype as any, 'pulse').mockName('pulseSpy');
});
afterEach(() => {
pulseSpy.mockClear();
});
xtest('should trigger pulse in intervals', async () => {
expect(pulseSpy).toBeCalledTimes(0);
jest.advanceTimersToNextTimer();
await flushPromises();
expect(pulseSpy).toBeCalledTimes(1);
expect(spyTrack).toHaveBeenCalledTimes(1);
expect(spyTrack).toHaveBeenCalledWith('pulse', {
plan_name_current: 'Community',
quota: -1,
usage: 0,
});
jest.advanceTimersToNextTimer();
await flushPromises();
expect(pulseSpy).toBeCalledTimes(2);
expect(spyTrack).toHaveBeenCalledTimes(2);
expect(spyTrack).toHaveBeenCalledWith('pulse', {
plan_name_current: 'Community',
quota: -1,
usage: 0,
});
});
xtest('should track workflow counts correctly', async () => {
expect(pulseSpy).toBeCalledTimes(0);
let execBuffer = telemetry.getCountsBuffer();
// expect clear counters on start
expect(Object.keys(execBuffer).length).toBe(0);
const payload = {
workflow_id: '1',
is_manual: true,
success: true,
error_node_type: 'custom-nodes-base.node-type',
};
telemetry.trackWorkflowExecution(payload);
telemetry.trackWorkflowExecution(payload);
payload.is_manual = false;
payload.success = true;
telemetry.trackWorkflowExecution(payload);
telemetry.trackWorkflowExecution(payload);
payload.is_manual = true;
payload.success = false;
telemetry.trackWorkflowExecution(payload);
telemetry.trackWorkflowExecution(payload);
payload.is_manual = false;
payload.success = false;
telemetry.trackWorkflowExecution(payload);
telemetry.trackWorkflowExecution(payload);
payload.workflow_id = '2';
telemetry.trackWorkflowExecution(payload);
telemetry.trackWorkflowExecution(payload);
expect(spyTrack).toHaveBeenCalledTimes(0);
expect(pulseSpy).toBeCalledTimes(0);
jest.advanceTimersToNextTimer();
execBuffer = telemetry.getCountsBuffer();
await flushPromises();
expect(pulseSpy).toBeCalledTimes(1);
expect(spyTrack).toHaveBeenCalledTimes(3);
expect(spyTrack).toHaveBeenNthCalledWith(
1,
'Workflow execution count',
{
event_version: '2',
workflow_id: '1',
user_id: undefined,
manual_error: {
count: 2,
first: testDateTime,
},
manual_success: {
count: 2,
first: testDateTime,
},
prod_error: {
count: 2,
first: testDateTime,
},
prod_success: {
count: 2,
first: testDateTime,
},
},
{ withPostHog: true },
);
expect(spyTrack).toHaveBeenNthCalledWith(
2,
'Workflow execution count',
{
event_version: '2',
workflow_id: '2',
user_id: undefined,
prod_error: {
count: 2,
first: testDateTime,
},
},
{ withPostHog: true },
);
expect(spyTrack).toHaveBeenNthCalledWith(3, 'pulse', {
plan_name_current: 'Community',
quota: -1,
usage: 0,
});
expect(Object.keys(execBuffer).length).toBe(0);
// Adding a second step here because we believe PostHog may use timers for sending data
// and adding posthog to the above metric was causing the pulseSpy timer to not be ran
jest.advanceTimersToNextTimer();
execBuffer = telemetry.getCountsBuffer();
expect(Object.keys(execBuffer).length).toBe(0);
// @TODO: Flushing promises here is not working
// expect(pulseSpy).toBeCalledTimes(2);
// expect(spyTrack).toHaveBeenCalledTimes(4);
// expect(spyTrack).toHaveBeenNthCalledWith(4, 'pulse', {
// plan_name_current: 'Community',
// quota: -1,
// usage: 0,
// });
});
});
});
const fakeJestSystemTime = (dateTime: string | Date): Date => {
const dt = new Date(dateTime);
jest.setSystemTime(dt);
return dt;
};

View File

@@ -1,134 +0,0 @@
import { mock } from 'jest-mock-extended';
import { TestWebhooks } from '@/TestWebhooks';
import { WebhookNotFoundError } from '@/errors/response-errors/webhook-not-found.error';
import { v4 as uuid } from 'uuid';
import { generateNanoId } from '@/databases/utils/generators';
import { NotFoundError } from '@/errors/response-errors/not-found.error';
import * as WebhookHelpers from '@/WebhookHelpers';
import type * as express from 'express';
import type { IWorkflowDb, WebhookRequest } from '@/Interfaces';
import type { IWebhookData, IWorkflowExecuteAdditionalData, Workflow } from 'n8n-workflow';
import type {
TestWebhookRegistrationsService,
TestWebhookRegistration,
} from '@/services/test-webhook-registrations.service';
import * as AdditionalData from '@/WorkflowExecuteAdditionalData';
jest.mock('@/WorkflowExecuteAdditionalData');
const mockedAdditionalData = AdditionalData as jest.Mocked<typeof AdditionalData>;
const workflowEntity = mock<IWorkflowDb>({ id: generateNanoId(), nodes: [] });
const httpMethod = 'GET';
const path = uuid();
const userId = '04ab4baf-85df-478f-917b-d303934a97de';
const webhook = mock<IWebhookData>({
httpMethod,
path,
workflowId: workflowEntity.id,
userId,
});
const registrations = mock<TestWebhookRegistrationsService>();
let testWebhooks: TestWebhooks;
describe('TestWebhooks', () => {
beforeAll(() => {
testWebhooks = new TestWebhooks(mock(), mock(), registrations, mock());
jest.useFakeTimers();
});
describe('needsWebhook()', () => {
const args: Parameters<typeof testWebhooks.needsWebhook> = [
userId,
workflowEntity,
mock<IWorkflowExecuteAdditionalData>(),
];
test('if webhook is needed, should register then create webhook and return true', async () => {
const workflow = mock<Workflow>();
jest.spyOn(testWebhooks, 'toWorkflow').mockReturnValueOnce(workflow);
jest.spyOn(WebhookHelpers, 'getWorkflowWebhooks').mockReturnValue([webhook]);
const needsWebhook = await testWebhooks.needsWebhook(...args);
const [registerOrder] = registrations.register.mock.invocationCallOrder;
const [createOrder] = workflow.createWebhookIfNotExists.mock.invocationCallOrder;
expect(registerOrder).toBeLessThan(createOrder);
expect(needsWebhook).toBe(true);
});
test('if webhook activation fails, should deactivate workflow webhooks', async () => {
const msg = 'Failed to add webhook to active webhooks';
jest.spyOn(WebhookHelpers, 'getWorkflowWebhooks').mockReturnValue([webhook]);
jest.spyOn(registrations, 'register').mockRejectedValueOnce(new Error(msg));
registrations.getAllRegistrations.mockResolvedValue([]);
const needsWebhook = testWebhooks.needsWebhook(...args);
await expect(needsWebhook).rejects.toThrowError(msg);
});
test('if no webhook is found to start workflow, should return false', async () => {
webhook.webhookDescription.restartWebhook = true;
jest.spyOn(WebhookHelpers, 'getWorkflowWebhooks').mockReturnValue([webhook]);
const result = await testWebhooks.needsWebhook(...args);
expect(result).toBe(false);
});
});
describe('executeWebhook()', () => {
test('if webhook is not registered, should throw', async () => {
jest.spyOn(testWebhooks, 'getActiveWebhook').mockResolvedValue(webhook);
jest.spyOn(testWebhooks, 'getWebhookMethods').mockResolvedValue([]);
const promise = testWebhooks.executeWebhook(
mock<WebhookRequest>({ params: { path } }),
mock(),
);
await expect(promise).rejects.toThrowError(WebhookNotFoundError);
});
test('if webhook is registered but missing from workflow, should throw', async () => {
jest.spyOn(testWebhooks, 'getActiveWebhook').mockResolvedValue(webhook);
jest.spyOn(testWebhooks, 'getWebhookMethods').mockResolvedValue([]);
const registration = mock<TestWebhookRegistration>({
pushRef: 'some-session-id',
workflowEntity,
});
await registrations.register(registration);
const promise = testWebhooks.executeWebhook(
mock<WebhookRequest>({ params: { path } }),
mock<express.Response>(),
);
await expect(promise).rejects.toThrowError(NotFoundError);
});
});
describe('deactivateWebhooks()', () => {
test('should add additional data to workflow', async () => {
registrations.getAllRegistrations.mockResolvedValue([{ workflowEntity, webhook }]);
const workflow = testWebhooks.toWorkflow(workflowEntity);
await testWebhooks.deactivateWebhooks(workflow);
expect(mockedAdditionalData.getBase).toHaveBeenCalledWith(userId);
});
});
});

View File

@@ -1,88 +0,0 @@
import type { GlobalConfig } from '@n8n/config';
import { mock } from 'jest-mock-extended';
import type { InviteEmailData, PasswordResetData } from '@/UserManagement/email/Interfaces';
import { NodeMailer } from '@/UserManagement/email/NodeMailer';
import { UserManagementMailer } from '@/UserManagement/email/UserManagementMailer';
import { mockInstance } from '@test/mocking';
describe('UserManagementMailer', () => {
const email = 'test@user.com';
const nodeMailer = mockInstance(NodeMailer);
const inviteEmailData = mock<InviteEmailData>({
email,
inviteAcceptUrl: 'https://accept.url',
});
const passwordResetData = mock<PasswordResetData>({
email,
passwordResetUrl: 'https://reset.url',
});
beforeEach(() => {
jest.clearAllMocks();
nodeMailer.sendMail.mockResolvedValue({ emailSent: true });
});
describe('when SMTP is not configured', () => {
const config = mock<GlobalConfig>({
userManagement: {
emails: {
mode: '',
},
},
});
const userManagementMailer = new UserManagementMailer(config, mock(), mock(), mock());
it('should not setup email transport', async () => {
expect(userManagementMailer.isEmailSetUp).toBe(false);
expect(userManagementMailer.mailer).toBeUndefined();
});
it('should not send emails', async () => {
const result = await userManagementMailer.invite(inviteEmailData);
expect(result.emailSent).toBe(false);
expect(nodeMailer.sendMail).not.toHaveBeenCalled();
});
});
describe('when SMTP is configured', () => {
const config = mock<GlobalConfig>({
userManagement: {
emails: {
mode: 'smtp',
smtp: {
host: 'email.host',
},
},
},
});
const userManagementMailer = new UserManagementMailer(config, mock(), mock(), mock());
it('should setup email transport', async () => {
expect(userManagementMailer.isEmailSetUp).toBe(true);
expect(userManagementMailer.mailer).toEqual(nodeMailer);
});
it('should send invitation emails', async () => {
const result = await userManagementMailer.invite(inviteEmailData);
expect(result.emailSent).toBe(true);
expect(nodeMailer.sendMail).toHaveBeenCalledWith({
body: expect.stringContaining(
`<a href="${inviteEmailData.inviteAcceptUrl}" target="_blank">`,
),
emailRecipients: email,
subject: 'You have been invited to n8n',
});
});
it('should send password reset emails', async () => {
const result = await userManagementMailer.passwordReset(passwordResetData);
expect(result.emailSent).toBe(true);
expect(nodeMailer.sendMail).toHaveBeenCalledWith({
body: expect.stringContaining(`<a href="${passwordResetData.passwordResetUrl}">`),
emailRecipients: email,
subject: 'n8n password reset',
});
});
});
});

View File

@@ -1,142 +0,0 @@
import { WaitTracker } from '@/WaitTracker';
import { mock } from 'jest-mock-extended';
import type { ExecutionRepository } from '@/databases/repositories/execution.repository';
import type { IExecutionResponse } from '@/Interfaces';
import { OrchestrationService } from '@/services/orchestration.service';
import type { MultiMainSetup } from '@/services/orchestration/main/MultiMainSetup.ee';
jest.useFakeTimers();
describe('WaitTracker', () => {
const executionRepository = mock<ExecutionRepository>();
const multiMainSetup = mock<MultiMainSetup>();
const orchestrationService = new OrchestrationService(mock(), mock(), mock(), multiMainSetup);
const execution = mock<IExecutionResponse>({
id: '123',
waitTill: new Date(Date.now() + 1000),
});
let waitTracker: WaitTracker;
beforeEach(() => {
waitTracker = new WaitTracker(
mock(),
executionRepository,
mock(),
mock(),
orchestrationService,
);
multiMainSetup.on.mockReturnThis();
});
afterEach(() => {
jest.clearAllMocks();
});
describe('init()', () => {
it('should query DB for waiting executions if leader', async () => {
jest.spyOn(orchestrationService, 'isLeader', 'get').mockReturnValue(true);
executionRepository.getWaitingExecutions.mockResolvedValue([execution]);
waitTracker.init();
expect(executionRepository.getWaitingExecutions).toHaveBeenCalledTimes(1);
});
it('if follower, should do nothing', () => {
executionRepository.getWaitingExecutions.mockResolvedValue([]);
waitTracker.init();
expect(executionRepository.findSingleExecution).not.toHaveBeenCalled();
});
it('if no executions to start, should do nothing', () => {
executionRepository.getWaitingExecutions.mockResolvedValue([]);
waitTracker.init();
expect(executionRepository.findSingleExecution).not.toHaveBeenCalled();
});
describe('if execution to start', () => {
it('if not enough time passed, should not start execution', async () => {
executionRepository.getWaitingExecutions.mockResolvedValue([execution]);
waitTracker.init();
executionRepository.getWaitingExecutions.mockResolvedValue([execution]);
await waitTracker.getWaitingExecutions();
const startExecutionSpy = jest.spyOn(waitTracker, 'startExecution');
jest.advanceTimersByTime(100);
expect(startExecutionSpy).not.toHaveBeenCalled();
});
it('if enough time passed, should start execution', async () => {
executionRepository.getWaitingExecutions.mockResolvedValue([]);
waitTracker.init();
executionRepository.getWaitingExecutions.mockResolvedValue([execution]);
await waitTracker.getWaitingExecutions();
const startExecutionSpy = jest.spyOn(waitTracker, 'startExecution');
jest.advanceTimersByTime(2_000);
expect(startExecutionSpy).toHaveBeenCalledWith(execution.id);
});
});
});
describe('startExecution()', () => {
it('should query for execution to start', async () => {
executionRepository.getWaitingExecutions.mockResolvedValue([]);
waitTracker.init();
executionRepository.findSingleExecution.mockResolvedValue(execution);
waitTracker.startExecution(execution.id);
jest.advanceTimersByTime(5);
expect(executionRepository.findSingleExecution).toHaveBeenCalledWith(execution.id, {
includeData: true,
unflattenData: true,
});
});
});
describe('single-main setup', () => {
it('should start tracking', () => {
executionRepository.getWaitingExecutions.mockResolvedValue([]);
waitTracker.init();
expect(executionRepository.getWaitingExecutions).toHaveBeenCalledTimes(1);
});
});
describe('multi-main setup', () => {
it('should start tracking if leader', () => {
jest.spyOn(orchestrationService, 'isLeader', 'get').mockReturnValue(true);
jest.spyOn(orchestrationService, 'isSingleMainSetup', 'get').mockReturnValue(false);
executionRepository.getWaitingExecutions.mockResolvedValue([]);
waitTracker.init();
expect(executionRepository.getWaitingExecutions).toHaveBeenCalledTimes(1);
});
it('should not start tracking if follower', () => {
jest.spyOn(orchestrationService, 'isLeader', 'get').mockReturnValue(false);
jest.spyOn(orchestrationService, 'isSingleMainSetup', 'get').mockReturnValue(false);
executionRepository.getWaitingExecutions.mockResolvedValue([]);
waitTracker.init();
expect(executionRepository.getWaitingExecutions).not.toHaveBeenCalled();
});
});
});

View File

@@ -1,142 +0,0 @@
import { type Response } from 'express';
import { mock } from 'jest-mock-extended';
import { randomString } from 'n8n-workflow';
import type { IHttpRequestMethods } from 'n8n-workflow';
import type { IWebhookManager, WebhookCORSRequest, WebhookRequest } from '@/Interfaces';
import { webhookRequestHandler } from '@/WebhookHelpers';
describe('WebhookHelpers', () => {
describe('webhookRequestHandler', () => {
const webhookManager = mock<Required<IWebhookManager>>();
const handler = webhookRequestHandler(webhookManager);
beforeEach(() => {
jest.resetAllMocks();
});
it('should throw for unsupported methods', async () => {
const req = mock<WebhookRequest | WebhookCORSRequest>({
method: 'CONNECT' as IHttpRequestMethods,
});
const res = mock<Response>();
res.status.mockReturnValue(res);
await handler(req, res);
expect(res.status).toHaveBeenCalledWith(500);
expect(res.json).toHaveBeenCalledWith({
code: 0,
message: 'The method CONNECT is not supported.',
});
});
describe('preflight requests', () => {
it('should handle missing header for requested method', async () => {
const req = mock<WebhookRequest | WebhookCORSRequest>({
method: 'OPTIONS',
headers: {
origin: 'https://example.com',
'access-control-request-method': undefined,
},
params: { path: 'test' },
});
const res = mock<Response>();
res.status.mockReturnValue(res);
webhookManager.getWebhookMethods.mockResolvedValue(['GET', 'PATCH']);
await handler(req, res);
expect(res.status).toHaveBeenCalledWith(204);
expect(res.header).toHaveBeenCalledWith(
'Access-Control-Allow-Methods',
'OPTIONS, GET, PATCH',
);
});
it('should handle default origin and max-age', async () => {
const req = mock<WebhookRequest | WebhookCORSRequest>({
method: 'OPTIONS',
headers: {
origin: 'https://example.com',
'access-control-request-method': 'GET',
},
params: { path: 'test' },
});
const res = mock<Response>();
res.status.mockReturnValue(res);
webhookManager.getWebhookMethods.mockResolvedValue(['GET', 'PATCH']);
await handler(req, res);
expect(res.status).toHaveBeenCalledWith(204);
expect(res.header).toHaveBeenCalledWith(
'Access-Control-Allow-Methods',
'OPTIONS, GET, PATCH',
);
expect(res.header).toHaveBeenCalledWith(
'Access-Control-Allow-Origin',
'https://example.com',
);
expect(res.header).toHaveBeenCalledWith('Access-Control-Max-Age', '300');
});
it('should handle wildcard origin', async () => {
const randomOrigin = randomString(10);
const req = mock<WebhookRequest | WebhookCORSRequest>({
method: 'OPTIONS',
headers: {
origin: randomOrigin,
'access-control-request-method': 'GET',
},
params: { path: 'test' },
});
const res = mock<Response>();
res.status.mockReturnValue(res);
webhookManager.getWebhookMethods.mockResolvedValue(['GET', 'PATCH']);
webhookManager.findAccessControlOptions.mockResolvedValue({
allowedOrigins: '*',
});
await handler(req, res);
expect(res.status).toHaveBeenCalledWith(204);
expect(res.header).toHaveBeenCalledWith(
'Access-Control-Allow-Methods',
'OPTIONS, GET, PATCH',
);
expect(res.header).toHaveBeenCalledWith('Access-Control-Allow-Origin', randomOrigin);
});
it('should handle custom origin', async () => {
const req = mock<WebhookRequest | WebhookCORSRequest>({
method: 'OPTIONS',
headers: {
origin: 'https://example.com',
'access-control-request-method': 'GET',
},
params: { path: 'test' },
});
const res = mock<Response>();
res.status.mockReturnValue(res);
webhookManager.getWebhookMethods.mockResolvedValue(['GET', 'PATCH']);
webhookManager.findAccessControlOptions.mockResolvedValue({
allowedOrigins: 'https://test.com',
});
await handler(req, res);
expect(res.status).toHaveBeenCalledWith(204);
expect(res.header).toHaveBeenCalledWith(
'Access-Control-Allow-Methods',
'OPTIONS, GET, PATCH',
);
expect(res.header).toHaveBeenCalledWith('Access-Control-Allow-Origin', 'https://test.com');
});
});
});
});

View File

@@ -1,41 +0,0 @@
import { VariablesService } from '@/environments/variables/variables.service.ee';
import { mockInstance } from '../shared/mocking';
import { MessageEventBus } from '@/eventbus/MessageEventBus/MessageEventBus';
import { getBase } from '@/WorkflowExecuteAdditionalData';
import Container from 'typedi';
import { CredentialsHelper } from '@/CredentialsHelper';
import { SecretsHelper } from '@/SecretsHelpers';
describe('WorkflowExecuteAdditionalData', () => {
const messageEventBus = mockInstance(MessageEventBus);
const variablesService = mockInstance(VariablesService);
variablesService.getAllCached.mockResolvedValue([]);
const credentialsHelper = mockInstance(CredentialsHelper);
const secretsHelper = mockInstance(SecretsHelper);
Container.set(MessageEventBus, messageEventBus);
Container.set(VariablesService, variablesService);
Container.set(CredentialsHelper, credentialsHelper);
Container.set(SecretsHelper, secretsHelper);
test('logAiEvent should call MessageEventBus', async () => {
const additionalData = await getBase('user-id');
const eventName = 'n8n.ai.memory.get.messages';
const payload = {
msg: 'test message',
executionId: '123',
nodeName: 'n8n-memory',
workflowId: 'workflow-id',
workflowName: 'workflow-name',
nodeType: 'n8n-memory',
};
await additionalData.logAiEvent(eventName, payload);
expect(messageEventBus.sendAiNodeEvent).toHaveBeenCalledTimes(1);
expect(messageEventBus.sendAiNodeEvent).toHaveBeenCalledWith({
eventName,
payload,
});
});
});

View File

@@ -1,46 +0,0 @@
import { type Workflow } from 'n8n-workflow';
import { getExecutionStartNode } from '@/WorkflowHelpers';
import type { IWorkflowExecutionDataProcess } from '@/Interfaces';
describe('WorkflowHelpers', () => {
describe('getExecutionStartNode', () => {
it('Should return undefined', () => {
const data = {
pinData: {},
startNodes: [],
} as unknown as IWorkflowExecutionDataProcess;
const workflow = {
getNode(nodeName: string) {
return {
name: nodeName,
};
},
} as unknown as Workflow;
const executionStartNode = getExecutionStartNode(data, workflow);
expect(executionStartNode).toBeUndefined();
});
it('Should return startNode', () => {
const data = {
pinData: {
node1: {},
node2: {},
},
startNodes: [{ name: 'node2' }],
} as unknown as IWorkflowExecutionDataProcess;
const workflow = {
getNode(nodeName: string) {
if (nodeName === 'node2') {
return {
name: 'node2',
};
}
return undefined;
},
} as unknown as Workflow;
const executionStartNode = getExecutionStartNode(data, workflow);
expect(executionStartNode).toEqual({
name: 'node2',
});
});
});
});

View File

@@ -1,84 +0,0 @@
import Container from 'typedi';
import { WorkflowHooks, type ExecutionError, type IWorkflowExecuteHooks } from 'n8n-workflow';
import type { User } from '@db/entities/User';
import { WorkflowRunner } from '@/WorkflowRunner';
import config from '@/config';
import * as testDb from '../integration/shared/testDb';
import { setupTestServer } from '../integration/shared/utils';
import { createUser } from '../integration/shared/db/users';
import { createWorkflow } from '../integration/shared/db/workflows';
import { createExecution } from '../integration/shared/db/executions';
import { mockInstance } from '@test/mocking';
import { Telemetry } from '@/telemetry';
let owner: User;
let runner: WorkflowRunner;
let hookFunctions: IWorkflowExecuteHooks;
setupTestServer({ endpointGroups: [] });
mockInstance(Telemetry);
class Watchers {
workflowExecuteAfter = jest.fn();
}
const watchers = new Watchers();
const watchedWorkflowExecuteAfter = jest.spyOn(watchers, 'workflowExecuteAfter');
beforeAll(async () => {
owner = await createUser({ role: 'global:owner' });
runner = Container.get(WorkflowRunner);
hookFunctions = {
workflowExecuteAfter: [watchers.workflowExecuteAfter],
};
});
afterAll(() => {
jest.restoreAllMocks();
});
beforeEach(async () => {
await testDb.truncate(['Workflow', 'SharedWorkflow']);
});
test('processError should return early in Bull stalled edge case', async () => {
const workflow = await createWorkflow({}, owner);
const execution = await createExecution(
{
status: 'success',
finished: true,
},
workflow,
);
config.set('executions.mode', 'queue');
await runner.processError(
new Error('test') as ExecutionError,
new Date(),
'webhook',
execution.id,
new WorkflowHooks(hookFunctions, 'webhook', execution.id, workflow),
);
expect(watchedWorkflowExecuteAfter).toHaveBeenCalledTimes(0);
});
test('processError should process error', async () => {
const workflow = await createWorkflow({}, owner);
const execution = await createExecution(
{
status: 'success',
finished: true,
},
workflow,
);
config.set('executions.mode', 'regular');
await runner.processError(
new Error('test') as ExecutionError,
new Date(),
'webhook',
execution.id,
new WorkflowHooks(hookFunctions, 'webhook', execution.id, workflow),
);
expect(watchedWorkflowExecuteAfter).toHaveBeenCalledTimes(1);
});

View File

@@ -1,305 +0,0 @@
import jwt from 'jsonwebtoken';
import { mock } from 'jest-mock-extended';
import type { NextFunction, Response } from 'express';
import { AuthService } from '@/auth/auth.service';
import config from '@/config';
import { AUTH_COOKIE_NAME, Time } from '@/constants';
import type { User } from '@db/entities/User';
import type { UserRepository } from '@db/repositories/user.repository';
import { JwtService } from '@/services/jwt.service';
import type { UrlService } from '@/services/url.service';
import type { AuthenticatedRequest } from '@/requests';
describe('AuthService', () => {
config.set('userManagement.jwtSecret', 'random-secret');
const browserId = 'test-browser-id';
const userData = {
id: '123',
email: 'test@example.com',
password: 'passwordHash',
disabled: false,
mfaEnabled: false,
};
const user = mock<User>(userData);
const jwtService = new JwtService(mock());
const urlService = mock<UrlService>();
const userRepository = mock<UserRepository>();
const authService = new AuthService(mock(), mock(), jwtService, urlService, userRepository);
const now = new Date('2024-02-01T01:23:45.678Z');
jest.useFakeTimers({ now });
const validToken =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjEyMyIsImhhc2giOiJtSkFZeDRXYjdrIiwiYnJvd3NlcklkIjoiOFpDVXE1YU1uSFhnMFZvcURLcm9hMHNaZ0NwdWlPQ1AzLzB2UmZKUXU0MD0iLCJpYXQiOjE3MDY3NTA2MjUsImV4cCI6MTcwNzM1NTQyNX0.YE-ZGGIQRNQ4DzUe9rjXvOOFFN9ufU34WibsCxAsc4o'; // Generated using `authService.issueJWT(user, browserId)`
beforeEach(() => {
jest.clearAllMocks();
jest.setSystemTime(now);
config.set('userManagement.jwtSessionDurationHours', 168);
config.set('userManagement.jwtRefreshTimeoutHours', 0);
});
describe('createJWTHash', () => {
it('should generate unique hashes', () => {
expect(authService.createJWTHash(user)).toEqual('mJAYx4Wb7k');
expect(
authService.createJWTHash(mock<User>({ email: user.email, password: 'newPasswordHash' })),
).toEqual('FVALtU7AE0');
expect(
authService.createJWTHash(
mock<User>({ email: 'test1@example.com', password: user.password }),
),
).toEqual('y8ha6X01jd');
});
});
describe('authMiddleware', () => {
const req = mock<AuthenticatedRequest>({
cookies: {},
user: undefined,
browserId,
});
const res = mock<Response>();
const next = jest.fn() as NextFunction;
beforeEach(() => {
res.status.mockReturnThis();
});
it('should 401 if no cookie is set', async () => {
req.cookies[AUTH_COOKIE_NAME] = undefined;
await authService.authMiddleware(req, res, next);
expect(next).not.toHaveBeenCalled();
expect(res.status).toHaveBeenCalledWith(401);
});
it('should 401 and clear the cookie if the JWT is expired', async () => {
req.cookies[AUTH_COOKIE_NAME] = validToken;
jest.advanceTimersByTime(365 * Time.days.toMilliseconds);
await authService.authMiddleware(req, res, next);
expect(next).not.toHaveBeenCalled();
expect(res.status).toHaveBeenCalledWith(401);
expect(res.clearCookie).toHaveBeenCalledWith(AUTH_COOKIE_NAME);
});
it('should refresh the cookie before it expires', async () => {
req.cookies[AUTH_COOKIE_NAME] = validToken;
jest.advanceTimersByTime(6 * Time.days.toMilliseconds);
userRepository.findOne.mockResolvedValue(user);
await authService.authMiddleware(req, res, next);
expect(next).toHaveBeenCalled();
expect(res.cookie).toHaveBeenCalledWith('n8n-auth', expect.any(String), {
httpOnly: true,
maxAge: 604800000,
sameSite: 'lax',
secure: false,
});
});
});
describe('issueJWT', () => {
describe('when not setting userManagement.jwtSessionDuration', () => {
it('should default to expire in 7 days', () => {
const defaultInSeconds = 7 * Time.days.toSeconds;
const token = authService.issueJWT(user, browserId);
expect(authService.jwtExpiration).toBe(defaultInSeconds);
const decodedToken = jwtService.verify(token);
if (decodedToken.exp === undefined || decodedToken.iat === undefined) {
fail('Expected exp and iat to be defined');
}
expect(decodedToken.exp - decodedToken.iat).toBe(defaultInSeconds);
});
});
describe('when setting userManagement.jwtSessionDuration', () => {
const testDurationHours = 1;
const testDurationSeconds = testDurationHours * Time.hours.toSeconds;
it('should apply it to tokens', () => {
config.set('userManagement.jwtSessionDurationHours', testDurationHours);
const token = authService.issueJWT(user, browserId);
const decodedToken = jwtService.verify(token);
if (decodedToken.exp === undefined || decodedToken.iat === undefined) {
fail('Expected exp and iat to be defined on decodedToken');
}
expect(decodedToken.exp - decodedToken.iat).toBe(testDurationSeconds);
});
});
});
describe('resolveJwt', () => {
const req = mock<AuthenticatedRequest>({
cookies: {},
user: undefined,
browserId,
});
const res = mock<Response>();
it('should throw on invalid tokens', async () => {
await expect(authService.resolveJwt('random-string', req, res)).rejects.toThrow(
'jwt malformed',
);
expect(res.cookie).not.toHaveBeenCalled();
});
it('should throw on expired tokens', async () => {
jest.advanceTimersByTime(365 * Time.days.toMilliseconds);
await expect(authService.resolveJwt(validToken, req, res)).rejects.toThrow('jwt expired');
expect(res.cookie).not.toHaveBeenCalled();
});
it('should throw on tampered tokens', async () => {
const [header, payload, signature] = validToken.split('.');
const tamperedToken = [header, payload, signature + '123'].join('.');
await expect(authService.resolveJwt(tamperedToken, req, res)).rejects.toThrow(
'invalid signature',
);
expect(res.cookie).not.toHaveBeenCalled();
});
it('should throw on hijacked tokens', async () => {
userRepository.findOne.mockResolvedValue(user);
const req = mock<AuthenticatedRequest>({ browserId: 'another-browser' });
await expect(authService.resolveJwt(validToken, req, res)).rejects.toThrow('Unauthorized');
expect(res.cookie).not.toHaveBeenCalled();
});
test.each([
['no user is found', null],
['the user is disabled', { ...userData, disabled: true }],
[
'user password does not match the one on the token',
{ ...userData, password: 'something else' },
],
[
'user email does not match the one on the token',
{ ...userData, email: 'someone@example.com' },
],
])('should throw if %s', async (_, data) => {
userRepository.findOne.mockResolvedValueOnce(data && mock<User>(data));
await expect(authService.resolveJwt(validToken, req, res)).rejects.toThrow('Unauthorized');
expect(res.cookie).not.toHaveBeenCalled();
});
it('should refresh the cookie before it expires', async () => {
userRepository.findOne.mockResolvedValue(user);
expect(await authService.resolveJwt(validToken, req, res)).toEqual(user);
expect(res.cookie).not.toHaveBeenCalled();
jest.advanceTimersByTime(6 * Time.days.toMilliseconds); // 6 Days
expect(await authService.resolveJwt(validToken, req, res)).toEqual(user);
expect(res.cookie).toHaveBeenCalledWith('n8n-auth', expect.any(String), {
httpOnly: true,
maxAge: 604800000,
sameSite: 'lax',
secure: false,
});
const newToken = res.cookie.mock.calls[0].at(1);
expect(newToken).not.toBe(validToken);
expect(await authService.resolveJwt(newToken, req, res)).toEqual(user);
expect((jwt.decode(newToken) as jwt.JwtPayload).browserId).toEqual(
(jwt.decode(validToken) as jwt.JwtPayload).browserId,
);
});
it('should refresh the cookie only if less than 1/4th of time is left', async () => {
userRepository.findOne.mockResolvedValue(user);
expect(await authService.resolveJwt(validToken, req, res)).toEqual(user);
expect(res.cookie).not.toHaveBeenCalled();
jest.advanceTimersByTime(5 * Time.days.toMilliseconds);
expect(await authService.resolveJwt(validToken, req, res)).toEqual(user);
expect(res.cookie).not.toHaveBeenCalled();
jest.advanceTimersByTime(1 * Time.days.toMilliseconds);
expect(await authService.resolveJwt(validToken, req, res)).toEqual(user);
expect(res.cookie).toHaveBeenCalled();
});
it('should not refresh the cookie if jwtRefreshTimeoutHours is set to -1', async () => {
config.set('userManagement.jwtRefreshTimeoutHours', -1);
userRepository.findOne.mockResolvedValue(user);
expect(await authService.resolveJwt(validToken, req, res)).toEqual(user);
expect(res.cookie).not.toHaveBeenCalled();
jest.advanceTimersByTime(6 * Time.days.toMilliseconds); // 6 Days
expect(await authService.resolveJwt(validToken, req, res)).toEqual(user);
expect(res.cookie).not.toHaveBeenCalled();
});
});
describe('generatePasswordResetUrl', () => {
it('should generate a valid url', () => {
urlService.getInstanceBaseUrl.mockReturnValue('https://n8n.instance');
const url = authService.generatePasswordResetUrl(user);
expect(url).toEqual(
'https://n8n.instance/change-password?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjMiLCJoYXNoIjoibUpBWXg0V2I3ayIsImlhdCI6MTcwNjc1MDYyNSwiZXhwIjoxNzA2NzUxODI1fQ.rg90I7MKjc_KC77mov59XYAeRc-CoW9ka4mt1dCfrnk&mfaEnabled=false',
);
});
});
describe('generatePasswordResetToken', () => {
it('should generate valid password-reset tokens', () => {
const token = authService.generatePasswordResetToken(user);
const decoded = jwt.decode(token) as jwt.JwtPayload;
if (!decoded.exp) fail('Token does not contain expiry');
if (!decoded.iat) fail('Token does not contain issued-at');
expect(decoded.sub).toEqual(user.id);
expect(decoded.exp - decoded.iat).toEqual(1200); // Expires in 20 minutes
expect(decoded.hash).toEqual('mJAYx4Wb7k');
});
});
describe('resolvePasswordResetToken', () => {
it('should not return a user if the token in invalid', async () => {
const resolvedUser = await authService.resolvePasswordResetToken('invalid-token');
expect(resolvedUser).toBeUndefined();
});
it('should not return a user if the token in expired', async () => {
const token = authService.generatePasswordResetToken(user, '-1h');
const resolvedUser = await authService.resolvePasswordResetToken(token);
expect(resolvedUser).toBeUndefined();
});
it('should not return a user if the user does not exist in the DB', async () => {
userRepository.findOne.mockResolvedValueOnce(null);
const token = authService.generatePasswordResetToken(user);
const resolvedUser = await authService.resolvePasswordResetToken(token);
expect(resolvedUser).toBeUndefined();
});
it('should not return a user if the password sha does not match', async () => {
const token = authService.generatePasswordResetToken(user);
const updatedUser = Object.create(user);
updatedUser.password = 'something-else';
userRepository.findOne.mockResolvedValueOnce(updatedUser);
const resolvedUser = await authService.resolvePasswordResetToken(token);
expect(resolvedUser).toBeUndefined();
});
it('should not return the user if all checks pass', async () => {
const token = authService.generatePasswordResetToken(user);
userRepository.findOne.mockResolvedValueOnce(user);
const resolvedUser = await authService.resolvePasswordResetToken(token);
expect(resolvedUser).toEqual(user);
});
});
});

View File

@@ -1,171 +0,0 @@
import { main } from '@/commands/db/revert';
import { mockInstance } from '../../../shared/mocking';
import { Logger } from '@/Logger';
import type { IrreversibleMigration, ReversibleMigration } from '@/databases/types';
import type { Migration, MigrationExecutor } from '@n8n/typeorm';
import { type DataSource } from '@n8n/typeorm';
import { mock } from 'jest-mock-extended';
const logger = mockInstance(Logger);
afterEach(() => {
jest.resetAllMocks();
});
test("don't revert migrations if there is no migration", async () => {
//
// ARRANGE
//
const migrations: Migration[] = [];
const dataSource = mock<DataSource>({ migrations });
const migrationExecutor = mock<MigrationExecutor>();
migrationExecutor.getExecutedMigrations.mockResolvedValue([]);
//
// ACT
//
await main(logger, dataSource, migrationExecutor);
//
// ASSERT
//
expect(logger.error).toHaveBeenCalledTimes(1);
expect(logger.error).toHaveBeenCalledWith(
"Cancelled command. The database was never migrated. Are you sure you're connected to the right database?.",
);
expect(dataSource.undoLastMigration).not.toHaveBeenCalled();
expect(dataSource.destroy).not.toHaveBeenCalled();
});
test("don't revert the last migration if it had no down migration", async () => {
//
// ARRANGE
//
class TestMigration implements IrreversibleMigration {
name = undefined;
async up() {}
down = undefined;
}
const migrationsInCode = [new TestMigration()];
const migrationsInDb: Migration[] = [{ id: 1, timestamp: Date.now(), name: 'TestMigration' }];
const dataSource = mock<DataSource>({ migrations: migrationsInCode });
const migrationExecutor = mock<MigrationExecutor>();
migrationExecutor.getExecutedMigrations.mockResolvedValue(migrationsInDb);
//
// ACT
//
await main(logger, dataSource, migrationExecutor);
//
// ASSERT
//
expect(logger.error).toHaveBeenCalledTimes(1);
expect(logger.error).toBeCalledWith('Cancelled command. The last migration was irreversible.');
expect(dataSource.undoLastMigration).not.toHaveBeenCalled();
expect(dataSource.destroy).not.toHaveBeenCalled();
});
test('print migration name instead of class name in error message if the migration has a name', async () => {
//
// ARRANGE
//
class TestMigration implements IrreversibleMigration {
name = 'Migration Name';
async up() {}
down = undefined;
}
const migrationsInCode = [new TestMigration()];
const migrationsInDb: Migration[] = [{ id: 1, timestamp: Date.now(), name: 'Migration Name' }];
const dataSource = mock<DataSource>({ migrations: migrationsInCode });
const migrationExecutor = mock<MigrationExecutor>();
migrationExecutor.getExecutedMigrations.mockResolvedValue(migrationsInDb);
//
// ACT
//
await main(logger, dataSource, migrationExecutor);
//
// ASSERT
//
expect(logger.error).toHaveBeenCalledTimes(1);
expect(logger.error).toHaveBeenCalledWith(
'Cancelled command. The last migration "Migration Name" was irreversible.',
);
expect(dataSource.undoLastMigration).not.toHaveBeenCalled();
expect(dataSource.destroy).not.toHaveBeenCalled();
});
test("don't revert the last migration if we cannot find the migration in the code", async () => {
//
// ARRANGE
//
const migrationsInDb: Migration[] = [{ id: 1, timestamp: Date.now(), name: 'TestMigration' }];
const dataSource = mock<DataSource>({ migrations: [] });
const migrationExecutor = mock<MigrationExecutor>();
migrationExecutor.getExecutedMigrations.mockResolvedValue(migrationsInDb);
//
// ACT
//
await main(logger, dataSource, migrationExecutor);
//
// ASSERT
//
expect(logger.error).toHaveBeenCalledTimes(2);
expect(logger.error).toHaveBeenNthCalledWith(
1,
'The last migration that was executed is "TestMigration", but I could not find that migration\'s code in the currently installed version of n8n.',
);
expect(logger.error).toHaveBeenNthCalledWith(
2,
'This usually means that you downgraded n8n before running `n8n db:revert`. Please upgrade n8n again and run `n8n db:revert` and then downgrade again.',
);
expect(dataSource.undoLastMigration).not.toHaveBeenCalled();
expect(dataSource.destroy).not.toHaveBeenCalled();
});
test('revert the last migration if it has a down migration', async () => {
//
// ARRANGE
//
class TestMigration implements ReversibleMigration {
name = 'ReversibleMigration';
async up() {}
async down() {}
}
const migrationsInDb: Migration[] = [
{ id: 1, timestamp: Date.now(), name: 'ReversibleMigration' },
];
const dataSource = mock<DataSource>({ migrations: [new TestMigration()] });
const migrationExecutor = mock<MigrationExecutor>();
migrationExecutor.getExecutedMigrations.mockResolvedValue(migrationsInDb);
//
// ACT
//
await main(logger, dataSource, migrationExecutor);
//
// ASSERT
//
expect(logger.error).not.toHaveBeenCalled();
expect(dataSource.undoLastMigration).toHaveBeenCalled();
expect(dataSource.destroy).toHaveBeenCalled();
});

View File

@@ -1,9 +0,0 @@
describe('userManagement.jwtRefreshTimeoutHours', () => {
it("resets jwtRefreshTimeoutHours to 0 if it's greater than or equal to jwtSessionDurationHours", async () => {
process.env.N8N_USER_MANAGEMENT_JWT_DURATION_HOURS = '1';
process.env.N8N_USER_MANAGEMENT_JWT_REFRESH_TIMEOUT_HOURS = '1';
const { default: config } = await import('@/config');
expect(config.getEnv('userManagement.jwtRefreshTimeoutHours')).toBe(0);
});
});

View File

@@ -1,51 +0,0 @@
import type { Request } from 'express';
import { mock } from 'jest-mock-extended';
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
import { CurlController } from '@/controllers/curl.controller';
import type { CurlService } from '@/services/curl.service';
describe('CurlController', () => {
const service = mock<CurlService>();
const controller = new CurlController(service);
beforeEach(() => jest.clearAllMocks());
describe('toJson', () => {
it('should throw BadRequestError when invalid cURL command is provided', () => {
const req = mock<Request>();
service.toHttpNodeParameters.mockImplementation(() => {
throw new Error();
});
expect(() => controller.toJson(req)).toThrow(BadRequestError);
});
it('should return flattened parameters when valid cURL command is provided', () => {
const curlCommand = 'curl -v -X GET https://test.n8n.berlin/users';
const req = mock<Request>();
req.body = { curlCommand };
service.toHttpNodeParameters.mockReturnValue({
url: 'https://test.n8n.berlin/users',
authentication: 'none',
method: 'GET',
sendHeaders: false,
sendQuery: false,
options: {
redirect: { redirect: {} },
response: { response: {} },
},
sendBody: false,
});
const result = controller.toJson(req);
expect(result).toEqual({
'parameters.method': 'GET',
'parameters.url': 'https://test.n8n.berlin/users',
'parameters.authentication': 'none',
'parameters.sendBody': false,
'parameters.sendHeaders': false,
'parameters.sendQuery': false,
});
});
});
});

View File

@@ -1,33 +0,0 @@
import { DynamicNodeParametersController } from '@/controllers/dynamicNodeParameters.controller';
import type { DynamicNodeParametersRequest } from '@/requests';
import type { DynamicNodeParametersService } from '@/services/dynamicNodeParameters.service';
import { mock } from 'jest-mock-extended';
import * as AdditionalData from '@/WorkflowExecuteAdditionalData';
import type { ILoadOptions, IWorkflowExecuteAdditionalData } from 'n8n-workflow';
describe('DynamicNodeParametersController', () => {
const service = mock<DynamicNodeParametersService>();
const controller = new DynamicNodeParametersController(service);
beforeEach(() => {
jest.clearAllMocks();
});
describe('getOptions', () => {
it('should take `loadOptions` as object', async () => {
jest
.spyOn(AdditionalData, 'getBase')
.mockResolvedValue(mock<IWorkflowExecuteAdditionalData>());
const req = mock<DynamicNodeParametersRequest.Options>();
const loadOptions: ILoadOptions = {};
req.body.loadOptions = loadOptions;
await controller.getOptions(req);
const zerothArg = service.getOptionsViaLoadOptions.mock.calls[0][0];
expect(zerothArg).toEqual(loadOptions);
});
});
});

View File

@@ -1,154 +0,0 @@
import { mock } from 'jest-mock-extended';
import { NotFoundError } from '@/errors/response-errors/not-found.error';
import { ExecutionsController } from '@/executions/executions.controller';
import type { ExecutionRequest, ExecutionSummaries } from '@/executions/execution.types';
import type { ExecutionService } from '@/executions/execution.service';
import type { WorkflowSharingService } from '@/workflows/workflowSharing.service';
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
describe('ExecutionsController', () => {
const executionService = mock<ExecutionService>();
const workflowSharingService = mock<WorkflowSharingService>();
const executionsController = new ExecutionsController(
executionService,
mock(),
workflowSharingService,
mock(),
);
beforeEach(() => {
jest.clearAllMocks();
});
describe('getOne', () => {
it('should 400 when execution is not a number', async () => {
const req = mock<ExecutionRequest.GetOne>({ params: { id: 'test' } });
await expect(executionsController.getOne(req)).rejects.toThrow(BadRequestError);
});
});
describe('getMany', () => {
const NO_EXECUTIONS = { count: 0, estimated: false, results: [] };
const QUERIES_WITH_EITHER_STATUS_OR_RANGE: ExecutionSummaries.RangeQuery[] = [
{
kind: 'range',
workflowId: undefined,
status: undefined,
range: { lastId: '999', firstId: '111', limit: 20 },
},
{
kind: 'range',
workflowId: undefined,
status: [],
range: { lastId: '999', firstId: '111', limit: 20 },
},
{
kind: 'range',
workflowId: undefined,
status: ['waiting'],
range: { lastId: undefined, firstId: undefined, limit: 20 },
},
{
kind: 'range',
workflowId: undefined,
status: [],
range: { lastId: '999', firstId: '111', limit: 20 },
},
];
const QUERIES_NEITHER_STATUS_NOR_RANGE_PROVIDED: ExecutionSummaries.RangeQuery[] = [
{
kind: 'range',
workflowId: undefined,
status: undefined,
range: { lastId: undefined, firstId: undefined, limit: 20 },
},
{
kind: 'range',
workflowId: undefined,
status: [],
range: { lastId: undefined, firstId: undefined, limit: 20 },
},
];
describe('if either status or range provided', () => {
test.each(QUERIES_WITH_EITHER_STATUS_OR_RANGE)(
'should fetch executions per query',
async (rangeQuery) => {
workflowSharingService.getSharedWorkflowIds.mockResolvedValue(['123']);
executionService.findLatestCurrentAndCompleted.mockResolvedValue(NO_EXECUTIONS);
const req = mock<ExecutionRequest.GetMany>({ rangeQuery });
await executionsController.getMany(req);
expect(executionService.findLatestCurrentAndCompleted).not.toHaveBeenCalled();
expect(executionService.findRangeWithCount).toHaveBeenCalledWith(rangeQuery);
},
);
});
describe('if neither status nor range provided', () => {
test.each(QUERIES_NEITHER_STATUS_NOR_RANGE_PROVIDED)(
'should fetch executions per query',
async (rangeQuery) => {
workflowSharingService.getSharedWorkflowIds.mockResolvedValue(['123']);
executionService.findLatestCurrentAndCompleted.mockResolvedValue(NO_EXECUTIONS);
const req = mock<ExecutionRequest.GetMany>({ rangeQuery });
await executionsController.getMany(req);
expect(executionService.findLatestCurrentAndCompleted).toHaveBeenCalled();
expect(executionService.findRangeWithCount).not.toHaveBeenCalled();
},
);
});
describe('if both status and range provided', () => {
it('should fetch executions per query', async () => {
workflowSharingService.getSharedWorkflowIds.mockResolvedValue(['123']);
executionService.findLatestCurrentAndCompleted.mockResolvedValue(NO_EXECUTIONS);
const rangeQuery: ExecutionSummaries.RangeQuery = {
kind: 'range',
workflowId: undefined,
status: ['success'],
range: { lastId: '999', firstId: '111', limit: 5 },
};
const req = mock<ExecutionRequest.GetMany>({ rangeQuery });
await executionsController.getMany(req);
expect(executionService.findLatestCurrentAndCompleted).not.toHaveBeenCalled();
expect(executionService.findRangeWithCount).toHaveBeenCalledWith(rangeQuery);
});
});
});
describe('stop', () => {
const executionId = '999';
const req = mock<ExecutionRequest.Stop>({ params: { id: executionId } });
it('should 404 when execution is inaccessible for user', async () => {
workflowSharingService.getSharedWorkflowIds.mockResolvedValue([]);
const promise = executionsController.stop(req);
await expect(promise).rejects.toThrow(NotFoundError);
expect(executionService.stop).not.toHaveBeenCalled();
});
it('should call ask for an execution to be stopped', async () => {
workflowSharingService.getSharedWorkflowIds.mockResolvedValue(['123']);
await executionsController.stop(req);
expect(executionService.stop).toHaveBeenCalledWith(executionId);
});
});
});

View File

@@ -1,250 +0,0 @@
import type { Response } from 'express';
import { Container } from 'typedi';
import jwt from 'jsonwebtoken';
import { mock, anyObject } from 'jest-mock-extended';
import type { PublicUser } from '@/Interfaces';
import type { User } from '@db/entities/User';
import { API_KEY_PREFIX, MeController } from '@/controllers/me.controller';
import { AUTH_COOKIE_NAME } from '@/constants';
import type { AuthenticatedRequest, MeRequest } from '@/requests';
import { UserService } from '@/services/user.service';
import { ExternalHooks } from '@/ExternalHooks';
import { InternalHooks } from '@/InternalHooks';
import { License } from '@/License';
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
import { UserRepository } from '@/databases/repositories/user.repository';
import { badPasswords } from '../shared/testData';
import { mockInstance } from '../../shared/mocking';
const browserId = 'test-browser-id';
describe('MeController', () => {
const externalHooks = mockInstance(ExternalHooks);
const internalHooks = mockInstance(InternalHooks);
const userService = mockInstance(UserService);
const userRepository = mockInstance(UserRepository);
mockInstance(License).isWithinUsersLimit.mockReturnValue(true);
const controller = Container.get(MeController);
describe('updateCurrentUser', () => {
it('should throw BadRequestError if email is missing in the payload', async () => {
const req = mock<MeRequest.UserUpdate>({});
await expect(controller.updateCurrentUser(req, mock())).rejects.toThrowError(
new BadRequestError('Email is mandatory'),
);
});
it('should throw BadRequestError if email is invalid', async () => {
const req = mock<MeRequest.UserUpdate>({ body: { email: 'invalid-email' } });
await expect(controller.updateCurrentUser(req, mock())).rejects.toThrowError(
new BadRequestError('Invalid email address'),
);
});
it('should update the user in the DB, and issue a new cookie', async () => {
const user = mock<User>({
id: '123',
password: 'password',
authIdentities: [],
role: 'global:owner',
});
const reqBody = { email: 'valid@email.com', firstName: 'John', lastName: 'Potato' };
const req = mock<MeRequest.UserUpdate>({ user, body: reqBody, browserId });
const res = mock<Response>();
userRepository.findOneOrFail.mockResolvedValue(user);
jest.spyOn(jwt, 'sign').mockImplementation(() => 'signed-token');
userService.toPublic.mockResolvedValue({} as unknown as PublicUser);
await controller.updateCurrentUser(req, res);
expect(externalHooks.run).toHaveBeenCalledWith('user.profile.beforeUpdate', [
user.id,
user.email,
reqBody,
]);
expect(userService.update).toHaveBeenCalled();
expect(res.cookie).toHaveBeenCalledWith(
AUTH_COOKIE_NAME,
'signed-token',
expect.objectContaining({
maxAge: expect.any(Number),
httpOnly: true,
sameSite: 'lax',
secure: false,
}),
);
expect(externalHooks.run).toHaveBeenCalledWith('user.profile.update', [
user.email,
anyObject(),
]);
});
it('should not allow updating any other fields on a user besides email and name', async () => {
const user = mock<User>({
id: '123',
password: 'password',
authIdentities: [],
role: 'global:member',
});
const reqBody = { email: 'valid@email.com', firstName: 'John', lastName: 'Potato' };
const req = mock<MeRequest.UserUpdate>({ user, browserId });
req.body = reqBody;
const res = mock<Response>();
userRepository.findOneOrFail.mockResolvedValue(user);
jest.spyOn(jwt, 'sign').mockImplementation(() => 'signed-token');
// Add invalid data to the request payload
Object.assign(reqBody, { id: '0', role: 'global:owner' });
await controller.updateCurrentUser(req, res);
expect(userService.update).toHaveBeenCalled();
const updatePayload = userService.update.mock.calls[0][1];
expect(updatePayload.email).toBe(reqBody.email);
expect(updatePayload.firstName).toBe(reqBody.firstName);
expect(updatePayload.lastName).toBe(reqBody.lastName);
expect(updatePayload.id).toBeUndefined();
expect(updatePayload.role).toBeUndefined();
});
it('should throw BadRequestError if beforeUpdate hook throws BadRequestError', async () => {
const user = mock<User>({
id: '123',
password: 'password',
authIdentities: [],
role: 'global:owner',
});
const reqBody = { email: 'valid@email.com', firstName: 'John', lastName: 'Potato' };
const req = mock<MeRequest.UserUpdate>({ user, body: reqBody });
// userService.findOneOrFail.mockResolvedValue(user);
externalHooks.run.mockImplementationOnce(async (hookName) => {
if (hookName === 'user.profile.beforeUpdate') {
throw new BadRequestError('Invalid email address');
}
});
await expect(controller.updateCurrentUser(req, mock())).rejects.toThrowError(
new BadRequestError('Invalid email address'),
);
});
});
describe('updatePassword', () => {
const passwordHash = '$2a$10$ffitcKrHT.Ls.m9FfWrMrOod76aaI0ogKbc3S96Q320impWpCbgj6'; // Hashed 'old_password'
it('should throw if the user does not have a password set', async () => {
const req = mock<MeRequest.Password>({
user: mock({ password: undefined }),
body: { currentPassword: '', newPassword: '' },
});
await expect(controller.updatePassword(req, mock())).rejects.toThrowError(
new BadRequestError('Requesting user not set up.'),
);
});
it("should throw if currentPassword does not match the user's password", async () => {
const req = mock<MeRequest.Password>({
user: mock({ password: passwordHash }),
body: { currentPassword: 'not_old_password', newPassword: '' },
});
await expect(controller.updatePassword(req, mock())).rejects.toThrowError(
new BadRequestError('Provided current password is incorrect.'),
);
});
describe('should throw if newPassword is not valid', () => {
Object.entries(badPasswords).forEach(([newPassword, errorMessage]) => {
it(newPassword, async () => {
const req = mock<MeRequest.Password>({
user: mock({ password: passwordHash }),
body: { currentPassword: 'old_password', newPassword },
browserId,
});
await expect(controller.updatePassword(req, mock())).rejects.toThrowError(
new BadRequestError(errorMessage),
);
});
});
});
it('should update the password in the DB, and issue a new cookie', async () => {
const req = mock<MeRequest.Password>({
user: mock({ password: passwordHash }),
body: { currentPassword: 'old_password', newPassword: 'NewPassword123' },
browserId,
});
const res = mock<Response>();
userRepository.save.calledWith(req.user).mockResolvedValue(req.user);
jest.spyOn(jwt, 'sign').mockImplementation(() => 'new-signed-token');
await controller.updatePassword(req, res);
expect(req.user.password).not.toBe(passwordHash);
expect(res.cookie).toHaveBeenCalledWith(
AUTH_COOKIE_NAME,
'new-signed-token',
expect.objectContaining({
maxAge: expect.any(Number),
httpOnly: true,
sameSite: 'lax',
secure: false,
}),
);
expect(externalHooks.run).toHaveBeenCalledWith('user.password.update', [
req.user.email,
req.user.password,
]);
expect(internalHooks.onUserUpdate).toHaveBeenCalledWith({
user: req.user,
fields_changed: ['password'],
});
});
});
describe('storeSurveyAnswers', () => {
it('should throw BadRequestError if answers are missing in the payload', async () => {
const req = mock<MeRequest.SurveyAnswers>({
body: undefined,
});
await expect(controller.storeSurveyAnswers(req)).rejects.toThrowError(
new BadRequestError('Personalization answers are mandatory'),
);
});
});
describe('API Key methods', () => {
let req: AuthenticatedRequest;
beforeAll(() => {
req = mock({ user: mock<Partial<User>>({ id: '123', apiKey: `${API_KEY_PREFIX}test-key` }) });
});
describe('createAPIKey', () => {
it('should create and save an API key', async () => {
const { apiKey } = await controller.createAPIKey(req);
expect(userService.update).toHaveBeenCalledWith(req.user.id, { apiKey });
});
});
describe('getAPIKey', () => {
it('should return the users api key redacted', async () => {
const { apiKey } = await controller.getAPIKey(req);
expect(apiKey).not.toEqual(req.user.apiKey);
});
});
describe('deleteAPIKey', () => {
it('should delete the API key', async () => {
await controller.deleteAPIKey(req);
expect(userService.update).toHaveBeenCalledWith(req.user.id, { apiKey: null });
});
});
});
});

View File

@@ -1,189 +0,0 @@
import nock from 'nock';
import Container from 'typedi';
import type { Response } from 'express';
import Csrf from 'csrf';
import { Cipher } from 'n8n-core';
import { mock } from 'jest-mock-extended';
import { OAuth1CredentialController } from '@/controllers/oauth/oAuth1Credential.controller';
import { CredentialsEntity } from '@db/entities/CredentialsEntity';
import type { User } from '@db/entities/User';
import type { OAuthRequest } from '@/requests';
import { CredentialsRepository } from '@db/repositories/credentials.repository';
import { SharedCredentialsRepository } from '@db/repositories/sharedCredentials.repository';
import { ExternalHooks } from '@/ExternalHooks';
import { Logger } from '@/Logger';
import { VariablesService } from '@/environments/variables/variables.service.ee';
import { SecretsHelper } from '@/SecretsHelpers';
import { CredentialsHelper } from '@/CredentialsHelper';
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
import { NotFoundError } from '@/errors/response-errors/not-found.error';
import { mockInstance } from '../../../shared/mocking';
describe('OAuth1CredentialController', () => {
mockInstance(Logger);
mockInstance(ExternalHooks);
mockInstance(SecretsHelper);
mockInstance(VariablesService, {
getAllCached: async () => [],
});
const cipher = mockInstance(Cipher);
const credentialsHelper = mockInstance(CredentialsHelper);
const credentialsRepository = mockInstance(CredentialsRepository);
const sharedCredentialsRepository = mockInstance(SharedCredentialsRepository);
const csrfSecret = 'csrf-secret';
const user = mock<User>({
id: '123',
password: 'password',
authIdentities: [],
role: 'global:owner',
});
const credential = mock<CredentialsEntity>({
id: '1',
name: 'Test Credential',
type: 'oAuth1Api',
});
const controller = Container.get(OAuth1CredentialController);
beforeEach(() => {
jest.resetAllMocks();
});
describe('getAuthUri', () => {
it('should throw a BadRequestError when credentialId is missing in the query', async () => {
const req = mock<OAuthRequest.OAuth1Credential.Auth>({ query: { id: '' } });
await expect(controller.getAuthUri(req)).rejects.toThrowError(
new BadRequestError('Required credential ID is missing'),
);
});
it('should throw a NotFoundError when no matching credential is found for the user', async () => {
sharedCredentialsRepository.findCredentialForUser.mockResolvedValueOnce(null);
const req = mock<OAuthRequest.OAuth1Credential.Auth>({ user, query: { id: '1' } });
await expect(controller.getAuthUri(req)).rejects.toThrowError(
new NotFoundError('Credential not found'),
);
});
it('should return a valid auth URI', async () => {
jest.spyOn(Csrf.prototype, 'secretSync').mockReturnValueOnce(csrfSecret);
jest.spyOn(Csrf.prototype, 'create').mockReturnValueOnce('token');
sharedCredentialsRepository.findCredentialForUser.mockResolvedValueOnce(credential);
credentialsHelper.getDecrypted.mockResolvedValueOnce({});
credentialsHelper.applyDefaultsAndOverwrites.mockReturnValueOnce({
requestTokenUrl: 'https://example.domain/oauth/request_token',
authUrl: 'https://example.domain/oauth/authorize',
signatureMethod: 'HMAC-SHA1',
});
nock('https://example.domain')
.post('/oauth/request_token', {
oauth_callback:
'http://localhost:5678/rest/oauth1-credential/callback?state=eyJ0b2tlbiI6InRva2VuIiwiY2lkIjoiMSJ9',
})
.reply(200, { oauth_token: 'random-token' });
cipher.encrypt.mockReturnValue('encrypted');
const req = mock<OAuthRequest.OAuth1Credential.Auth>({ user, query: { id: '1' } });
const authUri = await controller.getAuthUri(req);
expect(authUri).toEqual('https://example.domain/oauth/authorize?oauth_token=random-token');
expect(credentialsRepository.update).toHaveBeenCalledWith(
'1',
expect.objectContaining({
data: 'encrypted',
id: '1',
name: 'Test Credential',
type: 'oAuth1Api',
}),
);
expect(cipher.encrypt).toHaveBeenCalledWith({ csrfSecret });
});
});
describe('handleCallback', () => {
const validState = Buffer.from(
JSON.stringify({
token: 'token',
cid: '1',
}),
).toString('base64');
it('should render the error page when required query params are missing', async () => {
const req = mock<OAuthRequest.OAuth1Credential.Callback>();
const res = mock<Response>();
req.query = { state: 'test' } as OAuthRequest.OAuth1Credential.Callback['query'];
await controller.handleCallback(req, res);
expect(res.render).toHaveBeenCalledWith('oauth-error-callback', {
error: {
message: 'Insufficient parameters for OAuth1 callback.',
reason: 'Received following query parameters: {"state":"test"}',
},
});
expect(credentialsRepository.findOneBy).not.toHaveBeenCalled();
});
it('should render the error page when `state` query param is invalid', async () => {
const req = mock<OAuthRequest.OAuth1Credential.Callback>();
const res = mock<Response>();
req.query = {
oauth_verifier: 'verifier',
oauth_token: 'token',
state: 'test',
} as OAuthRequest.OAuth1Credential.Callback['query'];
await controller.handleCallback(req, res);
expect(res.render).toHaveBeenCalledWith('oauth-error-callback', {
error: {
message: 'Invalid state format',
},
});
expect(credentialsRepository.findOneBy).not.toHaveBeenCalled();
});
it('should render the error page when credential is not found in DB', async () => {
credentialsRepository.findOneBy.mockResolvedValueOnce(null);
const req = mock<OAuthRequest.OAuth1Credential.Callback>();
const res = mock<Response>();
req.query = {
oauth_verifier: 'verifier',
oauth_token: 'token',
state: validState,
} as OAuthRequest.OAuth1Credential.Callback['query'];
await controller.handleCallback(req, res);
expect(res.render).toHaveBeenCalledWith('oauth-error-callback', {
error: {
message: 'OAuth1 callback failed because of insufficient permissions',
},
});
expect(credentialsRepository.findOneBy).toHaveBeenCalledTimes(1);
expect(credentialsRepository.findOneBy).toHaveBeenCalledWith({ id: '1' });
});
it('should render the error page when state differs from the stored state in the credential', async () => {
credentialsRepository.findOneBy.mockResolvedValue(new CredentialsEntity());
credentialsHelper.getDecrypted.mockResolvedValue({ csrfSecret: 'invalid' });
const req = mock<OAuthRequest.OAuth1Credential.Callback>();
const res = mock<Response>();
req.query = {
oauth_verifier: 'verifier',
oauth_token: 'token',
state: validState,
} as OAuthRequest.OAuth1Credential.Callback['query'];
await controller.handleCallback(req, res);
expect(res.render).toHaveBeenCalledWith('oauth-error-callback', {
error: {
message: 'The OAuth1 callback state is invalid!',
},
});
});
});
});

View File

@@ -1,220 +0,0 @@
import nock from 'nock';
import Container from 'typedi';
import Csrf from 'csrf';
import { type Response } from 'express';
import { Cipher } from 'n8n-core';
import { mock } from 'jest-mock-extended';
import { OAuth2CredentialController } from '@/controllers/oauth/oAuth2Credential.controller';
import type { CredentialsEntity } from '@db/entities/CredentialsEntity';
import type { User } from '@db/entities/User';
import type { OAuthRequest } from '@/requests';
import { CredentialsRepository } from '@db/repositories/credentials.repository';
import { SharedCredentialsRepository } from '@db/repositories/sharedCredentials.repository';
import { ExternalHooks } from '@/ExternalHooks';
import { Logger } from '@/Logger';
import { VariablesService } from '@/environments/variables/variables.service.ee';
import { SecretsHelper } from '@/SecretsHelpers';
import { CredentialsHelper } from '@/CredentialsHelper';
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
import { NotFoundError } from '@/errors/response-errors/not-found.error';
import { mockInstance } from '../../../shared/mocking';
describe('OAuth2CredentialController', () => {
mockInstance(Logger);
mockInstance(SecretsHelper);
mockInstance(VariablesService, {
getAllCached: async () => [],
});
const cipher = mockInstance(Cipher);
const externalHooks = mockInstance(ExternalHooks);
const credentialsHelper = mockInstance(CredentialsHelper);
const credentialsRepository = mockInstance(CredentialsRepository);
const sharedCredentialsRepository = mockInstance(SharedCredentialsRepository);
const csrfSecret = 'csrf-secret';
const user = mock<User>({
id: '123',
password: 'password',
authIdentities: [],
role: 'global:owner',
});
const credential = mock<CredentialsEntity>({
id: '1',
name: 'Test Credential',
type: 'oAuth2Api',
});
const controller = Container.get(OAuth2CredentialController);
beforeEach(() => {
jest.resetAllMocks();
});
describe('getAuthUri', () => {
it('should throw a BadRequestError when credentialId is missing in the query', async () => {
const req = mock<OAuthRequest.OAuth2Credential.Auth>({ query: { id: '' } });
await expect(controller.getAuthUri(req)).rejects.toThrowError(
new BadRequestError('Required credential ID is missing'),
);
});
it('should throw a NotFoundError when no matching credential is found for the user', async () => {
sharedCredentialsRepository.findCredentialForUser.mockResolvedValueOnce(null);
const req = mock<OAuthRequest.OAuth2Credential.Auth>({ user, query: { id: '1' } });
await expect(controller.getAuthUri(req)).rejects.toThrowError(
new NotFoundError('Credential not found'),
);
});
it('should return a valid auth URI', async () => {
jest.spyOn(Csrf.prototype, 'secretSync').mockReturnValueOnce(csrfSecret);
jest.spyOn(Csrf.prototype, 'create').mockReturnValueOnce('token');
sharedCredentialsRepository.findCredentialForUser.mockResolvedValueOnce(credential);
credentialsHelper.getDecrypted.mockResolvedValueOnce({});
credentialsHelper.applyDefaultsAndOverwrites.mockReturnValue({
clientId: 'test-client-id',
authUrl: 'https://example.domain/o/oauth2/v2/auth',
});
cipher.encrypt.mockReturnValue('encrypted');
const req = mock<OAuthRequest.OAuth2Credential.Auth>({ user, query: { id: '1' } });
const authUri = await controller.getAuthUri(req);
expect(authUri).toEqual(
'https://example.domain/o/oauth2/v2/auth?client_id=test-client-id&redirect_uri=http%3A%2F%2Flocalhost%3A5678%2Frest%2Foauth2-credential%2Fcallback&response_type=code&state=eyJ0b2tlbiI6InRva2VuIiwiY2lkIjoiMSJ9&scope=openid',
);
expect(credentialsRepository.update).toHaveBeenCalledWith(
'1',
expect.objectContaining({
data: 'encrypted',
id: '1',
name: 'Test Credential',
type: 'oAuth2Api',
}),
);
});
});
describe('handleCallback', () => {
const validState = Buffer.from(
JSON.stringify({
token: 'token',
cid: '1',
}),
).toString('base64');
it('should render the error page when required query params are missing', async () => {
const req = mock<OAuthRequest.OAuth2Credential.Callback>({
query: { code: undefined, state: undefined },
});
const res = mock<Response>();
await controller.handleCallback(req, res);
expect(res.render).toHaveBeenCalledWith('oauth-error-callback', {
error: {
message: 'Insufficient parameters for OAuth2 callback.',
reason: 'Received following query parameters: undefined',
},
});
expect(credentialsRepository.findOneBy).not.toHaveBeenCalled();
});
it('should render the error page when `state` query param is invalid', async () => {
const req = mock<OAuthRequest.OAuth2Credential.Callback>({
query: { code: 'code', state: 'invalid-state' },
});
const res = mock<Response>();
await controller.handleCallback(req, res);
expect(res.render).toHaveBeenCalledWith('oauth-error-callback', {
error: {
message: 'Invalid state format',
},
});
expect(credentialsRepository.findOneBy).not.toHaveBeenCalled();
});
it('should render the error page when credential is not found in DB', async () => {
credentialsRepository.findOneBy.mockResolvedValueOnce(null);
const req = mock<OAuthRequest.OAuth2Credential.Callback>({
query: { code: 'code', state: validState },
});
const res = mock<Response>();
await controller.handleCallback(req, res);
expect(res.render).toHaveBeenCalledWith('oauth-error-callback', {
error: {
message: 'OAuth2 callback failed because of insufficient permissions',
},
});
expect(credentialsRepository.findOneBy).toHaveBeenCalledTimes(1);
expect(credentialsRepository.findOneBy).toHaveBeenCalledWith({ id: '1' });
});
it('should render the error page when csrfSecret on the saved credential does not match the state', async () => {
credentialsRepository.findOneBy.mockResolvedValueOnce(credential);
credentialsHelper.getDecrypted.mockResolvedValueOnce({ csrfSecret });
jest.spyOn(Csrf.prototype, 'verify').mockReturnValueOnce(false);
const req = mock<OAuthRequest.OAuth2Credential.Callback>({
query: { code: 'code', state: validState },
});
const res = mock<Response>();
await controller.handleCallback(req, res);
expect(res.render).toHaveBeenCalledWith('oauth-error-callback', {
error: {
message: 'The OAuth2 callback state is invalid!',
},
});
expect(externalHooks.run).not.toHaveBeenCalled();
});
it('should exchange the code for a valid token, and save it to DB', async () => {
credentialsRepository.findOneBy.mockResolvedValueOnce(credential);
credentialsHelper.getDecrypted.mockResolvedValueOnce({ csrfSecret });
credentialsHelper.applyDefaultsAndOverwrites.mockReturnValue({
clientId: 'test-client-id',
clientSecret: 'oauth-secret',
accessTokenUrl: 'https://example.domain/token',
});
jest.spyOn(Csrf.prototype, 'verify').mockReturnValueOnce(true);
nock('https://example.domain')
.post(
'/token',
'code=code&grant_type=authorization_code&redirect_uri=http%3A%2F%2Flocalhost%3A5678%2Frest%2Foauth2-credential%2Fcallback',
)
.reply(200, { access_token: 'access-token', refresh_token: 'refresh-token' });
cipher.encrypt.mockReturnValue('encrypted');
const req = mock<OAuthRequest.OAuth2Credential.Callback>({
query: { code: 'code', state: validState },
originalUrl: '?code=code',
});
const res = mock<Response>();
await controller.handleCallback(req, res);
expect(externalHooks.run).toHaveBeenCalledWith('oauth2.callback', [
expect.objectContaining({
clientId: 'test-client-id',
redirectUri: 'http://localhost:5678/rest/oauth2-credential/callback',
}),
]);
expect(cipher.encrypt).toHaveBeenCalledWith({
oauthTokenData: { access_token: 'access-token', refresh_token: 'refresh-token' },
});
expect(credentialsRepository.update).toHaveBeenCalledWith(
'1',
expect.objectContaining({
data: 'encrypted',
id: '1',
name: 'Test Credential',
type: 'oAuth2Api',
}),
);
expect(res.render).toHaveBeenCalledWith('oauth-callback');
});
});
});

View File

@@ -1,111 +0,0 @@
import Container from 'typedi';
import type { Response } from 'express';
import { anyObject, mock } from 'jest-mock-extended';
import jwt from 'jsonwebtoken';
import type { AuthService } from '@/auth/auth.service';
import config from '@/config';
import { OwnerController } from '@/controllers/owner.controller';
import type { User } from '@db/entities/User';
import type { SettingsRepository } from '@db/repositories/settings.repository';
import type { UserRepository } from '@db/repositories/user.repository';
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
import type { InternalHooks } from '@/InternalHooks';
import { License } from '@/License';
import type { OwnerRequest } from '@/requests';
import type { UserService } from '@/services/user.service';
import { PasswordUtility } from '@/services/password.utility';
import { mockInstance } from '../../shared/mocking';
import { badPasswords } from '../shared/testData';
describe('OwnerController', () => {
const configGetSpy = jest.spyOn(config, 'getEnv');
const internalHooks = mock<InternalHooks>();
const authService = mock<AuthService>();
const userService = mock<UserService>();
const userRepository = mock<UserRepository>();
const settingsRepository = mock<SettingsRepository>();
mockInstance(License).isWithinUsersLimit.mockReturnValue(true);
const controller = new OwnerController(
mock(),
internalHooks,
settingsRepository,
authService,
userService,
Container.get(PasswordUtility),
mock(),
userRepository,
);
describe('setupOwner', () => {
it('should throw a BadRequestError if the instance owner is already setup', async () => {
configGetSpy.mockReturnValue(true);
await expect(controller.setupOwner(mock(), mock())).rejects.toThrowError(
new BadRequestError('Instance owner already setup'),
);
});
it('should throw a BadRequestError if the email is invalid', async () => {
configGetSpy.mockReturnValue(false);
const req = mock<OwnerRequest.Post>({ body: { email: 'invalid email' } });
await expect(controller.setupOwner(req, mock())).rejects.toThrowError(
new BadRequestError('Invalid email address'),
);
});
describe('should throw if the password is invalid', () => {
Object.entries(badPasswords).forEach(([password, errorMessage]) => {
it(password, async () => {
configGetSpy.mockReturnValue(false);
const req = mock<OwnerRequest.Post>({ body: { email: 'valid@email.com', password } });
await expect(controller.setupOwner(req, mock())).rejects.toThrowError(
new BadRequestError(errorMessage),
);
});
});
});
it('should throw a BadRequestError if firstName & lastName are missing ', async () => {
configGetSpy.mockReturnValue(false);
const req = mock<OwnerRequest.Post>({
body: { email: 'valid@email.com', password: 'NewPassword123', firstName: '', lastName: '' },
});
await expect(controller.setupOwner(req, mock())).rejects.toThrowError(
new BadRequestError('First and last names are mandatory'),
);
});
it('should setup the instance owner successfully', async () => {
const user = mock<User>({
id: 'userId',
role: 'global:owner',
authIdentities: [],
});
const browserId = 'test-browser-id';
const req = mock<OwnerRequest.Post>({
body: {
email: 'valid@email.com',
password: 'NewPassword123',
firstName: 'Jane',
lastName: 'Doe',
},
user,
browserId,
});
const res = mock<Response>();
configGetSpy.mockReturnValue(false);
userRepository.findOneOrFail.calledWith(anyObject()).mockResolvedValue(user);
userRepository.save.calledWith(anyObject()).mockResolvedValue(user);
jest.spyOn(jwt, 'sign').mockImplementation(() => 'signed-token');
await controller.setupOwner(req, res);
expect(userRepository.findOneOrFail).toHaveBeenCalledWith({
where: { role: 'global:owner' },
});
expect(userRepository.save).toHaveBeenCalledWith(user, { transaction: false });
expect(authService.issueCookie).toHaveBeenCalledWith(res, user, browserId);
});
});
});

View File

@@ -1,40 +0,0 @@
import { mock } from 'jest-mock-extended';
import config from '@/config';
import type { TranslationRequest } from '@/controllers/translation.controller';
import {
TranslationController,
CREDENTIAL_TRANSLATIONS_DIR,
} from '@/controllers/translation.controller';
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
import type { CredentialTypes } from '@/CredentialTypes';
describe('TranslationController', () => {
const configGetSpy = jest.spyOn(config, 'getEnv');
const credentialTypes = mock<CredentialTypes>();
const controller = new TranslationController(credentialTypes);
describe('getCredentialTranslation', () => {
it('should throw 400 on invalid credential types', async () => {
const credentialType = 'not-a-valid-credential-type';
const req = mock<TranslationRequest.Credential>({ query: { credentialType } });
credentialTypes.recognizes.calledWith(credentialType).mockReturnValue(false);
await expect(controller.getCredentialTranslation(req)).rejects.toThrowError(
new BadRequestError(`Invalid Credential type: "${credentialType}"`),
);
});
it('should return translation json on valid credential types', async () => {
const credentialType = 'credential-type';
const req = mock<TranslationRequest.Credential>({ query: { credentialType } });
configGetSpy.mockReturnValue('de');
credentialTypes.recognizes.calledWith(credentialType).mockReturnValue(true);
const response = { translation: 'string' };
jest.mock(`${CREDENTIAL_TRANSLATIONS_DIR}/de/credential-type.json`, () => response, {
virtual: true,
});
expect(await controller.getCredentialTranslation(req)).toEqual(response);
});
});
});

View File

@@ -1,148 +0,0 @@
import { UserSettingsController } from '@/controllers/userSettings.controller';
import type { NpsSurveyRequest } from '@/requests';
import type { UserService } from '@/services/user.service';
import { mock } from 'jest-mock-extended';
import type { NpsSurveyState } from 'n8n-workflow';
const NOW = 1717607016208;
jest.useFakeTimers({
now: NOW,
});
describe('UserSettingsController', () => {
const userService = mock<UserService>();
const controller = new UserSettingsController(userService);
describe('NPS Survey', () => {
test.each([
[
'updates user settings, setting response state to done',
{
responded: true,
lastShownAt: 1717607016208,
},
[],
],
[
'updates user settings, setting response state to done, ignoring other keys like waitForResponse',
{
responded: true,
lastShownAt: 1717607016208,
waitingForResponse: true,
},
['waitingForResponse'],
],
[
'updates user settings, setting response state to done, ignoring other keys like ignoredCount',
{
responded: true,
lastShownAt: 1717607016208,
ignoredCount: 1,
},
['ignoredCount'],
],
[
'updates user settings, setting response state to done, ignoring other unknown keys',
{
responded: true,
lastShownAt: 1717607016208,
x: 1,
},
['x'],
],
[
'updates user settings, updating ignore count',
{
waitingForResponse: true,
lastShownAt: 1717607016208,
ignoredCount: 1,
},
[],
],
[
'updates user settings, reseting to waiting state',
{
waitingForResponse: true,
ignoredCount: 0,
lastShownAt: 1717607016208,
},
[],
],
[
'updates user settings, updating ignore count, ignoring unknown keys',
{
waitingForResponse: true,
lastShownAt: 1717607016208,
ignoredCount: 1,
x: 1,
},
['x'],
],
])('%s', async (_, toUpdate, toIgnore: string[] | undefined) => {
const req = mock<NpsSurveyRequest.NpsSurveyUpdate>();
req.user.id = '1';
req.body = toUpdate;
await controller.updateNpsSurvey(req);
const npsSurvey = Object.keys(toUpdate).reduce(
(accu, key) => {
if ((toIgnore ?? []).includes(key)) {
return accu;
}
accu[key] = (toUpdate as Record<string, unknown>)[key];
return accu;
},
{} as Record<string, unknown>,
);
expect(userService.updateSettings).toHaveBeenCalledWith('1', { npsSurvey });
});
it('updates user settings, setting response state to done', async () => {
const req = mock<NpsSurveyRequest.NpsSurveyUpdate>();
req.user.id = '1';
const npsSurvey: NpsSurveyState = {
responded: true,
lastShownAt: 1717607016208,
};
req.body = npsSurvey;
await controller.updateNpsSurvey(req);
expect(userService.updateSettings).toHaveBeenCalledWith('1', { npsSurvey });
});
it('updates user settings, updating ignore count', async () => {
const req = mock<NpsSurveyRequest.NpsSurveyUpdate>();
req.user.id = '1';
const npsSurvey: NpsSurveyState = {
waitingForResponse: true,
lastShownAt: 1717607016208,
ignoredCount: 1,
};
req.body = npsSurvey;
await controller.updateNpsSurvey(req);
expect(userService.updateSettings).toHaveBeenCalledWith('1', { npsSurvey });
});
test.each([
['is missing', {}],
['is undefined', undefined],
['is responded but missing lastShownAt', { responded: true }],
['is waitingForResponse but missing lastShownAt', { waitingForResponse: true }],
[
'is waitingForResponse but missing ignoredCount',
{ lastShownAt: 123, waitingForResponse: true },
],
])('thows error when request payload is %s', async (_, payload) => {
const req = mock<NpsSurveyRequest.NpsSurveyUpdate>();
req.user.id = '1';
req.body = payload;
await expect(controller.updateNpsSurvey(req)).rejects.toThrowError();
});
});
});

View File

@@ -1,32 +0,0 @@
import { CredentialsTester } from '@/services/credentials-tester.service';
import mock from 'jest-mock-extended/lib/Mock';
import type { CredentialTypes } from '@/CredentialTypes';
import type { ICredentialType, INodeType } from 'n8n-workflow';
import type { NodeTypes } from '@/NodeTypes';
describe('CredentialsTester', () => {
const credentialTypes = mock<CredentialTypes>();
const nodeTypes = mock<NodeTypes>();
const credentialsTester = new CredentialsTester(mock(), credentialTypes, nodeTypes, mock());
beforeEach(() => {
jest.clearAllMocks();
});
it('should find the OAuth2 credential test for a generic OAuth2 API credential', () => {
credentialTypes.getByName.mockReturnValue(mock<ICredentialType>({ test: undefined }));
credentialTypes.getSupportedNodes.mockReturnValue(['oAuth2Api']);
credentialTypes.getParentTypes.mockReturnValue([]);
nodeTypes.getByName.mockReturnValue(
mock<INodeType>({
description: { credentials: [{ name: 'oAuth2Api' }] },
}),
);
const testFn = credentialsTester.getCredentialTestFunction('oAuth2Api');
if (typeof testFn !== 'function') fail();
expect(testFn.name).toBe('oauth2CredTest');
});
});

View File

@@ -1,36 +0,0 @@
import { User } from '@db/entities/User';
describe('User Entity', () => {
describe('JSON.stringify', () => {
it('should not serialize sensitive data', () => {
const user = Object.assign(new User(), {
email: 'test@example.com',
firstName: 'Don',
lastName: 'Joe',
password: '123456789',
apiKey: '123',
});
expect(JSON.stringify(user)).toEqual(
'{"email":"test@example.com","firstName":"Don","lastName":"Joe"}',
);
});
});
describe('createPersonalProjectName', () => {
test.each([
['Nathan', 'Nathaniel', 'nathan@nathaniel.n8n', 'Nathan Nathaniel <nathan@nathaniel.n8n>'],
[undefined, 'Nathaniel', 'nathan@nathaniel.n8n', '<nathan@nathaniel.n8n>'],
['Nathan', undefined, 'nathan@nathaniel.n8n', '<nathan@nathaniel.n8n>'],
[undefined, undefined, 'nathan@nathaniel.n8n', '<nathan@nathaniel.n8n>'],
[undefined, undefined, undefined, 'Unnamed Project'],
['Nathan', 'Nathaniel', undefined, 'Unnamed Project'],
])(
'given fistName: %s, lastName: %s and email: %s this gives the projectName: "%s"',
async (firstName, lastName, email, projectName) => {
const user = new User();
Object.assign(user, { firstName, lastName, email });
expect(user.createPersonalProjectName()).toBe(projectName);
},
);
});
});

View File

@@ -1,43 +0,0 @@
import { NoXss } from '@db/utils/customValidators';
import { validate } from 'class-validator';
describe('customValidators', () => {
describe('NoXss', () => {
class Person {
@NoXss()
name: string;
}
const person = new Person();
const invalidNames = ['http://google.com', '<script src/>', 'www.domain.tld'];
const validNames = [
'Johann Strauß',
'Вагиф Сәмәдоғлу',
'René Magritte',
'সুকুমার রায়',
'མགོན་པོ་རྡོ་རྗེ།',
'عبدالحليم حافظ',
];
describe('Block XSS', () => {
for (const name of invalidNames) {
test(name, async () => {
person.name = name;
const validationErrors = await validate(person);
expect(validationErrors[0].property).toEqual('name');
expect(validationErrors[0].constraints).toEqual({ NoXss: 'Malicious name' });
});
}
});
describe('Allow Valid names', () => {
for (const name of validNames) {
test(name, async () => {
person.name = name;
expect(await validate(person)).toBeEmptyArray();
});
}
});
});
});

View File

@@ -1,66 +0,0 @@
import type { IrreversibleMigration, ReversibleMigration } from '@/databases/types';
import { wrapMigration } from '@/databases/utils/migrationHelpers';
describe('migrationHelpers.wrapMigration', () => {
test('throws if passed a migration without up method', async () => {
//
// ARRANGE
//
class TestMigration {}
//
// ACT & ASSERT
//
expect(() => wrapMigration(TestMigration as never)).toThrow(
'Migration "TestMigration" is missing the method `up`.',
);
});
test('wraps up method', async () => {
//
// ARRANGE
//
class TestMigration implements IrreversibleMigration {
async up() {}
}
const originalUp = jest.fn();
TestMigration.prototype.up = originalUp;
//
// ACT
//
wrapMigration(TestMigration);
await new TestMigration().up();
//
// ASSERT
//
expect(TestMigration.prototype.up).not.toBe(originalUp);
expect(originalUp).toHaveBeenCalledTimes(1);
});
test('wraps down method', async () => {
//
// ARRANGE
//
class TestMigration implements ReversibleMigration {
async up() {}
async down() {}
}
const originalDown = jest.fn();
TestMigration.prototype.down = originalDown;
//
// ACT
//
wrapMigration(TestMigration);
await new TestMigration().down();
//
// ASSERT
//
expect(TestMigration.prototype.down).not.toBe(originalDown);
expect(originalDown).toHaveBeenCalledTimes(1);
});
});

View File

@@ -1,98 +0,0 @@
import Container, { Service } from 'typedi';
import { OnShutdown } from '@/decorators/OnShutdown';
import { ShutdownService } from '@/shutdown/Shutdown.service';
import { mock } from 'jest-mock-extended';
describe('OnShutdown', () => {
let shutdownService: ShutdownService;
beforeEach(() => {
shutdownService = new ShutdownService(mock());
Container.set(ShutdownService, shutdownService);
jest.spyOn(shutdownService, 'register');
});
it('should register a methods that is decorated with OnShutdown', () => {
@Service()
class TestClass {
@OnShutdown()
async onShutdown() {}
}
expect(shutdownService.register).toHaveBeenCalledTimes(1);
expect(shutdownService.register).toHaveBeenCalledWith(100, {
methodName: 'onShutdown',
serviceClass: TestClass,
});
});
it('should register multiple methods in the same class', () => {
@Service()
class TestClass {
@OnShutdown()
async one() {}
@OnShutdown()
async two() {}
}
expect(shutdownService.register).toHaveBeenCalledTimes(2);
expect(shutdownService.register).toHaveBeenCalledWith(100, {
methodName: 'one',
serviceClass: TestClass,
});
expect(shutdownService.register).toHaveBeenCalledWith(100, {
methodName: 'two',
serviceClass: TestClass,
});
});
it('should use the given priority', () => {
// @ts-expect-error We are checking the decorator.
class TestClass {
@OnShutdown(10)
async onShutdown() {
// Will be called when the app is shutting down
}
}
expect(shutdownService.register).toHaveBeenCalledTimes(1);
// @ts-expect-error We are checking internal parts of the shutdown service
expect(shutdownService.handlersByPriority[10].length).toEqual(1);
});
it('should throw an error if the decorated member is not a function', () => {
expect(() => {
@Service()
class TestClass {
// @ts-expect-error Testing invalid code
@OnShutdown()
onShutdown = 'not a function';
}
new TestClass();
}).toThrow('TestClass.onShutdown() must be a method on TestClass to use "OnShutdown"');
});
it('should throw if the priority is invalid', () => {
expect(() => {
@Service()
class TestClass {
@OnShutdown(201)
async onShutdown() {}
}
new TestClass();
}).toThrow('Invalid shutdown priority. Please set it between 0 and 200.');
expect(() => {
@Service()
class TestClass {
@OnShutdown(-1)
async onShutdown() {}
}
new TestClass();
}).toThrow('Invalid shutdown priority. Please set it between 0 and 200.');
});
});

View File

@@ -1,117 +0,0 @@
jest.mock('@/constants', () => ({
inProduction: true,
}));
import express from 'express';
import { agent as testAgent } from 'supertest';
import { mock } from 'jest-mock-extended';
import { ControllerRegistry, Get, Licensed, RestController } from '@/decorators';
import type { AuthService } from '@/auth/auth.service';
import type { License } from '@/License';
import type { SuperAgentTest } from '@test-integration/types';
import type { GlobalConfig } from '@n8n/config';
describe('ControllerRegistry', () => {
const license = mock<License>();
const authService = mock<AuthService>();
const globalConfig = mock<GlobalConfig>({ endpoints: { rest: 'rest' } });
let agent: SuperAgentTest;
beforeEach(() => {
jest.resetAllMocks();
const app = express();
new ControllerRegistry(license, authService, globalConfig).activate(app);
agent = testAgent(app);
});
describe('Rate limiting', () => {
@RestController('/test')
// @ts-expect-error tsc complains about unused class
class TestController {
@Get('/unlimited')
unlimited() {
return { ok: true };
}
@Get('/rate-limited', { rateLimit: true })
rateLimited() {
return { ok: true };
}
}
beforeEach(() => {
authService.authMiddleware.mockImplementation(async (_req, _res, next) => next());
});
it('should not rate-limit by default', async () => {
for (let i = 0; i < 6; i++) {
await agent.get('/rest/test/unlimited').expect(200);
}
});
it('should rate-limit when configured', async () => {
for (let i = 0; i < 5; i++) {
await agent.get('/rest/test/rate-limited').expect(200);
}
await agent.get('/rest/test/rate-limited').expect(429);
});
});
describe('Authorization', () => {
@RestController('/test')
// @ts-expect-error tsc complains about unused class
class TestController {
@Get('/no-auth', { skipAuth: true })
noAuth() {
return { ok: true };
}
@Get('/auth')
auth() {
return { ok: true };
}
}
it('should not require auth if configured to skip', async () => {
await agent.get('/rest/test/no-auth').expect(200);
expect(authService.authMiddleware).not.toHaveBeenCalled();
});
it('should require auth by default', async () => {
authService.authMiddleware.mockImplementation(async (_req, res) => {
res.status(401).send();
});
await agent.get('/rest/test/auth').expect(401);
expect(authService.authMiddleware).toHaveBeenCalled();
});
});
describe('License checks', () => {
@RestController('/test')
// @ts-expect-error tsc complains about unused class
class TestController {
@Get('/with-sharing')
@Licensed('feat:sharing')
sharing() {
return { ok: true };
}
}
beforeEach(() => {
authService.authMiddleware.mockImplementation(async (_req, _res, next) => next());
});
it('should disallow when feature is missing', async () => {
license.isFeatureEnabled.calledWith('feat:sharing').mockReturnValue(false);
await agent.get('/rest/test/with-sharing').expect(403);
expect(license.isFeatureEnabled).toHaveBeenCalled();
});
it('should allow when feature is available', async () => {
license.isFeatureEnabled.calledWith('feat:sharing').mockReturnValue(true);
await agent.get('/rest/test/with-sharing').expect(200);
expect(license.isFeatureEnabled).toHaveBeenCalled();
});
});
});

View File

@@ -1,192 +0,0 @@
import { restoreBinaryDataId } from '@/executionLifecycleHooks/restoreBinaryDataId';
import { BinaryDataService } from 'n8n-core';
import { mockInstance } from '../../shared/mocking';
import type { IRun } from 'n8n-workflow';
import config from '@/config';
function toIRun(item?: object) {
return {
data: {
resultData: {
runData: {
myNode: [
{
data: {
main: [[item]],
},
},
],
},
},
},
} as unknown as IRun;
}
function getDataId(run: IRun, kind: 'binary' | 'json') {
// @ts-ignore
return run.data.resultData.runData.myNode[0].data.main[0][0][kind].data.id;
}
const binaryDataService = mockInstance(BinaryDataService);
for (const mode of ['filesystem-v2', 's3'] as const) {
describe(`on ${mode} mode`, () => {
beforeAll(() => {
config.set('binaryDataManager.mode', mode);
});
afterEach(() => {
jest.clearAllMocks();
});
it('should restore if binary data ID is missing execution ID', async () => {
const workflowId = '6HYhhKmJch2cYxGj';
const executionId = 'temp';
const binaryDataFileUuid = 'a5c3f1ed-9d59-4155-bc68-9a370b3c51f6';
const incorrectFileId = `workflows/${workflowId}/executions/temp/binary_data/${binaryDataFileUuid}`;
const run = toIRun({
binary: {
data: { id: `s3:${incorrectFileId}` },
},
});
await restoreBinaryDataId(run, executionId, 'webhook');
const correctFileId = incorrectFileId.replace('temp', executionId);
const correctBinaryDataId = `s3:${correctFileId}`;
expect(binaryDataService.rename).toHaveBeenCalledWith(incorrectFileId, correctFileId);
expect(getDataId(run, 'binary')).toBe(correctBinaryDataId);
});
it('should do nothing if binary data ID is not missing execution ID', async () => {
const workflowId = '6HYhhKmJch2cYxGj';
const executionId = '999';
const binaryDataFileUuid = 'a5c3f1ed-9d59-4155-bc68-9a370b3c51f6';
const fileId = `workflows/${workflowId}/executions/${executionId}/binary_data/${binaryDataFileUuid}`;
const binaryDataId = `s3:${fileId}`;
const run = toIRun({
binary: {
data: {
id: binaryDataId,
},
},
});
await restoreBinaryDataId(run, executionId, 'webhook');
expect(binaryDataService.rename).not.toHaveBeenCalled();
expect(getDataId(run, 'binary')).toBe(binaryDataId);
});
it('should do nothing if no binary data ID', async () => {
const executionId = '999';
const dataId = '123';
const run = toIRun({
json: {
data: { id: dataId },
},
});
await restoreBinaryDataId(run, executionId, 'webhook');
expect(binaryDataService.rename).not.toHaveBeenCalled();
expect(getDataId(run, 'json')).toBe(dataId);
});
it('should do nothing on itemless case', async () => {
const executionId = '999';
const promise = restoreBinaryDataId(toIRun(), executionId, 'webhook');
await expect(promise).resolves.not.toThrow();
expect(binaryDataService.rename).not.toHaveBeenCalled();
});
it('should do nothing if data is undefined', async () => {
const executionId = '999';
const run = toIRun({
json: {
data: undefined,
},
});
const promise = restoreBinaryDataId(run, executionId, 'webhook');
await expect(promise).resolves.not.toThrow();
expect(binaryDataService.rename).not.toHaveBeenCalled();
});
it('should do nothing if workflow execution mode is not `webhook`', async () => {
const executionId = '999';
const run = toIRun({
json: {
data: undefined,
},
});
const promise = restoreBinaryDataId(run, executionId, 'internal');
await expect(promise).resolves.not.toThrow();
expect(binaryDataService.rename).not.toHaveBeenCalled();
});
it('should ignore error thrown on renaming', async () => {
const workflowId = '6HYhhKmJch2cYxGj';
const executionId = 'temp';
const binaryDataFileUuid = 'a5c3f1ed-9d59-4155-bc68-9a370b3c51f6';
const incorrectFileId = `workflows/${workflowId}/executions/temp/binary_data/${binaryDataFileUuid}`;
const run = toIRun({
binary: {
data: { id: `s3:${incorrectFileId}` },
},
});
binaryDataService.rename.mockRejectedValueOnce(new Error('ENOENT'));
const promise = restoreBinaryDataId(run, executionId, 'webhook');
await expect(promise).resolves.not.toThrow();
expect(binaryDataService.rename).toHaveBeenCalled();
});
});
}
describe('on default mode', () => {
afterEach(() => {
config.load(config.default);
});
it('should do nothing', async () => {
config.set('binaryDataManager.mode', 'default');
const executionId = '999';
const run = toIRun({
json: {
data: undefined,
},
});
const promise = restoreBinaryDataId(run, executionId, 'internal');
await expect(promise).resolves.not.toThrow();
expect(binaryDataService.rename).not.toHaveBeenCalled();
});
});

View File

@@ -1,106 +0,0 @@
import { ExecutionRepository } from '@/databases/repositories/execution.repository';
import { mockInstance } from '../../shared/mocking';
import { Logger } from '@/Logger';
import { saveExecutionProgress } from '@/executionLifecycleHooks/saveExecutionProgress';
import * as fnModule from '@/executionLifecycleHooks/toSaveSettings';
import {
ErrorReporterProxy,
type IRunExecutionData,
type ITaskData,
type IWorkflowBase,
} from 'n8n-workflow';
import type { IExecutionResponse } from '@/Interfaces';
mockInstance(Logger);
const executionRepository = mockInstance(ExecutionRepository);
afterEach(() => {
jest.clearAllMocks();
});
const commonArgs: [IWorkflowBase, string, string, ITaskData, IRunExecutionData, string] = [
{} as IWorkflowBase,
'some-execution-id',
'My Node',
{} as ITaskData,
{} as IRunExecutionData,
'some-session-id',
];
const commonSettings = { error: true, success: true, manual: true };
test('should ignore if save settings say so', async () => {
jest.spyOn(fnModule, 'toSaveSettings').mockReturnValue({
...commonSettings,
progress: false,
});
await saveExecutionProgress(...commonArgs);
expect(executionRepository.updateExistingExecution).not.toHaveBeenCalled();
});
test('should ignore on leftover async call', async () => {
jest.spyOn(fnModule, 'toSaveSettings').mockReturnValue({
...commonSettings,
progress: true,
});
executionRepository.findSingleExecution.mockResolvedValue({
finished: true,
} as IExecutionResponse);
await saveExecutionProgress(...commonArgs);
expect(executionRepository.updateExistingExecution).not.toHaveBeenCalled();
});
test('should update execution', async () => {
jest.spyOn(fnModule, 'toSaveSettings').mockReturnValue({
...commonSettings,
progress: true,
});
const reporterSpy = jest.spyOn(ErrorReporterProxy, 'error');
executionRepository.findSingleExecution.mockResolvedValue({} as IExecutionResponse);
await saveExecutionProgress(...commonArgs);
expect(executionRepository.updateExistingExecution).toHaveBeenCalledWith('some-execution-id', {
data: {
executionData: undefined,
resultData: {
lastNodeExecuted: 'My Node',
runData: {
'My Node': [{}],
},
},
startData: {},
},
status: 'running',
});
expect(reporterSpy).not.toHaveBeenCalled();
});
test('should report error on failure', async () => {
jest.spyOn(fnModule, 'toSaveSettings').mockReturnValue({
...commonSettings,
progress: true,
});
const reporterSpy = jest.spyOn(ErrorReporterProxy, 'error');
const error = new Error('Something went wrong');
executionRepository.findSingleExecution.mockImplementation(() => {
throw error;
});
await saveExecutionProgress(...commonArgs);
expect(executionRepository.updateExistingExecution).not.toHaveBeenCalled();
expect(reporterSpy).toHaveBeenCalledWith(error);
});

View File

@@ -1,154 +0,0 @@
import config from '@/config';
import { toSaveSettings } from '@/executionLifecycleHooks/toSaveSettings';
afterEach(() => {
config.load(config.default);
});
describe('failed production executions', () => {
it('should favor workflow settings over defaults', () => {
config.set('executions.saveDataOnError', 'none');
const saveSettings = toSaveSettings({ saveDataErrorExecution: 'all' });
expect(saveSettings.error).toBe(true);
config.set('executions.saveDataOnError', 'all');
const _saveSettings = toSaveSettings({ saveDataErrorExecution: 'none' });
expect(_saveSettings.error).toBe(false);
});
it('should fall back to default if no workflow setting', () => {
config.set('executions.saveDataOnError', 'all');
const saveSettings = toSaveSettings();
expect(saveSettings.error).toBe(true);
config.set('executions.saveDataOnError', 'none');
const _saveSettings = toSaveSettings();
expect(_saveSettings.error).toBe(false);
});
});
describe('sucessful production executions', () => {
it('should favor workflow settings over defaults', () => {
config.set('executions.saveDataOnSuccess', 'none');
const saveSettings = toSaveSettings({ saveDataSuccessExecution: 'all' });
expect(saveSettings.success).toBe(true);
config.set('executions.saveDataOnSuccess', 'all');
const _saveSettings = toSaveSettings({ saveDataSuccessExecution: 'none' });
expect(_saveSettings.success).toBe(false);
});
it('should fall back to default if no workflow setting', () => {
config.set('executions.saveDataOnSuccess', 'all');
const saveSettings = toSaveSettings();
expect(saveSettings.success).toBe(true);
config.set('executions.saveDataOnSuccess', 'none');
const _saveSettings = toSaveSettings();
expect(_saveSettings.success).toBe(false);
});
});
describe('manual executions', () => {
it('should favor workflow setting over default', () => {
config.set('executions.saveDataManualExecutions', false);
const saveSettings = toSaveSettings({ saveManualExecutions: true });
expect(saveSettings.manual).toBe(true);
config.set('executions.saveDataManualExecutions', true);
const _saveSettings = toSaveSettings({ saveManualExecutions: false });
expect(_saveSettings.manual).toBe(false);
});
it('should favor fall back to default if workflow setting is explicit default', () => {
config.set('executions.saveDataManualExecutions', true);
const saveSettings = toSaveSettings({ saveManualExecutions: 'DEFAULT' });
expect(saveSettings.manual).toBe(true);
config.set('executions.saveDataManualExecutions', false);
const _saveSettings = toSaveSettings({ saveManualExecutions: 'DEFAULT' });
expect(_saveSettings.manual).toBe(false);
});
it('should fall back to default if no workflow setting', () => {
config.set('executions.saveDataManualExecutions', true);
const saveSettings = toSaveSettings();
expect(saveSettings.manual).toBe(true);
config.set('executions.saveDataManualExecutions', false);
const _saveSettings = toSaveSettings();
expect(_saveSettings.manual).toBe(false);
});
});
describe('execution progress', () => {
it('should favor workflow setting over default', () => {
config.set('executions.saveExecutionProgress', false);
const saveSettings = toSaveSettings({ saveExecutionProgress: true });
expect(saveSettings.progress).toBe(true);
config.set('executions.saveExecutionProgress', true);
const _saveSettings = toSaveSettings({ saveExecutionProgress: false });
expect(_saveSettings.progress).toBe(false);
});
it('should favor fall back to default if workflow setting is explicit default', () => {
config.set('executions.saveExecutionProgress', true);
const saveSettings = toSaveSettings({ saveExecutionProgress: 'DEFAULT' });
expect(saveSettings.progress).toBe(true);
config.set('executions.saveExecutionProgress', false);
const _saveSettings = toSaveSettings({ saveExecutionProgress: 'DEFAULT' });
expect(_saveSettings.progress).toBe(false);
});
it('should fall back to default if no workflow setting', () => {
config.set('executions.saveExecutionProgress', true);
const saveSettings = toSaveSettings();
expect(saveSettings.progress).toBe(true);
config.set('executions.saveExecutionProgress', false);
const _saveSettings = toSaveSettings();
expect(_saveSettings.progress).toBe(false);
});
});

View File

@@ -1,86 +0,0 @@
import { LicenseErrors, LicenseService } from '@/license/license.service';
import type { License } from '@/License';
import type { EventService } from '@/events/event.service';
import type { WorkflowRepository } from '@db/repositories/workflow.repository';
import type { TEntitlement } from '@n8n_io/license-sdk';
import { mock } from 'jest-mock-extended';
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
describe('LicenseService', () => {
const license = mock<License>();
const workflowRepository = mock<WorkflowRepository>();
const entitlement = mock<TEntitlement>({ productId: '123' });
const eventService = mock<EventService>();
const licenseService = new LicenseService(
mock(),
license,
workflowRepository,
mock(),
eventService,
);
license.getMainPlan.mockReturnValue(entitlement);
license.getTriggerLimit.mockReturnValue(400);
license.getPlanName.mockReturnValue('Test Plan');
workflowRepository.getActiveTriggerCount.mockResolvedValue(7);
beforeEach(() => jest.clearAllMocks());
class LicenseError extends Error {
constructor(readonly errorId: string) {
super(`License error: ${errorId}`);
}
}
describe('getLicenseData', () => {
it('should return usage and license data', async () => {
const data = await licenseService.getLicenseData();
expect(data).toEqual({
usage: {
executions: {
limit: 400,
value: 7,
warningThreshold: 0.8,
},
},
license: {
planId: '123',
planName: 'Test Plan',
},
});
});
});
describe('activateLicense', () => {
Object.entries(LicenseErrors).forEach(([errorId, message]) =>
it(`should handle ${errorId} error`, async () => {
license.activate.mockRejectedValueOnce(new LicenseError(errorId));
await expect(licenseService.activateLicense('')).rejects.toThrowError(
new BadRequestError(message),
);
}),
);
});
describe('renewLicense', () => {
test('on success', async () => {
license.renew.mockResolvedValueOnce();
await licenseService.renewLicense();
expect(eventService.emit).toHaveBeenCalledWith('license-renewal-attempted', {
success: true,
});
});
test('on failure', async () => {
license.renew.mockRejectedValueOnce(new LicenseError('RESERVATION_EXPIRED'));
await expect(licenseService.renewLicense()).rejects.toThrowError(
new BadRequestError('Activation key has expired'),
);
expect(eventService.emit).toHaveBeenCalledWith('license-renewal-attempted', {
success: false,
});
});
});
});

View File

@@ -1,178 +0,0 @@
import { parseRangeQuery } from '@/executions/parse-range-query.middleware';
import { mock } from 'jest-mock-extended';
import type { NextFunction } from 'express';
import type * as express from 'express';
import type { ExecutionRequest } from '@/executions/execution.types';
describe('`parseRangeQuery` middleware', () => {
const res = mock<express.Response>({
status: () => mock<express.Response>({ json: jest.fn() }),
});
const nextFn: NextFunction = jest.fn();
beforeEach(() => {
jest.restoreAllMocks();
});
describe('errors', () => {
test('should fail on invalid JSON', () => {
const statusSpy = jest.spyOn(res, 'status');
const req = mock<ExecutionRequest.GetMany>({
query: {
filter: '{ "status": ["waiting }',
limit: undefined,
firstId: undefined,
lastId: undefined,
},
});
parseRangeQuery(req, res, nextFn);
expect(nextFn).toBeCalledTimes(0);
expect(statusSpy).toBeCalledWith(400);
});
test('should fail on invalid schema', () => {
const statusSpy = jest.spyOn(res, 'status');
const req = mock<ExecutionRequest.GetMany>({
query: {
filter: '{ "status": 123 }',
limit: undefined,
firstId: undefined,
lastId: undefined,
},
});
parseRangeQuery(req, res, nextFn);
expect(nextFn).toBeCalledTimes(0);
expect(statusSpy).toBeCalledWith(400);
});
});
describe('filter', () => {
test('should parse status and mode fields', () => {
const req = mock<ExecutionRequest.GetMany>({
query: {
filter: '{ "status": ["waiting"], "mode": "manual" }',
limit: undefined,
firstId: undefined,
lastId: undefined,
},
});
parseRangeQuery(req, res, nextFn);
expect(req.rangeQuery.status).toEqual(['waiting']);
expect(req.rangeQuery.mode).toEqual('manual');
expect(nextFn).toBeCalledTimes(1);
});
test('should parse date-related fields', () => {
const req = mock<ExecutionRequest.GetMany>({
query: {
filter:
'{ "startedBefore": "2021-01-01", "startedAfter": "2020-01-01", "waitTill": "true" }',
limit: undefined,
firstId: undefined,
lastId: undefined,
},
});
parseRangeQuery(req, res, nextFn);
expect(req.rangeQuery.startedBefore).toBe('2021-01-01');
expect(req.rangeQuery.startedAfter).toBe('2020-01-01');
expect(req.rangeQuery.waitTill).toBe(true);
expect(nextFn).toBeCalledTimes(1);
});
test('should parse ID-related fields', () => {
const req = mock<ExecutionRequest.GetMany>({
query: {
filter: '{ "id": "123", "workflowId": "456" }',
limit: undefined,
firstId: undefined,
lastId: undefined,
},
});
parseRangeQuery(req, res, nextFn);
expect(req.rangeQuery.id).toBe('123');
expect(req.rangeQuery.workflowId).toBe('456');
expect(nextFn).toBeCalledTimes(1);
});
test('should delete invalid fields', () => {
const req = mock<ExecutionRequest.GetMany>({
query: {
filter: '{ "id": "123", "test": "789" }',
limit: undefined,
firstId: undefined,
lastId: undefined,
},
});
parseRangeQuery(req, res, nextFn);
expect(req.rangeQuery.id).toBe('123');
expect('test' in req.rangeQuery).toBe(false);
expect(nextFn).toBeCalledTimes(1);
});
});
describe('range', () => {
test('should parse first and last IDs', () => {
const req = mock<ExecutionRequest.GetMany>({
query: {
filter: undefined,
limit: undefined,
firstId: '111',
lastId: '999',
},
});
parseRangeQuery(req, res, nextFn);
expect(req.rangeQuery.range.firstId).toBe('111');
expect(req.rangeQuery.range.lastId).toBe('999');
expect(nextFn).toBeCalledTimes(1);
});
test('should parse limit', () => {
const req = mock<ExecutionRequest.GetMany>({
query: {
filter: undefined,
limit: '50',
firstId: undefined,
lastId: undefined,
},
});
parseRangeQuery(req, res, nextFn);
expect(req.rangeQuery.range.limit).toEqual(50);
expect(nextFn).toBeCalledTimes(1);
});
test('should default limit to 20 if absent', () => {
const req = mock<ExecutionRequest.GetMany>({
query: {
filter: undefined,
limit: undefined,
firstId: undefined,
lastId: undefined,
},
});
parseRangeQuery(req, res, nextFn);
expect(req.rangeQuery.range.limit).toEqual(20);
expect(nextFn).toBeCalledTimes(1);
});
});
});

View File

@@ -1,221 +0,0 @@
import { filterListQueryMiddleware } from '@/middlewares/listQuery/filter';
import { selectListQueryMiddleware } from '@/middlewares/listQuery/select';
import { paginationListQueryMiddleware } from '@/middlewares/listQuery/pagination';
import * as ResponseHelper from '@/ResponseHelper';
import type { ListQuery } from '@/requests';
import type { Response, NextFunction } from 'express';
describe('List query middleware', () => {
let mockReq: ListQuery.Request;
let mockRes: Response;
let nextFn: NextFunction = jest.fn();
let args: [ListQuery.Request, Response, NextFunction];
let sendErrorResponse: jest.SpyInstance;
beforeEach(() => {
jest.restoreAllMocks();
mockReq = { baseUrl: '/rest/workflows' } as ListQuery.Request;
mockRes = { status: () => ({ json: jest.fn() }) } as unknown as Response;
args = [mockReq, mockRes, nextFn];
sendErrorResponse = jest.spyOn(ResponseHelper, 'sendErrorResponse');
});
describe('Query filter', () => {
test('should not set filter on request if none sent', async () => {
mockReq.query = {};
await filterListQueryMiddleware(...args);
expect(mockReq.listQueryOptions).toBeUndefined();
expect(nextFn).toBeCalledTimes(1);
});
test('should parse valid filter', async () => {
mockReq.query = { filter: '{ "name": "My Workflow" }' };
await filterListQueryMiddleware(...args);
expect(mockReq.listQueryOptions).toEqual({ filter: { name: 'My Workflow' } });
expect(nextFn).toBeCalledTimes(1);
});
test('should ignore invalid filter', async () => {
mockReq.query = { filter: '{ "name": "My Workflow", "foo": "bar" }' };
await filterListQueryMiddleware(...args);
expect(mockReq.listQueryOptions).toEqual({ filter: { name: 'My Workflow' } });
expect(nextFn).toBeCalledTimes(1);
});
test('should throw on invalid JSON', async () => {
mockReq.query = { filter: '{ "name" : "My Workflow"' };
await filterListQueryMiddleware(...args);
expect(sendErrorResponse).toHaveBeenCalledTimes(1);
});
test('should throw on valid filter with invalid type', async () => {
mockReq.query = { filter: '{ "name" : 123 }' };
await filterListQueryMiddleware(...args);
expect(sendErrorResponse).toHaveBeenCalledTimes(1);
});
});
describe('Query select', () => {
test('should not set select on request if none sent', async () => {
mockReq.query = {};
await filterListQueryMiddleware(...args);
expect(mockReq.listQueryOptions).toBeUndefined();
expect(nextFn).toBeCalledTimes(1);
});
test('should parse valid select', () => {
mockReq.query = { select: '["name", "id"]' };
selectListQueryMiddleware(...args);
expect(mockReq.listQueryOptions).toEqual({ select: { name: true, id: true } });
expect(nextFn).toBeCalledTimes(1);
});
test('ignore invalid select', () => {
mockReq.query = { select: '["name", "foo"]' };
selectListQueryMiddleware(...args);
expect(mockReq.listQueryOptions).toEqual({ select: { name: true } });
expect(nextFn).toBeCalledTimes(1);
});
test('throw on invalid JSON', () => {
mockReq.query = { select: '["name"' };
selectListQueryMiddleware(...args);
expect(sendErrorResponse).toHaveBeenCalledTimes(1);
});
test('throw on non-string-array JSON for select', () => {
mockReq.query = { select: '"name"' };
selectListQueryMiddleware(...args);
expect(sendErrorResponse).toHaveBeenCalledTimes(1);
});
});
describe('Query pagination', () => {
test('should not set pagination options on request if none sent', async () => {
mockReq.query = {};
await filterListQueryMiddleware(...args);
expect(mockReq.listQueryOptions).toBeUndefined();
expect(nextFn).toBeCalledTimes(1);
});
test('should parse valid pagination', () => {
mockReq.query = { skip: '1', take: '2' };
paginationListQueryMiddleware(...args);
expect(mockReq.listQueryOptions).toEqual({ skip: 1, take: 2 });
expect(nextFn).toBeCalledTimes(1);
});
test('should throw on skip without take', () => {
mockReq.query = { skip: '1' };
paginationListQueryMiddleware(...args);
expect(mockReq.listQueryOptions).toBeUndefined();
expect(sendErrorResponse).toHaveBeenCalledTimes(1);
});
test('should default skip to 0', () => {
mockReq.query = { take: '2' };
paginationListQueryMiddleware(...args);
expect(mockReq.listQueryOptions).toEqual({ skip: 0, take: 2 });
expect(nextFn).toBeCalledTimes(1);
});
test('should cap take at 50', () => {
mockReq.query = { take: '51' };
paginationListQueryMiddleware(...args);
expect(mockReq.listQueryOptions).toEqual({ skip: 0, take: 50 });
expect(nextFn).toBeCalledTimes(1);
});
test('should throw on non-numeric-integer take', () => {
mockReq.query = { take: '3.2' };
paginationListQueryMiddleware(...args);
expect(sendErrorResponse).toHaveBeenCalledTimes(1);
});
test('should throw on non-numeric-integer skip', () => {
mockReq.query = { take: '3', skip: '3.2' };
paginationListQueryMiddleware(...args);
expect(sendErrorResponse).toHaveBeenCalledTimes(1);
});
});
describe('Combinations', () => {
test('should combine filter with select', async () => {
mockReq.query = { filter: '{ "name": "My Workflow" }', select: '["name", "id"]' };
await filterListQueryMiddleware(...args);
selectListQueryMiddleware(...args);
expect(mockReq.listQueryOptions).toEqual({
select: { name: true, id: true },
filter: { name: 'My Workflow' },
});
expect(nextFn).toBeCalledTimes(2);
});
test('should combine filter with pagination options', async () => {
mockReq.query = { filter: '{ "name": "My Workflow" }', skip: '1', take: '2' };
await filterListQueryMiddleware(...args);
paginationListQueryMiddleware(...args);
expect(mockReq.listQueryOptions).toEqual({
filter: { name: 'My Workflow' },
skip: 1,
take: 2,
});
expect(nextFn).toBeCalledTimes(2);
});
test('should combine select with pagination options', async () => {
mockReq.query = { select: '["name", "id"]', skip: '1', take: '2' };
selectListQueryMiddleware(...args);
paginationListQueryMiddleware(...args);
expect(mockReq.listQueryOptions).toEqual({
select: { name: true, id: true },
skip: 1,
take: 2,
});
expect(nextFn).toBeCalledTimes(2);
});
});
});

View File

@@ -1,44 +0,0 @@
import type { WebSocket } from 'ws';
import { mock } from 'jest-mock-extended';
import config from '@/config';
import type { User } from '@db/entities/User';
import { Push } from '@/push';
import { SSEPush } from '@/push/sse.push';
import { WebSocketPush } from '@/push/websocket.push';
import type { WebSocketPushRequest, SSEPushRequest } from '@/push/types';
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
import { mockInstance } from '../../shared/mocking';
jest.unmock('@/push');
describe('Push', () => {
const user = mock<User>();
const sseBackend = mockInstance(SSEPush);
const wsBackend = mockInstance(WebSocketPush);
test('should validate pushRef on requests for websocket backend', () => {
config.set('push.backend', 'websocket');
const push = new Push(mock());
const ws = mock<WebSocket>();
const request = mock<WebSocketPushRequest>({ user, ws });
request.query = { pushRef: '' };
push.handleRequest(request, mock());
expect(ws.send).toHaveBeenCalled();
expect(ws.close).toHaveBeenCalledWith(1008);
expect(wsBackend.add).not.toHaveBeenCalled();
});
test('should validate pushRef on requests for SSE backend', () => {
config.set('push.backend', 'sse');
const push = new Push(mock());
const request = mock<SSEPushRequest>({ user, ws: undefined });
request.query = { pushRef: '' };
expect(() => push.handleRequest(request, mock())).toThrow(BadRequestError);
expect(sseBackend.add).not.toHaveBeenCalled();
});
});

View File

@@ -1,116 +0,0 @@
import { Container } from 'typedi';
import { EventEmitter } from 'events';
import type WebSocket from 'ws';
import { WebSocketPush } from '@/push/websocket.push';
import { Logger } from '@/Logger';
import type { PushDataExecutionRecovered } from '@/Interfaces';
import { mockInstance } from '../../shared/mocking';
jest.useFakeTimers();
class MockWebSocket extends EventEmitter {
public isAlive = true;
public ping = jest.fn();
public send = jest.fn();
public terminate = jest.fn();
public close = jest.fn();
}
const createMockWebSocket = () => new MockWebSocket() as unknown as jest.Mocked<WebSocket>;
describe('WebSocketPush', () => {
const pushRef1 = 'test-session1';
const pushRef2 = 'test-session2';
mockInstance(Logger);
const webSocketPush = Container.get(WebSocketPush);
const mockWebSocket1 = createMockWebSocket();
const mockWebSocket2 = createMockWebSocket();
beforeEach(() => {
jest.resetAllMocks();
});
it('can add a connection', () => {
webSocketPush.add(pushRef1, mockWebSocket1);
expect(mockWebSocket1.listenerCount('close')).toBe(1);
expect(mockWebSocket1.listenerCount('pong')).toBe(1);
});
it('closes a connection', () => {
webSocketPush.add(pushRef1, mockWebSocket1);
mockWebSocket1.emit('close');
expect(mockWebSocket1.listenerCount('close')).toBe(0);
expect(mockWebSocket1.listenerCount('pong')).toBe(0);
});
it('sends data to one connection', () => {
webSocketPush.add(pushRef1, mockWebSocket1);
webSocketPush.add(pushRef2, mockWebSocket2);
const data: PushDataExecutionRecovered = {
type: 'executionRecovered',
data: {
executionId: 'test-execution-id',
},
};
webSocketPush.sendToOne('executionRecovered', data, pushRef1);
expect(mockWebSocket1.send).toHaveBeenCalledWith(
JSON.stringify({
type: 'executionRecovered',
data: {
type: 'executionRecovered',
data: {
executionId: 'test-execution-id',
},
},
}),
);
expect(mockWebSocket2.send).not.toHaveBeenCalled();
});
it('sends data to all connections', () => {
webSocketPush.add(pushRef1, mockWebSocket1);
webSocketPush.add(pushRef2, mockWebSocket2);
const data: PushDataExecutionRecovered = {
type: 'executionRecovered',
data: {
executionId: 'test-execution-id',
},
};
webSocketPush.sendToAll('executionRecovered', data);
const expectedMsg = JSON.stringify({
type: 'executionRecovered',
data: {
type: 'executionRecovered',
data: {
executionId: 'test-execution-id',
},
},
});
expect(mockWebSocket1.send).toHaveBeenCalledWith(expectedMsg);
expect(mockWebSocket2.send).toHaveBeenCalledWith(expectedMsg);
});
it('pings all connections', () => {
webSocketPush.add(pushRef1, mockWebSocket1);
webSocketPush.add(pushRef2, mockWebSocket2);
jest.runOnlyPendingTimers();
expect(mockWebSocket1.ping).toHaveBeenCalled();
expect(mockWebSocket2.ping).toHaveBeenCalled();
});
});

View File

@@ -1,70 +0,0 @@
import Container from 'typedi';
import { GlobalConfig } from '@n8n/config';
import type { SelectQueryBuilder } from '@n8n/typeorm';
import { Not, LessThanOrEqual } from '@n8n/typeorm';
import { BinaryDataService } from 'n8n-core';
import { nanoid } from 'nanoid';
import { mock } from 'jest-mock-extended';
import { ExecutionEntity } from '@db/entities/ExecutionEntity';
import { ExecutionRepository } from '@db/repositories/execution.repository';
import { mockEntityManager } from '../../shared/mocking';
import { mockInstance } from '../../shared/mocking';
describe('ExecutionRepository', () => {
const entityManager = mockEntityManager(ExecutionEntity);
const globalConfig = mockInstance(GlobalConfig);
const binaryDataService = mockInstance(BinaryDataService);
const executionRepository = Container.get(ExecutionRepository);
const mockDate = new Date('2023-12-28 12:34:56.789Z');
beforeAll(() => {
jest.clearAllMocks();
jest.useFakeTimers().setSystemTime(mockDate);
});
afterAll(() => jest.useRealTimers());
describe('getWaitingExecutions()', () => {
test.each(['sqlite', 'postgresdb'] as const)(
'on %s, should be called with expected args',
async (dbType) => {
globalConfig.database.type = dbType;
entityManager.find.mockResolvedValueOnce([]);
await executionRepository.getWaitingExecutions();
expect(entityManager.find).toHaveBeenCalledWith(ExecutionEntity, {
order: { waitTill: 'ASC' },
select: ['id', 'waitTill'],
where: {
status: Not('crashed'),
waitTill: LessThanOrEqual(
dbType === 'sqlite'
? '2023-12-28 12:36:06.789'
: new Date('2023-12-28T12:36:06.789Z'),
),
},
});
},
);
});
describe('deleteExecutionsByFilter', () => {
test('should delete binary data', async () => {
const workflowId = nanoid();
jest.spyOn(executionRepository, 'createQueryBuilder').mockReturnValue(
mock<SelectQueryBuilder<ExecutionEntity>>({
select: jest.fn().mockReturnThis(),
andWhere: jest.fn().mockReturnThis(),
getMany: jest.fn().mockResolvedValue([{ id: '1', workflowId }]),
}),
);
await executionRepository.deleteExecutionsByFilter({ id: '1' }, ['1'], { ids: ['1'] });
expect(binaryDataService.deleteMany).toHaveBeenCalledWith([{ executionId: '1', workflowId }]);
});
});
});

View File

@@ -1,105 +0,0 @@
import { Container } from 'typedi';
import { In } from '@n8n/typeorm';
import { mock } from 'jest-mock-extended';
import { hasScope } from '@n8n/permissions';
import type { User } from '@db/entities/User';
import type { CredentialsEntity } from '@db/entities/CredentialsEntity';
import { SharedCredentials } from '@db/entities/SharedCredentials';
import { SharedCredentialsRepository } from '@db/repositories/sharedCredentials.repository';
import { GLOBAL_MEMBER_SCOPES, GLOBAL_OWNER_SCOPES } from '@/permissions/global-roles';
import { mockEntityManager } from '../../shared/mocking';
describe('SharedCredentialsRepository', () => {
const entityManager = mockEntityManager(SharedCredentials);
const repository = Container.get(SharedCredentialsRepository);
describe('findCredentialForUser', () => {
const credentialsId = 'cred_123';
const sharedCredential = mock<SharedCredentials>();
sharedCredential.credentials = mock<CredentialsEntity>({ id: credentialsId });
const owner = mock<User>({
isOwner: true,
hasGlobalScope: (scope) =>
hasScope(scope, {
global: GLOBAL_OWNER_SCOPES,
}),
});
const member = mock<User>({
isOwner: false,
id: 'test',
hasGlobalScope: (scope) =>
hasScope(scope, {
global: GLOBAL_MEMBER_SCOPES,
}),
});
beforeEach(() => {
jest.resetAllMocks();
});
test('should allow instance owner access to all credentials', async () => {
entityManager.findOne.mockResolvedValueOnce(sharedCredential);
const credential = await repository.findCredentialForUser(credentialsId, owner, [
'credential:read',
]);
expect(entityManager.findOne).toHaveBeenCalledWith(SharedCredentials, {
relations: { credentials: { shared: { project: { projectRelations: { user: true } } } } },
where: { credentialsId },
});
expect(credential).toEqual(sharedCredential.credentials);
});
test('should allow members', async () => {
entityManager.findOne.mockResolvedValueOnce(sharedCredential);
const credential = await repository.findCredentialForUser(credentialsId, member, [
'credential:read',
]);
expect(entityManager.findOne).toHaveBeenCalledWith(SharedCredentials, {
relations: { credentials: { shared: { project: { projectRelations: { user: true } } } } },
where: {
credentialsId,
role: In(['credential:owner', 'credential:user']),
project: {
projectRelations: {
role: In([
'project:admin',
'project:personalOwner',
'project:editor',
'project:viewer',
]),
userId: member.id,
},
},
},
});
expect(credential).toEqual(sharedCredential.credentials);
});
test('should return null when no shared credential is found', async () => {
entityManager.findOne.mockResolvedValueOnce(null);
const credential = await repository.findCredentialForUser(credentialsId, member, [
'credential:read',
]);
expect(entityManager.findOne).toHaveBeenCalledWith(SharedCredentials, {
relations: { credentials: { shared: { project: { projectRelations: { user: true } } } } },
where: {
credentialsId,
role: In(['credential:owner', 'credential:user']),
project: {
projectRelations: {
role: In([
'project:admin',
'project:personalOwner',
'project:editor',
'project:viewer',
]),
userId: member.id,
},
},
},
});
expect(credential).toEqual(null);
});
});
});

View File

@@ -1,54 +0,0 @@
import { Container } from 'typedi';
import { type InsertResult, QueryFailedError } from '@n8n/typeorm';
import { mock, mockClear } from 'jest-mock-extended';
import { StatisticsNames, WorkflowStatistics } from '@db/entities/WorkflowStatistics';
import { WorkflowStatisticsRepository } from '@db/repositories/workflowStatistics.repository';
import { mockEntityManager } from '../../shared/mocking';
describe('insertWorkflowStatistics', () => {
const entityManager = mockEntityManager(WorkflowStatistics);
const workflowStatisticsRepository = Container.get(WorkflowStatisticsRepository);
beforeEach(() => {
mockClear(entityManager.insert);
});
it('Successfully inserts data when it is not yet present', async () => {
entityManager.findOne.mockResolvedValueOnce(null);
entityManager.insert.mockResolvedValueOnce(mock<InsertResult>());
const insertionResult = await workflowStatisticsRepository.insertWorkflowStatistics(
StatisticsNames.dataLoaded,
'workflowId',
);
expect(insertionResult).toBe('insert');
});
it('Does not insert when data is present', async () => {
entityManager.findOne.mockResolvedValueOnce(mock<WorkflowStatistics>());
const insertionResult = await workflowStatisticsRepository.insertWorkflowStatistics(
StatisticsNames.dataLoaded,
'workflowId',
);
expect(insertionResult).toBe('alreadyExists');
expect(entityManager.insert).not.toHaveBeenCalled();
});
it('throws an error when insertion fails', async () => {
entityManager.findOne.mockResolvedValueOnce(null);
entityManager.insert.mockImplementation(async () => {
throw new QueryFailedError('Query', [], new Error('driver error'));
});
const insertionResult = await workflowStatisticsRepository.insertWorkflowStatistics(
StatisticsNames.dataLoaded,
'workflowId',
);
expect(insertionResult).toBe('failed');
});
});

View File

@@ -1,86 +0,0 @@
import type { ActivationErrorsService } from '@/ActivationErrors.service';
import type { User } from '@db/entities/User';
import type { SharedWorkflowRepository } from '@db/repositories/sharedWorkflow.repository';
import type { WorkflowRepository } from '@db/repositories/workflow.repository';
import { ActiveWorkflowsService } from '@/services/activeWorkflows.service';
import { mock } from 'jest-mock-extended';
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
import { WorkflowEntity } from '@/databases/entities/WorkflowEntity';
describe('ActiveWorkflowsService', () => {
const user = mock<User>();
const workflowRepository = mock<WorkflowRepository>();
const sharedWorkflowRepository = mock<SharedWorkflowRepository>();
const activationErrorsService = mock<ActivationErrorsService>();
const service = new ActiveWorkflowsService(
mock(),
workflowRepository,
sharedWorkflowRepository,
activationErrorsService,
);
const activeIds = ['1', '2', '3', '4'];
beforeEach(() => jest.clearAllMocks());
describe('getAllActiveIdsInStorage', () => {
it('should filter out any workflow ids that have activation errors', async () => {
activationErrorsService.getAll.mockResolvedValue({ 1: 'some error' });
workflowRepository.getActiveIds.mockResolvedValue(activeIds);
const ids = await service.getAllActiveIdsInStorage();
expect(ids).toEqual(['2', '3', '4']);
});
});
describe('getAllActiveIdsFor', () => {
beforeEach(() => {
activationErrorsService.getAll.mockResolvedValue({ 1: 'some error' });
workflowRepository.getActiveIds.mockResolvedValue(activeIds);
});
it('should return all workflow ids when user has full access', async () => {
user.hasGlobalScope.mockReturnValue(true);
const ids = await service.getAllActiveIdsFor(user);
expect(ids).toEqual(['2', '3', '4']);
expect(user.hasGlobalScope).toHaveBeenCalledWith('workflow:list');
expect(sharedWorkflowRepository.getSharedWorkflowIds).not.toHaveBeenCalled();
});
it('should filter out workflow ids that the user does not have access to', async () => {
user.hasGlobalScope.mockReturnValue(false);
sharedWorkflowRepository.getSharedWorkflowIds.mockResolvedValue(['3']);
const ids = await service.getAllActiveIdsFor(user);
expect(ids).toEqual(['3']);
expect(user.hasGlobalScope).toHaveBeenCalledWith('workflow:list');
expect(sharedWorkflowRepository.getSharedWorkflowIds).toHaveBeenCalledWith(activeIds);
});
});
describe('getActivationError', () => {
const workflowId = 'workflowId';
it('should throw a BadRequestError a user does not have access to the workflow id', async () => {
sharedWorkflowRepository.findWorkflowForUser.mockResolvedValue(null);
await expect(service.getActivationError(workflowId, user)).rejects.toThrow(BadRequestError);
expect(sharedWorkflowRepository.findWorkflowForUser).toHaveBeenCalledWith(workflowId, user, [
'workflow:read',
]);
expect(activationErrorsService.get).not.toHaveBeenCalled();
});
it('should return the error when the user has access', async () => {
sharedWorkflowRepository.findWorkflowForUser.mockResolvedValue(new WorkflowEntity());
activationErrorsService.get.mockResolvedValue('some-error');
const error = await service.getActivationError(workflowId, user);
expect(error).toEqual('some-error');
expect(sharedWorkflowRepository.findWorkflowForUser).toHaveBeenCalledWith(workflowId, user, [
'workflow:read',
]);
expect(activationErrorsService.get).toHaveBeenCalledWith(workflowId);
});
});
});

View File

@@ -1,67 +0,0 @@
import Container from 'typedi';
import { mock } from 'jest-mock-extended';
import { CacheService } from '@/services/cache/cache.service';
const cacheService = Container.get(CacheService);
const store = mock<NonNullable<CacheService['cache']>['store']>({ isCacheable: () => true });
Object.assign(cacheService, { cache: { store } });
describe('CacheService (Mock)', () => {
beforeEach(() => jest.clearAllMocks());
describe('should prevent use of empty keys', () => {
test('get', async () => {
await cacheService.get('');
expect(store.get).not.toHaveBeenCalled();
await cacheService.get('key');
expect(store.get).toHaveBeenCalledWith('key');
});
test('getMany', async () => {
await cacheService.getMany([]);
expect(store.mget).not.toHaveBeenCalled();
await cacheService.getMany(['key1', 'key2']);
expect(store.mget).toHaveBeenCalledWith('key1', 'key2');
});
test('set', async () => {
await cacheService.set('', '');
expect(store.set).not.toHaveBeenCalled();
await cacheService.set('key', 'value');
expect(store.set).toHaveBeenCalledWith('key', 'value', undefined);
await cacheService.set('key', 'value', 123);
expect(store.set).toHaveBeenCalledWith('key', 'value', 123);
});
test('setMany', async () => {
await cacheService.setMany([]);
expect(store.mset).not.toHaveBeenCalled();
await cacheService.setMany([['key', 'value']]);
expect(store.mset).toHaveBeenCalledWith([['key', 'value']], undefined);
await cacheService.setMany([['key', 'value']], 123);
expect(store.mset).toHaveBeenCalledWith([['key', 'value']], 123);
});
test('delete', async () => {
await cacheService.delete('');
expect(store.del).not.toHaveBeenCalled();
await cacheService.delete('key');
expect(store.del).toHaveBeenCalledWith('key');
});
test('deleteMany', async () => {
await cacheService.deleteMany([]);
expect(store.mdel).not.toHaveBeenCalled();
await cacheService.deleteMany(['key1', 'key2']);
expect(store.mdel).toHaveBeenCalledWith('key1', 'key2');
});
});
});

View File

@@ -1,245 +0,0 @@
import { CacheService } from '@/services/cache/cache.service';
import config from '@/config';
import { sleep } from 'n8n-workflow';
import { GlobalConfig } from '@n8n/config';
import Container from 'typedi';
jest.mock('ioredis', () => {
const Redis = require('ioredis-mock');
return function (...args: unknown[]) {
return new Redis(args);
};
});
for (const backend of ['memory', 'redis'] as const) {
describe(backend, () => {
let cacheService: CacheService;
let globalConfig: GlobalConfig;
beforeAll(async () => {
globalConfig = Container.get(GlobalConfig);
globalConfig.cache.backend = backend;
cacheService = new CacheService(globalConfig);
await cacheService.init();
});
afterEach(async () => {
await cacheService.reset();
config.load(config.default);
});
describe('init', () => {
test('should select backend based on config', () => {
expect(cacheService.isMemory()).toBe(backend === 'memory');
expect(cacheService.isRedis()).toBe(backend === 'redis');
});
if (backend === 'redis') {
test('with auto backend and queue mode, should select redis', async () => {
config.set('executions.mode', 'queue');
await cacheService.init();
expect(cacheService.isRedis()).toBe(true);
});
}
if (backend === 'memory') {
test('should honor max size when enough', async () => {
globalConfig.cache.memory.maxSize = 16; // enough bytes for "withoutUnicode"
await cacheService.init();
await cacheService.set('key', 'withoutUnicode');
await expect(cacheService.get('key')).resolves.toBe('withoutUnicode');
// restore
globalConfig.cache.memory.maxSize = 3 * 1024 * 1024;
await cacheService.init();
});
test('should honor max size when not enough', async () => {
globalConfig.cache.memory.maxSize = 16; // not enough bytes for "withUnicodeԱԲԳ"
await cacheService.init();
await cacheService.set('key', 'withUnicodeԱԲԳ');
await expect(cacheService.get('key')).resolves.toBeUndefined();
// restore
globalConfig.cache.memory.maxSize = 3 * 1024 * 1024;
// restore
await cacheService.init();
});
}
});
describe('set', () => {
test('should set a string value', async () => {
await cacheService.set('key', 'value');
await expect(cacheService.get('key')).resolves.toBe('value');
});
test('should set a number value', async () => {
await cacheService.set('key', 123);
await expect(cacheService.get('key')).resolves.toBe(123);
});
test('should set an object value', async () => {
const object = { a: { b: { c: { d: 1 } } } };
await cacheService.set('key', object);
await expect(cacheService.get('key')).resolves.toMatchObject(object);
});
test('should not cache `null` or `undefined` values', async () => {
await cacheService.set('key1', null);
await cacheService.set('key2', undefined);
await cacheService.set('key3', 'value');
await expect(cacheService.get('key1')).resolves.toBeUndefined();
await expect(cacheService.get('key2')).resolves.toBeUndefined();
await expect(cacheService.get('key3')).resolves.toBe('value');
});
test('should disregard zero-length keys', async () => {
await cacheService.set('', 'value');
await expect(cacheService.get('')).resolves.toBeUndefined();
});
test('should honor ttl', async () => {
await cacheService.set('key', 'value', 100);
await expect(cacheService.get('key')).resolves.toBe('value');
await sleep(200);
await expect(cacheService.get('key')).resolves.toBeUndefined();
});
});
describe('get', () => {
test('should fall back to fallback value', async () => {
const promise = cacheService.get('key', { fallbackValue: 'fallback' });
await expect(promise).resolves.toBe('fallback');
});
test('should refresh value', async () => {
const promise = cacheService.get('testString', {
refreshFn: async () => 'refreshValue',
});
await expect(promise).resolves.toBe('refreshValue');
});
test('should handle non-ASCII key', async () => {
const nonAsciiKey = 'ԱԲԳ';
await cacheService.set(nonAsciiKey, 'value');
await expect(cacheService.get(nonAsciiKey)).resolves.toBe('value');
});
});
describe('delete', () => {
test('should delete a key', async () => {
await cacheService.set('key', 'value');
await cacheService.delete('key');
await expect(cacheService.get('key')).resolves.toBeUndefined();
});
});
describe('setMany', () => {
test('should set multiple string values', async () => {
await cacheService.setMany([
['key1', 'value1'],
['key2', 'value2'],
]);
const promise = cacheService.getMany(['key1', 'key2']);
await expect(promise).resolves.toStrictEqual(['value1', 'value2']);
});
test('should set multiple number values', async () => {
await cacheService.setMany([
['key1', 123],
['key2', 456],
]);
const promise = cacheService.getMany(['key1', 'key2']);
await expect(promise).resolves.toStrictEqual([123, 456]);
});
test('should disregard zero-length keys', async () => {
await cacheService.setMany([['', 'value1']]);
await expect(cacheService.get('')).resolves.toBeUndefined();
});
});
describe('getMany', () => {
test('should return undefined on missing result', async () => {
await cacheService.setMany([
['key1', 123],
['key2', 456],
]);
const promise = cacheService.getMany(['key2', 'key3']);
await expect(promise).resolves.toStrictEqual([456, undefined]);
});
});
describe('delete', () => {
test('should handle non-ASCII key', async () => {
const nonAsciiKey = 'ԱԲԳ';
await cacheService.set(nonAsciiKey, 'value');
await expect(cacheService.get(nonAsciiKey)).resolves.toBe('value');
await cacheService.delete(nonAsciiKey);
await expect(cacheService.get(nonAsciiKey)).resolves.toBeUndefined();
});
});
describe('setHash', () => {
test('should set a hash if non-existing', async () => {
await cacheService.setHash('keyW', { field: 'value' });
await expect(cacheService.getHash('keyW')).resolves.toStrictEqual({ field: 'value' });
});
test('should add to a hash value if existing', async () => {
await cacheService.setHash('key', { field1: 'value1' });
await cacheService.setHash('key', { field2: 'value2' });
await expect(cacheService.getHash('key')).resolves.toStrictEqual({
field1: 'value1',
field2: 'value2',
});
});
});
describe('deleteFromHash', () => {
test('should delete a hash field', async () => {
await cacheService.setHash('key', { field1: 'value1', field2: 'value2' });
await cacheService.deleteFromHash('key', 'field1');
await expect(cacheService.getHash('key')).resolves.toStrictEqual({ field2: 'value2' });
});
});
describe('getHashValue', () => {
test('should return a hash field value', async () => {
await cacheService.setHash('key', { field1: 'value1', field2: 'value2' });
await expect(cacheService.getHashValue('key', 'field1')).resolves.toBe('value1');
});
});
});
}

View File

@@ -1,429 +0,0 @@
import { exec } from 'child_process';
import { access as fsAccess, mkdir as fsMkdir } from 'fs/promises';
import axios from 'axios';
import { mocked } from 'jest-mock';
import Container from 'typedi';
import type { PublicInstalledPackage } from 'n8n-workflow';
import {
NODE_PACKAGE_PREFIX,
NPM_COMMAND_TOKENS,
NPM_PACKAGE_STATUS_GOOD,
RESPONSE_ERROR_MESSAGES,
} from '@/constants';
import config from '@/config';
import { InstalledPackages } from '@db/entities/InstalledPackages';
import type { CommunityPackages } from '@/Interfaces';
import { CommunityPackagesService } from '@/services/communityPackages.service';
import { InstalledNodesRepository } from '@db/repositories/installedNodes.repository';
import { InstalledPackagesRepository } from '@db/repositories/installedPackages.repository';
import { InstalledNodes } from '@db/entities/InstalledNodes';
import { LoadNodesAndCredentials } from '@/LoadNodesAndCredentials';
import { mockInstance } from '../../shared/mocking';
import {
COMMUNITY_NODE_VERSION,
COMMUNITY_PACKAGE_VERSION,
} from '../../integration/shared/constants';
import { randomName } from '../../integration/shared/random';
import { mockPackageName, mockPackagePair } from '../../integration/shared/utils';
import { InstanceSettings, PackageDirectoryLoader } from 'n8n-core';
import { Logger } from '@/Logger';
jest.mock('fs/promises');
jest.mock('child_process');
jest.mock('axios');
type ExecOptions = NonNullable<Parameters<typeof exec>[1]>;
type ExecCallback = NonNullable<Parameters<typeof exec>[2]>;
const execMock = ((...args) => {
const cb = args[args.length - 1] as ExecCallback;
cb(null, 'Done', '');
}) as typeof exec;
describe('CommunityPackagesService', () => {
const installedNodesRepository = mockInstance(InstalledNodesRepository);
installedNodesRepository.create.mockImplementation(() => {
const nodeName = randomName();
return Object.assign(new InstalledNodes(), {
name: nodeName,
type: nodeName,
latestVersion: COMMUNITY_NODE_VERSION.CURRENT.toString(),
packageName: 'test',
});
});
const installedPackageRepository = mockInstance(InstalledPackagesRepository);
installedPackageRepository.create.mockImplementation(() => {
return Object.assign(new InstalledPackages(), {
packageName: mockPackageName(),
installedVersion: COMMUNITY_PACKAGE_VERSION.CURRENT,
});
});
mockInstance(LoadNodesAndCredentials);
const communityPackagesService = Container.get(CommunityPackagesService);
beforeEach(() => {
config.load(config.default);
});
describe('parseNpmPackageName()', () => {
test('should fail with empty package name', () => {
expect(() => communityPackagesService.parseNpmPackageName('')).toThrowError();
});
test('should fail with invalid package prefix name', () => {
expect(() =>
communityPackagesService.parseNpmPackageName('INVALID_PREFIX@123'),
).toThrowError();
});
test('should parse valid package name', () => {
const name = mockPackageName();
const parsed = communityPackagesService.parseNpmPackageName(name);
expect(parsed.rawString).toBe(name);
expect(parsed.packageName).toBe(name);
expect(parsed.scope).toBeUndefined();
expect(parsed.version).toBeUndefined();
});
test('should parse valid package name and version', () => {
const name = mockPackageName();
const version = '0.1.1';
const fullPackageName = `${name}@${version}`;
const parsed = communityPackagesService.parseNpmPackageName(fullPackageName);
expect(parsed.rawString).toBe(fullPackageName);
expect(parsed.packageName).toBe(name);
expect(parsed.scope).toBeUndefined();
expect(parsed.version).toBe(version);
});
test('should parse valid package name, scope and version', () => {
const scope = '@n8n';
const name = mockPackageName();
const version = '0.1.1';
const fullPackageName = `${scope}/${name}@${version}`;
const parsed = communityPackagesService.parseNpmPackageName(fullPackageName);
expect(parsed.rawString).toBe(fullPackageName);
expect(parsed.packageName).toBe(`${scope}/${name}`);
expect(parsed.scope).toBe(scope);
expect(parsed.version).toBe(version);
});
});
describe('executeCommand()', () => {
beforeEach(() => {
mocked(fsAccess).mockReset();
mocked(fsMkdir).mockReset();
mocked(exec).mockReset();
});
test('should call command with valid options', async () => {
const execMock = ((...args) => {
const arg = args[1] as ExecOptions;
expect(arg.cwd).toBeDefined();
expect(arg.env).toBeDefined();
// PATH or NODE_PATH may be undefined depending on environment so we don't check for these keys.
const cb = args[args.length - 1] as ExecCallback;
cb(null, 'Done', '');
}) as typeof exec;
mocked(exec).mockImplementation(execMock);
await communityPackagesService.executeNpmCommand('ls');
expect(fsAccess).toHaveBeenCalled();
expect(exec).toHaveBeenCalled();
expect(fsMkdir).toBeCalledTimes(0);
});
test('should make sure folder exists', async () => {
mocked(exec).mockImplementation(execMock);
await communityPackagesService.executeNpmCommand('ls');
expect(fsAccess).toHaveBeenCalled();
expect(exec).toHaveBeenCalled();
expect(fsMkdir).toBeCalledTimes(0);
});
test('should try to create folder if it does not exist', async () => {
mocked(exec).mockImplementation(execMock);
mocked(fsAccess).mockImplementation(() => {
throw new Error('Folder does not exist.');
});
await communityPackagesService.executeNpmCommand('ls');
expect(fsAccess).toHaveBeenCalled();
expect(exec).toHaveBeenCalled();
expect(fsMkdir).toHaveBeenCalled();
});
test('should throw especial error when package is not found', async () => {
const erroringExecMock = ((...args) => {
const cb = args[args.length - 1] as ExecCallback;
const msg = `Something went wrong - ${NPM_COMMAND_TOKENS.NPM_PACKAGE_NOT_FOUND_ERROR}. Aborting.`;
cb(new Error(msg), '', '');
}) as typeof exec;
mocked(exec).mockImplementation(erroringExecMock);
const call = async () => await communityPackagesService.executeNpmCommand('ls');
await expect(call).rejects.toThrowError(RESPONSE_ERROR_MESSAGES.PACKAGE_NOT_FOUND);
expect(fsAccess).toHaveBeenCalled();
expect(exec).toHaveBeenCalled();
expect(fsMkdir).toHaveBeenCalledTimes(0);
});
});
describe('crossInformationPackage()', () => {
test('should return same list if availableUpdates is undefined', () => {
const fakePkgs = mockPackagePair();
const crossedPkgs = communityPackagesService.matchPackagesWithUpdates(fakePkgs);
expect(crossedPkgs).toEqual(fakePkgs);
});
test('should correctly match update versions for packages', () => {
const [pkgA, pkgB] = mockPackagePair();
const updates: CommunityPackages.AvailableUpdates = {
[pkgA.packageName]: {
current: pkgA.installedVersion,
wanted: pkgA.installedVersion,
latest: '0.2.0',
location: pkgA.packageName,
},
[pkgB.packageName]: {
current: pkgA.installedVersion,
wanted: pkgA.installedVersion,
latest: '0.3.0',
location: pkgA.packageName,
},
};
const [crossedPkgA, crossedPkgB]: PublicInstalledPackage[] =
communityPackagesService.matchPackagesWithUpdates([pkgA, pkgB], updates);
expect(crossedPkgA.updateAvailable).toBe('0.2.0');
expect(crossedPkgB.updateAvailable).toBe('0.3.0');
});
test('should correctly match update versions for single package', () => {
const [pkgA, pkgB] = mockPackagePair();
const updates: CommunityPackages.AvailableUpdates = {
[pkgB.packageName]: {
current: pkgA.installedVersion,
wanted: pkgA.installedVersion,
latest: '0.3.0',
location: pkgA.packageName,
},
};
const [crossedPkgA, crossedPkgB]: PublicInstalledPackage[] =
communityPackagesService.matchPackagesWithUpdates([pkgA, pkgB], updates);
expect(crossedPkgA.updateAvailable).toBeUndefined();
expect(crossedPkgB.updateAvailable).toBe('0.3.0');
});
});
describe('matchMissingPackages()', () => {
test('should not match failed packages that do not exist', () => {
const fakePkgs = mockPackagePair();
setMissingPackages([
`${NODE_PACKAGE_PREFIX}very-long-name-that-should-never-be-generated@1.0.0`,
`${NODE_PACKAGE_PREFIX}another-very-long-name-that-never-is-seen`,
]);
const matchedPackages = communityPackagesService.matchMissingPackages(fakePkgs);
expect(matchedPackages).toEqual(fakePkgs);
const [first, second] = matchedPackages;
expect(first.failedLoading).toBeUndefined();
expect(second.failedLoading).toBeUndefined();
});
test('should match failed packages that should be present', () => {
const [pkgA, pkgB] = mockPackagePair();
setMissingPackages([
`${NODE_PACKAGE_PREFIX}very-long-name-that-should-never-be-generated@1.0.0`,
`${pkgA.packageName}@${pkgA.installedVersion}`,
]);
const [matchedPkgA, matchedPkgB] = communityPackagesService.matchMissingPackages([
pkgA,
pkgB,
]);
expect(matchedPkgA.failedLoading).toBe(true);
expect(matchedPkgB.failedLoading).toBeUndefined();
});
test('should match failed packages even if version is wrong', () => {
const [pkgA, pkgB] = mockPackagePair();
setMissingPackages([
`${NODE_PACKAGE_PREFIX}very-long-name-that-should-never-be-generated@1.0.0`,
`${pkgA.packageName}@123.456.789`,
]);
const [matchedPkgA, matchedPkgB] = communityPackagesService.matchMissingPackages([
pkgA,
pkgB,
]);
expect(matchedPkgA.failedLoading).toBe(true);
expect(matchedPkgB.failedLoading).toBeUndefined();
});
});
describe('checkNpmPackageStatus()', () => {
test('should call axios.post', async () => {
await communityPackagesService.checkNpmPackageStatus(mockPackageName());
expect(axios.post).toHaveBeenCalled();
});
test('should not fail if request fails', async () => {
mocked(axios.post).mockImplementation(() => {
throw new Error('Something went wrong');
});
const result = await communityPackagesService.checkNpmPackageStatus(mockPackageName());
expect(result.status).toBe(NPM_PACKAGE_STATUS_GOOD);
});
test('should warn if package is banned', async () => {
mocked(axios.post).mockResolvedValue({ data: { status: 'Banned', reason: 'Not good' } });
const result = (await communityPackagesService.checkNpmPackageStatus(
mockPackageName(),
)) as CommunityPackages.PackageStatusCheck;
expect(result.status).toBe('Banned');
expect(result.reason).toBe('Not good');
});
});
describe('hasPackageLoadedSuccessfully()', () => {
test('should return true when failed package list does not exist', () => {
setMissingPackages([]);
expect(communityPackagesService.hasPackageLoaded('package')).toBe(true);
});
test('should return true when package is not in the list of missing packages', () => {
setMissingPackages(['packageA@0.1.0', 'packageB@0.1.0']);
expect(communityPackagesService.hasPackageLoaded('packageC')).toBe(true);
});
test('should return false when package is in the list of missing packages', () => {
setMissingPackages(['packageA@0.1.0', 'packageB@0.1.0']);
expect(communityPackagesService.hasPackageLoaded('packageA')).toBe(false);
});
});
describe('removePackageFromMissingList()', () => {
test('should do nothing if key does not exist', () => {
setMissingPackages([]);
communityPackagesService.removePackageFromMissingList('packageA');
expect(communityPackagesService.missingPackages).toBeEmptyArray();
});
test('should remove only correct package from list', () => {
setMissingPackages(['packageA@0.1.0', 'packageB@0.2.0', 'packageC@0.2.0']);
communityPackagesService.removePackageFromMissingList('packageB');
expect(communityPackagesService.missingPackages).toEqual([
'packageA@0.1.0',
'packageC@0.2.0',
]);
});
test('should not remove if package is not in the list', () => {
const failedToLoadList = ['packageA@0.1.0', 'packageB@0.2.0', 'packageB@0.2.0'];
setMissingPackages(failedToLoadList);
communityPackagesService.removePackageFromMissingList('packageC');
expect(communityPackagesService.missingPackages).toEqual(failedToLoadList);
});
});
const setMissingPackages = (missingPackages: string[]) => {
Object.assign(communityPackagesService, { missingPackages });
};
describe('updateNpmModule', () => {
let packageDirectoryLoader: PackageDirectoryLoader;
let communityPackagesService: CommunityPackagesService;
beforeEach(async () => {
jest.restoreAllMocks();
packageDirectoryLoader = mockInstance(PackageDirectoryLoader);
const loadNodesAndCredentials = mockInstance(LoadNodesAndCredentials);
loadNodesAndCredentials.loadPackage.mockResolvedValue(packageDirectoryLoader);
const instanceSettings = mockInstance(InstanceSettings);
const logger = mockInstance(Logger);
const installedPackagesRepository = mockInstance(InstalledPackagesRepository);
communityPackagesService = new CommunityPackagesService(
instanceSettings,
logger,
installedPackagesRepository,
loadNodesAndCredentials,
);
});
afterEach(async () => {
jest.restoreAllMocks();
});
test('should call `exec` with the correct command ', async () => {
//
// ARRANGE
//
const nodeName = randomName();
packageDirectoryLoader.loadedNodes = [{ name: nodeName, version: 1 }];
const installedPackage = new InstalledPackages();
installedPackage.packageName = mockPackageName();
mocked(exec).mockImplementation(execMock);
//
// ACT
//
await communityPackagesService.updateNpmModule(
installedPackage.packageName,
installedPackage,
);
//
// ASSERT
//
expect(exec).toHaveBeenCalledTimes(1);
expect(exec).toHaveBeenNthCalledWith(
1,
`npm install ${installedPackage.packageName}@latest`,
expect.any(Object),
expect.any(Function),
);
});
});
});

View File

@@ -1,297 +0,0 @@
import { CurlService } from '@/services/curl.service';
describe('CurlService', () => {
const service = new CurlService();
test('Should parse form-urlencoded content type correctly', () => {
const curl =
'curl -X POST https://reqbin.com/echo/post/form -H "Content-Type: application/x-www-form-urlencoded" -d "param1=value1&param2=value2"';
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo/post/form');
expect(parameters.sendBody).toBe(true);
expect(parameters.bodyParameters?.parameters[0].name).toBe('param1');
expect(parameters.bodyParameters?.parameters[0].value).toBe('value1');
expect(parameters.bodyParameters?.parameters[1].name).toBe('param2');
expect(parameters.bodyParameters?.parameters[1].value).toBe('value2');
expect(parameters.contentType).toBe('form-urlencoded');
expect(parameters.sendHeaders).toBe(false);
expect(parameters.sendQuery).toBe(false);
});
test('Should parse JSON content type correctly', () => {
const curl =
'curl -X POST https://reqbin.com/echo/post/json -H \'Content-Type: application/json\' -d \'{"login":"my_login","password":"my_password"}\'';
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo/post/json');
expect(parameters.sendBody).toBe(true);
expect(parameters.bodyParameters?.parameters[0].name).toBe('login');
expect(parameters.bodyParameters?.parameters[0].value).toBe('my_login');
expect(parameters.bodyParameters?.parameters[1].name).toBe('password');
expect(parameters.bodyParameters?.parameters[1].value).toBe('my_password');
expect(parameters.contentType).toBe('json');
expect(parameters.sendHeaders).toBe(false);
expect(parameters.sendQuery).toBe(false);
});
test('Should parse multipart-form-data content type correctly', () => {
const curl =
'curl -X POST https://reqbin.com/echo/post/json -v -F key1=value1 -F upload=@localfilename';
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo/post/json');
expect(parameters.sendBody).toBe(true);
expect(parameters.bodyParameters?.parameters[0].parameterType).toBe('formData');
expect(parameters.bodyParameters?.parameters[0].name).toBe('key1');
expect(parameters.bodyParameters?.parameters[0].value).toBe('value1');
expect(parameters.bodyParameters?.parameters[1].parameterType).toBe('formBinaryData');
expect(parameters.bodyParameters?.parameters[1].name).toBe('upload');
expect(parameters.contentType).toBe('multipart-form-data');
expect(parameters.sendHeaders).toBe(false);
expect(parameters.sendQuery).toBe(false);
});
test('Should parse binary request correctly', () => {
const curl =
"curl --location --request POST 'https://www.website.com' --header 'Content-Type: image/png' --data-binary '@/Users/image.png";
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://www.website.com');
expect(parameters.method).toBe('POST');
expect(parameters.sendBody).toBe(true);
expect(parameters.contentType).toBe('binaryData');
expect(parameters.sendHeaders).toBe(false);
expect(parameters.sendQuery).toBe(false);
});
test('Should parse unknown content type correctly', () => {
const curl = `curl -X POST https://reqbin.com/echo/post/xml
-H "Content-Type: application/xml"
-H "Accept: application/xml"
-d "<Request><Login>my_login</Login><Password>my_password</Password></Request>"`;
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo/post/xml');
expect(parameters.method).toBe('POST');
expect(parameters.sendBody).toBe(true);
expect(parameters.contentType).toBe('raw');
expect(parameters.rawContentType).toBe('application/xml');
expect(parameters.sendHeaders).toBe(true);
expect(parameters.headerParameters?.parameters[0].name).toBe('Accept');
expect(parameters.headerParameters?.parameters[0].value).toBe('application/xml');
expect(parameters.sendQuery).toBe(false);
});
test('Should parse header properties and keep the original case', () => {
const curl =
'curl -X POST https://reqbin.com/echo/post/json -v -F key1=value1 -F upload=@localfilename -H "ACCEPT: text/javascript" -H "content-type: multipart/form-data"';
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo/post/json');
expect(parameters.sendBody).toBe(true);
expect(parameters.bodyParameters?.parameters[0].parameterType).toBe('formData');
expect(parameters.bodyParameters?.parameters[0].name).toBe('key1');
expect(parameters.bodyParameters?.parameters[0].value).toBe('value1');
expect(parameters.bodyParameters?.parameters[1].parameterType).toBe('formBinaryData');
expect(parameters.bodyParameters?.parameters[1].name).toBe('upload');
expect(parameters.contentType).toBe('multipart-form-data');
expect(parameters.sendHeaders).toBe(true);
expect(parameters.headerParameters?.parameters[0].name).toBe('ACCEPT');
expect(parameters.headerParameters?.parameters[0].value).toBe('text/javascript');
expect(parameters.sendQuery).toBe(false);
});
test('Should parse querystring properties', () => {
const curl = "curl -G -d 'q=kitties' -d 'count=20' https://google.com/search";
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://google.com/search');
expect(parameters.sendBody).toBe(false);
expect(parameters.contentType).toBeUndefined();
expect(parameters.sendHeaders).toBe(false);
expect(parameters.sendQuery).toBe(true);
expect(parameters.queryParameters?.parameters[0].name).toBe('q');
expect(parameters.queryParameters?.parameters[0].value).toBe('kitties');
expect(parameters.queryParameters?.parameters[1].name).toBe('count');
expect(parameters.queryParameters?.parameters[1].value).toBe('20');
});
test('Should parse basic authentication property and keep the original case', () => {
const curl = 'curl https://reqbin.com/echo -u "login:password"';
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo');
expect(parameters.sendBody).toBe(false);
expect(parameters.contentType).toBeUndefined();
expect(parameters.sendQuery).toBe(false);
expect(parameters.sendHeaders).toBe(true);
expect(parameters.headerParameters?.parameters[0].name).toBe('authorization');
expect(parameters.headerParameters?.parameters[0].value).toBe(
`Basic ${Buffer.from('login:password').toString('base64')}`,
);
});
test('Should parse location flag with --location', () => {
const curl = 'curl https://reqbin.com/echo -u "login:password" --location';
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo');
expect(parameters.sendBody).toBe(false);
expect(parameters.contentType).toBeUndefined();
expect(parameters.sendQuery).toBe(false);
expect(parameters.sendHeaders).toBe(true);
expect(parameters.headerParameters?.parameters[0].name).toBe('authorization');
expect(parameters.headerParameters?.parameters[0].value).toBe(
`Basic ${Buffer.from('login:password').toString('base64')}`,
);
expect(parameters.options.redirect.redirect.followRedirects).toBe(true);
});
test('Should parse location flag with --L', () => {
const curl = 'curl https://reqbin.com/echo -u "login:password" -L';
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo');
expect(parameters.sendBody).toBe(false);
expect(parameters.contentType).toBeUndefined();
expect(parameters.sendQuery).toBe(false);
expect(parameters.sendHeaders).toBe(true);
expect(parameters.headerParameters?.parameters[0].name).toBe('authorization');
expect(parameters.headerParameters?.parameters[0].value).toBe(
`Basic ${Buffer.from('login:password').toString('base64')}`,
);
expect(parameters.options.redirect.redirect.followRedirects).toBe(true);
});
test('Should parse location and max redirects flags with --location and --max-redirs 10', () => {
const curl = 'curl https://reqbin.com/echo -u "login:password" --location --max-redirs 10';
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo');
expect(parameters.sendBody).toBe(false);
expect(parameters.contentType).toBeUndefined();
expect(parameters.sendQuery).toBe(false);
expect(parameters.sendHeaders).toBe(true);
expect(parameters.headerParameters?.parameters[0].name).toBe('authorization');
expect(parameters.headerParameters?.parameters[0].value).toBe(
`Basic ${Buffer.from('login:password').toString('base64')}`,
);
expect(parameters.options.redirect.redirect.followRedirects).toBe(true);
expect(parameters.options.redirect.redirect.maxRedirects).toBe('10');
});
test('Should parse proxy flag -x', () => {
const curl = 'curl https://reqbin.com/echo -u "login:password" -x https://google.com';
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo');
expect(parameters.sendBody).toBe(false);
expect(parameters.contentType).toBeUndefined();
expect(parameters.sendQuery).toBe(false);
expect(parameters.sendHeaders).toBe(true);
expect(parameters.headerParameters?.parameters[0].name).toBe('authorization');
expect(parameters.headerParameters?.parameters[0].value).toBe(
`Basic ${Buffer.from('login:password').toString('base64')}`,
);
expect(parameters.options.proxy).toBe('https://google.com');
});
test('Should parse proxy flag --proxy', () => {
const curl = 'curl https://reqbin.com/echo -u "login:password" -x https://google.com';
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo');
expect(parameters.sendBody).toBe(false);
expect(parameters.contentType).toBeUndefined();
expect(parameters.sendQuery).toBe(false);
expect(parameters.sendHeaders).toBe(true);
expect(parameters.headerParameters?.parameters[0].name).toBe('authorization');
expect(parameters.headerParameters?.parameters[0].value).toBe(
`Basic ${Buffer.from('login:password').toString('base64')}`,
);
expect(parameters.options.proxy).toBe('https://google.com');
});
test('Should parse include headers on output flag --include', () => {
const curl = 'curl https://reqbin.com/echo -u "login:password" --include -x https://google.com';
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo');
expect(parameters.sendBody).toBe(false);
expect(parameters.contentType).toBeUndefined();
expect(parameters.sendQuery).toBe(false);
expect(parameters.sendHeaders).toBe(true);
expect(parameters.headerParameters?.parameters[0].name).toBe('authorization');
expect(parameters.headerParameters?.parameters[0].value).toBe(
`Basic ${Buffer.from('login:password').toString('base64')}`,
);
expect(parameters.options.response.response.fullResponse).toBe(true);
});
test('Should parse include headers on output flag -i', () => {
const curl = 'curl https://reqbin.com/echo -u "login:password" -x https://google.com -i';
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo');
expect(parameters.sendBody).toBe(false);
expect(parameters.contentType).toBeUndefined();
expect(parameters.sendQuery).toBe(false);
expect(parameters.sendHeaders).toBe(true);
expect(parameters.headerParameters?.parameters[0].name).toBe('authorization');
expect(parameters.headerParameters?.parameters[0].value).toBe(
`Basic ${Buffer.from('login:password').toString('base64')}`,
);
expect(parameters.options.response.response.fullResponse).toBe(true);
});
test('Should parse include request flag -X', () => {
const curl = 'curl -X POST https://reqbin.com/echo -u "login:password" -x https://google.com';
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo');
expect(parameters.method).toBe('POST');
expect(parameters.sendBody).toBe(false);
});
test('Should parse include request flag --request', () => {
const curl =
'curl --request POST https://reqbin.com/echo -u "login:password" -x https://google.com';
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo');
expect(parameters.method).toBe('POST');
expect(parameters.sendBody).toBe(false);
});
test('Should parse include timeout flag --connect-timeout', () => {
const curl =
'curl --request POST https://reqbin.com/echo -u "login:password" --connect-timeout 20';
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo');
expect(parameters.method).toBe('POST');
expect(parameters.sendBody).toBe(false);
expect(parameters.options.timeout).toBe(20000);
});
test('Should parse download file flag -O', () => {
const curl = 'curl --request POST https://reqbin.com/echo -u "login:password" -O';
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo');
expect(parameters.method).toBe('POST');
expect(parameters.sendBody).toBe(false);
expect(parameters.options.response.response.responseFormat).toBe('file');
expect(parameters.options.response.response.outputPropertyName).toBe('data');
});
test('Should parse download file flag -o', () => {
const curl = 'curl --request POST https://reqbin.com/echo -u "login:password" -o';
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo');
expect(parameters.method).toBe('POST');
expect(parameters.sendBody).toBe(false);
expect(parameters.options.response.response.responseFormat).toBe('file');
expect(parameters.options.response.response.outputPropertyName).toBe('data');
});
test('Should parse ignore SSL flag -k', () => {
const curl = 'curl --request POST https://reqbin.com/echo -u "login:password" -k';
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo');
expect(parameters.method).toBe('POST');
expect(parameters.sendBody).toBe(false);
expect(parameters.options.allowUnauthorizedCerts).toBe(true);
});
test('Should parse ignore SSL flag --insecure', () => {
const curl = 'curl --request POST https://reqbin.com/echo -u "login:password" --insecure';
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo');
expect(parameters.method).toBe('POST');
expect(parameters.sendBody).toBe(false);
expect(parameters.options.allowUnauthorizedCerts).toBe(true);
});
});

View File

@@ -1,164 +0,0 @@
import type { Response } from 'express';
import { mock } from 'jest-mock-extended';
import type { AuthUser } from '@db/entities/AuthUser';
import type { CredentialsRepository } from '@db/repositories/credentials.repository';
import type { SettingsRepository } from '@db/repositories/settings.repository';
import type { UserRepository } from '@db/repositories/user.repository';
import type { WorkflowRepository } from '@db/repositories/workflow.repository';
import type { AuthService } from '@/auth/auth.service';
import type { UserService } from '@/services/user.service';
import { HooksService } from '@/services/hooks.service';
import type { Invitation } from '@/Interfaces';
import type { AuthenticatedRequest } from '@/requests';
import type { AuthUserRepository } from '@/databases/repositories/authUser.repository';
import RudderStack from '@rudderstack/rudder-sdk-node';
jest.mock('@rudderstack/rudder-sdk-node');
describe('HooksService', () => {
const mockedUser = mock<AuthUser>();
const userService = mock<UserService>();
const authService = mock<AuthService>();
const userRepository = mock<UserRepository>();
const settingsRepository = mock<SettingsRepository>();
const workflowRepository = mock<WorkflowRepository>();
const credentialsRepository = mock<CredentialsRepository>();
const authUserRepository = mock<AuthUserRepository>();
const hooksService = new HooksService(
userService,
authService,
userRepository,
settingsRepository,
workflowRepository,
credentialsRepository,
authUserRepository,
);
beforeEach(() => {
jest.clearAllMocks();
});
it('hooksService.inviteUsers should call userService.inviteUsers', async () => {
// ARRANGE
const usersToInvite: Invitation[] = [{ email: 'test@n8n.io', role: 'global:member' }];
// ACT
await hooksService.inviteUsers(mockedUser, usersToInvite);
// ASSERT
expect(userService.inviteUsers).toHaveBeenCalledWith(mockedUser, usersToInvite);
});
it('hooksService.issueCookie should call authService.issueCookie', async () => {
// ARRANGE
const res = mock<Response>();
// ACT
hooksService.issueCookie(res, mockedUser);
// ASSERT
expect(authService.issueCookie).toHaveBeenCalledWith(res, mockedUser);
});
it('hooksService.findOneUser should call authUserRepository.findOne', async () => {
// ARRANGE
const filter = { where: { id: '1' } };
// ACT
await hooksService.findOneUser(filter);
// ASSERT
expect(authUserRepository.findOne).toHaveBeenCalledWith(filter);
});
it('hooksService.saveUser should call userRepository.save', async () => {
// ACT
await hooksService.saveUser(mockedUser);
// ASSERT
expect(userRepository.save).toHaveBeenCalledWith(mockedUser);
});
it('hooksService.updateSettings should call settingRepository.update', async () => {
// ARRANGE
const filter = { key: 'test' };
const set = { value: 'true' };
// ACT
await hooksService.updateSettings(filter, set);
// ASSERT
expect(settingsRepository.update).toHaveBeenCalledWith(filter, set);
});
it('hooksService.workflowsCount should call workflowRepository.count', async () => {
// ARRANGE
const filter = { where: { active: true } };
// ACT
await hooksService.workflowsCount(filter);
// ASSERT
expect(workflowRepository.count).toHaveBeenCalledWith(filter);
});
it('hooksService.credentialsCount should call credentialRepository.count', async () => {
// ARRANGE
const filter = { where: {} };
// ACT
await hooksService.credentialsCount(filter);
// ASSERT
expect(credentialsRepository.count).toHaveBeenCalledWith(filter);
});
it('hooksService.settingsCount should call settingsRepository.count', async () => {
// ARRANGE
const filter = { where: { key: 'test' } };
// ACT
await hooksService.settingsCount(filter);
// ASSERT
expect(settingsRepository.count).toHaveBeenCalledWith(filter);
});
it('hooksService.authMiddleware should call authService.authMiddleware', async () => {
// ARRANGE
const res = mock<Response>();
const req = mock<AuthenticatedRequest>();
const next = jest.fn();
// ACT
await hooksService.authMiddleware(req, res, next);
// ASSERT
expect(authService.authMiddleware).toHaveBeenCalledWith(req, res, next);
});
it('hooksService.dbCollections should return valid repositories', async () => {
// ACT
const collections = hooksService.dbCollections();
// ASSERT
expect(collections).toHaveProperty('User');
expect(collections).toHaveProperty('Settings');
expect(collections).toHaveProperty('Credentials');
expect(collections).toHaveProperty('Workflow');
});
it('hooksService.getRudderStackClient', async () => {
// ACT
const key = 'TEST';
const opts = { dataPlaneUrl: 'test.com' };
const client = hooksService.getRudderStackClient(key, opts);
expect(client instanceof RudderStack).toBeTruthy();
expect(RudderStack).toHaveBeenCalledWith(key, opts);
});
});

View File

@@ -1,62 +0,0 @@
import jwt from 'jsonwebtoken';
import type { InstanceSettings } from 'n8n-core';
import { mock } from 'jest-mock-extended';
import config from '@/config';
import { JwtService } from '@/services/jwt.service';
describe('JwtService', () => {
const iat = 1699984313;
const jwtSecret = 'random-string';
const payload = { sub: 1 };
const signedToken =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOjEsImlhdCI6MTY5OTk4NDMxM30.xNZOAmcidW5ovEF_mwIOzCWkJ70FEO6MFNLK2QRDOeQ';
const instanceSettings = mock<InstanceSettings>({ encryptionKey: 'test-key' });
beforeEach(() => {
jest.clearAllMocks();
});
describe('secret initialization', () => {
it('should read the secret from config, when set', () => {
config.set('userManagement.jwtSecret', jwtSecret);
const jwtService = new JwtService(instanceSettings);
expect(jwtService.jwtSecret).toEqual(jwtSecret);
});
it('should derive the secret from encryption key when not set in config', () => {
config.set('userManagement.jwtSecret', '');
const jwtService = new JwtService(instanceSettings);
expect(jwtService.jwtSecret).toEqual(
'e9e2975005eddefbd31b2c04a0b0f2d9c37d9d718cf3676cddf76d65dec555cb',
);
});
});
describe('with a secret set', () => {
config.set('userManagement.jwtSecret', jwtSecret);
const jwtService = new JwtService(instanceSettings);
beforeAll(() => {
jest.useFakeTimers().setSystemTime(new Date(iat * 1000));
});
afterAll(() => jest.useRealTimers());
it('should sign', () => {
const token = jwtService.sign(payload);
expect(token).toEqual(signedToken);
});
it('should decode and verify payload', () => {
const decodedToken = jwtService.verify(signedToken);
expect(decodedToken.sub).toEqual(1);
expect(decodedToken.iat).toEqual(iat);
});
it('should throw an error on verify if the token is expired', () => {
const expiredToken = jwt.sign(payload, jwtSecret, { expiresIn: -10 });
expect(() => jwtService.verify(expiredToken)).toThrow(jwt.TokenExpiredError);
});
});
});

View File

@@ -1,87 +0,0 @@
import { WorkflowRepository } from '@/databases/repositories/workflow.repository';
import { CredentialsRepository } from '@/databases/repositories/credentials.repository';
import { mockInstance } from '../../shared/mocking';
import { NamingService } from '@/services/naming.service';
import type { WorkflowEntity } from '@/databases/entities/WorkflowEntity';
import type { CredentialsEntity } from '@/databases/entities/CredentialsEntity';
describe('NamingService', () => {
const workflowRepository = mockInstance(WorkflowRepository);
const credentialsRepository = mockInstance(CredentialsRepository);
const namingService = new NamingService(workflowRepository, credentialsRepository);
beforeEach(() => {
jest.restoreAllMocks();
});
describe('getUniqueWorkflowName()', () => {
test('should return requested name if already unique', async () => {
workflowRepository.findStartingWith.mockResolvedValue([]);
const name = await namingService.getUniqueWorkflowName('foo');
expect(name).toEqual('foo');
});
test('should return requested name suffixed if already existing once', async () => {
workflowRepository.findStartingWith.mockResolvedValue([{ name: 'foo' }] as WorkflowEntity[]);
const name = await namingService.getUniqueWorkflowName('foo');
expect(name).toEqual('foo 2');
});
test('should return requested name with incremented suffix if already suffixed', async () => {
const existingNames = [{ name: 'foo' }, { name: 'foo 2' }] as WorkflowEntity[];
workflowRepository.findStartingWith.mockResolvedValue(existingNames);
const name = await namingService.getUniqueWorkflowName('foo');
expect(name).toEqual('foo 3');
existingNames.push({ name: 'foo 3' } as WorkflowEntity);
const _name = await namingService.getUniqueWorkflowName('foo');
expect(_name).toEqual('foo 4');
});
});
describe('getUniqueCredentialName()', () => {
test('should return requested name if already unique', async () => {
credentialsRepository.findStartingWith.mockResolvedValue([]);
const name = await namingService.getUniqueCredentialName('foo');
expect(name).toEqual('foo');
});
test('should return requested name suffixed if already existing once', async () => {
credentialsRepository.findStartingWith.mockResolvedValue([
{ name: 'foo' },
] as CredentialsEntity[]);
const name = await namingService.getUniqueCredentialName('foo');
expect(name).toEqual('foo 2');
});
test('should return requested name with incremented suffix if already suffixed', async () => {
const existingNames = [{ name: 'foo' }, { name: 'foo 2' }] as CredentialsEntity[];
credentialsRepository.findStartingWith.mockResolvedValue(existingNames);
const name = await namingService.getUniqueCredentialName('foo');
expect(name).toEqual('foo 3');
existingNames.push({ name: 'foo 3' } as CredentialsEntity);
const _name = await namingService.getUniqueCredentialName('foo');
expect(_name).toEqual('foo 4');
});
});
});

View File

@@ -1,184 +0,0 @@
import Container from 'typedi';
import type Redis from 'ioredis';
import { mock } from 'jest-mock-extended';
import { InstanceSettings } from 'n8n-core';
import type { WorkflowActivateMode } from 'n8n-workflow';
import config from '@/config';
import { OrchestrationService } from '@/services/orchestration.service';
import type { RedisServiceWorkerResponseObject } from '@/services/redis/RedisServiceCommands';
import { MessageEventBus } from '@/eventbus/MessageEventBus/MessageEventBus';
import { RedisService } from '@/services/redis.service';
import { handleWorkerResponseMessageMain } from '@/services/orchestration/main/handleWorkerResponseMessageMain';
import { handleCommandMessageMain } from '@/services/orchestration/main/handleCommandMessageMain';
import { OrchestrationHandlerMainService } from '@/services/orchestration/main/orchestration.handler.main.service';
import * as helpers from '@/services/orchestration/helpers';
import { ExternalSecretsManager } from '@/ExternalSecrets/ExternalSecretsManager.ee';
import { Logger } from '@/Logger';
import { Push } from '@/push';
import { ActiveWorkflowManager } from '@/ActiveWorkflowManager';
import { mockInstance } from '../../shared/mocking';
import { RedisClientService } from '@/services/redis/redis-client.service';
const instanceSettings = Container.get(InstanceSettings);
const redisClientService = mockInstance(RedisClientService);
const mockRedisClient = mock<Redis>();
redisClientService.createClient.mockReturnValue(mockRedisClient);
const os = Container.get(OrchestrationService);
const handler = Container.get(OrchestrationHandlerMainService);
mockInstance(ActiveWorkflowManager);
let queueModeId: string;
function setDefaultConfig() {
config.set('executions.mode', 'queue');
config.set('generic.instanceType', 'main');
}
const workerRestartEventBusResponse: RedisServiceWorkerResponseObject = {
senderId: 'test',
workerId: 'test',
command: 'restartEventBus',
payload: {
result: 'success',
},
};
describe('Orchestration Service', () => {
const logger = mockInstance(Logger);
mockInstance(Push);
mockInstance(RedisService);
mockInstance(ExternalSecretsManager);
const eventBus = mockInstance(MessageEventBus);
beforeAll(async () => {
jest.mock('@/services/redis/RedisServicePubSubPublisher', () => {
return jest.fn().mockImplementation(() => {
return {
init: jest.fn(),
publishToEventLog: jest.fn(),
publishToWorkerChannel: jest.fn(),
destroy: jest.fn(),
};
});
});
jest.mock('@/services/redis/RedisServicePubSubSubscriber', () => {
return jest.fn().mockImplementation(() => {
return {
subscribeToCommandChannel: jest.fn(),
destroy: jest.fn(),
};
});
});
setDefaultConfig();
queueModeId = config.get('redis.queueModeId');
});
beforeEach(() => {
instanceSettings.markAsLeader();
});
afterAll(async () => {
jest.mock('@/services/redis/RedisServicePubSubPublisher').restoreAllMocks();
jest.mock('@/services/redis/RedisServicePubSubSubscriber').restoreAllMocks();
await os.shutdown();
});
test('should initialize', async () => {
await os.init();
await handler.init();
expect(os.redisPublisher).toBeDefined();
expect(handler.redisSubscriber).toBeDefined();
expect(queueModeId).toBeDefined();
});
test('should handle worker responses', async () => {
const response = await handleWorkerResponseMessageMain(
JSON.stringify(workerRestartEventBusResponse),
);
expect(response.command).toEqual('restartEventBus');
});
test('should handle command messages from others', async () => {
const responseFalseId = await handleCommandMessageMain(
JSON.stringify({
senderId: 'test',
command: 'reloadLicense',
}),
);
expect(responseFalseId).toBeDefined();
expect(responseFalseId!.command).toEqual('reloadLicense');
expect(responseFalseId!.senderId).toEqual('test');
expect(logger.error).toHaveBeenCalled();
});
test('should reject command messages from itself', async () => {
const response = await handleCommandMessageMain(
JSON.stringify({ ...workerRestartEventBusResponse, senderId: queueModeId }),
);
expect(response).toBeDefined();
expect(response!.command).toEqual('restartEventBus');
expect(response!.senderId).toEqual(queueModeId);
expect(eventBus.restart).not.toHaveBeenCalled();
});
test('should send command messages', async () => {
setDefaultConfig();
jest.spyOn(os.redisPublisher, 'publishToCommandChannel').mockImplementation(async () => {});
await os.getWorkerIds();
expect(os.redisPublisher.publishToCommandChannel).toHaveBeenCalled();
jest.spyOn(os.redisPublisher, 'publishToCommandChannel').mockRestore();
});
test('should prevent receiving commands too often', async () => {
setDefaultConfig();
jest.spyOn(helpers, 'debounceMessageReceiver');
const res1 = await handleCommandMessageMain(
JSON.stringify({
senderId: 'test',
command: 'reloadExternalSecretsProviders',
}),
);
const res2 = await handleCommandMessageMain(
JSON.stringify({
senderId: 'test',
command: 'reloadExternalSecretsProviders',
}),
);
expect(helpers.debounceMessageReceiver).toHaveBeenCalledTimes(2);
expect(res1!.payload).toBeUndefined();
expect(res2!.payload).toEqual({ result: 'debounced' });
});
describe('shouldAddWebhooks', () => {
test('should return true for init', () => {
// We want to ensure that webhooks are populated on init
// more https://github.com/n8n-io/n8n/pull/8830
const result = os.shouldAddWebhooks('init');
expect(result).toBe(true);
});
test('should return false for leadershipChange', () => {
const result = os.shouldAddWebhooks('leadershipChange');
expect(result).toBe(false);
});
test('should return true for update or activate when is leader', () => {
const modes = ['update', 'activate'] as WorkflowActivateMode[];
for (const mode of modes) {
const result = os.shouldAddWebhooks(mode);
expect(result).toBe(true);
}
});
test('should return false for update or activate when not leader', () => {
instanceSettings.markAsFollower();
const modes = ['update', 'activate'] as WorkflowActivateMode[];
for (const mode of modes) {
const result = os.shouldAddWebhooks(mode);
expect(result).toBe(false);
}
});
});
});

View File

@@ -1,195 +0,0 @@
import { OwnershipService } from '@/services/ownership.service';
import { SharedWorkflowRepository } from '@db/repositories/sharedWorkflow.repository';
import { SharedWorkflow } from '@db/entities/SharedWorkflow';
import { User } from '@db/entities/User';
import type { SharedCredentials } from '@db/entities/SharedCredentials';
import { mockInstance } from '../../shared/mocking';
import { WorkflowEntity } from '@/databases/entities/WorkflowEntity';
import { UserRepository } from '@/databases/repositories/user.repository';
import { mock } from 'jest-mock-extended';
import { Project } from '@/databases/entities/Project';
import { ProjectRelationRepository } from '@/databases/repositories/projectRelation.repository';
import { ProjectRelation } from '@/databases/entities/ProjectRelation';
import { mockCredential, mockProject } from '../shared/mockObjects';
describe('OwnershipService', () => {
const userRepository = mockInstance(UserRepository);
const sharedWorkflowRepository = mockInstance(SharedWorkflowRepository);
const projectRelationRepository = mockInstance(ProjectRelationRepository);
const ownershipService = new OwnershipService(
mock(),
userRepository,
mock(),
projectRelationRepository,
sharedWorkflowRepository,
);
beforeEach(() => {
jest.clearAllMocks();
});
describe('getWorkflowProjectCached()', () => {
test('should retrieve a workflow owner project', async () => {
const mockProject = new Project();
const sharedWorkflow = Object.assign(new SharedWorkflow(), {
role: 'workflow:owner',
project: mockProject,
});
sharedWorkflowRepository.findOneOrFail.mockResolvedValueOnce(sharedWorkflow);
const returnedProject = await ownershipService.getWorkflowProjectCached('some-workflow-id');
expect(returnedProject).toBe(mockProject);
});
test('should throw if no workflow owner project found', async () => {
sharedWorkflowRepository.findOneOrFail.mockRejectedValue(new Error());
await expect(ownershipService.getWorkflowProjectCached('some-workflow-id')).rejects.toThrow();
});
});
describe('getProjectOwnerCached()', () => {
test('should retrieve a project owner', async () => {
const mockProject = new Project();
const mockOwner = new User();
const projectRelation = Object.assign(new ProjectRelation(), {
role: 'project:personalOwner',
project: mockProject,
user: mockOwner,
});
projectRelationRepository.getPersonalProjectOwners.mockResolvedValueOnce([projectRelation]);
const returnedOwner = await ownershipService.getProjectOwnerCached('some-project-id');
expect(returnedOwner).toBe(mockOwner);
});
test('should not throw if no project owner found, should return null instead', async () => {
projectRelationRepository.getPersonalProjectOwners.mockResolvedValueOnce([]);
const owner = await ownershipService.getProjectOwnerCached('some-project-id');
expect(owner).toBeNull();
});
});
describe('getProjectOwnerCached()', () => {
test('should retrieve a project owner', async () => {
const mockProject = new Project();
const mockOwner = new User();
const projectRelation = Object.assign(new ProjectRelation(), {
role: 'project:personalOwner',
project: mockProject,
user: mockOwner,
});
projectRelationRepository.getPersonalProjectOwners.mockResolvedValueOnce([projectRelation]);
const returnedOwner = await ownershipService.getProjectOwnerCached('some-project-id');
expect(returnedOwner).toBe(mockOwner);
});
test('should not throw if no project owner found, should return null instead', async () => {
projectRelationRepository.getPersonalProjectOwners.mockResolvedValueOnce([]);
const owner = await ownershipService.getProjectOwnerCached('some-project-id');
expect(owner).toBeNull();
});
});
describe('addOwnedByAndSharedWith()', () => {
test('should add `ownedBy` and `sharedWith` to credential', async () => {
const ownerProject = mockProject();
const editorProject = mockProject();
const credential = mockCredential();
credential.shared = [
{ role: 'credential:owner', project: ownerProject },
{ role: 'credential:editor', project: editorProject },
] as SharedCredentials[];
const { homeProject, sharedWithProjects } =
ownershipService.addOwnedByAndSharedWith(credential);
expect(homeProject).toMatchObject({
id: ownerProject.id,
name: ownerProject.name,
type: ownerProject.type,
});
expect(sharedWithProjects).toMatchObject([
{
id: editorProject.id,
name: editorProject.name,
type: editorProject.type,
},
]);
});
test('should add `ownedBy` and `sharedWith` to workflow', async () => {
const projectOwner = mockProject();
const projectEditor = mockProject();
const workflow = new WorkflowEntity();
workflow.shared = [
{ role: 'workflow:owner', project: projectOwner },
{ role: 'workflow:editor', project: projectEditor },
] as SharedWorkflow[];
const { homeProject, sharedWithProjects } =
ownershipService.addOwnedByAndSharedWith(workflow);
expect(homeProject).toMatchObject({
id: projectOwner.id,
name: projectOwner.name,
type: projectOwner.type,
});
expect(sharedWithProjects).toMatchObject([
{
id: projectEditor.id,
name: projectEditor.name,
type: projectEditor.type,
},
]);
});
test('should produce an empty sharedWith if no sharee', async () => {
const credential = mockCredential();
const project = mockProject();
credential.shared = [{ role: 'credential:owner', project }] as SharedCredentials[];
const { homeProject, sharedWithProjects } =
ownershipService.addOwnedByAndSharedWith(credential);
expect(homeProject).toMatchObject({
id: project.id,
name: project.name,
type: project.type,
});
expect(sharedWithProjects).toHaveLength(0);
});
});
describe('getInstanceOwner()', () => {
test('should find owner using global owner role ID', async () => {
await ownershipService.getInstanceOwner();
expect(userRepository.findOneOrFail).toHaveBeenCalledWith({
where: { role: 'global:owner' },
});
});
});
});

View File

@@ -1,56 +0,0 @@
import Container from 'typedi';
import { Logger } from '@/Logger';
import config from '@/config';
import { RedisService } from '@/services/redis.service';
import { mockInstance } from '../../shared/mocking';
jest.mock('ioredis', () => {
const Redis = require('ioredis-mock');
if (typeof Redis === 'object') {
// the first mock is an ioredis shim because ioredis-mock depends on it
// https://github.com/stipsan/ioredis-mock/blob/master/src/index.js#L101-L111
return {
Command: { _transformer: { argument: {}, reply: {} } },
};
}
// second mock for our code
return function (...args: any) {
return new Redis(args);
};
});
mockInstance(Logger);
const redisService = Container.get(RedisService);
function setDefaultConfig() {
config.set('executions.mode', 'queue');
}
const PUBSUB_CHANNEL = 'testchannel';
describe('RedisService', () => {
beforeAll(async () => {
setDefaultConfig();
});
test('should create pubsub publisher and subscriber with handler', async () => {
const pub = await redisService.getPubSubPublisher();
const sub = await redisService.getPubSubSubscriber();
expect(pub).toBeDefined();
expect(sub).toBeDefined();
const mockHandler = jest.fn();
mockHandler.mockImplementation((_channel: string, _message: string) => {});
sub.addMessageHandler(PUBSUB_CHANNEL, mockHandler);
await sub.subscribe(PUBSUB_CHANNEL);
await pub.publish(PUBSUB_CHANNEL, 'test');
await new Promise((resolve) =>
setTimeout(async () => {
resolve(0);
}, 50),
);
expect(mockHandler).toHaveBeenCalled();
await sub.destroy();
await pub.destroy();
});
});

View File

@@ -1,98 +0,0 @@
import type { CacheService } from '@/services/cache/cache.service';
import type { OrchestrationService } from '@/services/orchestration.service';
import type { TestWebhookRegistration } from '@/services/test-webhook-registrations.service';
import { TestWebhookRegistrationsService } from '@/services/test-webhook-registrations.service';
import { mock } from 'jest-mock-extended';
describe('TestWebhookRegistrationsService', () => {
const cacheService = mock<CacheService>();
const registrations = new TestWebhookRegistrationsService(
cacheService,
mock<OrchestrationService>({ isMultiMainSetupEnabled: false }),
);
const registration = mock<TestWebhookRegistration>({
webhook: { httpMethod: 'GET', path: 'hello', webhookId: undefined },
});
const webhookKey = 'GET|hello';
const cacheKey = 'test-webhooks';
describe('register()', () => {
test('should register a test webhook registration', async () => {
await registrations.register(registration);
expect(cacheService.setHash).toHaveBeenCalledWith(cacheKey, { [webhookKey]: registration });
});
test('should skip setting TTL in single-main setup', async () => {
await registrations.register(registration);
expect(cacheService.expire).not.toHaveBeenCalled();
});
});
describe('deregister()', () => {
test('should deregister a test webhook registration', async () => {
await registrations.register(registration);
await registrations.deregister(webhookKey);
expect(cacheService.deleteFromHash).toHaveBeenCalledWith(cacheKey, webhookKey);
});
});
describe('get()', () => {
test('should retrieve a test webhook registration', async () => {
cacheService.getHashValue.mockResolvedValueOnce(registration);
const promise = registrations.get(webhookKey);
await expect(promise).resolves.toBe(registration);
});
test('should return undefined if no such test webhook registration was found', async () => {
cacheService.getHashValue.mockResolvedValueOnce(undefined);
const promise = registrations.get(webhookKey);
await expect(promise).resolves.toBeUndefined();
});
});
describe('getAllKeys()', () => {
test('should retrieve all test webhook registration keys', async () => {
cacheService.getHash.mockResolvedValueOnce({ [webhookKey]: registration });
const result = await registrations.getAllKeys();
expect(result).toEqual([webhookKey]);
});
});
describe('getAllRegistrations()', () => {
test('should retrieve all test webhook registrations', async () => {
cacheService.getHash.mockResolvedValueOnce({ [webhookKey]: registration });
const result = await registrations.getAllRegistrations();
expect(result).toEqual([registration]);
});
});
describe('deregisterAll()', () => {
test('should deregister all test webhook registrations', async () => {
await registrations.deregisterAll();
expect(cacheService.delete).toHaveBeenCalledWith(cacheKey);
});
});
describe('toKey()', () => {
test('should convert a test webhook registration to a key', () => {
const result = registrations.toKey(registration.webhook);
expect(result).toBe(webhookKey);
});
});
});

View File

@@ -1,102 +0,0 @@
import { mock } from 'jest-mock-extended';
import { v4 as uuid } from 'uuid';
import { User } from '@db/entities/User';
import { UserService } from '@/services/user.service';
import { UrlService } from '@/services/url.service';
import { mockInstance } from '../../shared/mocking';
import { UserRepository } from '@/databases/repositories/user.repository';
import { GlobalConfig } from '@n8n/config';
describe('UserService', () => {
const globalConfig = mockInstance(GlobalConfig, {
host: 'localhost',
path: '/',
port: 5678,
listen_address: '0.0.0.0',
protocol: 'http',
});
const urlService = new UrlService(globalConfig);
const userRepository = mockInstance(UserRepository);
const userService = new UserService(mock(), userRepository, mock(), urlService, mock());
const commonMockUser = Object.assign(new User(), {
id: uuid(),
password: 'passwordHash',
});
describe('toPublic', () => {
it('should remove sensitive properties', async () => {
const mockUser = Object.assign(new User(), {
id: uuid(),
password: 'passwordHash',
mfaEnabled: false,
mfaSecret: 'test',
mfaRecoveryCodes: ['test'],
updatedAt: new Date(),
authIdentities: [],
});
type MaybeSensitiveProperties = Partial<
Pick<User, 'password' | 'updatedAt' | 'authIdentities'>
>;
// to prevent typechecking from blocking assertions
const publicUser: MaybeSensitiveProperties = await userService.toPublic(mockUser);
expect(publicUser.password).toBeUndefined();
expect(publicUser.updatedAt).toBeUndefined();
expect(publicUser.authIdentities).toBeUndefined();
});
it('should add scopes if requested', async () => {
const scoped = await userService.toPublic(commonMockUser, { withScopes: true });
const unscoped = await userService.toPublic(commonMockUser);
expect(scoped.globalScopes).toEqual([]);
expect(unscoped.globalScopes).toBeUndefined();
});
it('should add invite URL if requested', async () => {
const firstUser = Object.assign(new User(), { id: uuid() });
const secondUser = Object.assign(new User(), { id: uuid(), isPending: true });
const withoutUrl = await userService.toPublic(secondUser);
const withUrl = await userService.toPublic(secondUser, {
withInviteUrl: true,
inviterId: firstUser.id,
});
expect(withoutUrl.inviteAcceptUrl).toBeUndefined();
const url = new URL(withUrl.inviteAcceptUrl ?? '');
expect(url.searchParams.get('inviterId')).toBe(firstUser.id);
expect(url.searchParams.get('inviteeId')).toBe(secondUser.id);
});
});
describe('update', () => {
// We need to use `save` so that that the subscriber in
// packages/cli/src/databases/entities/Project.ts receives the full user.
// With `update` it would only receive the updated fields, e.g. the `id`
// would be missing.
it('should use `save` instead of `update`', async () => {
const user = new User();
user.firstName = 'Not Nathan';
user.lastName = 'Nathaniel';
const userId = '1234';
const data = {
firstName: 'Nathan',
};
userRepository.findOneBy.mockResolvedValueOnce(user);
await userService.update(userId, data);
expect(userRepository.save).toHaveBeenCalledWith({ ...user, ...data }, { transaction: true });
expect(userRepository.update).not.toHaveBeenCalled();
});
});
});

View File

@@ -1,190 +0,0 @@
import { v4 as uuid } from 'uuid';
import config from '@/config';
import { WebhookRepository } from '@db/repositories/webhook.repository';
import { CacheService } from '@/services/cache/cache.service';
import { WebhookService } from '@/services/webhook.service';
import { WebhookEntity } from '@db/entities/WebhookEntity';
import { mockInstance } from '../../shared/mocking';
const createWebhook = (method: string, path: string, webhookId?: string, pathSegments?: number) =>
Object.assign(new WebhookEntity(), {
method,
webhookPath: path,
webhookId,
pathSegments,
}) as WebhookEntity;
describe('WebhookService', () => {
const webhookRepository = mockInstance(WebhookRepository);
const cacheService = mockInstance(CacheService);
const webhookService = new WebhookService(webhookRepository, cacheService);
beforeEach(() => {
config.load(config.default);
jest.clearAllMocks();
});
[true, false].forEach((isCacheEnabled) => {
const tag = '[' + ['cache', isCacheEnabled ? 'enabled' : 'disabled'].join(' ') + ']';
describe(`findWebhook() - static case ${tag}`, () => {
test('should return the webhook if found', async () => {
const method = 'GET';
const path = 'user/profile';
const mockWebhook = createWebhook(method, path);
webhookRepository.findOneBy.mockResolvedValue(mockWebhook);
const returnedWebhook = await webhookService.findWebhook(method, path);
expect(returnedWebhook).toBe(mockWebhook);
});
test('should return null if not found', async () => {
webhookRepository.findOneBy.mockResolvedValue(null); // static
webhookRepository.findBy.mockResolvedValue([]);
const returnValue = await webhookService.findWebhook('GET', 'user/profile');
expect(returnValue).toBeNull();
});
});
describe(`findWebhook() - dynamic case ${tag}`, () => {
test('should return the webhook if found', async () => {
const method = 'GET';
const webhookId = uuid();
const path = 'user/:id/posts';
const mockWebhook = createWebhook(method, path, webhookId, 3);
webhookRepository.findOneBy.mockResolvedValue(null); // static
webhookRepository.findBy.mockResolvedValue([mockWebhook]); // dynamic
const returnedWebhook = await webhookService.findWebhook(
method,
[webhookId, 'user/123/posts'].join('/'),
);
expect(returnedWebhook).toBe(mockWebhook);
});
test('should handle subset dynamic path case', async () => {
const method1 = 'GET';
const webhookId1 = uuid();
const path1 = 'user/:id/posts';
const mockWebhook1 = createWebhook(method1, path1, webhookId1, 3);
const method2 = 'GET';
const webhookId2 = uuid();
const path2 = 'user/:id/posts/:postId/comments';
const mockWebhook2 = createWebhook(method2, path2, webhookId2, 3);
webhookRepository.findOneBy.mockResolvedValue(null); // static
webhookRepository.findBy.mockResolvedValue([mockWebhook1, mockWebhook2]); // dynamic
const fullPath1 = [webhookId1, 'user/123/posts'].join('/');
const returnedWebhook1 = await webhookService.findWebhook(method1, fullPath1);
const fullPath2 = [webhookId1, 'user/123/posts/456/comments'].join('/');
const returnedWebhook2 = await webhookService.findWebhook(method2, fullPath2);
expect(returnedWebhook1).toBe(mockWebhook1);
expect(returnedWebhook2).toBe(mockWebhook2);
});
test('should handle single-segment dynamic path case', async () => {
const method1 = 'GET';
const webhookId1 = uuid();
const path1 = ':var';
const mockWebhook1 = createWebhook(method1, path1, webhookId1, 3);
const method2 = 'GET';
const webhookId2 = uuid();
const path2 = 'user/:id/posts/:postId/comments';
const mockWebhook2 = createWebhook(method2, path2, webhookId2, 3);
webhookRepository.findOneBy.mockResolvedValue(null); // static
webhookRepository.findBy.mockResolvedValue([mockWebhook1, mockWebhook2]); // dynamic
const fullPath = [webhookId1, 'user/123/posts/456'].join('/');
const returnedWebhook = await webhookService.findWebhook(method1, fullPath);
expect(returnedWebhook).toBe(mockWebhook1);
});
test('should return null if not found', async () => {
const fullPath = [uuid(), 'user/:id/posts'].join('/');
webhookRepository.findOneBy.mockResolvedValue(null); // static
webhookRepository.findBy.mockResolvedValue([]); // dynamic
const returnValue = await webhookService.findWebhook('GET', fullPath);
expect(returnValue).toBeNull();
});
});
});
describe('getWebhookMethods()', () => {
test('should return all methods for webhook', async () => {
const path = 'user/profile';
webhookRepository.find.mockResolvedValue([
createWebhook('GET', path),
createWebhook('POST', path),
createWebhook('PUT', path),
createWebhook('PATCH', path),
]);
const returnedMethods = await webhookService.getWebhookMethods(path);
expect(returnedMethods).toEqual(['GET', 'POST', 'PUT', 'PATCH']);
});
test('should return empty array if no webhooks found', async () => {
webhookRepository.find.mockResolvedValue([]);
const returnedMethods = await webhookService.getWebhookMethods('user/profile');
expect(returnedMethods).toEqual([]);
});
});
describe('deleteWorkflowWebhooks()', () => {
test('should delete all webhooks of the workflow', async () => {
const mockWorkflowWebhooks = [
createWebhook('PUT', 'users'),
createWebhook('GET', 'user/:id'),
createWebhook('POST', ':var'),
];
webhookRepository.findBy.mockResolvedValue(mockWorkflowWebhooks);
const workflowId = uuid();
await webhookService.deleteWorkflowWebhooks(workflowId);
expect(webhookRepository.remove).toHaveBeenCalledWith(mockWorkflowWebhooks);
});
test('should not delete any webhooks if none found', async () => {
webhookRepository.findBy.mockResolvedValue([]);
const workflowId = uuid();
await webhookService.deleteWorkflowWebhooks(workflowId);
expect(webhookRepository.remove).toHaveBeenCalledWith([]);
});
});
describe('createWebhook()', () => {
test('should create the webhook', async () => {
const mockWebhook = createWebhook('GET', 'user/:id');
await webhookService.storeWebhook(mockWebhook);
expect(webhookRepository.insert).toHaveBeenCalledWith(mockWebhook);
});
});
});

View File

@@ -1,223 +0,0 @@
import { Container } from 'typedi';
import { GlobalConfig } from '@n8n/config';
import type { IRun, WorkflowExecuteMode } from 'n8n-workflow';
import {
QueryFailedError,
type DataSource,
type EntityManager,
type EntityMetadata,
} from '@n8n/typeorm';
import { mocked } from 'jest-mock';
import { mock } from 'jest-mock-extended';
import config from '@/config';
import type { User } from '@db/entities/User';
import type { WorkflowStatistics } from '@db/entities/WorkflowStatistics';
import { WorkflowStatisticsRepository } from '@db/repositories/workflowStatistics.repository';
import { WorkflowStatisticsService } from '@/services/workflow-statistics.service';
import { UserService } from '@/services/user.service';
import { OwnershipService } from '@/services/ownership.service';
import { mockInstance } from '../../shared/mocking';
import type { Project } from '@/databases/entities/Project';
describe('WorkflowStatisticsService', () => {
const fakeUser = mock<User>({ id: 'abcde-fghij' });
const fakeProject = mock<Project>({ id: '12345-67890', type: 'personal' });
const ownershipService = mockInstance(OwnershipService);
const userService = mockInstance(UserService);
const globalConfig = Container.get(GlobalConfig);
const dbType = globalConfig.database.type;
const entityManager = mock<EntityManager>();
const dataSource = mock<DataSource>({
manager: entityManager,
getMetadata: () =>
mock<EntityMetadata>({
tableName: 'workflow_statistics',
}),
});
Object.assign(entityManager, { connection: dataSource });
config.set('diagnostics.enabled', true);
config.set('deployment.type', 'n8n-testing');
mocked(ownershipService.getWorkflowProjectCached).mockResolvedValue(fakeProject);
mocked(ownershipService.getProjectOwnerCached).mockResolvedValue(fakeUser);
const updateSettingsMock = jest.spyOn(userService, 'updateSettings').mockImplementation();
const workflowStatisticsService = new WorkflowStatisticsService(
mock(),
new WorkflowStatisticsRepository(dataSource, globalConfig),
ownershipService,
userService,
);
const onFirstProductionWorkflowSuccess = jest.fn();
const onFirstWorkflowDataLoad = jest.fn();
workflowStatisticsService.on(
'telemetry.onFirstProductionWorkflowSuccess',
onFirstProductionWorkflowSuccess,
);
workflowStatisticsService.on('telemetry.onFirstWorkflowDataLoad', onFirstWorkflowDataLoad);
beforeEach(() => {
jest.clearAllMocks();
});
const mockDBCall = (count = 1) => {
if (dbType === 'sqlite') {
entityManager.findOne.mockResolvedValueOnce(mock<WorkflowStatistics>({ count }));
} else {
const result = dbType === 'postgresdb' ? [{ count }] : { affectedRows: count };
entityManager.query.mockImplementationOnce(async (query) =>
query.startsWith('INSERT INTO') ? result : null,
);
}
};
describe('workflowExecutionCompleted', () => {
test('should create metrics for production successes', async () => {
// Call the function with a production success result, ensure metrics hook gets called
const workflow = {
id: '1',
name: '',
active: false,
createdAt: new Date(),
updatedAt: new Date(),
nodes: [],
connections: {},
};
const runData: IRun = {
finished: true,
status: 'success',
data: { resultData: { runData: {} } },
mode: 'internal' as WorkflowExecuteMode,
startedAt: new Date(),
};
mockDBCall();
await workflowStatisticsService.workflowExecutionCompleted(workflow, runData);
expect(updateSettingsMock).toHaveBeenCalledTimes(1);
expect(onFirstProductionWorkflowSuccess).toBeCalledTimes(1);
expect(onFirstProductionWorkflowSuccess).toHaveBeenNthCalledWith(1, {
project_id: fakeProject.id,
user_id: fakeUser.id,
workflow_id: workflow.id,
});
});
test('should only create metrics for production successes', async () => {
// Call the function with a non production success result, ensure metrics hook is never called
const workflow = {
id: '1',
name: '',
active: false,
createdAt: new Date(),
updatedAt: new Date(),
nodes: [],
connections: {},
};
const runData: IRun = {
finished: false,
status: 'error',
data: { resultData: { runData: {} } },
mode: 'internal' as WorkflowExecuteMode,
startedAt: new Date(),
};
await workflowStatisticsService.workflowExecutionCompleted(workflow, runData);
expect(onFirstProductionWorkflowSuccess).toBeCalledTimes(0);
});
test('should not send metrics for updated entries', async () => {
// Call the function with a fail insert, ensure update is called *and* metrics aren't sent
const workflow = {
id: '1',
name: '',
active: false,
createdAt: new Date(),
updatedAt: new Date(),
nodes: [],
connections: {},
};
const runData: IRun = {
finished: true,
status: 'success',
data: { resultData: { runData: {} } },
mode: 'internal' as WorkflowExecuteMode,
startedAt: new Date(),
};
mockDBCall(2);
await workflowStatisticsService.workflowExecutionCompleted(workflow, runData);
expect(onFirstProductionWorkflowSuccess).toBeCalledTimes(0);
});
});
describe('nodeFetchedData', () => {
test('should create metrics when the db is updated', async () => {
// Call the function with a production success result, ensure metrics hook gets called
const workflowId = '1';
const node = {
id: 'abcde',
name: 'test node',
typeVersion: 1,
type: '',
position: [0, 0] as [number, number],
parameters: {},
};
await workflowStatisticsService.nodeFetchedData(workflowId, node);
expect(onFirstWorkflowDataLoad).toBeCalledTimes(1);
expect(onFirstWorkflowDataLoad).toHaveBeenNthCalledWith(1, {
user_id: fakeUser.id,
project_id: fakeProject.id,
workflow_id: workflowId,
node_type: node.type,
node_id: node.id,
});
});
test('should create metrics with credentials when the db is updated', async () => {
// Call the function with a production success result, ensure metrics hook gets called
const workflowId = '1';
const node = {
id: 'abcde',
name: 'test node',
typeVersion: 1,
type: '',
position: [0, 0] as [number, number],
parameters: {},
credentials: {
testCredentials: {
id: '1',
name: 'Test Credentials',
},
},
};
await workflowStatisticsService.nodeFetchedData(workflowId, node);
expect(onFirstWorkflowDataLoad).toBeCalledTimes(1);
expect(onFirstWorkflowDataLoad).toHaveBeenNthCalledWith(1, {
user_id: fakeUser.id,
project_id: fakeProject.id,
workflow_id: workflowId,
node_type: node.type,
node_id: node.id,
credential_type: 'testCredentials',
credential_id: node.credentials.testCredentials.id,
});
});
test('should not send metrics for entries that already have the flag set', async () => {
// Fetch data for workflow 2 which is set up to not be altered in the mocks
entityManager.insert.mockRejectedValueOnce(new QueryFailedError('', undefined, new Error()));
const workflowId = '1';
const node = {
id: 'abcde',
name: 'test node',
typeVersion: 1,
type: '',
position: [0, 0] as [number, number],
parameters: {},
};
await workflowStatisticsService.nodeFetchedData(workflowId, node);
expect(onFirstWorkflowDataLoad).toBeCalledTimes(0);
});
});
});

View File

@@ -1,114 +0,0 @@
import { mockClear } from 'jest-mock-extended';
import { User } from '@db/entities/User';
import { WorkflowHistoryRepository } from '@db/repositories/workflowHistory.repository';
import { SharedWorkflowRepository } from '@db/repositories/sharedWorkflow.repository';
import { WorkflowHistoryService } from '@/workflows/workflowHistory/workflowHistory.service.ee';
import { Logger } from '@/Logger';
import { mockInstance } from '../../shared/mocking';
import { getWorkflow } from '../../integration/shared/workflow';
const workflowHistoryRepository = mockInstance(WorkflowHistoryRepository);
const logger = mockInstance(Logger);
const sharedWorkflowRepository = mockInstance(SharedWorkflowRepository);
const workflowHistoryService = new WorkflowHistoryService(
logger,
workflowHistoryRepository,
sharedWorkflowRepository,
);
const testUser = Object.assign(new User(), {
id: '1234',
password: 'passwordHash',
mfaEnabled: false,
firstName: 'John',
lastName: 'Doe',
});
let isWorkflowHistoryEnabled = true;
jest.mock('@/workflows/workflowHistory/workflowHistoryHelper.ee', () => {
return {
isWorkflowHistoryEnabled: jest.fn(() => isWorkflowHistoryEnabled),
};
});
describe('WorkflowHistoryService', () => {
beforeEach(() => {
mockClear(workflowHistoryRepository.insert);
});
describe('saveVersion', () => {
it('should save a new version when workflow history is enabled and nodes and connections are present', async () => {
// Arrange
isWorkflowHistoryEnabled = true;
const workflow = getWorkflow({ addNodeWithoutCreds: true });
const workflowId = '123';
workflow.connections = {};
workflow.id = workflowId;
workflow.versionId = '456';
// Act
await workflowHistoryService.saveVersion(testUser, workflow, workflowId);
// Assert
expect(workflowHistoryRepository.insert).toHaveBeenCalledWith({
authors: 'John Doe',
connections: {},
nodes: workflow.nodes,
versionId: workflow.versionId,
workflowId,
});
});
it('should not save a new version when workflow history is disabled', async () => {
// Arrange
isWorkflowHistoryEnabled = false;
const workflow = getWorkflow({ addNodeWithoutCreds: true });
const workflowId = '123';
workflow.connections = {};
workflow.id = workflowId;
workflow.versionId = '456';
// Act
await workflowHistoryService.saveVersion(testUser, workflow, workflowId);
// Assert
expect(workflowHistoryRepository.insert).not.toHaveBeenCalled();
});
it('should not save a new version when nodes or connections are missing', async () => {
// Arrange
isWorkflowHistoryEnabled = true;
const workflow = getWorkflow({ addNodeWithoutCreds: true });
const workflowId = '123';
workflow.id = workflowId;
workflow.versionId = '456';
// Nodes are set but connections is empty
// Act
await workflowHistoryService.saveVersion(testUser, workflow, workflowId);
// Assert
expect(workflowHistoryRepository.insert).not.toHaveBeenCalled();
});
it('should log an error when failed to save workflow history version', async () => {
// Arrange
isWorkflowHistoryEnabled = true;
const workflow = getWorkflow({ addNodeWithoutCreds: true });
const workflowId = '123';
workflow.connections = {};
workflow.id = workflowId;
workflow.versionId = '456';
workflowHistoryRepository.insert.mockRejectedValueOnce(new Error('Test error'));
// Act
await workflowHistoryService.saveVersion(testUser, workflow, workflowId);
// Assert
expect(workflowHistoryRepository.insert).toHaveBeenCalled();
expect(logger.error).toHaveBeenCalledWith(
'Failed to save workflow history version for workflow 123',
expect.any(Error),
);
});
});
});

View File

@@ -1,159 +0,0 @@
import { ApplicationError, ErrorReporterProxy } from 'n8n-workflow';
import { mock } from 'jest-mock-extended';
import type { ServiceClass } from '@/shutdown/Shutdown.service';
import { ShutdownService } from '@/shutdown/Shutdown.service';
import Container from 'typedi';
class MockComponent {
onShutdown() {}
}
describe('ShutdownService', () => {
let shutdownService: ShutdownService;
let mockComponent: MockComponent;
let onShutdownSpy: jest.SpyInstance;
let mockErrorReporterProxy: jest.SpyInstance;
beforeEach(() => {
shutdownService = new ShutdownService(mock());
mockComponent = new MockComponent();
Container.set(MockComponent, mockComponent);
onShutdownSpy = jest.spyOn(mockComponent, 'onShutdown');
mockErrorReporterProxy = jest.spyOn(ErrorReporterProxy, 'error').mockImplementation(() => {});
});
describe('shutdown', () => {
it('should signal shutdown', () => {
shutdownService.register(10, {
serviceClass: MockComponent as unknown as ServiceClass,
methodName: 'onShutdown',
});
shutdownService.shutdown();
expect(onShutdownSpy).toBeCalledTimes(1);
});
it('should signal shutdown in the priority order', async () => {
class MockService {
onShutdownHighPrio() {}
onShutdownLowPrio() {}
}
const order: string[] = [];
const mockService = new MockService();
Container.set(MockService, mockService);
jest.spyOn(mockService, 'onShutdownHighPrio').mockImplementation(() => order.push('high'));
jest.spyOn(mockService, 'onShutdownLowPrio').mockImplementation(() => order.push('low'));
shutdownService.register(100, {
serviceClass: MockService as unknown as ServiceClass,
methodName: 'onShutdownHighPrio',
});
shutdownService.register(10, {
serviceClass: MockService as unknown as ServiceClass,
methodName: 'onShutdownLowPrio',
});
shutdownService.shutdown();
await shutdownService.waitForShutdown();
expect(order).toEqual(['high', 'low']);
});
it('should throw error if shutdown is already in progress', () => {
shutdownService.register(10, {
methodName: 'onShutdown',
serviceClass: MockComponent as unknown as ServiceClass,
});
shutdownService.shutdown();
expect(() => shutdownService.shutdown()).toThrow('App is already shutting down');
});
it('should report error if component shutdown fails', async () => {
const componentError = new Error('Something went wrong');
onShutdownSpy.mockImplementation(() => {
throw componentError;
});
shutdownService.register(10, {
serviceClass: MockComponent as unknown as ServiceClass,
methodName: 'onShutdown',
});
shutdownService.shutdown();
await shutdownService.waitForShutdown();
expect(mockErrorReporterProxy).toHaveBeenCalledTimes(1);
const error = mockErrorReporterProxy.mock.calls[0][0];
expect(error).toBeInstanceOf(ApplicationError);
expect(error.message).toBe('Failed to shutdown gracefully');
expect(error.extra).toEqual({
component: 'MockComponent.onShutdown()',
});
expect(error.cause).toBe(componentError);
});
});
describe('waitForShutdown', () => {
it('should wait for shutdown', async () => {
shutdownService.register(10, {
serviceClass: MockComponent as unknown as ServiceClass,
methodName: 'onShutdown',
});
shutdownService.shutdown();
await expect(shutdownService.waitForShutdown()).resolves.toBeUndefined();
});
it('should throw error if app is not shutting down', async () => {
await expect(async () => await shutdownService.waitForShutdown()).rejects.toThrow(
'App is not shutting down',
);
});
});
describe('isShuttingDown', () => {
it('should return true if app is shutting down', () => {
shutdownService.register(10, {
serviceClass: MockComponent as unknown as ServiceClass,
methodName: 'onShutdown',
});
shutdownService.shutdown();
expect(shutdownService.isShuttingDown()).toBe(true);
});
it('should return false if app is not shutting down', () => {
expect(shutdownService.isShuttingDown()).toBe(false);
});
});
describe('validate', () => {
it('should throw error if component is not registered with the DI container', () => {
class UnregisteredComponent {
onShutdown() {}
}
shutdownService.register(10, {
serviceClass: UnregisteredComponent as unknown as ServiceClass,
methodName: 'onShutdown',
});
expect(() => shutdownService.validate()).toThrow(
'Component "UnregisteredComponent" is not registered with the DI container. Any component using @OnShutdown() must be decorated with @Service()',
);
});
it('should throw error if component is missing the shutdown method', () => {
class TestComponent {}
shutdownService.register(10, {
serviceClass: TestComponent as unknown as ServiceClass,
methodName: 'onShutdown',
});
Container.set(TestComponent, new TestComponent());
expect(() => shutdownService.validate()).toThrow(
'Component "TestComponent" does not have a "onShutdown" method',
);
});
});
});

View File

@@ -1,53 +0,0 @@
import { mock } from 'jest-mock-extended';
import type express from 'express';
import { SamlService } from '@/sso/saml/saml.service.ee';
import { mockInstance } from '../../../shared/mocking';
import { UrlService } from '@/services/url.service';
import { Logger } from '@/Logger';
import type { IdentityProviderInstance, ServiceProviderInstance } from 'samlify';
import * as samlHelpers from '@/sso/saml/samlHelpers';
describe('SamlService', () => {
const logger = mockInstance(Logger);
const urlService = mockInstance(UrlService);
const samlService = new SamlService(logger, urlService);
describe('getAttributesFromLoginResponse', () => {
test('throws when any attribute is missing', async () => {
//
// ARRANGE
//
jest
.spyOn(samlService, 'getIdentityProviderInstance')
.mockReturnValue(mock<IdentityProviderInstance>());
const serviceProviderInstance = mock<ServiceProviderInstance>();
serviceProviderInstance.parseLoginResponse.mockResolvedValue({
samlContent: '',
extract: {},
});
jest
.spyOn(samlService, 'getServiceProviderInstance')
.mockReturnValue(serviceProviderInstance);
jest.spyOn(samlHelpers, 'getMappedSamlAttributesFromFlowResult').mockReturnValue({
attributes: {} as never,
missingAttributes: [
'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress',
'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/firstname',
'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/lastname',
'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn',
],
});
//
// ACT & ASSERT
//
await expect(
samlService.getAttributesFromLoginResponse(mock<express.Request>(), 'post'),
).rejects.toThrowError(
'SAML Authentication failed. Invalid SAML response (missing attributes: http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress, http://schemas.xmlsoap.org/ws/2005/05/identity/claims/firstname, http://schemas.xmlsoap.org/ws/2005/05/identity/claims/lastname, http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn).',
);
});
});
});

View File

@@ -1,55 +0,0 @@
import { User } from '@/databases/entities/User';
import { generateNanoId } from '@/databases/utils/generators';
import * as helpers from '@/sso/saml/samlHelpers';
import type { SamlUserAttributes } from '@/sso/saml/types/samlUserAttributes';
import { mockInstance } from '../../../shared/mocking';
import { UserRepository } from '@/databases/repositories/user.repository';
import type { AuthIdentity } from '@/databases/entities/AuthIdentity';
import { AuthIdentityRepository } from '@/databases/repositories/authIdentity.repository';
const userRepository = mockInstance(UserRepository);
mockInstance(AuthIdentityRepository);
describe('sso/saml/samlHelpers', () => {
describe('updateUserFromSamlAttributes', () => {
// We need to use `save` so that that the subscriber in
// packages/cli/src/databases/entities/Project.ts receives the full user.
// With `update` it would only receive the updated fields, e.g. the `id`
// would be missing.
test('does not user `Repository.update`, but `Repository.save` instead', async () => {
//
// ARRANGE
//
const user = Object.assign(new User(), {
id: generateNanoId(),
authIdentities: [] as AuthIdentity[],
} as User);
const samlUserAttributes: SamlUserAttributes = {
firstName: 'Nathan',
lastName: 'Nathaniel',
email: 'n@8.n',
userPrincipalName: 'Huh?',
};
userRepository.save.mockImplementationOnce(async (user) => user as User);
//
// ACT
//
await helpers.updateUserFromSamlAttributes(user, samlUserAttributes);
//
// ASSERT
//
expect(userRepository.save).toHaveBeenCalledWith(
{
...user,
firstName: samlUserAttributes.firstName,
lastName: samlUserAttributes.lastName,
},
{ transaction: false },
);
expect(userRepository.update).not.toHaveBeenCalled();
});
});
});

View File

@@ -1,104 +0,0 @@
import { PasswordUtility } from '@/services/password.utility';
import Container from 'typedi';
function toComponents(hash: string) {
const BCRYPT_HASH_REGEX =
/^\$(?<version>.{2})\$(?<costFactor>\d{2})\$(?<salt>.{22})(?<hashedPassword>.{31})$/;
const match = hash.match(BCRYPT_HASH_REGEX);
if (!match?.groups) throw new Error('Invalid bcrypt hash format');
return match.groups;
}
describe('PasswordUtility', () => {
const passwordUtility = Container.get(PasswordUtility);
describe('hash()', () => {
test('should hash a plaintext password', async () => {
const plaintext = 'abcd1234X';
const hashed = await passwordUtility.hash(plaintext);
const { version, costFactor, salt, hashedPassword } = toComponents(hashed);
expect(version).toBe('2a');
expect(costFactor).toBe('10');
expect(salt).toHaveLength(22);
expect(hashedPassword).toHaveLength(31);
});
});
describe('compare()', () => {
test('should return true on match', async () => {
const plaintext = 'abcd1234X';
const hashed = await passwordUtility.hash(plaintext);
const isMatch = await passwordUtility.compare(plaintext, hashed);
expect(isMatch).toBe(true);
});
test('should return false on mismatch', async () => {
const secondPlaintext = 'abcd1234Y';
const hashed = await passwordUtility.hash('abcd1234X');
const isMatch = await passwordUtility.compare(secondPlaintext, hashed);
expect(isMatch).toBe(false);
});
});
describe('validate()', () => {
test('should throw on empty password', () => {
const check = () => passwordUtility.validate();
expect(check).toThrowError('Password is mandatory');
});
test('should return same password if valid', () => {
const validPassword = 'abcd1234X';
const validated = passwordUtility.validate(validPassword);
expect(validated).toBe(validPassword);
});
test('should require at least one uppercase letter', () => {
const invalidPassword = 'abcd1234';
const failingCheck = () => passwordUtility.validate(invalidPassword);
expect(failingCheck).toThrowError('Password must contain at least 1 uppercase letter.');
});
test('should require at least one number', () => {
const validPassword = 'abcd1234X';
const invalidPassword = 'abcdEFGH';
const validated = passwordUtility.validate(validPassword);
expect(validated).toBe(validPassword);
const check = () => passwordUtility.validate(invalidPassword);
expect(check).toThrowError('Password must contain at least 1 number.');
});
test('should require a minimum length of 8 characters', () => {
const invalidPassword = 'a'.repeat(7);
const check = () => passwordUtility.validate(invalidPassword);
expect(check).toThrowError('Password must be 8 to 64 characters long.');
});
test('should require a maximum length of 64 characters', () => {
const invalidPassword = 'a'.repeat(65);
const check = () => passwordUtility.validate(invalidPassword);
expect(check).toThrowError('Password must be 8 to 64 characters long.');
});
});
});

View File

@@ -1,47 +0,0 @@
import { webhookNotFoundErrorMessage } from '@/errors/response-errors/webhook-not-found.error';
describe('utils test webhookNotFoundErrorMessage ', () => {
it('should return a message with path and method', () => {
const message = webhookNotFoundErrorMessage({ path: 'webhook12345', httpMethod: 'GET' });
expect(message).toEqual('The requested webhook "GET webhook12345" is not registered.');
});
it('should return a message with path', () => {
const message = webhookNotFoundErrorMessage({ path: 'webhook12345' });
expect(message).toEqual('The requested webhook "webhook12345" is not registered.');
});
it('should return a message with method with tip', () => {
const message = webhookNotFoundErrorMessage({
path: 'webhook12345',
httpMethod: 'POST',
webhookMethods: ['GET', 'PUT'],
});
expect(message).toEqual(
'This webhook is not registered for POST requests. Did you mean to make a GET or PUT request?',
);
});
it('should return a message with method with tip', () => {
const message = webhookNotFoundErrorMessage({
path: 'webhook12345',
httpMethod: 'POST',
webhookMethods: ['PUT'],
});
expect(message).toEqual(
'This webhook is not registered for POST requests. Did you mean to make a PUT request?',
);
});
it('should return a message with method with tip', () => {
const message = webhookNotFoundErrorMessage({
path: 'webhook12345',
httpMethod: 'POST',
webhookMethods: ['GET', 'PUT', 'DELETE'],
});
expect(message).toEqual(
'This webhook is not registered for POST requests. Did you mean to make a GET, PUT or DELETE request?',
);
});
});

View File

@@ -1,152 +0,0 @@
import type { INode } from 'n8n-workflow';
import { mock } from 'jest-mock-extended';
import type { WorkflowEntity } from '@db/entities/WorkflowEntity';
import type { IWorkflowDb } from '@/Interfaces';
import { WorkflowExecutionService } from '@/workflows/workflowExecution.service';
import type { WorkflowRunner } from '@/WorkflowRunner';
const webhookNode: INode = {
name: 'Webhook',
type: 'n8n-nodes-base.webhook',
id: '111f1db0-e7be-44c5-9ce9-3e35362490f0',
parameters: {},
typeVersion: 1,
position: [0, 0],
webhookId: 'de0f8dcb-7b64-4f22-b66d-d8f74d6aefb7',
};
const secondWebhookNode = {
...webhookNode,
name: 'Webhook 2',
id: '222f1db0-e7be-44c5-9ce9-3e35362490f1',
};
const executeWorkflowTriggerNode: INode = {
name: 'Execute Workflow Trigger',
type: 'n8n-nodes-base.executeWorkflowTrigger',
id: '78d63bca-bb6c-4568-948f-8ed9aacb1fe9',
parameters: {},
typeVersion: 1,
position: [0, 0],
};
const respondToWebhookNode: INode = {
name: 'Respond to Webhook',
type: 'n8n-nodes-base.respondToWebhook',
id: '66d63bca-bb6c-4568-948f-8ed9aacb1fe9',
parameters: {},
typeVersion: 1,
position: [0, 0],
};
const hackerNewsNode: INode = {
name: 'Hacker News',
type: 'n8n-nodes-base.hackerNews',
id: '55d63bca-bb6c-4568-948f-8ed9aacb1fe9',
parameters: {},
typeVersion: 1,
position: [0, 0],
};
describe('WorkflowExecutionService', () => {
const workflowRunner = mock<WorkflowRunner>();
const workflowExecutionService = new WorkflowExecutionService(
mock(),
mock(),
mock(),
mock(),
mock(),
workflowRunner,
mock(),
mock(),
);
describe('runWorkflow()', () => {
test('should call `WorkflowRunner.run()`', async () => {
const node = mock<INode>();
const workflow = mock<WorkflowEntity>({ active: true, nodes: [node] });
workflowRunner.run.mockResolvedValue('fake-execution-id');
await workflowExecutionService.runWorkflow(workflow, node, [[]], mock(), 'trigger');
expect(workflowRunner.run).toHaveBeenCalledTimes(1);
});
});
describe('selectPinnedActivatorStarter()', () => {
const workflow = mock<IWorkflowDb>({
nodes: [],
});
const pinData = {
[webhookNode.name]: [{ json: { key: 'value' } }],
[executeWorkflowTriggerNode.name]: [{ json: { key: 'value' } }],
};
afterEach(() => {
workflow.nodes = [];
});
it('should return `null` if no pindata', () => {
const node = workflowExecutionService.selectPinnedActivatorStarter(workflow, []);
expect(node).toBeNull();
});
it('should return `null` if no starter nodes', () => {
const node = workflowExecutionService.selectPinnedActivatorStarter(workflow);
expect(node).toBeNull();
});
it('should select webhook node if only choice', () => {
workflow.nodes.push(webhookNode);
const node = workflowExecutionService.selectPinnedActivatorStarter(workflow, [], pinData);
expect(node).toEqual(webhookNode);
});
it('should return `null` if no choice', () => {
workflow.nodes.push(hackerNewsNode);
const node = workflowExecutionService.selectPinnedActivatorStarter(workflow, [], pinData);
expect(node).toBeNull();
});
it('should return ignore Respond to Webhook', () => {
workflow.nodes.push(respondToWebhookNode);
const node = workflowExecutionService.selectPinnedActivatorStarter(workflow, [], pinData);
expect(node).toBeNull();
});
it('should select execute workflow trigger if only choice', () => {
workflow.nodes.push(executeWorkflowTriggerNode);
const node = workflowExecutionService.selectPinnedActivatorStarter(workflow, [], pinData);
expect(node).toEqual(executeWorkflowTriggerNode);
});
it('should favor webhook node over execute workflow trigger', () => {
workflow.nodes.push(webhookNode, executeWorkflowTriggerNode);
const node = workflowExecutionService.selectPinnedActivatorStarter(workflow, [], pinData);
expect(node).toEqual(webhookNode);
});
it('should favor first webhook node over second webhook node', () => {
workflow.nodes.push(webhookNode, secondWebhookNode);
const node = workflowExecutionService.selectPinnedActivatorStarter(workflow, [], pinData);
expect(node).toEqual(webhookNode);
});
});
});

View File

@@ -1,54 +0,0 @@
import { License } from '@/License';
import config from '@/config';
import { getWorkflowHistoryPruneTime } from '@/workflows/workflowHistory/workflowHistoryHelper.ee';
import { mockInstance } from '../shared/mocking';
let licensePruneTime = -1;
beforeAll(async () => {
mockInstance(License, {
getWorkflowHistoryPruneLimit() {
return licensePruneTime;
},
});
});
beforeEach(() => {
licensePruneTime = -1;
config.set('workflowHistory.pruneTime', -1);
});
describe('getWorkflowHistoryPruneTime', () => {
test('should return -1 (infinite) if config and license are -1', () => {
licensePruneTime = -1;
config.set('workflowHistory.pruneTime', -1);
expect(getWorkflowHistoryPruneTime()).toBe(-1);
});
test('should return config time if license is infinite and config is not', () => {
licensePruneTime = -1;
config.set('workflowHistory.pruneTime', 24);
expect(getWorkflowHistoryPruneTime()).toBe(24);
});
test('should return license time if config is infinite and license is not', () => {
licensePruneTime = 25;
config.set('workflowHistory.pruneTime', -1);
expect(getWorkflowHistoryPruneTime()).toBe(25);
});
test('should return lowest of config and license time if both are not -1', () => {
licensePruneTime = 26;
config.set('workflowHistory.pruneTime', 100);
expect(getWorkflowHistoryPruneTime()).toBe(26);
licensePruneTime = 100;
config.set('workflowHistory.pruneTime', 27);
expect(getWorkflowHistoryPruneTime()).toBe(27);
});
});