mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
feat: add saving new workflow endpoint (#4330) (no-changelog)
* feat: add saving new workflow endpoint
This commit is contained in:
@@ -8,7 +8,9 @@ import { v4 as uuid } from 'uuid';
|
||||
|
||||
import type { Role } from '../../src/databases/entities/Role';
|
||||
import config from '../../config';
|
||||
import type { AuthAgent } from './shared/types';
|
||||
import type { AuthAgent, SaveCredentialFunction } from './shared/types';
|
||||
import { makeWorkflow } from './shared/utils';
|
||||
import { randomCredentialPayload } from './shared/random';
|
||||
|
||||
jest.mock('../../src/telemetry');
|
||||
|
||||
@@ -20,7 +22,9 @@ let testDbName = '';
|
||||
|
||||
let globalOwnerRole: Role;
|
||||
let globalMemberRole: Role;
|
||||
let credentialOwnerRole: Role;
|
||||
let authAgent: AuthAgent;
|
||||
let saveCredential: SaveCredentialFunction;
|
||||
|
||||
beforeAll(async () => {
|
||||
app = await utils.initTestServer({
|
||||
@@ -32,6 +36,9 @@ beforeAll(async () => {
|
||||
|
||||
globalOwnerRole = await testDb.getGlobalOwnerRole();
|
||||
globalMemberRole = await testDb.getGlobalMemberRole();
|
||||
credentialOwnerRole = await testDb.getCredentialOwnerRole();
|
||||
|
||||
saveCredential = testDb.affixRoleToSaveCredential(credentialOwnerRole);
|
||||
|
||||
authAgent = utils.createAuthAgent(app);
|
||||
|
||||
@@ -123,21 +130,27 @@ describe('PUT /workflows/:id', () => {
|
||||
});
|
||||
|
||||
describe('GET /workflows/:id', () => {
|
||||
test('GET should fail with invalid id', async () => {
|
||||
test('GET should fail with invalid id due to route rule', async () => {
|
||||
const owner = await testDb.createUser({ globalRole: globalOwnerRole });
|
||||
const authOwnerAgent = utils.createAgent(app, { auth: true, user: owner });
|
||||
|
||||
const response = await authOwnerAgent.get('/workflows/potatoes');
|
||||
const response = await authAgent(owner).get('/workflows/potatoes');
|
||||
|
||||
expect(response.statusCode).toBe(400);
|
||||
expect(response.statusCode).toBe(404);
|
||||
});
|
||||
|
||||
test('GET should return 404 for non existing workflow', async () => {
|
||||
const owner = await testDb.createUser({ globalRole: globalOwnerRole });
|
||||
|
||||
const response = await authAgent(owner).get('/workflows/9001');
|
||||
|
||||
expect(response.statusCode).toBe(404);
|
||||
});
|
||||
|
||||
test('GET should return a workflow with owner', async () => {
|
||||
const owner = await testDb.createUser({ globalRole: globalOwnerRole });
|
||||
const workflow = await createWorkflow({}, owner);
|
||||
const authOwnerAgent = utils.createAgent(app, { auth: true, user: owner });
|
||||
|
||||
const response = await authOwnerAgent.get(`/workflows/${workflow.id}`);
|
||||
const response = await authAgent(owner).get(`/workflows/${workflow.id}`);
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.body.data.ownedBy).toMatchObject({
|
||||
@@ -154,10 +167,9 @@ describe('GET /workflows/:id', () => {
|
||||
const owner = await testDb.createUser({ globalRole: globalOwnerRole });
|
||||
const member = await testDb.createUser({ globalRole: globalMemberRole });
|
||||
const workflow = await createWorkflow({}, owner);
|
||||
const authOwnerAgent = utils.createAgent(app, { auth: true, user: owner });
|
||||
await testDb.shareWorkflowWithUsers(workflow, [member]);
|
||||
|
||||
const response = await authOwnerAgent.get(`/workflows/${workflow.id}`);
|
||||
const response = await authAgent(owner).get(`/workflows/${workflow.id}`);
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.body.data.ownedBy).toMatchObject({
|
||||
@@ -181,10 +193,9 @@ describe('GET /workflows/:id', () => {
|
||||
const member1 = await testDb.createUser({ globalRole: globalMemberRole });
|
||||
const member2 = await testDb.createUser({ globalRole: globalMemberRole });
|
||||
const workflow = await createWorkflow({}, owner);
|
||||
const authOwnerAgent = utils.createAgent(app, { auth: true, user: owner });
|
||||
await testDb.shareWorkflowWithUsers(workflow, [member1, member2]);
|
||||
|
||||
const response = await authOwnerAgent.get(`/workflows/${workflow.id}`);
|
||||
const response = await authAgent(owner).get(`/workflows/${workflow.id}`);
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.body.data.ownedBy).toMatchObject({
|
||||
@@ -197,3 +208,90 @@ describe('GET /workflows/:id', () => {
|
||||
expect(response.body.data.sharedWith).toHaveLength(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST /workflows', () => {
|
||||
it('Should create a workflow that uses no credential', async () => {
|
||||
const owner = await testDb.createUser({ globalRole: globalOwnerRole });
|
||||
|
||||
const workflow = makeWorkflow({ withPinData: false });
|
||||
|
||||
const response = await authAgent(owner).post('/workflows').send(workflow);
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
|
||||
const usedCredentials = await testDb.getCredentialUsageInWorkflow(response.body.data.id);
|
||||
expect(usedCredentials).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('Should save credential usage when saving a new workflow', async () => {
|
||||
const owner = await testDb.createUser({ globalRole: globalOwnerRole });
|
||||
|
||||
const savedCredential = await saveCredential(randomCredentialPayload(), { user: owner });
|
||||
const workflow = makeWorkflow({
|
||||
withPinData: false,
|
||||
withCredential: { id: savedCredential.id.toString(), name: savedCredential.name },
|
||||
});
|
||||
|
||||
const response = await authAgent(owner).post('/workflows').send(workflow);
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
|
||||
const usedCredentials = await testDb.getCredentialUsageInWorkflow(response.body.data.id);
|
||||
expect(usedCredentials).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('Should not allow saving a workflow using credential you have no access', async () => {
|
||||
const owner = await testDb.createUser({ globalRole: globalOwnerRole });
|
||||
const member = await testDb.createUser({ globalRole: globalMemberRole });
|
||||
|
||||
// Credential belongs to owner, member cannot use it.
|
||||
const savedCredential = await saveCredential(randomCredentialPayload(), { user: owner });
|
||||
const workflow = makeWorkflow({
|
||||
withPinData: false,
|
||||
withCredential: { id: savedCredential.id.toString(), name: savedCredential.name },
|
||||
});
|
||||
|
||||
const response = await authAgent(member).post('/workflows').send(workflow);
|
||||
|
||||
expect(response.statusCode).toBe(400);
|
||||
expect(response.body.message).toBe(
|
||||
'The workflow contains credentials that you do not have access to',
|
||||
);
|
||||
});
|
||||
|
||||
it('Should allow owner to save a workflow using credential owned by others', async () => {
|
||||
const owner = await testDb.createUser({ globalRole: globalOwnerRole });
|
||||
const member = await testDb.createUser({ globalRole: globalMemberRole });
|
||||
|
||||
// Credential belongs to owner, member cannot use it.
|
||||
const savedCredential = await saveCredential(randomCredentialPayload(), { user: member });
|
||||
const workflow = makeWorkflow({
|
||||
withPinData: false,
|
||||
withCredential: { id: savedCredential.id.toString(), name: savedCredential.name },
|
||||
});
|
||||
|
||||
const response = await authAgent(owner).post('/workflows').send(workflow);
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const usedCredentials = await testDb.getCredentialUsageInWorkflow(response.body.data.id);
|
||||
expect(usedCredentials).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('Should allow saving a workflow using a credential owned by others and shared with you', async () => {
|
||||
const member1 = await testDb.createUser({ globalRole: globalMemberRole });
|
||||
const member2 = await testDb.createUser({ globalRole: globalMemberRole });
|
||||
|
||||
const savedCredential = await saveCredential(randomCredentialPayload(), { user: member1 });
|
||||
await testDb.shareCredentialWithUsers(savedCredential, [member2]);
|
||||
|
||||
const workflow = makeWorkflow({
|
||||
withPinData: false,
|
||||
withCredential: { id: savedCredential.id.toString(), name: savedCredential.name },
|
||||
});
|
||||
|
||||
const response = await authAgent(member2).post('/workflows').send(workflow);
|
||||
expect(response.statusCode).toBe(200);
|
||||
const usedCredentials = await testDb.getCredentialUsageInWorkflow(response.body.data.id);
|
||||
expect(usedCredentials).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user