mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-22 12:19:09 +00:00
refactor(core): Move methods from WorkflowHelpers into various workflow services (no-changelog) (#8348)
This commit is contained in:
committed by
GitHub
parent
ab52aaf7e9
commit
7cdbb424e3
@@ -0,0 +1,180 @@
|
||||
import Container from 'typedi';
|
||||
import { mock } from 'jest-mock-extended';
|
||||
import { CredentialsEntity } from '@db/entities/CredentialsEntity';
|
||||
import { CredentialsRepository } from '@db/repositories/credentials.repository';
|
||||
import { SharedWorkflowRepository } from '@db/repositories/sharedWorkflow.repository';
|
||||
import { WorkflowRepository } from '@db/repositories/workflow.repository';
|
||||
import { Telemetry } from '@/telemetry';
|
||||
import { EnterpriseWorkflowService } from '@/workflows/workflow.service.ee';
|
||||
|
||||
import * as testDb from '../shared/testDb';
|
||||
import { mockInstance } from '../../shared/mocking';
|
||||
import {
|
||||
FIRST_CREDENTIAL_ID,
|
||||
SECOND_CREDENTIAL_ID,
|
||||
THIRD_CREDENTIAL_ID,
|
||||
getWorkflow,
|
||||
} from '../shared/workflow';
|
||||
|
||||
describe('EnterpriseWorkflowService', () => {
|
||||
let service: EnterpriseWorkflowService;
|
||||
|
||||
beforeAll(async () => {
|
||||
await testDb.init();
|
||||
mockInstance(Telemetry);
|
||||
|
||||
service = new EnterpriseWorkflowService(
|
||||
mock(),
|
||||
Container.get(SharedWorkflowRepository),
|
||||
Container.get(WorkflowRepository),
|
||||
Container.get(CredentialsRepository),
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await testDb.truncate(['Workflow']);
|
||||
jest.restoreAllMocks();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await testDb.terminate();
|
||||
});
|
||||
|
||||
describe('validateWorkflowCredentialUsage', () => {
|
||||
function generateCredentialEntity(credentialId: string) {
|
||||
const credentialEntity = new CredentialsEntity();
|
||||
credentialEntity.id = credentialId;
|
||||
return credentialEntity;
|
||||
}
|
||||
|
||||
it('Should throw error saving a workflow using credential without access', () => {
|
||||
const newWorkflowVersion = getWorkflow({ addNodeWithOneCred: true });
|
||||
const previousWorkflowVersion = getWorkflow();
|
||||
expect(() => {
|
||||
service.validateWorkflowCredentialUsage(newWorkflowVersion, previousWorkflowVersion, []);
|
||||
}).toThrow();
|
||||
});
|
||||
|
||||
it('Should not throw error when saving a workflow using credential with access', () => {
|
||||
const newWorkflowVersion = getWorkflow({ addNodeWithOneCred: true });
|
||||
const previousWorkflowVersion = getWorkflow();
|
||||
expect(() => {
|
||||
service.validateWorkflowCredentialUsage(newWorkflowVersion, previousWorkflowVersion, [
|
||||
generateCredentialEntity('1'),
|
||||
]);
|
||||
}).not.toThrow();
|
||||
});
|
||||
|
||||
it('Should not throw error when saving a workflow removing node without credential access', () => {
|
||||
const newWorkflowVersion = getWorkflow();
|
||||
const previousWorkflowVersion = getWorkflow({ addNodeWithOneCred: true });
|
||||
expect(() => {
|
||||
service.validateWorkflowCredentialUsage(newWorkflowVersion, previousWorkflowVersion, [
|
||||
generateCredentialEntity('1'),
|
||||
]);
|
||||
}).not.toThrow();
|
||||
});
|
||||
|
||||
it('Should save fine when not making changes to workflow without access', () => {
|
||||
const workflowWithOneCredential = getWorkflow({ addNodeWithOneCred: true });
|
||||
expect(() => {
|
||||
service.validateWorkflowCredentialUsage(
|
||||
workflowWithOneCredential,
|
||||
workflowWithOneCredential,
|
||||
[],
|
||||
);
|
||||
}).not.toThrow();
|
||||
});
|
||||
|
||||
it('Should throw error saving a workflow adding node without credential access', () => {
|
||||
const newWorkflowVersion = getWorkflow({
|
||||
addNodeWithOneCred: true,
|
||||
addNodeWithTwoCreds: true,
|
||||
});
|
||||
const previousWorkflowVersion = getWorkflow({ addNodeWithOneCred: true });
|
||||
expect(() => {
|
||||
service.validateWorkflowCredentialUsage(newWorkflowVersion, previousWorkflowVersion, []);
|
||||
}).toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('getNodesWithInaccessibleCreds', () => {
|
||||
test('Should return an empty list for a workflow without nodes', () => {
|
||||
const workflow = getWorkflow();
|
||||
const nodesWithInaccessibleCreds = service.getNodesWithInaccessibleCreds(workflow, []);
|
||||
expect(nodesWithInaccessibleCreds).toHaveLength(0);
|
||||
});
|
||||
|
||||
test('Should return an empty list for a workflow with nodes without credentials', () => {
|
||||
const workflow = getWorkflow({ addNodeWithoutCreds: true });
|
||||
const nodesWithInaccessibleCreds = service.getNodesWithInaccessibleCreds(workflow, []);
|
||||
expect(nodesWithInaccessibleCreds).toHaveLength(0);
|
||||
});
|
||||
|
||||
test('Should return an element for a node with a credential without access', () => {
|
||||
const workflow = getWorkflow({ addNodeWithOneCred: true });
|
||||
const nodesWithInaccessibleCreds = service.getNodesWithInaccessibleCreds(workflow, []);
|
||||
expect(nodesWithInaccessibleCreds).toHaveLength(1);
|
||||
});
|
||||
|
||||
test('Should return an empty list for a node with a credential with access', () => {
|
||||
const workflow = getWorkflow({ addNodeWithOneCred: true });
|
||||
const nodesWithInaccessibleCreds = service.getNodesWithInaccessibleCreds(workflow, [
|
||||
FIRST_CREDENTIAL_ID,
|
||||
]);
|
||||
expect(nodesWithInaccessibleCreds).toHaveLength(0);
|
||||
});
|
||||
|
||||
test('Should return an element for a node with two credentials and mixed access', () => {
|
||||
const workflow = getWorkflow({ addNodeWithTwoCreds: true });
|
||||
const nodesWithInaccessibleCreds = service.getNodesWithInaccessibleCreds(workflow, [
|
||||
SECOND_CREDENTIAL_ID,
|
||||
]);
|
||||
expect(nodesWithInaccessibleCreds).toHaveLength(1);
|
||||
});
|
||||
|
||||
test('Should return one node for a workflow with two nodes and two credentials', () => {
|
||||
const workflow = getWorkflow({ addNodeWithOneCred: true, addNodeWithTwoCreds: true });
|
||||
const nodesWithInaccessibleCreds = service.getNodesWithInaccessibleCreds(workflow, [
|
||||
SECOND_CREDENTIAL_ID,
|
||||
THIRD_CREDENTIAL_ID,
|
||||
]);
|
||||
expect(nodesWithInaccessibleCreds).toHaveLength(1);
|
||||
});
|
||||
|
||||
test('Should return one element for a workflows with two nodes and one credential', () => {
|
||||
const workflow = getWorkflow({
|
||||
addNodeWithoutCreds: true,
|
||||
addNodeWithOneCred: true,
|
||||
addNodeWithTwoCreds: true,
|
||||
});
|
||||
const nodesWithInaccessibleCreds = service.getNodesWithInaccessibleCreds(workflow, [
|
||||
FIRST_CREDENTIAL_ID,
|
||||
]);
|
||||
expect(nodesWithInaccessibleCreds).toHaveLength(1);
|
||||
});
|
||||
|
||||
test('Should return one element for a workflows with two nodes and partial credential access', () => {
|
||||
const workflow = getWorkflow({ addNodeWithOneCred: true, addNodeWithTwoCreds: true });
|
||||
const nodesWithInaccessibleCreds = service.getNodesWithInaccessibleCreds(workflow, [
|
||||
FIRST_CREDENTIAL_ID,
|
||||
SECOND_CREDENTIAL_ID,
|
||||
]);
|
||||
expect(nodesWithInaccessibleCreds).toHaveLength(1);
|
||||
});
|
||||
|
||||
test('Should return two elements for a workflows with two nodes and partial credential access', () => {
|
||||
const workflow = getWorkflow({ addNodeWithOneCred: true, addNodeWithTwoCreds: true });
|
||||
const nodesWithInaccessibleCreds = service.getNodesWithInaccessibleCreds(workflow, [
|
||||
SECOND_CREDENTIAL_ID,
|
||||
]);
|
||||
expect(nodesWithInaccessibleCreds).toHaveLength(2);
|
||||
});
|
||||
|
||||
test('Should return two elements for a workflows with two nodes and no credential access', () => {
|
||||
const workflow = getWorkflow({ addNodeWithOneCred: true, addNodeWithTwoCreds: true });
|
||||
const nodesWithInaccessibleCreds = service.getNodesWithInaccessibleCreds(workflow, []);
|
||||
expect(nodesWithInaccessibleCreds).toHaveLength(2);
|
||||
});
|
||||
});
|
||||
});
|
||||
118
packages/cli/test/integration/workflows/workflow.service.test.ts
Normal file
118
packages/cli/test/integration/workflows/workflow.service.test.ts
Normal file
@@ -0,0 +1,118 @@
|
||||
import Container from 'typedi';
|
||||
import { mock } from 'jest-mock-extended';
|
||||
import { ActiveWorkflowRunner } from '@/ActiveWorkflowRunner';
|
||||
import { SharedWorkflowRepository } from '@db/repositories/sharedWorkflow.repository';
|
||||
import { WorkflowRepository } from '@db/repositories/workflow.repository';
|
||||
import { Telemetry } from '@/telemetry';
|
||||
import { MultiMainSetup } from '@/services/orchestration/main/MultiMainSetup.ee';
|
||||
import { WorkflowService } from '@/workflows/workflow.service';
|
||||
|
||||
import * as testDb from '../shared/testDb';
|
||||
import { mockInstance } from '../../shared/mocking';
|
||||
import { createOwner } from '../shared/db/users';
|
||||
import { createWorkflow } from '../shared/db/workflows';
|
||||
|
||||
let workflowService: WorkflowService;
|
||||
let activeWorkflowRunner: ActiveWorkflowRunner;
|
||||
let multiMainSetup: MultiMainSetup;
|
||||
|
||||
beforeAll(async () => {
|
||||
await testDb.init();
|
||||
|
||||
activeWorkflowRunner = mockInstance(ActiveWorkflowRunner);
|
||||
multiMainSetup = mockInstance(MultiMainSetup);
|
||||
mockInstance(Telemetry);
|
||||
|
||||
workflowService = new WorkflowService(
|
||||
mock(),
|
||||
mock(),
|
||||
Container.get(SharedWorkflowRepository),
|
||||
Container.get(WorkflowRepository),
|
||||
mock(),
|
||||
mock(),
|
||||
mock(),
|
||||
mock(),
|
||||
mock(),
|
||||
multiMainSetup,
|
||||
mock(),
|
||||
activeWorkflowRunner,
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await testDb.truncate(['Workflow']);
|
||||
jest.restoreAllMocks();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await testDb.terminate();
|
||||
});
|
||||
|
||||
describe('update()', () => {
|
||||
test('should remove and re-add to active workflows on `active: true` payload', async () => {
|
||||
const owner = await createOwner();
|
||||
const workflow = await createWorkflow({ active: true }, owner);
|
||||
|
||||
const removeSpy = jest.spyOn(activeWorkflowRunner, 'remove');
|
||||
const addSpy = jest.spyOn(activeWorkflowRunner, 'add');
|
||||
|
||||
await workflowService.update(owner, workflow, workflow.id);
|
||||
|
||||
expect(removeSpy).toHaveBeenCalledTimes(1);
|
||||
const [removedWorkflowId] = removeSpy.mock.calls[0];
|
||||
expect(removedWorkflowId).toBe(workflow.id);
|
||||
|
||||
expect(addSpy).toHaveBeenCalledTimes(1);
|
||||
const [addedWorkflowId, activationMode] = addSpy.mock.calls[0];
|
||||
expect(addedWorkflowId).toBe(workflow.id);
|
||||
expect(activationMode).toBe('update');
|
||||
});
|
||||
|
||||
test('should remove from active workflows on `active: false` payload', async () => {
|
||||
const owner = await createOwner();
|
||||
const workflow = await createWorkflow({ active: true }, owner);
|
||||
|
||||
const removeSpy = jest.spyOn(activeWorkflowRunner, 'remove');
|
||||
const addSpy = jest.spyOn(activeWorkflowRunner, 'add');
|
||||
|
||||
workflow.active = false;
|
||||
await workflowService.update(owner, workflow, workflow.id);
|
||||
|
||||
expect(removeSpy).toHaveBeenCalledTimes(1);
|
||||
const [removedWorkflowId] = removeSpy.mock.calls[0];
|
||||
expect(removedWorkflowId).toBe(workflow.id);
|
||||
|
||||
expect(addSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('should broadcast active workflow state change if state changed', async () => {
|
||||
const owner = await createOwner();
|
||||
const workflow = await createWorkflow({ active: true }, owner);
|
||||
|
||||
const publishSpy = jest.spyOn(multiMainSetup, 'publish');
|
||||
|
||||
workflow.active = false;
|
||||
await workflowService.update(owner, workflow, workflow.id);
|
||||
|
||||
expect(publishSpy).toHaveBeenCalledTimes(1);
|
||||
expect(publishSpy).toHaveBeenCalledWith(
|
||||
'workflowActiveStateChanged',
|
||||
expect.objectContaining({
|
||||
newState: false,
|
||||
oldState: true,
|
||||
workflowId: workflow.id,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
test('should not broadcast active workflow state change if state did not change', async () => {
|
||||
const owner = await createOwner();
|
||||
const workflow = await createWorkflow({ active: true }, owner);
|
||||
|
||||
const publishSpy = jest.spyOn(multiMainSetup, 'publish');
|
||||
|
||||
await workflowService.update(owner, workflow, workflow.id);
|
||||
|
||||
expect(publishSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,663 @@
|
||||
import Container from 'typedi';
|
||||
import type { SuperAgentTest } from 'supertest';
|
||||
import { v4 as uuid } from 'uuid';
|
||||
import type { INode, IPinData } from 'n8n-workflow';
|
||||
|
||||
import * as UserManagementHelpers from '@/UserManagement/UserManagementHelper';
|
||||
import type { User } from '@db/entities/User';
|
||||
import { WorkflowRepository } from '@db/repositories/workflow.repository';
|
||||
import type { WorkflowEntity } from '@db/entities/WorkflowEntity';
|
||||
import { RoleService } from '@/services/role.service';
|
||||
import type { ListQuery } from '@/requests';
|
||||
import { WorkflowHistoryRepository } from '@db/repositories/workflowHistory.repository';
|
||||
import { ActiveWorkflowRunner } from '@/ActiveWorkflowRunner';
|
||||
import { Push } from '@/push';
|
||||
import { EnterpriseWorkflowService } from '@/workflows/workflow.service.ee';
|
||||
|
||||
import { mockInstance } from '../../shared/mocking';
|
||||
import * as utils from '../shared/utils/';
|
||||
import * as testDb from '../shared/testDb';
|
||||
import { makeWorkflow, MOCK_PINDATA } from '../shared/utils/';
|
||||
import { randomCredentialPayload } from '../shared/random';
|
||||
import { saveCredential } from '../shared/db/credentials';
|
||||
import { createOwner } from '../shared/db/users';
|
||||
import { createWorkflow } from '../shared/db/workflows';
|
||||
import { createTag } from '../shared/db/tags';
|
||||
|
||||
let owner: User;
|
||||
let authOwnerAgent: SuperAgentTest;
|
||||
|
||||
jest.spyOn(UserManagementHelpers, 'isSharingEnabled').mockReturnValue(false);
|
||||
|
||||
const testServer = utils.setupTestServer({ endpointGroups: ['workflows'] });
|
||||
const license = testServer.license;
|
||||
|
||||
const { objectContaining, arrayContaining, any } = expect;
|
||||
|
||||
const activeWorkflowRunnerLike = mockInstance(ActiveWorkflowRunner);
|
||||
mockInstance(Push);
|
||||
|
||||
beforeAll(async () => {
|
||||
owner = await createOwner();
|
||||
authOwnerAgent = testServer.authAgentFor(owner);
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
jest.resetAllMocks();
|
||||
await testDb.truncate(['Workflow', 'SharedWorkflow', 'Tag', 'WorkflowHistory']);
|
||||
});
|
||||
|
||||
describe('POST /workflows', () => {
|
||||
const testWithPinData = async (withPinData: boolean) => {
|
||||
const workflow = makeWorkflow({ withPinData });
|
||||
const response = await authOwnerAgent.post('/workflows').send(workflow);
|
||||
expect(response.statusCode).toBe(200);
|
||||
return (response.body.data as { pinData: IPinData }).pinData;
|
||||
};
|
||||
|
||||
test('should store pin data for node in workflow', async () => {
|
||||
const pinData = await testWithPinData(true);
|
||||
expect(pinData).toMatchObject(MOCK_PINDATA);
|
||||
});
|
||||
|
||||
test('should set pin data to null if no pin data', async () => {
|
||||
const pinData = await testWithPinData(false);
|
||||
expect(pinData).toBeNull();
|
||||
});
|
||||
|
||||
test('should create workflow history version when licensed', async () => {
|
||||
license.enable('feat:workflowHistory');
|
||||
const payload = {
|
||||
name: 'testing',
|
||||
nodes: [
|
||||
{
|
||||
id: 'uuid-1234',
|
||||
parameters: {},
|
||||
name: 'Start',
|
||||
type: 'n8n-nodes-base.start',
|
||||
typeVersion: 1,
|
||||
position: [240, 300],
|
||||
},
|
||||
],
|
||||
connections: {},
|
||||
staticData: null,
|
||||
settings: {
|
||||
saveExecutionProgress: true,
|
||||
saveManualExecutions: true,
|
||||
saveDataErrorExecution: 'all',
|
||||
saveDataSuccessExecution: 'all',
|
||||
executionTimeout: 3600,
|
||||
timezone: 'America/New_York',
|
||||
},
|
||||
active: false,
|
||||
};
|
||||
|
||||
const response = await authOwnerAgent.post('/workflows').send(payload);
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
|
||||
const {
|
||||
data: { id },
|
||||
} = response.body;
|
||||
|
||||
expect(id).toBeDefined();
|
||||
expect(
|
||||
await Container.get(WorkflowHistoryRepository).count({ where: { workflowId: id } }),
|
||||
).toBe(1);
|
||||
const historyVersion = await Container.get(WorkflowHistoryRepository).findOne({
|
||||
where: {
|
||||
workflowId: id,
|
||||
},
|
||||
});
|
||||
expect(historyVersion).not.toBeNull();
|
||||
expect(historyVersion!.connections).toEqual(payload.connections);
|
||||
expect(historyVersion!.nodes).toEqual(payload.nodes);
|
||||
});
|
||||
|
||||
test('should not create workflow history version when not licensed', async () => {
|
||||
license.disable('feat:workflowHistory');
|
||||
const payload = {
|
||||
name: 'testing',
|
||||
nodes: [
|
||||
{
|
||||
id: 'uuid-1234',
|
||||
parameters: {},
|
||||
name: 'Start',
|
||||
type: 'n8n-nodes-base.start',
|
||||
typeVersion: 1,
|
||||
position: [240, 300],
|
||||
},
|
||||
],
|
||||
connections: {},
|
||||
staticData: null,
|
||||
settings: {
|
||||
saveExecutionProgress: true,
|
||||
saveManualExecutions: true,
|
||||
saveDataErrorExecution: 'all',
|
||||
saveDataSuccessExecution: 'all',
|
||||
executionTimeout: 3600,
|
||||
timezone: 'America/New_York',
|
||||
},
|
||||
active: false,
|
||||
};
|
||||
|
||||
const response = await authOwnerAgent.post('/workflows').send(payload);
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
|
||||
const {
|
||||
data: { id },
|
||||
} = response.body;
|
||||
|
||||
expect(id).toBeDefined();
|
||||
expect(
|
||||
await Container.get(WorkflowHistoryRepository).count({ where: { workflowId: id } }),
|
||||
).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /workflows/:id', () => {
|
||||
test('should return pin data', async () => {
|
||||
const workflow = makeWorkflow({ withPinData: true });
|
||||
const workflowCreationResponse = await authOwnerAgent.post('/workflows').send(workflow);
|
||||
|
||||
const { id } = workflowCreationResponse.body.data as { id: string };
|
||||
const workflowRetrievalResponse = await authOwnerAgent.get(`/workflows/${id}`);
|
||||
|
||||
expect(workflowRetrievalResponse.statusCode).toBe(200);
|
||||
const { pinData } = workflowRetrievalResponse.body.data as { pinData: IPinData };
|
||||
expect(pinData).toMatchObject(MOCK_PINDATA);
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /workflows', () => {
|
||||
test('should return zero workflows if none exist', async () => {
|
||||
const response = await authOwnerAgent.get('/workflows').expect(200);
|
||||
|
||||
expect(response.body).toEqual({ count: 0, data: [] });
|
||||
});
|
||||
|
||||
test('should return workflows', async () => {
|
||||
const credential = await saveCredential(randomCredentialPayload(), {
|
||||
user: owner,
|
||||
role: await Container.get(RoleService).findCredentialOwnerRole(),
|
||||
});
|
||||
|
||||
const nodes: INode[] = [
|
||||
{
|
||||
id: uuid(),
|
||||
name: 'Action Network',
|
||||
type: 'n8n-nodes-base.actionNetwork',
|
||||
parameters: {},
|
||||
typeVersion: 1,
|
||||
position: [0, 0],
|
||||
credentials: {
|
||||
actionNetworkApi: {
|
||||
id: credential.id,
|
||||
name: credential.name,
|
||||
},
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
const tag = await createTag({ name: 'A' });
|
||||
|
||||
await createWorkflow({ name: 'First', nodes, tags: [tag] }, owner);
|
||||
await createWorkflow({ name: 'Second' }, owner);
|
||||
|
||||
const response = await authOwnerAgent.get('/workflows').expect(200);
|
||||
|
||||
expect(response.body).toEqual({
|
||||
count: 2,
|
||||
data: arrayContaining([
|
||||
objectContaining({
|
||||
id: any(String),
|
||||
name: 'First',
|
||||
active: any(Boolean),
|
||||
createdAt: any(String),
|
||||
updatedAt: any(String),
|
||||
tags: [{ id: any(String), name: 'A' }],
|
||||
versionId: any(String),
|
||||
ownedBy: {
|
||||
id: owner.id,
|
||||
email: any(String),
|
||||
firstName: any(String),
|
||||
lastName: any(String),
|
||||
},
|
||||
sharedWith: [],
|
||||
}),
|
||||
objectContaining({
|
||||
id: any(String),
|
||||
name: 'Second',
|
||||
active: any(Boolean),
|
||||
createdAt: any(String),
|
||||
updatedAt: any(String),
|
||||
tags: [],
|
||||
versionId: any(String),
|
||||
ownedBy: {
|
||||
id: owner.id,
|
||||
email: any(String),
|
||||
firstName: any(String),
|
||||
lastName: any(String),
|
||||
},
|
||||
sharedWith: [],
|
||||
}),
|
||||
]),
|
||||
});
|
||||
|
||||
const found = response.body.data.find(
|
||||
(w: ListQuery.Workflow.WithOwnership) => w.name === 'First',
|
||||
);
|
||||
|
||||
expect(found.nodes).toBeUndefined();
|
||||
expect(found.sharedWith).toHaveLength(0);
|
||||
expect(found.usedCredentials).toBeUndefined();
|
||||
});
|
||||
|
||||
describe('filter', () => {
|
||||
test('should filter workflows by field: name', async () => {
|
||||
await createWorkflow({ name: 'First' }, owner);
|
||||
await createWorkflow({ name: 'Second' }, owner);
|
||||
|
||||
const response = await authOwnerAgent
|
||||
.get('/workflows')
|
||||
.query('filter={"name":"First"}')
|
||||
.expect(200);
|
||||
|
||||
expect(response.body).toEqual({
|
||||
count: 1,
|
||||
data: [objectContaining({ name: 'First' })],
|
||||
});
|
||||
});
|
||||
|
||||
test('should filter workflows by field: active', async () => {
|
||||
await createWorkflow({ active: true }, owner);
|
||||
await createWorkflow({ active: false }, owner);
|
||||
|
||||
const response = await authOwnerAgent
|
||||
.get('/workflows')
|
||||
.query('filter={ "active": true }')
|
||||
.expect(200);
|
||||
|
||||
expect(response.body).toEqual({
|
||||
count: 1,
|
||||
data: [objectContaining({ active: true })],
|
||||
});
|
||||
});
|
||||
|
||||
test('should filter workflows by field: tags', async () => {
|
||||
const workflow = await createWorkflow({ name: 'First' }, owner);
|
||||
|
||||
await createTag({ name: 'A' }, workflow);
|
||||
await createTag({ name: 'B' }, workflow);
|
||||
|
||||
const response = await authOwnerAgent
|
||||
.get('/workflows')
|
||||
.query('filter={ "tags": ["A"] }')
|
||||
.expect(200);
|
||||
|
||||
expect(response.body).toEqual({
|
||||
count: 1,
|
||||
data: [objectContaining({ name: 'First', tags: [{ id: any(String), name: 'A' }] })],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('select', () => {
|
||||
test('should select workflow field: name', async () => {
|
||||
await createWorkflow({ name: 'First' }, owner);
|
||||
await createWorkflow({ name: 'Second' }, owner);
|
||||
|
||||
const response = await authOwnerAgent.get('/workflows').query('select=["name"]').expect(200);
|
||||
|
||||
expect(response.body).toEqual({
|
||||
count: 2,
|
||||
data: arrayContaining([
|
||||
{ id: any(String), name: 'First' },
|
||||
{ id: any(String), name: 'Second' },
|
||||
]),
|
||||
});
|
||||
});
|
||||
|
||||
test('should select workflow field: active', async () => {
|
||||
await createWorkflow({ active: true }, owner);
|
||||
await createWorkflow({ active: false }, owner);
|
||||
|
||||
const response = await authOwnerAgent
|
||||
.get('/workflows')
|
||||
.query('select=["active"]')
|
||||
.expect(200);
|
||||
|
||||
expect(response.body).toEqual({
|
||||
count: 2,
|
||||
data: arrayContaining([
|
||||
{ id: any(String), active: true },
|
||||
{ id: any(String), active: false },
|
||||
]),
|
||||
});
|
||||
});
|
||||
|
||||
test('should select workflow field: tags', async () => {
|
||||
const firstWorkflow = await createWorkflow({ name: 'First' }, owner);
|
||||
const secondWorkflow = await createWorkflow({ name: 'Second' }, owner);
|
||||
|
||||
await createTag({ name: 'A' }, firstWorkflow);
|
||||
await createTag({ name: 'B' }, secondWorkflow);
|
||||
|
||||
const response = await authOwnerAgent.get('/workflows').query('select=["tags"]').expect(200);
|
||||
|
||||
expect(response.body).toEqual({
|
||||
count: 2,
|
||||
data: arrayContaining([
|
||||
objectContaining({ id: any(String), tags: [{ id: any(String), name: 'A' }] }),
|
||||
objectContaining({ id: any(String), tags: [{ id: any(String), name: 'B' }] }),
|
||||
]),
|
||||
});
|
||||
});
|
||||
|
||||
test('should select workflow fields: createdAt and updatedAt', async () => {
|
||||
const firstWorkflowCreatedAt = '2023-08-08T09:31:25.000Z';
|
||||
const firstWorkflowUpdatedAt = '2023-08-08T09:31:40.000Z';
|
||||
const secondWorkflowCreatedAt = '2023-07-07T09:31:25.000Z';
|
||||
const secondWorkflowUpdatedAt = '2023-07-07T09:31:40.000Z';
|
||||
|
||||
await createWorkflow(
|
||||
{
|
||||
createdAt: new Date(firstWorkflowCreatedAt),
|
||||
updatedAt: new Date(firstWorkflowUpdatedAt),
|
||||
},
|
||||
owner,
|
||||
);
|
||||
await createWorkflow(
|
||||
{
|
||||
createdAt: new Date(secondWorkflowCreatedAt),
|
||||
updatedAt: new Date(secondWorkflowUpdatedAt),
|
||||
},
|
||||
owner,
|
||||
);
|
||||
|
||||
const response = await authOwnerAgent
|
||||
.get('/workflows')
|
||||
.query('select=["createdAt", "updatedAt"]')
|
||||
.expect(200);
|
||||
|
||||
expect(response.body).toEqual({
|
||||
count: 2,
|
||||
data: arrayContaining([
|
||||
objectContaining({
|
||||
id: any(String),
|
||||
createdAt: firstWorkflowCreatedAt,
|
||||
updatedAt: firstWorkflowUpdatedAt,
|
||||
}),
|
||||
objectContaining({
|
||||
id: any(String),
|
||||
createdAt: secondWorkflowCreatedAt,
|
||||
updatedAt: secondWorkflowUpdatedAt,
|
||||
}),
|
||||
]),
|
||||
});
|
||||
});
|
||||
|
||||
test('should select workflow field: versionId', async () => {
|
||||
const firstWorkflowVersionId = 'e95ccdde-2b4e-4fd0-8834-220a2b5b4353';
|
||||
const secondWorkflowVersionId = 'd099b8dc-b1d8-4b2d-9b02-26f32c0ee785';
|
||||
|
||||
await createWorkflow({ versionId: firstWorkflowVersionId }, owner);
|
||||
await createWorkflow({ versionId: secondWorkflowVersionId }, owner);
|
||||
|
||||
const response = await authOwnerAgent
|
||||
.get('/workflows')
|
||||
.query('select=["versionId"]')
|
||||
.expect(200);
|
||||
|
||||
expect(response.body).toEqual({
|
||||
count: 2,
|
||||
data: arrayContaining([
|
||||
{ id: any(String), versionId: firstWorkflowVersionId },
|
||||
{ id: any(String), versionId: secondWorkflowVersionId },
|
||||
]),
|
||||
});
|
||||
});
|
||||
|
||||
test('should select workflow field: ownedBy', async () => {
|
||||
await createWorkflow({}, owner);
|
||||
await createWorkflow({}, owner);
|
||||
|
||||
const response = await authOwnerAgent
|
||||
.get('/workflows')
|
||||
.query('select=["ownedBy"]')
|
||||
.expect(200);
|
||||
|
||||
expect(response.body).toEqual({
|
||||
count: 2,
|
||||
data: arrayContaining([
|
||||
{
|
||||
id: any(String),
|
||||
ownedBy: {
|
||||
id: owner.id,
|
||||
email: any(String),
|
||||
firstName: any(String),
|
||||
lastName: any(String),
|
||||
},
|
||||
sharedWith: [],
|
||||
},
|
||||
{
|
||||
id: any(String),
|
||||
ownedBy: {
|
||||
id: owner.id,
|
||||
email: any(String),
|
||||
firstName: any(String),
|
||||
lastName: any(String),
|
||||
},
|
||||
sharedWith: [],
|
||||
},
|
||||
]),
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('PATCH /workflows/:id', () => {
|
||||
test('should create workflow history version when licensed', async () => {
|
||||
license.enable('feat:workflowHistory');
|
||||
const workflow = await createWorkflow({}, owner);
|
||||
const payload = {
|
||||
name: 'name updated',
|
||||
versionId: workflow.versionId,
|
||||
nodes: [
|
||||
{
|
||||
id: 'uuid-1234',
|
||||
parameters: {},
|
||||
name: 'Start',
|
||||
type: 'n8n-nodes-base.start',
|
||||
typeVersion: 1,
|
||||
position: [240, 300],
|
||||
},
|
||||
{
|
||||
id: 'uuid-1234',
|
||||
parameters: {},
|
||||
name: 'Cron',
|
||||
type: 'n8n-nodes-base.cron',
|
||||
typeVersion: 1,
|
||||
position: [400, 300],
|
||||
},
|
||||
],
|
||||
connections: {},
|
||||
staticData: '{"id":1}',
|
||||
settings: {
|
||||
saveExecutionProgress: false,
|
||||
saveManualExecutions: false,
|
||||
saveDataErrorExecution: 'all',
|
||||
saveDataSuccessExecution: 'all',
|
||||
executionTimeout: 3600,
|
||||
timezone: 'America/New_York',
|
||||
},
|
||||
};
|
||||
|
||||
const response = await authOwnerAgent.patch(`/workflows/${workflow.id}`).send(payload);
|
||||
|
||||
const {
|
||||
data: { id },
|
||||
} = response.body;
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
|
||||
expect(id).toBe(workflow.id);
|
||||
expect(
|
||||
await Container.get(WorkflowHistoryRepository).count({ where: { workflowId: id } }),
|
||||
).toBe(1);
|
||||
const historyVersion = await Container.get(WorkflowHistoryRepository).findOne({
|
||||
where: {
|
||||
workflowId: id,
|
||||
},
|
||||
});
|
||||
expect(historyVersion).not.toBeNull();
|
||||
expect(historyVersion!.connections).toEqual(payload.connections);
|
||||
expect(historyVersion!.nodes).toEqual(payload.nodes);
|
||||
});
|
||||
|
||||
test('should not create workflow history version when not licensed', async () => {
|
||||
license.disable('feat:workflowHistory');
|
||||
const workflow = await createWorkflow({}, owner);
|
||||
const payload = {
|
||||
name: 'name updated',
|
||||
versionId: workflow.versionId,
|
||||
nodes: [
|
||||
{
|
||||
id: 'uuid-1234',
|
||||
parameters: {},
|
||||
name: 'Start',
|
||||
type: 'n8n-nodes-base.start',
|
||||
typeVersion: 1,
|
||||
position: [240, 300],
|
||||
},
|
||||
{
|
||||
id: 'uuid-1234',
|
||||
parameters: {},
|
||||
name: 'Cron',
|
||||
type: 'n8n-nodes-base.cron',
|
||||
typeVersion: 1,
|
||||
position: [400, 300],
|
||||
},
|
||||
],
|
||||
connections: {},
|
||||
staticData: '{"id":1}',
|
||||
settings: {
|
||||
saveExecutionProgress: false,
|
||||
saveManualExecutions: false,
|
||||
saveDataErrorExecution: 'all',
|
||||
saveDataSuccessExecution: 'all',
|
||||
executionTimeout: 3600,
|
||||
timezone: 'America/New_York',
|
||||
},
|
||||
};
|
||||
|
||||
const response = await authOwnerAgent.patch(`/workflows/${workflow.id}`).send(payload);
|
||||
|
||||
const {
|
||||
data: { id },
|
||||
} = response.body;
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
|
||||
expect(id).toBe(workflow.id);
|
||||
expect(
|
||||
await Container.get(WorkflowHistoryRepository).count({ where: { workflowId: id } }),
|
||||
).toBe(0);
|
||||
});
|
||||
|
||||
test('should activate workflow without changing version ID', async () => {
|
||||
license.disable('feat:workflowHistory');
|
||||
const workflow = await createWorkflow({}, owner);
|
||||
const payload = {
|
||||
versionId: workflow.versionId,
|
||||
active: true,
|
||||
};
|
||||
|
||||
const response = await authOwnerAgent.patch(`/workflows/${workflow.id}`).send(payload);
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(activeWorkflowRunnerLike.add).toBeCalled();
|
||||
|
||||
const {
|
||||
data: { id, versionId, active },
|
||||
} = response.body;
|
||||
|
||||
expect(id).toBe(workflow.id);
|
||||
expect(versionId).toBe(workflow.versionId);
|
||||
expect(active).toBe(true);
|
||||
});
|
||||
|
||||
test('should deactivate workflow without changing version ID', async () => {
|
||||
license.disable('feat:workflowHistory');
|
||||
const workflow = await createWorkflow({ active: true }, owner);
|
||||
const payload = {
|
||||
versionId: workflow.versionId,
|
||||
active: false,
|
||||
};
|
||||
|
||||
const response = await authOwnerAgent.patch(`/workflows/${workflow.id}`).send(payload);
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(activeWorkflowRunnerLike.add).not.toBeCalled();
|
||||
expect(activeWorkflowRunnerLike.remove).toBeCalled();
|
||||
|
||||
const {
|
||||
data: { id, versionId, active },
|
||||
} = response.body;
|
||||
|
||||
expect(id).toBe(workflow.id);
|
||||
expect(versionId).toBe(workflow.versionId);
|
||||
expect(active).toBe(false);
|
||||
});
|
||||
|
||||
test('should update workflow meta', async () => {
|
||||
const workflow = await createWorkflow({}, owner);
|
||||
const payload = {
|
||||
...workflow,
|
||||
meta: {
|
||||
templateCredsSetupCompleted: true,
|
||||
},
|
||||
};
|
||||
|
||||
const response = await authOwnerAgent.patch(`/workflows/${workflow.id}`).send(payload);
|
||||
|
||||
const { data: updatedWorkflow } = response.body;
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
|
||||
expect(updatedWorkflow.id).toBe(workflow.id);
|
||||
expect(updatedWorkflow.meta).toEqual(payload.meta);
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST /workflows/run', () => {
|
||||
let sharingSpy: jest.SpyInstance;
|
||||
let tamperingSpy: jest.SpyInstance;
|
||||
let workflow: WorkflowEntity;
|
||||
|
||||
beforeAll(() => {
|
||||
const enterpriseWorkflowService = Container.get(EnterpriseWorkflowService);
|
||||
const workflowRepository = Container.get(WorkflowRepository);
|
||||
|
||||
sharingSpy = jest.spyOn(UserManagementHelpers, 'isSharingEnabled');
|
||||
tamperingSpy = jest.spyOn(enterpriseWorkflowService, 'preventTampering');
|
||||
workflow = workflowRepository.create({ id: uuid() });
|
||||
});
|
||||
|
||||
test('should prevent tampering if sharing is enabled', async () => {
|
||||
sharingSpy.mockReturnValue(true);
|
||||
|
||||
await authOwnerAgent.post('/workflows/run').send({ workflowData: workflow });
|
||||
|
||||
expect(tamperingSpy).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
test('should skip tampering prevention if sharing is disabled', async () => {
|
||||
sharingSpy.mockReturnValue(false);
|
||||
|
||||
await authOwnerAgent.post('/workflows/run').send({ workflowData: workflow });
|
||||
|
||||
expect(tamperingSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user