mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-24 20:24:05 -08:00
93cfabbeac
Github issue / Community forum post (link here to close automatically):
578 lines
16 KiB
TypeScript
578 lines
16 KiB
TypeScript
import type { SuperAgentTest } from 'supertest';
|
|
import type { INode, IPinData } from 'n8n-workflow';
|
|
import * as UserManagementHelpers from '@/UserManagement/UserManagementHelper';
|
|
|
|
import * as utils from './shared/utils/';
|
|
import * as testDb from './shared/testDb';
|
|
import { makeWorkflow, MOCK_PINDATA } from './shared/utils/';
|
|
import type { User } from '@/databases/entities/User';
|
|
import { randomCredentialPayload } from './shared/random';
|
|
import { v4 as uuid } from 'uuid';
|
|
import { RoleService } from '@/services/role.service';
|
|
import Container from 'typedi';
|
|
import type { ListQuery } from '@/requests';
|
|
import { License } from '@/License';
|
|
import { WorkflowHistoryRepository } from '@/databases/repositories';
|
|
import { ActiveWorkflowRunner } from '@/ActiveWorkflowRunner';
|
|
|
|
let owner: User;
|
|
let authOwnerAgent: SuperAgentTest;
|
|
|
|
jest.spyOn(UserManagementHelpers, 'isSharingEnabled').mockReturnValue(false);
|
|
const testServer = utils.setupTestServer({ endpointGroups: ['workflows'] });
|
|
|
|
const { objectContaining, arrayContaining, any } = expect;
|
|
|
|
const licenseLike = utils.mockInstance(License, {
|
|
isWorkflowHistoryLicensed: jest.fn().mockReturnValue(false),
|
|
isWithinUsersLimit: jest.fn().mockReturnValue(true),
|
|
});
|
|
|
|
const activeWorkflowRunnerLike = utils.mockInstance(ActiveWorkflowRunner);
|
|
|
|
beforeAll(async () => {
|
|
owner = await testDb.createOwner();
|
|
authOwnerAgent = testServer.authAgentFor(owner);
|
|
});
|
|
|
|
beforeEach(async () => {
|
|
jest.resetAllMocks();
|
|
await testDb.truncate(['Workflow', 'SharedWorkflow', 'Tag', WorkflowHistoryRepository]);
|
|
licenseLike.isWorkflowHistoryLicensed.mockReturnValue(false);
|
|
});
|
|
|
|
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 () => {
|
|
licenseLike.isWorkflowHistoryLicensed.mockReturnValue(true);
|
|
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 () => {
|
|
licenseLike.isWorkflowHistoryLicensed.mockReturnValue(false);
|
|
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 testDb.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 testDb.createTag({ name: 'A' });
|
|
|
|
await testDb.createWorkflow({ name: 'First', nodes, tags: [tag] }, owner);
|
|
await testDb.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 },
|
|
}),
|
|
objectContaining({
|
|
id: any(String),
|
|
name: 'Second',
|
|
active: any(Boolean),
|
|
createdAt: any(String),
|
|
updatedAt: any(String),
|
|
tags: [],
|
|
versionId: any(String),
|
|
ownedBy: { id: owner.id },
|
|
}),
|
|
]),
|
|
});
|
|
|
|
const found = response.body.data.find(
|
|
(w: ListQuery.Workflow.WithOwnership) => w.name === 'First',
|
|
);
|
|
|
|
expect(found.nodes).toBeUndefined();
|
|
expect(found.sharedWith).toBeUndefined();
|
|
expect(found.usedCredentials).toBeUndefined();
|
|
});
|
|
|
|
describe('filter', () => {
|
|
test('should filter workflows by field: name', async () => {
|
|
await testDb.createWorkflow({ name: 'First' }, owner);
|
|
await testDb.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 testDb.createWorkflow({ active: true }, owner);
|
|
await testDb.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 testDb.createWorkflow({ name: 'First' }, owner);
|
|
|
|
await testDb.createTag({ name: 'A' }, workflow);
|
|
await testDb.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 testDb.createWorkflow({ name: 'First' }, owner);
|
|
await testDb.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 testDb.createWorkflow({ active: true }, owner);
|
|
await testDb.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 testDb.createWorkflow({ name: 'First' }, owner);
|
|
const secondWorkflow = await testDb.createWorkflow({ name: 'Second' }, owner);
|
|
|
|
await testDb.createTag({ name: 'A' }, firstWorkflow);
|
|
await testDb.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 testDb.createWorkflow(
|
|
{
|
|
createdAt: new Date(firstWorkflowCreatedAt),
|
|
updatedAt: new Date(firstWorkflowUpdatedAt),
|
|
},
|
|
owner,
|
|
);
|
|
await testDb.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 testDb.createWorkflow({ versionId: firstWorkflowVersionId }, owner);
|
|
await testDb.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 testDb.createWorkflow({}, owner);
|
|
await testDb.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 } },
|
|
{ id: any(String), ownedBy: { id: owner.id } },
|
|
]),
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('PATCH /workflows/:id', () => {
|
|
test('should create workflow history version when licensed', async () => {
|
|
licenseLike.isWorkflowHistoryLicensed.mockReturnValue(true);
|
|
const workflow = await testDb.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 () => {
|
|
licenseLike.isWorkflowHistoryLicensed.mockReturnValue(false);
|
|
const workflow = await testDb.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 () => {
|
|
licenseLike.isWorkflowHistoryLicensed.mockReturnValue(false);
|
|
const workflow = await testDb.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 () => {
|
|
licenseLike.isWorkflowHistoryLicensed.mockReturnValue(false);
|
|
const workflow = await testDb.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);
|
|
});
|
|
});
|