Skip to content

Commit

Permalink
test: Add tests for ActiveWorkflowRunner class (#5278)
Browse files Browse the repository at this point in the history
  • Loading branch information
krynble authored Feb 10, 2023
1 parent 538984d commit a2e2ec5
Show file tree
Hide file tree
Showing 4 changed files with 357 additions and 39 deletions.
40 changes: 23 additions & 17 deletions packages/cli/src/ActiveWorkflowRunner.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable prefer-spread */
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable no-param-reassign */
/* eslint-disable no-console */
/* eslint-disable no-await-in-loop */
/* eslint-disable no-restricted-syntax */
/* eslint-disable @typescript-eslint/no-floating-promises */
Expand Down Expand Up @@ -93,7 +92,7 @@ export class ActiveWorkflowRunner {
// so instead of pulling all the active webhooks just pull the actives that have a trigger
const workflowsData: IWorkflowDb[] = (await Db.collections.Workflow.find({
where: { active: true },
relations: ['shared', 'shared.user', 'shared.user.globalRole'],
relations: ['shared', 'shared.user', 'shared.user.globalRole', 'shared.role'],
})) as IWorkflowDb[];

if (!config.getEnv('endpoints.skipWebhooksDeregistrationOnShutdown')) {
Expand All @@ -109,12 +108,12 @@ export class ActiveWorkflowRunner {
}

if (workflowsData.length !== 0) {
console.info(' ================================');
console.info(' Start Active Workflows:');
console.info(' ================================');
Logger.info(' ================================');
Logger.info(' Start Active Workflows:');
Logger.info(' ================================');

for (const workflowData of workflowsData) {
console.log(` - ${workflowData.name} (ID: ${workflowData.id})`);
Logger.info(` - ${workflowData.name} (ID: ${workflowData.id})`);
Logger.debug(`Initializing active workflow "${workflowData.name}" (startup)`, {
workflowName: workflowData.name,
workflowId: workflowData.id,
Expand All @@ -125,14 +124,14 @@ export class ActiveWorkflowRunner {
workflowName: workflowData.name,
workflowId: workflowData.id,
});
console.log(' => Started');
Logger.info(' => Started');
} catch (error) {
ErrorReporter.error(error);
console.log(
Logger.info(
' => ERROR: Workflow could not be activated on first try, keep on trying',
);
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
console.log(` ${error.message}`);
Logger.info(` ${error.message}`);
Logger.error(
`Issue on intital workflow activation try "${workflowData.name}" (startup)`,
{
Expand Down Expand Up @@ -166,7 +165,10 @@ export class ActiveWorkflowRunner {
}

const activeWorkflows = await this.getActiveWorkflows();
activeWorkflowIds = [...activeWorkflowIds, ...activeWorkflows.map((workflow) => workflow.id)];
activeWorkflowIds = [
...activeWorkflowIds,
...activeWorkflows.map((workflow) => workflow.id.toString()),
];

// Make sure IDs are unique
activeWorkflowIds = Array.from(new Set(activeWorkflowIds));
Expand Down Expand Up @@ -480,7 +482,7 @@ export class ActiveWorkflowRunner {
try {
await this.removeWorkflowWebhooks(workflow.id as string);
} catch (error) {
console.error(
Logger.error(
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`Could not remove webhooks of workflow "${workflow.id}" because of error: "${error.message}"`,
);
Expand Down Expand Up @@ -649,7 +651,7 @@ export class ActiveWorkflowRunner {
.catch(donePromise.reject);
});
} else {
executePromise.catch(console.error);
executePromise.catch(Logger.error);
}
};

Expand Down Expand Up @@ -706,7 +708,7 @@ export class ActiveWorkflowRunner {
.catch(donePromise.reject);
});
} else {
executePromise.catch(console.error);
executePromise.catch(Logger.error);
}
};
returnFunctions.emitError = async (error: Error): Promise<void> => {
Expand Down Expand Up @@ -783,7 +785,7 @@ export class ActiveWorkflowRunner {
if (workflowData === undefined) {
workflowData = (await Db.collections.Workflow.findOne({
where: { id: workflowId },
relations: ['shared', 'shared.user', 'shared.user.globalRole'],
relations: ['shared', 'shared.user', 'shared.user.globalRole', 'shared.role'],
})) as IWorkflowDb;
}

Expand Down Expand Up @@ -811,9 +813,13 @@ export class ActiveWorkflowRunner {
}

const mode = 'trigger';
const additionalData = await WorkflowExecuteAdditionalData.getBase(
(workflowData as WorkflowEntity).shared[0].user.id,
const workflowOwner = (workflowData as WorkflowEntity).shared.find(
(shared) => shared.role.name === 'owner',
);
if (!workflowOwner) {
throw new Error('Workflow cannot be activated because it has no owner');
}
const additionalData = await WorkflowExecuteAdditionalData.getBase(workflowOwner.user.id);
const getTriggerFunctions = this.getExecuteTriggerFunctions(
workflowData,
additionalData,
Expand Down Expand Up @@ -977,7 +983,7 @@ export class ActiveWorkflowRunner {
await this.removeWorkflowWebhooks(workflowId);
} catch (error) {
ErrorReporter.error(error);
console.error(
Logger.error(
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`Could not remove webhooks of workflow "${workflowId}" because of error: "${error.message}"`,
);
Expand Down
265 changes: 265 additions & 0 deletions packages/cli/test/unit/ActiveWorkflowRunner.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
import { v4 as uuid } from 'uuid';
import { mocked } from 'jest-mock';

import { ICredentialTypes, LoggerProxy, NodeOperationError, Workflow } from 'n8n-workflow';

import { ActiveWorkflowRunner } from '@/ActiveWorkflowRunner';
import * as Db from '@/Db';
import { WorkflowEntity } from '@/databases/entities/WorkflowEntity';
import { SharedWorkflow } from '@/databases/entities/SharedWorkflow';
import { Role } from '@/databases/entities/Role';
import { User } from '@/databases/entities/User';
import { getLogger } from '@/Logger';
import { NodeTypes } from '@/NodeTypes';
import { CredentialTypes } from '@/CredentialTypes';
import { randomEmail, randomName } from '../integration/shared/random';
import * as Helpers from './Helpers';
import { WorkflowExecuteAdditionalData } from '@/index';
import { WorkflowRunner } from '@/WorkflowRunner';

/**
* 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, {
id: i + 1,
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: {
find: jest.fn(async () => Promise.resolve(generateWorkflows(databaseActiveWorkflowsCount))),
findOne: jest.fn(async (searchParams) => {
const foundWorkflow = databaseActiveWorkflowsList.find(
(workflow) => workflow.id.toString() === searchParams.where.id.toString(),
);
return Promise.resolve(foundWorkflow);
}),
update: jest.fn(),
createQueryBuilder: jest.fn(() => {
const fakeQueryBuilder = {
update: () => fakeQueryBuilder,
set: () => fakeQueryBuilder,
where: () => fakeQueryBuilder,
execute: () => Promise.resolve(),
};
return fakeQueryBuilder;
}),
},
Webhook: {
clear: jest.fn(),
delete: jest.fn(),
},
},
};
});

const mockExternalHooksRunFunction = jest.fn();

jest.mock('@/ExternalHooks', () => {
return {
ExternalHooks: () => {
return {
run: () => mockExternalHooksRunFunction(),
init: () => Promise.resolve(),
};
},
};
});

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', () => {
let activeWorkflowRunner: ActiveWorkflowRunner;

beforeAll(async () => {
LoggerProxy.init(getLogger());
NodeTypes({
loaded: {
nodes: MOCK_NODE_TYPES_DATA,
credentials: {},
},
known: { nodes: {}, credentials: {} },
credentialTypes: {} as ICredentialTypes,
});
CredentialTypes({
loaded: {
nodes: MOCK_NODE_TYPES_DATA,
credentials: {},
},
known: { nodes: {}, credentials: {} },
credentialTypes: {} as ICredentialTypes,
});
});

beforeEach(() => {
activeWorkflowRunner = new ActiveWorkflowRunner();
});

afterEach(async () => {
await activeWorkflowRunner.removeAll();
databaseActiveWorkflowsCount = 0;
jest.clearAllMocks();
});

test('Should initialize activeWorkflowRunner with empty list of active workflows and call External Hooks', async () => {
void (await activeWorkflowRunner.init());
expect(await activeWorkflowRunner.getActiveWorkflows()).toHaveLength(0);
expect(mocked(Db.collections.Workflow.find)).toHaveBeenCalled();
expect(mocked(Db.collections.Webhook.clear)).toHaveBeenCalled();
expect(mockExternalHooksRunFunction).toHaveBeenCalledTimes(1);
});

test('Should initialize activeWorkflowRunner with one active workflow', async () => {
databaseActiveWorkflowsCount = 1;
void (await activeWorkflowRunner.init());
expect(await activeWorkflowRunner.getActiveWorkflows()).toHaveLength(
databaseActiveWorkflowsCount,
);
expect(mocked(Db.collections.Workflow.find)).toHaveBeenCalled();
expect(mocked(Db.collections.Webhook.clear)).toHaveBeenCalled();
expect(mockExternalHooksRunFunction).toHaveBeenCalled();
});

test('Should make sure function checkIfWorkflowCanBeActivated was called for every workflow', async () => {
databaseActiveWorkflowsCount = 2;
void (await activeWorkflowRunner.init());
expect(workflowCheckIfCanBeActivated).toHaveBeenCalledTimes(databaseActiveWorkflowsCount);
});

test('Call to removeAll should remove every workflow', async () => {
databaseActiveWorkflowsCount = 2;
void (await activeWorkflowRunner.init());
expect(await activeWorkflowRunner.getActiveWorkflows()).toHaveLength(
databaseActiveWorkflowsCount,
);
void (await activeWorkflowRunner.removeAll());
expect(removeFunction).toHaveBeenCalledTimes(databaseActiveWorkflowsCount);
});

test('Call to remove should also call removeWorkflowWebhooks', async () => {
databaseActiveWorkflowsCount = 1;
void (await activeWorkflowRunner.init());
expect(await activeWorkflowRunner.getActiveWorkflows()).toHaveLength(
databaseActiveWorkflowsCount,
);
void (await activeWorkflowRunner.remove('1'));
expect(removeWebhooksFunction).toHaveBeenCalledTimes(1);
});

test('Call to isActive should return true for valid workflow', async () => {
databaseActiveWorkflowsCount = 1;
void (await activeWorkflowRunner.init());
expect(await activeWorkflowRunner.isActive('1')).toBe(true);
});

test('Call to isActive should return false for invalid workflow', async () => {
databaseActiveWorkflowsCount = 1;
void (await activeWorkflowRunner.init());
expect(await activeWorkflowRunner.isActive('2')).toBe(false);
});

test('Calling add should call checkIfWorkflowCanBeActivated', async () => {
// Initialize with default (0) workflows
void (await activeWorkflowRunner.init());
generateWorkflows(1);
void (await activeWorkflowRunner.add('1', 'activate'));
expect(workflowCheckIfCanBeActivated).toHaveBeenCalledTimes(1);
});

test('runWorkflow should call run method in WorkflowRunner', async () => {
void (await activeWorkflowRunner.init());
const workflow = generateWorkflows(1);
const additionalData = await WorkflowExecuteAdditionalData.getBase('fake-user-id');

workflowRunnerRun.mockImplementationOnce(() => Promise.resolve('invalid-execution-id'));

void (await activeWorkflowRunner.runWorkflow(
workflow[0],
workflow[0].nodes[0],
[[]],
additionalData,
'trigger',
));

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');
void (await activeWorkflowRunner.init());
activeWorkflowRunner.executeErrorWorkflow(error, workflowData, 'trigger');
expect(workflowExecuteAdditionalDataExecuteErrorWorkflowSpy).toHaveBeenCalledTimes(1);
});
});
Loading

0 comments on commit a2e2ec5

Please sign in to comment.