2023-02-10 06:24:20 -08:00
|
|
|
import { v4 as uuid } from 'uuid';
|
|
|
|
import { mocked } from 'jest-mock';
|
|
|
|
|
2023-07-18 06:57:14 -07:00
|
|
|
import type { ICredentialTypes, INode, INodesAndCredentials } from 'n8n-workflow';
|
|
|
|
import { LoggerProxy, NodeApiError, NodeOperationError, Workflow } from 'n8n-workflow';
|
2023-02-10 06:24:20 -08:00
|
|
|
|
|
|
|
import { ActiveWorkflowRunner } from '@/ActiveWorkflowRunner';
|
|
|
|
import * as Db from '@/Db';
|
2023-04-12 01:59:14 -07:00
|
|
|
import { WorkflowEntity } from '@db/entities/WorkflowEntity';
|
|
|
|
import { SharedWorkflow } from '@db/entities/SharedWorkflow';
|
|
|
|
import { Role } from '@db/entities/Role';
|
|
|
|
import { User } from '@db/entities/User';
|
2023-02-10 06:24:20 -08:00
|
|
|
import { getLogger } from '@/Logger';
|
|
|
|
import { randomEmail, randomName } from '../integration/shared/random';
|
|
|
|
import * as Helpers from './Helpers';
|
2023-03-17 09:24:05 -07:00
|
|
|
import * as WorkflowExecuteAdditionalData from '@/WorkflowExecuteAdditionalData';
|
2023-02-21 10:21:56 -08:00
|
|
|
|
2023-02-10 06:24:20 -08:00
|
|
|
import { WorkflowRunner } from '@/WorkflowRunner';
|
2023-02-21 10:21:56 -08:00
|
|
|
import { mock } from 'jest-mock-extended';
|
2023-05-02 01:37:19 -07:00
|
|
|
import type { ExternalHooks } from '@/ExternalHooks';
|
2023-02-21 10:21:56 -08:00
|
|
|
import { Container } from 'typedi';
|
|
|
|
import { LoadNodesAndCredentials } from '@/LoadNodesAndCredentials';
|
2023-07-13 01:14:48 -07:00
|
|
|
import { mockInstance } from '../integration/shared/utils/';
|
2023-02-21 10:21:56 -08:00
|
|
|
import { Push } from '@/push';
|
2023-03-16 07:34:13 -07:00
|
|
|
import { ActiveExecutions } from '@/ActiveExecutions';
|
|
|
|
import { NodeTypes } from '@/NodeTypes';
|
2023-07-25 09:17:34 -07:00
|
|
|
import type { WebhookRepository } from '@/databases/repositories';
|
2023-02-10 06:24:20 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO:
|
|
|
|
* - test workflow webhooks activation (that trigger `executeWebhook`and other webhook methods)
|
|
|
|
* - test activation error catching and getters such as `getActivationError` (requires building a workflow that fails to activate)
|
|
|
|
* - test queued workflow activation functions (might need to create a non-working workflow to test this)
|
|
|
|
*/
|
|
|
|
|
|
|
|
let databaseActiveWorkflowsCount = 0;
|
|
|
|
let databaseActiveWorkflowsList: WorkflowEntity[] = [];
|
|
|
|
|
|
|
|
const generateWorkflows = (count: number): WorkflowEntity[] => {
|
|
|
|
const workflows: WorkflowEntity[] = [];
|
|
|
|
const ownerRole = new Role();
|
|
|
|
ownerRole.scope = 'workflow';
|
|
|
|
ownerRole.name = 'owner';
|
|
|
|
ownerRole.id = '1';
|
|
|
|
|
|
|
|
const owner = new User();
|
|
|
|
owner.id = uuid();
|
|
|
|
owner.firstName = randomName();
|
|
|
|
owner.lastName = randomName();
|
|
|
|
owner.email = randomEmail();
|
|
|
|
|
|
|
|
for (let i = 0; i < count; i++) {
|
|
|
|
const workflow = new WorkflowEntity();
|
|
|
|
Object.assign(workflow, {
|
2023-07-18 06:57:14 -07:00
|
|
|
id: (i + 1).toString(),
|
2023-02-10 06:24:20 -08:00
|
|
|
name: randomName(),
|
|
|
|
active: true,
|
|
|
|
createdAt: new Date(),
|
|
|
|
updatedAt: new Date(),
|
|
|
|
nodes: [
|
|
|
|
{
|
|
|
|
parameters: {
|
|
|
|
rule: {
|
|
|
|
interval: [{}],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
id: uuid(),
|
|
|
|
name: 'Schedule Trigger',
|
|
|
|
type: 'n8n-nodes-base.scheduleTrigger',
|
|
|
|
typeVersion: 1,
|
|
|
|
position: [900, 460],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
connections: {},
|
|
|
|
tags: [],
|
|
|
|
});
|
|
|
|
const sharedWorkflow = new SharedWorkflow();
|
|
|
|
sharedWorkflow.workflowId = workflow.id;
|
|
|
|
sharedWorkflow.role = ownerRole;
|
|
|
|
sharedWorkflow.user = owner;
|
|
|
|
|
|
|
|
workflow.shared = [sharedWorkflow];
|
|
|
|
|
|
|
|
workflows.push(workflow);
|
|
|
|
}
|
|
|
|
databaseActiveWorkflowsList = workflows;
|
|
|
|
return workflows;
|
|
|
|
};
|
|
|
|
|
|
|
|
const MOCK_NODE_TYPES_DATA = Helpers.mockNodeTypesData(['scheduleTrigger'], {
|
|
|
|
addTrigger: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
jest.mock('@/Db', () => {
|
|
|
|
return {
|
|
|
|
collections: {
|
|
|
|
Workflow: {
|
2023-04-24 04:17:08 -07:00
|
|
|
find: jest.fn(async () => generateWorkflows(databaseActiveWorkflowsCount)),
|
2023-02-10 06:24:20 -08:00
|
|
|
findOne: jest.fn(async (searchParams) => {
|
2023-04-24 04:17:08 -07:00
|
|
|
return databaseActiveWorkflowsList.find(
|
2023-02-10 06:24:20 -08:00
|
|
|
(workflow) => workflow.id.toString() === searchParams.where.id.toString(),
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
update: jest.fn(),
|
|
|
|
createQueryBuilder: jest.fn(() => {
|
|
|
|
const fakeQueryBuilder = {
|
|
|
|
update: () => fakeQueryBuilder,
|
|
|
|
set: () => fakeQueryBuilder,
|
|
|
|
where: () => fakeQueryBuilder,
|
2023-04-24 04:17:08 -07:00
|
|
|
execute: async () => {},
|
2023-02-10 06:24:20 -08:00
|
|
|
};
|
|
|
|
return fakeQueryBuilder;
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
Webhook: {
|
|
|
|
clear: jest.fn(),
|
|
|
|
delete: jest.fn(),
|
|
|
|
},
|
2023-04-18 03:41:55 -07:00
|
|
|
Variables: {
|
|
|
|
find: jest.fn(() => []),
|
|
|
|
},
|
2023-02-10 06:24:20 -08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
const workflowCheckIfCanBeActivated = jest.fn(() => true);
|
|
|
|
|
|
|
|
jest
|
|
|
|
.spyOn(Workflow.prototype, 'checkIfWorkflowCanBeActivated')
|
|
|
|
.mockImplementation(workflowCheckIfCanBeActivated);
|
|
|
|
|
|
|
|
const removeFunction = jest.spyOn(ActiveWorkflowRunner.prototype, 'remove');
|
|
|
|
const removeWebhooksFunction = jest.spyOn(ActiveWorkflowRunner.prototype, 'removeWorkflowWebhooks');
|
|
|
|
const workflowRunnerRun = jest.spyOn(WorkflowRunner.prototype, 'run');
|
|
|
|
const workflowExecuteAdditionalDataExecuteErrorWorkflowSpy = jest.spyOn(
|
|
|
|
WorkflowExecuteAdditionalData,
|
|
|
|
'executeErrorWorkflow',
|
|
|
|
);
|
|
|
|
|
|
|
|
describe('ActiveWorkflowRunner', () => {
|
2023-02-21 10:21:56 -08:00
|
|
|
let externalHooks: ExternalHooks;
|
2023-02-10 06:24:20 -08:00
|
|
|
let activeWorkflowRunner: ActiveWorkflowRunner;
|
2023-07-25 09:17:34 -07:00
|
|
|
let webhookRepository = mock<WebhookRepository>();
|
2023-02-10 06:24:20 -08:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
LoggerProxy.init(getLogger());
|
2023-02-21 10:21:56 -08:00
|
|
|
const nodesAndCredentials: INodesAndCredentials = {
|
2023-02-10 06:24:20 -08:00
|
|
|
loaded: {
|
|
|
|
nodes: MOCK_NODE_TYPES_DATA,
|
|
|
|
credentials: {},
|
|
|
|
},
|
|
|
|
known: { nodes: {}, credentials: {} },
|
|
|
|
credentialTypes: {} as ICredentialTypes,
|
2023-02-21 10:21:56 -08:00
|
|
|
};
|
|
|
|
Container.set(LoadNodesAndCredentials, nodesAndCredentials);
|
|
|
|
mockInstance(Push);
|
2023-02-10 06:24:20 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2023-02-21 10:21:56 -08:00
|
|
|
externalHooks = mock();
|
2023-03-16 07:34:13 -07:00
|
|
|
activeWorkflowRunner = new ActiveWorkflowRunner(
|
|
|
|
new ActiveExecutions(),
|
|
|
|
externalHooks,
|
|
|
|
Container.get(NodeTypes),
|
2023-07-25 09:17:34 -07:00
|
|
|
webhookRepository,
|
2023-03-16 07:34:13 -07:00
|
|
|
);
|
2023-02-10 06:24:20 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
await activeWorkflowRunner.removeAll();
|
|
|
|
databaseActiveWorkflowsCount = 0;
|
2023-03-16 07:34:13 -07:00
|
|
|
databaseActiveWorkflowsList = [];
|
2023-02-10 06:24:20 -08:00
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Should initialize activeWorkflowRunner with empty list of active workflows and call External Hooks', async () => {
|
2023-02-21 10:21:56 -08:00
|
|
|
await activeWorkflowRunner.init();
|
2023-02-10 06:24:20 -08:00
|
|
|
expect(await activeWorkflowRunner.getActiveWorkflows()).toHaveLength(0);
|
|
|
|
expect(mocked(Db.collections.Workflow.find)).toHaveBeenCalled();
|
2023-07-25 09:17:34 -07:00
|
|
|
expect(mocked(webhookRepository.clear)).toHaveBeenCalled();
|
2023-02-21 10:21:56 -08:00
|
|
|
expect(externalHooks.run).toHaveBeenCalledTimes(1);
|
2023-02-10 06:24:20 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Should initialize activeWorkflowRunner with one active workflow', async () => {
|
|
|
|
databaseActiveWorkflowsCount = 1;
|
2023-02-21 10:21:56 -08:00
|
|
|
await activeWorkflowRunner.init();
|
2023-02-10 06:24:20 -08:00
|
|
|
expect(await activeWorkflowRunner.getActiveWorkflows()).toHaveLength(
|
|
|
|
databaseActiveWorkflowsCount,
|
|
|
|
);
|
|
|
|
expect(mocked(Db.collections.Workflow.find)).toHaveBeenCalled();
|
2023-07-25 09:17:34 -07:00
|
|
|
expect(mocked(webhookRepository.clear)).toHaveBeenCalled();
|
2023-02-21 10:21:56 -08:00
|
|
|
expect(externalHooks.run).toHaveBeenCalled();
|
2023-02-10 06:24:20 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Should make sure function checkIfWorkflowCanBeActivated was called for every workflow', async () => {
|
|
|
|
databaseActiveWorkflowsCount = 2;
|
2023-02-21 10:21:56 -08:00
|
|
|
await activeWorkflowRunner.init();
|
2023-02-10 06:24:20 -08:00
|
|
|
expect(workflowCheckIfCanBeActivated).toHaveBeenCalledTimes(databaseActiveWorkflowsCount);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Call to removeAll should remove every workflow', async () => {
|
|
|
|
databaseActiveWorkflowsCount = 2;
|
2023-02-21 10:21:56 -08:00
|
|
|
await activeWorkflowRunner.init();
|
2023-02-10 06:24:20 -08:00
|
|
|
expect(await activeWorkflowRunner.getActiveWorkflows()).toHaveLength(
|
|
|
|
databaseActiveWorkflowsCount,
|
|
|
|
);
|
2023-02-21 10:21:56 -08:00
|
|
|
await activeWorkflowRunner.removeAll();
|
2023-02-10 06:24:20 -08:00
|
|
|
expect(removeFunction).toHaveBeenCalledTimes(databaseActiveWorkflowsCount);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Call to remove should also call removeWorkflowWebhooks', async () => {
|
|
|
|
databaseActiveWorkflowsCount = 1;
|
2023-02-21 10:21:56 -08:00
|
|
|
await activeWorkflowRunner.init();
|
2023-02-10 06:24:20 -08:00
|
|
|
expect(await activeWorkflowRunner.getActiveWorkflows()).toHaveLength(
|
|
|
|
databaseActiveWorkflowsCount,
|
|
|
|
);
|
2023-02-21 10:21:56 -08:00
|
|
|
await activeWorkflowRunner.remove('1');
|
2023-02-10 06:24:20 -08:00
|
|
|
expect(removeWebhooksFunction).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Call to isActive should return true for valid workflow', async () => {
|
|
|
|
databaseActiveWorkflowsCount = 1;
|
2023-02-21 10:21:56 -08:00
|
|
|
await activeWorkflowRunner.init();
|
2023-02-10 06:24:20 -08:00
|
|
|
expect(await activeWorkflowRunner.isActive('1')).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Call to isActive should return false for invalid workflow', async () => {
|
|
|
|
databaseActiveWorkflowsCount = 1;
|
2023-02-21 10:21:56 -08:00
|
|
|
await activeWorkflowRunner.init();
|
2023-02-10 06:24:20 -08:00
|
|
|
expect(await activeWorkflowRunner.isActive('2')).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Calling add should call checkIfWorkflowCanBeActivated', async () => {
|
|
|
|
// Initialize with default (0) workflows
|
2023-02-21 10:21:56 -08:00
|
|
|
await activeWorkflowRunner.init();
|
2023-02-10 06:24:20 -08:00
|
|
|
generateWorkflows(1);
|
2023-02-21 10:21:56 -08:00
|
|
|
await activeWorkflowRunner.add('1', 'activate');
|
2023-02-10 06:24:20 -08:00
|
|
|
expect(workflowCheckIfCanBeActivated).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('runWorkflow should call run method in WorkflowRunner', async () => {
|
2023-02-21 10:21:56 -08:00
|
|
|
await activeWorkflowRunner.init();
|
2023-02-10 06:24:20 -08:00
|
|
|
const workflow = generateWorkflows(1);
|
|
|
|
const additionalData = await WorkflowExecuteAdditionalData.getBase('fake-user-id');
|
|
|
|
|
2023-04-24 04:17:08 -07:00
|
|
|
workflowRunnerRun.mockResolvedValueOnce('invalid-execution-id');
|
2023-02-10 06:24:20 -08:00
|
|
|
|
2023-02-21 10:21:56 -08:00
|
|
|
await activeWorkflowRunner.runWorkflow(
|
2023-02-10 06:24:20 -08:00
|
|
|
workflow[0],
|
|
|
|
workflow[0].nodes[0],
|
|
|
|
[[]],
|
|
|
|
additionalData,
|
|
|
|
'trigger',
|
2023-02-21 10:21:56 -08:00
|
|
|
);
|
2023-02-10 06:24:20 -08:00
|
|
|
|
|
|
|
expect(workflowRunnerRun).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('executeErrorWorkflow should call function with same name in WorkflowExecuteAdditionalData', async () => {
|
|
|
|
const workflowData = generateWorkflows(1)[0];
|
|
|
|
const error = new NodeOperationError(workflowData.nodes[0], 'Fake error message');
|
2023-02-21 10:21:56 -08:00
|
|
|
await activeWorkflowRunner.init();
|
2023-02-10 06:24:20 -08:00
|
|
|
activeWorkflowRunner.executeErrorWorkflow(error, workflowData, 'trigger');
|
|
|
|
expect(workflowExecuteAdditionalDataExecuteErrorWorkflowSpy).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
2023-07-18 06:57:14 -07:00
|
|
|
|
|
|
|
describe('init()', () => {
|
|
|
|
it('should execute error workflow on failure to activate due to 401', async () => {
|
|
|
|
databaseActiveWorkflowsCount = 1;
|
|
|
|
|
|
|
|
jest.spyOn(ActiveWorkflowRunner.prototype, 'add').mockImplementation(() => {
|
|
|
|
throw new NodeApiError(
|
|
|
|
{
|
|
|
|
id: 'a75dcd1b-9fed-4643-90bd-75933d67936c',
|
|
|
|
name: 'Github Trigger',
|
|
|
|
type: 'n8n-nodes-base.githubTrigger',
|
|
|
|
typeVersion: 1,
|
|
|
|
position: [0, 0],
|
|
|
|
} as INode,
|
|
|
|
{
|
|
|
|
httpCode: '401',
|
|
|
|
message: 'Authorization failed - please check your credentials',
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
const executeSpy = jest.spyOn(ActiveWorkflowRunner.prototype, 'executeErrorWorkflow');
|
|
|
|
|
|
|
|
await activeWorkflowRunner.init();
|
|
|
|
|
|
|
|
const [error, workflow] = executeSpy.mock.calls[0];
|
|
|
|
|
|
|
|
expect(error.message).toContain('Authorization');
|
|
|
|
expect(workflow.id).toBe('1');
|
|
|
|
});
|
|
|
|
});
|
2023-02-10 06:24:20 -08:00
|
|
|
});
|