2023-11-08 07:29:39 -08:00
|
|
|
import Container from 'typedi';
|
2023-03-17 09:24:05 -07:00
|
|
|
import type { SuperAgentTest } from 'supertest';
|
2022-11-09 06:25:00 -08:00
|
|
|
import { v4 as uuid } from 'uuid';
|
2023-03-17 09:24:05 -07:00
|
|
|
import type { INode } from 'n8n-workflow';
|
|
|
|
|
|
|
|
import * as UserManagementHelpers from '@/UserManagement/UserManagementHelper';
|
|
|
|
import type { User } from '@db/entities/User';
|
2023-07-13 01:14:48 -07:00
|
|
|
import { getSharedWorkflowIds } from '@/WorkflowHelpers';
|
2023-11-08 07:29:39 -08:00
|
|
|
import { License } from '@/License';
|
|
|
|
import { WorkflowHistoryRepository } from '@/databases/repositories';
|
|
|
|
import { ActiveWorkflowRunner } from '@/ActiveWorkflowRunner';
|
2022-10-11 05:55:05 -07:00
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
import * as utils from './shared/utils/';
|
2022-10-11 05:55:05 -07:00
|
|
|
import * as testDb from './shared/testDb';
|
2023-03-17 09:24:05 -07:00
|
|
|
import type { SaveCredentialFunction } from './shared/types';
|
2023-07-13 01:14:48 -07:00
|
|
|
import { makeWorkflow } from './shared/utils/';
|
2022-10-13 02:55:58 -07:00
|
|
|
import { randomCredentialPayload } from './shared/random';
|
2023-11-08 07:29:39 -08:00
|
|
|
import { affixRoleToSaveCredential, shareCredentialWithUsers } from './shared/db/credentials';
|
|
|
|
import { getCredentialOwnerRole, getGlobalMemberRole, getGlobalOwnerRole } from './shared/db/roles';
|
|
|
|
import { createUser } from './shared/db/users';
|
|
|
|
import { createWorkflow, getWorkflowSharing, shareWorkflowWithUsers } from './shared/db/workflows';
|
2022-10-11 05:55:05 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
let owner: User;
|
|
|
|
let member: User;
|
|
|
|
let anotherMember: User;
|
|
|
|
let authOwnerAgent: SuperAgentTest;
|
|
|
|
let authMemberAgent: SuperAgentTest;
|
|
|
|
let authAnotherMemberAgent: SuperAgentTest;
|
2022-10-13 02:55:58 -07:00
|
|
|
let saveCredential: SaveCredentialFunction;
|
2022-10-11 05:55:05 -07:00
|
|
|
|
2023-10-25 03:07:11 -07:00
|
|
|
const licenseLike = utils.mockInstance(License, {
|
|
|
|
isWorkflowHistoryLicensed: jest.fn().mockReturnValue(false),
|
|
|
|
isWithinUsersLimit: jest.fn().mockReturnValue(true),
|
|
|
|
});
|
|
|
|
const activeWorkflowRunnerLike = utils.mockInstance(ActiveWorkflowRunner);
|
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
const sharingSpy = jest.spyOn(UserManagementHelpers, 'isSharingEnabled').mockReturnValue(true);
|
|
|
|
const testServer = utils.setupTestServer({
|
|
|
|
endpointGroups: ['workflows'],
|
|
|
|
enabledFeatures: ['feat:sharing'],
|
|
|
|
});
|
2022-10-11 05:55:05 -07:00
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
beforeAll(async () => {
|
2023-11-08 07:29:39 -08:00
|
|
|
const globalOwnerRole = await getGlobalOwnerRole();
|
|
|
|
const globalMemberRole = await getGlobalMemberRole();
|
|
|
|
const credentialOwnerRole = await getCredentialOwnerRole();
|
2022-10-13 02:55:58 -07:00
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
owner = await createUser({ globalRole: globalOwnerRole });
|
|
|
|
member = await createUser({ globalRole: globalMemberRole });
|
|
|
|
anotherMember = await createUser({ globalRole: globalMemberRole });
|
2022-10-11 05:55:05 -07:00
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
authOwnerAgent = testServer.authAgentFor(owner);
|
|
|
|
authMemberAgent = testServer.authAgentFor(member);
|
|
|
|
authAnotherMemberAgent = testServer.authAgentFor(anotherMember);
|
2022-10-11 05:55:05 -07:00
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
saveCredential = affixRoleToSaveCredential(credentialOwnerRole);
|
2022-11-11 02:14:45 -08:00
|
|
|
|
2022-10-31 02:35:24 -07:00
|
|
|
await utils.initNodeTypes();
|
2022-10-11 05:55:05 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
2023-10-25 03:07:11 -07:00
|
|
|
activeWorkflowRunnerLike.add.mockReset();
|
|
|
|
activeWorkflowRunnerLike.remove.mockReset();
|
|
|
|
|
|
|
|
await testDb.truncate(['Workflow', 'SharedWorkflow', WorkflowHistoryRepository]);
|
|
|
|
licenseLike.isWorkflowHistoryLicensed.mockReturnValue(false);
|
2022-10-11 05:55:05 -07:00
|
|
|
});
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
describe('router should switch based on flag', () => {
|
|
|
|
let savedWorkflowId: string;
|
2022-11-11 02:14:45 -08:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
beforeEach(async () => {
|
|
|
|
const createWorkflowResponse = await authOwnerAgent.post('/workflows').send(makeWorkflow());
|
|
|
|
savedWorkflowId = createWorkflowResponse.body.data.id;
|
|
|
|
});
|
2022-11-11 02:14:45 -08:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
test('when sharing is disabled', async () => {
|
|
|
|
sharingSpy.mockReturnValueOnce(false);
|
2022-11-11 02:14:45 -08:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
await authOwnerAgent
|
|
|
|
.put(`/workflows/${savedWorkflowId}/share`)
|
|
|
|
.send({ shareWithIds: [member.id] })
|
|
|
|
.expect(404);
|
|
|
|
});
|
2022-11-11 02:14:45 -08:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
test('when sharing is enabled', async () => {
|
|
|
|
await authOwnerAgent
|
|
|
|
.put(`/workflows/${savedWorkflowId}/share`)
|
|
|
|
.send({ shareWithIds: [member.id] })
|
|
|
|
.expect(200);
|
|
|
|
});
|
2022-11-11 02:14:45 -08:00
|
|
|
});
|
|
|
|
|
2022-10-11 07:40:39 -07:00
|
|
|
describe('PUT /workflows/:id', () => {
|
|
|
|
test('PUT /workflows/:id/share should save sharing with new users', async () => {
|
|
|
|
const workflow = await createWorkflow({}, owner);
|
2022-10-11 05:55:05 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const response = await authOwnerAgent
|
2022-10-11 07:40:39 -07:00
|
|
|
.put(`/workflows/${workflow.id}/share`)
|
|
|
|
.send({ shareWithIds: [member.id] });
|
2022-10-11 05:55:05 -07:00
|
|
|
|
2022-10-11 07:40:39 -07:00
|
|
|
expect(response.statusCode).toBe(200);
|
2022-10-11 05:55:05 -07:00
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
const sharedWorkflows = await getWorkflowSharing(workflow);
|
2022-10-11 07:40:39 -07:00
|
|
|
expect(sharedWorkflows).toHaveLength(2);
|
|
|
|
});
|
2022-10-11 05:55:05 -07:00
|
|
|
|
2022-10-26 06:49:43 -07:00
|
|
|
test('PUT /workflows/:id/share should succeed when sharing with invalid user-id', async () => {
|
2022-10-11 07:40:39 -07:00
|
|
|
const workflow = await createWorkflow({}, owner);
|
2022-10-11 05:55:05 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const response = await authOwnerAgent
|
2022-10-11 07:40:39 -07:00
|
|
|
.put(`/workflows/${workflow.id}/share`)
|
|
|
|
.send({ shareWithIds: [uuid()] });
|
2022-10-11 05:55:05 -07:00
|
|
|
|
2022-10-11 07:40:39 -07:00
|
|
|
expect(response.statusCode).toBe(200);
|
2022-10-11 05:55:05 -07:00
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
const sharedWorkflows = await getWorkflowSharing(workflow);
|
2022-10-11 07:40:39 -07:00
|
|
|
expect(sharedWorkflows).toHaveLength(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('PUT /workflows/:id/share should allow sharing with multiple users', async () => {
|
|
|
|
const workflow = await createWorkflow({}, owner);
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const response = await authOwnerAgent
|
2022-10-11 07:40:39 -07:00
|
|
|
.put(`/workflows/${workflow.id}/share`)
|
|
|
|
.send({ shareWithIds: [member.id, anotherMember.id] });
|
|
|
|
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
const sharedWorkflows = await getWorkflowSharing(workflow);
|
2022-10-11 07:40:39 -07:00
|
|
|
expect(sharedWorkflows).toHaveLength(3);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('PUT /workflows/:id/share should override sharing', async () => {
|
|
|
|
const workflow = await createWorkflow({}, owner);
|
2022-10-11 05:55:05 -07:00
|
|
|
|
2022-10-11 07:40:39 -07:00
|
|
|
const response = await authOwnerAgent
|
|
|
|
.put(`/workflows/${workflow.id}/share`)
|
|
|
|
.send({ shareWithIds: [member.id, anotherMember.id] });
|
2022-10-11 05:55:05 -07:00
|
|
|
|
2022-10-11 07:40:39 -07:00
|
|
|
expect(response.statusCode).toBe(200);
|
2022-10-11 05:55:05 -07:00
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
const sharedWorkflows = await getWorkflowSharing(workflow);
|
2022-10-11 07:40:39 -07:00
|
|
|
expect(sharedWorkflows).toHaveLength(3);
|
|
|
|
|
|
|
|
const secondResponse = await authOwnerAgent
|
|
|
|
.put(`/workflows/${workflow.id}/share`)
|
|
|
|
.send({ shareWithIds: [member.id] });
|
|
|
|
expect(secondResponse.statusCode).toBe(200);
|
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
const secondSharedWorkflows = await getWorkflowSharing(workflow);
|
2022-10-11 07:40:39 -07:00
|
|
|
expect(secondSharedWorkflows).toHaveLength(2);
|
|
|
|
});
|
2022-10-11 05:55:05 -07:00
|
|
|
});
|
|
|
|
|
2023-06-29 23:51:39 -07:00
|
|
|
describe('GET /workflows/new', () => {
|
|
|
|
[true, false].forEach((sharingEnabled) => {
|
|
|
|
test(`should return an auto-incremented name, even when sharing is ${
|
|
|
|
sharingEnabled ? 'enabled' : 'disabled'
|
|
|
|
}`, async () => {
|
|
|
|
sharingSpy.mockReturnValueOnce(sharingEnabled);
|
|
|
|
|
|
|
|
await createWorkflow({ name: 'My workflow' }, owner);
|
|
|
|
await createWorkflow({ name: 'My workflow 7' }, owner);
|
|
|
|
|
|
|
|
const response = await authOwnerAgent.get('/workflows/new');
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
expect(response.body.data.name).toEqual('My workflow 8');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-10-11 07:40:39 -07:00
|
|
|
describe('GET /workflows/:id', () => {
|
2022-10-13 02:55:58 -07:00
|
|
|
test('GET should fail with invalid id due to route rule', async () => {
|
2023-03-17 09:24:05 -07:00
|
|
|
const response = await authOwnerAgent.get('/workflows/potatoes');
|
2022-10-11 05:55:05 -07:00
|
|
|
|
2022-10-13 02:55:58 -07:00
|
|
|
expect(response.statusCode).toBe(404);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('GET should return 404 for non existing workflow', async () => {
|
2023-03-17 09:24:05 -07:00
|
|
|
const response = await authOwnerAgent.get('/workflows/9001');
|
2022-10-13 02:55:58 -07:00
|
|
|
|
|
|
|
expect(response.statusCode).toBe(404);
|
2022-10-11 07:40:39 -07:00
|
|
|
});
|
2022-10-11 05:55:05 -07:00
|
|
|
|
2022-10-11 07:40:39 -07:00
|
|
|
test('GET should return a workflow with owner', async () => {
|
|
|
|
const workflow = await createWorkflow({}, owner);
|
2022-10-11 05:55:05 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const response = await authOwnerAgent.get(`/workflows/${workflow.id}`);
|
2022-10-11 05:55:05 -07:00
|
|
|
|
2022-10-11 07:40:39 -07:00
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
expect(response.body.data.ownedBy).toMatchObject({
|
|
|
|
id: owner.id,
|
|
|
|
email: owner.email,
|
|
|
|
firstName: owner.firstName,
|
|
|
|
lastName: owner.lastName,
|
|
|
|
});
|
2022-10-11 05:55:05 -07:00
|
|
|
|
2022-10-11 07:40:39 -07:00
|
|
|
expect(response.body.data.sharedWith).toHaveLength(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('GET should return shared workflow with user data', async () => {
|
|
|
|
const workflow = await createWorkflow({}, owner);
|
2023-11-08 07:29:39 -08:00
|
|
|
await shareWorkflowWithUsers(workflow, [member]);
|
2022-10-11 07:40:39 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const response = await authOwnerAgent.get(`/workflows/${workflow.id}`);
|
2022-10-11 07:40:39 -07:00
|
|
|
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
expect(response.body.data.ownedBy).toMatchObject({
|
|
|
|
id: owner.id,
|
|
|
|
email: owner.email,
|
|
|
|
firstName: owner.firstName,
|
|
|
|
lastName: owner.lastName,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(response.body.data.sharedWith).toHaveLength(1);
|
|
|
|
expect(response.body.data.sharedWith[0]).toMatchObject({
|
|
|
|
id: member.id,
|
|
|
|
email: member.email,
|
|
|
|
firstName: member.firstName,
|
|
|
|
lastName: member.lastName,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('GET should return all sharees', async () => {
|
|
|
|
const workflow = await createWorkflow({}, owner);
|
2023-11-08 07:29:39 -08:00
|
|
|
await shareWorkflowWithUsers(workflow, [member, anotherMember]);
|
2022-10-11 07:40:39 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const response = await authOwnerAgent.get(`/workflows/${workflow.id}`);
|
2022-10-11 07:40:39 -07:00
|
|
|
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
expect(response.body.data.ownedBy).toMatchObject({
|
|
|
|
id: owner.id,
|
|
|
|
email: owner.email,
|
|
|
|
firstName: owner.firstName,
|
|
|
|
lastName: owner.lastName,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(response.body.data.sharedWith).toHaveLength(2);
|
|
|
|
});
|
2022-10-31 07:08:25 -07:00
|
|
|
|
|
|
|
test('GET should return workflow with credentials owned by user', async () => {
|
|
|
|
const savedCredential = await saveCredential(randomCredentialPayload(), { user: owner });
|
|
|
|
|
|
|
|
const workflowPayload = makeWorkflow({
|
|
|
|
withPinData: false,
|
2023-01-02 08:42:32 -08:00
|
|
|
withCredential: { id: savedCredential.id, name: savedCredential.name },
|
2022-10-31 07:08:25 -07:00
|
|
|
});
|
|
|
|
const workflow = await createWorkflow(workflowPayload, owner);
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const response = await authOwnerAgent.get(`/workflows/${workflow.id}`);
|
2022-10-31 07:08:25 -07:00
|
|
|
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
expect(response.body.data.usedCredentials).toMatchObject([
|
|
|
|
{
|
2023-01-02 08:42:32 -08:00
|
|
|
id: savedCredential.id,
|
2022-10-31 07:08:25 -07:00
|
|
|
name: savedCredential.name,
|
|
|
|
currentUserHasAccess: true,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(response.body.data.sharedWith).toHaveLength(0);
|
|
|
|
});
|
|
|
|
|
2022-11-29 06:54:24 -08:00
|
|
|
test('GET should return workflow with credentials saying owner does not have access when not shared', async () => {
|
2022-10-31 07:08:25 -07:00
|
|
|
const savedCredential = await saveCredential(randomCredentialPayload(), { user: member });
|
|
|
|
|
|
|
|
const workflowPayload = makeWorkflow({
|
|
|
|
withPinData: false,
|
2023-01-02 08:42:32 -08:00
|
|
|
withCredential: { id: savedCredential.id, name: savedCredential.name },
|
2022-10-31 07:08:25 -07:00
|
|
|
});
|
|
|
|
const workflow = await createWorkflow(workflowPayload, owner);
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const response = await authOwnerAgent.get(`/workflows/${workflow.id}`);
|
2022-10-31 07:08:25 -07:00
|
|
|
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
expect(response.body.data.usedCredentials).toMatchObject([
|
|
|
|
{
|
2023-01-02 08:42:32 -08:00
|
|
|
id: savedCredential.id,
|
2022-10-31 07:08:25 -07:00
|
|
|
name: savedCredential.name,
|
2023-04-19 07:00:49 -07:00
|
|
|
currentUserHasAccess: false, // although owner can see, they do not have access
|
2022-10-31 07:08:25 -07:00
|
|
|
},
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(response.body.data.sharedWith).toHaveLength(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('GET should return workflow with credentials for all users with or without access', async () => {
|
2023-03-17 09:24:05 -07:00
|
|
|
const savedCredential = await saveCredential(randomCredentialPayload(), { user: member });
|
2022-10-31 07:08:25 -07:00
|
|
|
|
|
|
|
const workflowPayload = makeWorkflow({
|
|
|
|
withPinData: false,
|
2023-01-02 08:42:32 -08:00
|
|
|
withCredential: { id: savedCredential.id, name: savedCredential.name },
|
2022-10-31 07:08:25 -07:00
|
|
|
});
|
2023-03-17 09:24:05 -07:00
|
|
|
const workflow = await createWorkflow(workflowPayload, member);
|
2023-11-08 07:29:39 -08:00
|
|
|
await shareWorkflowWithUsers(workflow, [anotherMember]);
|
2022-10-31 07:08:25 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const responseMember1 = await authMemberAgent.get(`/workflows/${workflow.id}`);
|
2022-10-31 07:08:25 -07:00
|
|
|
expect(responseMember1.statusCode).toBe(200);
|
|
|
|
expect(responseMember1.body.data.usedCredentials).toMatchObject([
|
|
|
|
{
|
2023-01-02 08:42:32 -08:00
|
|
|
id: savedCredential.id,
|
2022-10-31 07:08:25 -07:00
|
|
|
name: savedCredential.name,
|
|
|
|
currentUserHasAccess: true, // one user has access
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
expect(responseMember1.body.data.sharedWith).toHaveLength(1);
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const responseMember2 = await authAnotherMemberAgent.get(`/workflows/${workflow.id}`);
|
2022-10-31 07:08:25 -07:00
|
|
|
expect(responseMember2.statusCode).toBe(200);
|
|
|
|
expect(responseMember2.body.data.usedCredentials).toMatchObject([
|
|
|
|
{
|
2023-01-02 08:42:32 -08:00
|
|
|
id: savedCredential.id,
|
2022-10-31 07:08:25 -07:00
|
|
|
name: savedCredential.name,
|
|
|
|
currentUserHasAccess: false, // the other one doesn't
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
expect(responseMember2.body.data.sharedWith).toHaveLength(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('GET should return workflow with credentials for all users with access', async () => {
|
2023-03-17 09:24:05 -07:00
|
|
|
const savedCredential = await saveCredential(randomCredentialPayload(), { user: member });
|
2022-10-31 07:08:25 -07:00
|
|
|
// Both users have access to the credential (none is owner)
|
2023-11-08 07:29:39 -08:00
|
|
|
await shareCredentialWithUsers(savedCredential, [anotherMember]);
|
2022-10-31 07:08:25 -07:00
|
|
|
|
|
|
|
const workflowPayload = makeWorkflow({
|
|
|
|
withPinData: false,
|
2023-01-02 08:42:32 -08:00
|
|
|
withCredential: { id: savedCredential.id, name: savedCredential.name },
|
2022-10-31 07:08:25 -07:00
|
|
|
});
|
2023-03-17 09:24:05 -07:00
|
|
|
const workflow = await createWorkflow(workflowPayload, member);
|
2023-11-08 07:29:39 -08:00
|
|
|
await shareWorkflowWithUsers(workflow, [anotherMember]);
|
2022-10-31 07:08:25 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const responseMember1 = await authMemberAgent.get(`/workflows/${workflow.id}`);
|
2022-10-31 07:08:25 -07:00
|
|
|
expect(responseMember1.statusCode).toBe(200);
|
|
|
|
expect(responseMember1.body.data.usedCredentials).toMatchObject([
|
|
|
|
{
|
2023-01-02 08:42:32 -08:00
|
|
|
id: savedCredential.id,
|
2022-10-31 07:08:25 -07:00
|
|
|
name: savedCredential.name,
|
|
|
|
currentUserHasAccess: true,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
expect(responseMember1.body.data.sharedWith).toHaveLength(1);
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const responseMember2 = await authAnotherMemberAgent.get(`/workflows/${workflow.id}`);
|
2022-10-31 07:08:25 -07:00
|
|
|
expect(responseMember2.statusCode).toBe(200);
|
|
|
|
expect(responseMember2.body.data.usedCredentials).toMatchObject([
|
|
|
|
{
|
2023-01-02 08:42:32 -08:00
|
|
|
id: savedCredential.id,
|
2022-10-31 07:08:25 -07:00
|
|
|
name: savedCredential.name,
|
|
|
|
currentUserHasAccess: true,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
expect(responseMember2.body.data.sharedWith).toHaveLength(1);
|
|
|
|
});
|
2022-10-11 05:55:05 -07:00
|
|
|
});
|
2022-10-13 02:55:58 -07:00
|
|
|
|
|
|
|
describe('POST /workflows', () => {
|
|
|
|
it('Should create a workflow that uses no credential', async () => {
|
|
|
|
const workflow = makeWorkflow({ withPinData: false });
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const response = await authOwnerAgent.post('/workflows').send(workflow);
|
2022-10-13 02:55:58 -07:00
|
|
|
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
});
|
|
|
|
|
2022-10-26 06:49:43 -07:00
|
|
|
it('Should save a new workflow with credentials', async () => {
|
2022-10-13 02:55:58 -07:00
|
|
|
const savedCredential = await saveCredential(randomCredentialPayload(), { user: owner });
|
|
|
|
const workflow = makeWorkflow({
|
|
|
|
withPinData: false,
|
2023-01-02 08:42:32 -08:00
|
|
|
withCredential: { id: savedCredential.id, name: savedCredential.name },
|
2022-10-13 02:55:58 -07:00
|
|
|
});
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const response = await authOwnerAgent.post('/workflows').send(workflow);
|
2022-10-13 02:55:58 -07:00
|
|
|
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Should not allow saving a workflow using credential you have no access', async () => {
|
|
|
|
// Credential belongs to owner, member cannot use it.
|
|
|
|
const savedCredential = await saveCredential(randomCredentialPayload(), { user: owner });
|
|
|
|
const workflow = makeWorkflow({
|
|
|
|
withPinData: false,
|
2023-01-02 08:42:32 -08:00
|
|
|
withCredential: { id: savedCredential.id, name: savedCredential.name },
|
2022-10-13 02:55:58 -07:00
|
|
|
});
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const response = await authMemberAgent.post('/workflows').send(workflow);
|
2022-10-13 02:55:58 -07:00
|
|
|
|
|
|
|
expect(response.statusCode).toBe(400);
|
|
|
|
expect(response.body.message).toBe(
|
2022-11-22 04:05:51 -08:00
|
|
|
'The workflow you are trying to save contains credentials that are not shared with you',
|
2022-10-13 02:55:58 -07:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Should allow owner to save a workflow using credential owned by others', async () => {
|
|
|
|
// Credential belongs to owner, member cannot use it.
|
|
|
|
const savedCredential = await saveCredential(randomCredentialPayload(), { user: member });
|
|
|
|
const workflow = makeWorkflow({
|
|
|
|
withPinData: false,
|
2023-01-02 08:42:32 -08:00
|
|
|
withCredential: { id: savedCredential.id, name: savedCredential.name },
|
2022-10-13 02:55:58 -07:00
|
|
|
});
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const response = await authOwnerAgent.post('/workflows').send(workflow);
|
2022-10-13 02:55:58 -07:00
|
|
|
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Should allow saving a workflow using a credential owned by others and shared with you', async () => {
|
2023-03-17 09:24:05 -07:00
|
|
|
const savedCredential = await saveCredential(randomCredentialPayload(), { user: member });
|
2023-11-08 07:29:39 -08:00
|
|
|
await shareCredentialWithUsers(savedCredential, [anotherMember]);
|
2022-10-13 02:55:58 -07:00
|
|
|
|
|
|
|
const workflow = makeWorkflow({
|
|
|
|
withPinData: false,
|
2023-01-02 08:42:32 -08:00
|
|
|
withCredential: { id: savedCredential.id, name: savedCredential.name },
|
2022-10-13 02:55:58 -07:00
|
|
|
});
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const response = await authAnotherMemberAgent.post('/workflows').send(workflow);
|
2022-10-13 02:55:58 -07:00
|
|
|
expect(response.statusCode).toBe(200);
|
2022-10-26 06:49:43 -07:00
|
|
|
});
|
2023-10-25 03:07:11 -07:00
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
2022-10-26 06:49:43 -07:00
|
|
|
});
|
|
|
|
|
2022-10-31 02:35:24 -07:00
|
|
|
describe('PATCH /workflows/:id - validate credential permissions to user', () => {
|
2022-10-26 06:49:43 -07:00
|
|
|
it('Should succeed when saving unchanged workflow nodes', async () => {
|
|
|
|
const savedCredential = await saveCredential(randomCredentialPayload(), { user: owner });
|
2022-10-31 02:35:24 -07:00
|
|
|
const workflow = {
|
|
|
|
name: 'test',
|
|
|
|
active: false,
|
|
|
|
connections: {},
|
|
|
|
nodes: [
|
|
|
|
{
|
|
|
|
id: 'uuid-1234',
|
|
|
|
name: 'Start',
|
|
|
|
parameters: {},
|
|
|
|
position: [-20, 260],
|
|
|
|
type: 'n8n-nodes-base.start',
|
|
|
|
typeVersion: 1,
|
|
|
|
credentials: {
|
|
|
|
default: {
|
2023-01-02 08:42:32 -08:00
|
|
|
id: savedCredential.id,
|
2022-10-31 02:35:24 -07:00
|
|
|
name: savedCredential.name,
|
2022-10-26 06:49:43 -07:00
|
|
|
},
|
|
|
|
},
|
2022-10-31 02:35:24 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const createResponse = await authOwnerAgent.post('/workflows').send(workflow);
|
2022-12-06 00:25:39 -08:00
|
|
|
const { id, versionId } = createResponse.body.data;
|
2022-10-26 06:49:43 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const response = await authOwnerAgent.patch(`/workflows/${id}`).send({
|
2022-10-26 06:49:43 -07:00
|
|
|
name: 'new name',
|
2022-12-06 00:25:39 -08:00
|
|
|
versionId,
|
2022-10-26 06:49:43 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Should allow owner to add node containing credential not shared with the owner', async () => {
|
|
|
|
const savedCredential = await saveCredential(randomCredentialPayload(), { user: member });
|
2022-10-31 02:35:24 -07:00
|
|
|
const workflow = {
|
|
|
|
name: 'test',
|
|
|
|
active: false,
|
|
|
|
connections: {},
|
|
|
|
nodes: [
|
|
|
|
{
|
|
|
|
id: 'uuid-1234',
|
|
|
|
name: 'Start',
|
|
|
|
parameters: {},
|
|
|
|
position: [-20, 260],
|
|
|
|
type: 'n8n-nodes-base.start',
|
|
|
|
typeVersion: 1,
|
|
|
|
credentials: {
|
|
|
|
default: {
|
2023-01-02 08:42:32 -08:00
|
|
|
id: savedCredential.id,
|
2022-10-31 02:35:24 -07:00
|
|
|
name: savedCredential.name,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const createResponse = await authOwnerAgent.post('/workflows').send(workflow);
|
2022-12-06 00:25:39 -08:00
|
|
|
const { id, versionId } = createResponse.body.data;
|
2022-10-26 06:49:43 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const response = await authOwnerAgent.patch(`/workflows/${id}`).send({
|
|
|
|
versionId,
|
|
|
|
nodes: [
|
|
|
|
{
|
|
|
|
id: 'uuid-1234',
|
|
|
|
name: 'Start',
|
|
|
|
parameters: {},
|
|
|
|
position: [-20, 260],
|
|
|
|
type: 'n8n-nodes-base.start',
|
|
|
|
typeVersion: 1,
|
|
|
|
credentials: {
|
|
|
|
default: {
|
|
|
|
id: savedCredential.id,
|
|
|
|
name: savedCredential.name,
|
2022-10-26 06:49:43 -07:00
|
|
|
},
|
|
|
|
},
|
2023-03-17 09:24:05 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
2022-10-26 06:49:43 -07:00
|
|
|
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Should prevent member from adding node containing credential inaccessible to member', async () => {
|
|
|
|
const savedCredential = await saveCredential(randomCredentialPayload(), { user: owner });
|
2022-10-31 02:35:24 -07:00
|
|
|
|
|
|
|
const workflow = {
|
|
|
|
name: 'test',
|
|
|
|
active: false,
|
|
|
|
connections: {},
|
|
|
|
nodes: [
|
|
|
|
{
|
|
|
|
id: 'uuid-1234',
|
|
|
|
name: 'Start',
|
|
|
|
parameters: {},
|
|
|
|
position: [-20, 260],
|
|
|
|
type: 'n8n-nodes-base.start',
|
|
|
|
typeVersion: 1,
|
|
|
|
credentials: {
|
|
|
|
default: {
|
2023-01-02 08:42:32 -08:00
|
|
|
id: savedCredential.id,
|
2022-10-31 02:35:24 -07:00
|
|
|
name: savedCredential.name,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const createResponse = await authOwnerAgent.post('/workflows').send(workflow);
|
2022-12-06 00:25:39 -08:00
|
|
|
const { id, versionId } = createResponse.body.data;
|
2022-10-26 06:49:43 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const response = await authMemberAgent.patch(`/workflows/${id}`).send({
|
|
|
|
versionId,
|
|
|
|
nodes: [
|
|
|
|
{
|
|
|
|
id: 'uuid-1234',
|
|
|
|
name: 'Start',
|
|
|
|
parameters: {},
|
|
|
|
position: [-20, 260],
|
|
|
|
type: 'n8n-nodes-base.start',
|
|
|
|
typeVersion: 1,
|
|
|
|
credentials: {},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'uuid-12345',
|
|
|
|
name: 'Start',
|
|
|
|
parameters: {},
|
|
|
|
position: [-20, 260],
|
|
|
|
type: 'n8n-nodes-base.start',
|
|
|
|
typeVersion: 1,
|
|
|
|
credentials: {
|
|
|
|
default: {
|
|
|
|
id: savedCredential.id,
|
|
|
|
name: savedCredential.name,
|
2022-10-26 06:49:43 -07:00
|
|
|
},
|
|
|
|
},
|
2023-03-17 09:24:05 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
2022-10-26 06:49:43 -07:00
|
|
|
expect(response.statusCode).toBe(400);
|
|
|
|
});
|
|
|
|
|
2022-12-21 07:42:07 -08:00
|
|
|
it('Should succeed but prevent modifying node attributes other than position, name and disabled', async () => {
|
2023-03-17 09:24:05 -07:00
|
|
|
const savedCredential = await saveCredential(randomCredentialPayload(), { user: member });
|
2022-10-26 06:49:43 -07:00
|
|
|
|
|
|
|
const originalNodes: INode[] = [
|
|
|
|
{
|
|
|
|
id: 'uuid-1234',
|
|
|
|
name: 'Start',
|
2022-12-21 07:42:07 -08:00
|
|
|
parameters: {
|
|
|
|
firstParam: 123,
|
|
|
|
},
|
2022-10-26 06:49:43 -07:00
|
|
|
position: [-20, 260],
|
|
|
|
type: 'n8n-nodes-base.start',
|
|
|
|
typeVersion: 1,
|
|
|
|
credentials: {
|
|
|
|
default: {
|
2023-01-02 08:42:32 -08:00
|
|
|
id: savedCredential.id,
|
2022-10-26 06:49:43 -07:00
|
|
|
name: savedCredential.name,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const changedNodes: INode[] = [
|
|
|
|
{
|
|
|
|
id: 'uuid-1234',
|
|
|
|
name: 'End',
|
2022-12-21 07:42:07 -08:00
|
|
|
parameters: {
|
|
|
|
firstParam: 456,
|
|
|
|
},
|
|
|
|
position: [-20, 555],
|
2022-10-26 06:49:43 -07:00
|
|
|
type: 'n8n-nodes-base.no-op',
|
|
|
|
typeVersion: 1,
|
|
|
|
credentials: {
|
|
|
|
default: {
|
|
|
|
id: '200',
|
|
|
|
name: 'fake credential',
|
|
|
|
},
|
|
|
|
},
|
2022-12-21 07:42:07 -08:00
|
|
|
disabled: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const expectedNodes: INode[] = [
|
|
|
|
{
|
|
|
|
id: 'uuid-1234',
|
|
|
|
name: 'End',
|
|
|
|
parameters: {
|
|
|
|
firstParam: 123,
|
|
|
|
},
|
|
|
|
position: [-20, 555],
|
|
|
|
type: 'n8n-nodes-base.start',
|
|
|
|
typeVersion: 1,
|
|
|
|
credentials: {
|
|
|
|
default: {
|
2023-01-02 08:42:32 -08:00
|
|
|
id: savedCredential.id,
|
2022-12-21 07:42:07 -08:00
|
|
|
name: savedCredential.name,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
disabled: true,
|
2022-10-26 06:49:43 -07:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2022-10-31 02:35:24 -07:00
|
|
|
const workflow = {
|
|
|
|
name: 'test',
|
|
|
|
active: false,
|
|
|
|
connections: {},
|
|
|
|
nodes: originalNodes,
|
|
|
|
};
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const createResponse = await authMemberAgent.post('/workflows').send(workflow);
|
2022-12-06 00:25:39 -08:00
|
|
|
const { id, versionId } = createResponse.body.data;
|
2022-10-31 02:35:24 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
await authMemberAgent.put(`/workflows/${id}/share`).send({ shareWithIds: [anotherMember.id] });
|
2022-10-26 06:49:43 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const response = await authAnotherMemberAgent.patch(`/workflows/${id}`).send({
|
2022-12-06 00:25:39 -08:00
|
|
|
versionId,
|
2022-10-26 06:49:43 -07:00
|
|
|
nodes: changedNodes,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(response.statusCode).toBe(200);
|
2022-12-21 07:42:07 -08:00
|
|
|
expect(response.body.data.nodes).toMatchObject(expectedNodes);
|
2022-10-13 02:55:58 -07:00
|
|
|
});
|
|
|
|
});
|
2022-10-31 02:35:24 -07:00
|
|
|
|
2022-11-14 06:38:19 -08:00
|
|
|
describe('PATCH /workflows/:id - validate interim updates', () => {
|
2022-12-06 00:25:39 -08:00
|
|
|
it('should block owner updating workflow nodes on interim update by member', async () => {
|
2022-10-31 02:35:24 -07:00
|
|
|
// owner creates and shares workflow
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const createResponse = await authOwnerAgent.post('/workflows').send(makeWorkflow());
|
2022-12-06 00:25:39 -08:00
|
|
|
const { id, versionId: ownerVersionId } = createResponse.body.data;
|
2023-03-17 09:24:05 -07:00
|
|
|
await authOwnerAgent.put(`/workflows/${id}/share`).send({ shareWithIds: [member.id] });
|
2022-10-31 02:35:24 -07:00
|
|
|
|
|
|
|
// member accesses and updates workflow name
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const memberGetResponse = await authMemberAgent.get(`/workflows/${id}`);
|
2022-12-06 00:25:39 -08:00
|
|
|
const { versionId: memberVersionId } = memberGetResponse.body.data;
|
2022-10-31 02:35:24 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
await authMemberAgent
|
2022-10-31 02:35:24 -07:00
|
|
|
.patch(`/workflows/${id}`)
|
2022-12-06 00:25:39 -08:00
|
|
|
.send({ name: 'Update by member', versionId: memberVersionId });
|
2022-10-31 02:35:24 -07:00
|
|
|
|
|
|
|
// owner blocked from updating workflow nodes
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const updateAttemptResponse = await authOwnerAgent
|
2022-10-31 02:35:24 -07:00
|
|
|
.patch(`/workflows/${id}`)
|
2022-12-06 00:25:39 -08:00
|
|
|
.send({ nodes: [], versionId: ownerVersionId });
|
2022-10-31 02:35:24 -07:00
|
|
|
|
|
|
|
expect(updateAttemptResponse.status).toBe(400);
|
2022-12-06 00:25:39 -08:00
|
|
|
expect(updateAttemptResponse.body.code).toBe(100);
|
2022-10-31 02:35:24 -07:00
|
|
|
});
|
|
|
|
|
2022-12-06 00:25:39 -08:00
|
|
|
it('should block member updating workflow nodes on interim update by owner', async () => {
|
2022-10-31 02:35:24 -07:00
|
|
|
// owner creates, updates and shares workflow
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const createResponse = await authOwnerAgent.post('/workflows').send(makeWorkflow());
|
2022-12-06 00:25:39 -08:00
|
|
|
const { id, versionId: ownerFirstVersionId } = createResponse.body.data;
|
2022-10-31 02:35:24 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const updateResponse = await authOwnerAgent
|
2022-10-31 02:35:24 -07:00
|
|
|
.patch(`/workflows/${id}`)
|
2022-12-06 00:25:39 -08:00
|
|
|
.send({ name: 'Update by owner', versionId: ownerFirstVersionId });
|
2022-10-31 02:35:24 -07:00
|
|
|
|
2022-12-06 00:25:39 -08:00
|
|
|
const { versionId: ownerSecondVersionId } = updateResponse.body.data;
|
2022-10-31 02:35:24 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
await authOwnerAgent.put(`/workflows/${id}/share`).send({ shareWithIds: [member.id] });
|
2022-10-31 02:35:24 -07:00
|
|
|
|
|
|
|
// member accesses workflow
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const memberGetResponse = await authMemberAgent.get(`/workflows/${id}`);
|
2022-12-06 00:25:39 -08:00
|
|
|
const { versionId: memberVersionId } = memberGetResponse.body.data;
|
2022-10-31 02:35:24 -07:00
|
|
|
|
|
|
|
// owner re-updates workflow
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
await authOwnerAgent
|
2022-10-31 02:35:24 -07:00
|
|
|
.patch(`/workflows/${id}`)
|
2022-12-06 00:25:39 -08:00
|
|
|
.send({ name: 'Owner update again', versionId: ownerSecondVersionId });
|
2022-10-31 02:35:24 -07:00
|
|
|
|
|
|
|
// member blocked from updating workflow
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const updateAttemptResponse = await authMemberAgent
|
2022-10-31 02:35:24 -07:00
|
|
|
.patch(`/workflows/${id}`)
|
2022-12-06 00:25:39 -08:00
|
|
|
.send({ nodes: [], versionId: memberVersionId });
|
2022-10-31 02:35:24 -07:00
|
|
|
|
|
|
|
expect(updateAttemptResponse.status).toBe(400);
|
2022-12-06 00:25:39 -08:00
|
|
|
expect(updateAttemptResponse.body.code).toBe(100);
|
2022-10-31 02:35:24 -07:00
|
|
|
});
|
|
|
|
|
2022-12-06 00:25:39 -08:00
|
|
|
it('should block owner activation on interim activation by member', async () => {
|
2022-10-31 02:35:24 -07:00
|
|
|
// owner creates and shares workflow
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const createResponse = await authOwnerAgent.post('/workflows').send(makeWorkflow());
|
2022-12-06 00:25:39 -08:00
|
|
|
const { id, versionId: ownerVersionId } = createResponse.body.data;
|
2023-03-17 09:24:05 -07:00
|
|
|
await authOwnerAgent.put(`/workflows/${id}/share`).send({ shareWithIds: [member.id] });
|
2022-10-31 02:35:24 -07:00
|
|
|
|
|
|
|
// member accesses and activates workflow
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const memberGetResponse = await authMemberAgent.get(`/workflows/${id}`);
|
2022-12-06 00:25:39 -08:00
|
|
|
const { versionId: memberVersionId } = memberGetResponse.body.data;
|
2023-03-17 09:24:05 -07:00
|
|
|
await authMemberAgent
|
2022-12-06 00:25:39 -08:00
|
|
|
.patch(`/workflows/${id}`)
|
2023-07-26 00:25:01 -07:00
|
|
|
.send({ active: true, versionId: memberVersionId, name: 'Update by member' });
|
2022-10-31 02:35:24 -07:00
|
|
|
// owner blocked from activating workflow
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const activationAttemptResponse = await authOwnerAgent
|
2022-10-31 02:35:24 -07:00
|
|
|
.patch(`/workflows/${id}`)
|
2023-07-26 00:25:01 -07:00
|
|
|
.send({ active: true, versionId: ownerVersionId, name: 'Update by owner' });
|
2022-10-31 02:35:24 -07:00
|
|
|
|
|
|
|
expect(activationAttemptResponse.status).toBe(400);
|
2022-12-06 00:25:39 -08:00
|
|
|
expect(activationAttemptResponse.body.code).toBe(100);
|
2022-10-31 02:35:24 -07:00
|
|
|
});
|
|
|
|
|
2022-12-06 00:25:39 -08:00
|
|
|
it('should block member activation on interim activation by owner', async () => {
|
2022-10-31 02:35:24 -07:00
|
|
|
// owner creates, updates and shares workflow
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const createResponse = await authOwnerAgent.post('/workflows').send(makeWorkflow());
|
2022-12-06 00:25:39 -08:00
|
|
|
const { id, versionId: ownerFirstVersionId } = createResponse.body.data;
|
2022-10-31 02:35:24 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const updateResponse = await authOwnerAgent
|
2022-10-31 02:35:24 -07:00
|
|
|
.patch(`/workflows/${id}`)
|
2022-12-06 00:25:39 -08:00
|
|
|
.send({ name: 'Update by owner', versionId: ownerFirstVersionId });
|
|
|
|
const { versionId: ownerSecondVersionId } = updateResponse.body.data;
|
2022-10-31 02:35:24 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
await authOwnerAgent.put(`/workflows/${id}/share`).send({ shareWithIds: [member.id] });
|
2022-10-31 02:35:24 -07:00
|
|
|
|
|
|
|
// member accesses workflow
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const memberGetResponse = await authMemberAgent.get(`/workflows/${id}`);
|
2022-12-06 00:25:39 -08:00
|
|
|
const { versionId: memberVersionId } = memberGetResponse.body.data;
|
2022-10-31 02:35:24 -07:00
|
|
|
|
|
|
|
// owner activates workflow
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
await authOwnerAgent
|
2022-12-06 00:25:39 -08:00
|
|
|
.patch(`/workflows/${id}`)
|
2023-07-26 00:25:01 -07:00
|
|
|
.send({ active: true, versionId: ownerSecondVersionId, name: 'Owner update again' });
|
2022-10-31 02:35:24 -07:00
|
|
|
|
|
|
|
// member blocked from activating workflow
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const updateAttemptResponse = await authMemberAgent
|
2022-10-31 02:35:24 -07:00
|
|
|
.patch(`/workflows/${id}`)
|
2023-07-26 00:25:01 -07:00
|
|
|
.send({ active: true, versionId: memberVersionId, name: 'Update by member' });
|
2022-10-31 02:35:24 -07:00
|
|
|
|
|
|
|
expect(updateAttemptResponse.status).toBe(400);
|
2022-12-06 00:25:39 -08:00
|
|
|
expect(updateAttemptResponse.body.code).toBe(100);
|
2022-10-31 02:35:24 -07:00
|
|
|
});
|
|
|
|
|
2022-12-06 00:25:39 -08:00
|
|
|
it('should block member updating workflow settings on interim update by owner', async () => {
|
2022-10-31 02:35:24 -07:00
|
|
|
// owner creates and shares workflow
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const createResponse = await authOwnerAgent.post('/workflows').send(makeWorkflow());
|
2022-12-06 00:25:39 -08:00
|
|
|
const { id, versionId: ownerVersionId } = createResponse.body.data;
|
2023-03-17 09:24:05 -07:00
|
|
|
await authOwnerAgent.put(`/workflows/${id}/share`).send({ shareWithIds: [member.id] });
|
2022-10-31 02:35:24 -07:00
|
|
|
|
|
|
|
// member accesses workflow
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const memberGetResponse = await authMemberAgent.get(`/workflows/${id}`);
|
2022-12-06 00:25:39 -08:00
|
|
|
const { versionId: memberVersionId } = memberGetResponse.body.data;
|
2022-10-31 02:35:24 -07:00
|
|
|
|
|
|
|
// owner updates workflow name
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
await authOwnerAgent
|
2022-10-31 02:35:24 -07:00
|
|
|
.patch(`/workflows/${id}`)
|
2022-12-06 00:25:39 -08:00
|
|
|
.send({ name: 'Another name', versionId: ownerVersionId });
|
2022-10-31 02:35:24 -07:00
|
|
|
|
|
|
|
// member blocked from updating workflow settings
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const updateAttemptResponse = await authMemberAgent
|
2022-10-31 02:35:24 -07:00
|
|
|
.patch(`/workflows/${id}`)
|
2022-12-06 00:25:39 -08:00
|
|
|
.send({ settings: { saveManualExecutions: true }, versionId: memberVersionId });
|
2022-10-31 02:35:24 -07:00
|
|
|
|
|
|
|
expect(updateAttemptResponse.status).toBe(400);
|
2022-12-06 00:25:39 -08:00
|
|
|
expect(updateAttemptResponse.body.code).toBe(100);
|
2022-10-31 02:35:24 -07:00
|
|
|
});
|
|
|
|
|
2022-12-06 00:25:39 -08:00
|
|
|
it('should block member updating workflow name on interim update by owner', async () => {
|
2022-10-31 02:35:24 -07:00
|
|
|
// owner creates and shares workflow
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const createResponse = await authOwnerAgent.post('/workflows').send(makeWorkflow());
|
2022-12-06 00:25:39 -08:00
|
|
|
const { id, versionId: ownerVersionId } = createResponse.body.data;
|
2023-03-17 09:24:05 -07:00
|
|
|
await authOwnerAgent.put(`/workflows/${id}/share`).send({ shareWithIds: [member.id] });
|
2022-10-31 02:35:24 -07:00
|
|
|
|
|
|
|
// member accesses workflow
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const memberGetResponse = await authMemberAgent.get(`/workflows/${id}`);
|
2022-12-06 00:25:39 -08:00
|
|
|
const { versionId: memberVersionId } = memberGetResponse.body.data;
|
2022-10-31 02:35:24 -07:00
|
|
|
|
|
|
|
// owner updates workflow settings
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
await authOwnerAgent
|
2022-10-31 02:35:24 -07:00
|
|
|
.patch(`/workflows/${id}`)
|
2022-12-06 00:25:39 -08:00
|
|
|
.send({ settings: { saveManualExecutions: true }, versionId: ownerVersionId });
|
2022-10-31 02:35:24 -07:00
|
|
|
|
|
|
|
// member blocked from updating workflow name
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const updateAttemptResponse = await authMemberAgent
|
2022-10-31 02:35:24 -07:00
|
|
|
.patch(`/workflows/${id}`)
|
2022-12-06 00:25:39 -08:00
|
|
|
.send({ settings: { saveManualExecutions: true }, versionId: memberVersionId });
|
2022-10-31 02:35:24 -07:00
|
|
|
|
|
|
|
expect(updateAttemptResponse.status).toBe(400);
|
2022-12-06 00:25:39 -08:00
|
|
|
expect(updateAttemptResponse.body.code).toBe(100);
|
2022-10-31 02:35:24 -07:00
|
|
|
});
|
|
|
|
});
|
2023-05-26 09:02:55 -07:00
|
|
|
|
|
|
|
describe('getSharedWorkflowIds', () => {
|
|
|
|
it('should show all workflows to owners', async () => {
|
|
|
|
owner.globalRole = await getGlobalOwnerRole();
|
|
|
|
const workflow1 = await createWorkflow({}, member);
|
|
|
|
const workflow2 = await createWorkflow({}, anotherMember);
|
|
|
|
const sharedWorkflowIds = await getSharedWorkflowIds(owner);
|
|
|
|
expect(sharedWorkflowIds).toHaveLength(2);
|
|
|
|
expect(sharedWorkflowIds).toContain(workflow1.id);
|
|
|
|
expect(sharedWorkflowIds).toContain(workflow2.id);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should show shared workflows to users', async () => {
|
|
|
|
member.globalRole = await getGlobalMemberRole();
|
|
|
|
const workflow1 = await createWorkflow({}, anotherMember);
|
|
|
|
const workflow2 = await createWorkflow({}, anotherMember);
|
|
|
|
const workflow3 = await createWorkflow({}, anotherMember);
|
2023-11-08 07:29:39 -08:00
|
|
|
await shareWorkflowWithUsers(workflow1, [member]);
|
|
|
|
await shareWorkflowWithUsers(workflow3, [member]);
|
2023-05-26 09:02:55 -07:00
|
|
|
const sharedWorkflowIds = await getSharedWorkflowIds(member);
|
|
|
|
expect(sharedWorkflowIds).toHaveLength(2);
|
|
|
|
expect(sharedWorkflowIds).toContain(workflow1.id);
|
|
|
|
expect(sharedWorkflowIds).toContain(workflow3.id);
|
|
|
|
});
|
|
|
|
});
|
2023-10-25 03:07:11 -07:00
|
|
|
|
|
|
|
describe('PATCH /workflows/:id - workflow history', () => {
|
|
|
|
test('Should create workflow history version when licensed', async () => {
|
|
|
|
licenseLike.isWorkflowHistoryLicensed.mockReturnValue(true);
|
2023-11-08 07:29:39 -08:00
|
|
|
const workflow = await createWorkflow({}, owner);
|
2023-10-25 03:07:11 -07:00
|
|
|
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);
|
2023-11-08 07:29:39 -08:00
|
|
|
const workflow = await createWorkflow({}, owner);
|
2023-10-25 03:07:11 -07:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('PATCH /workflows/:id - activate workflow', () => {
|
|
|
|
test('should activate workflow without changing version ID', async () => {
|
|
|
|
licenseLike.isWorkflowHistoryLicensed.mockReturnValue(false);
|
2023-11-08 07:29:39 -08:00
|
|
|
const workflow = await createWorkflow({}, owner);
|
2023-10-25 03:07:11 -07:00
|
|
|
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);
|
2023-11-08 07:29:39 -08:00
|
|
|
const workflow = await createWorkflow({ active: true }, owner);
|
2023-10-25 03:07:11 -07:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|