refactor: Rewrite workflow public-api tests to avoid timeouts (no-changelog) (#5696)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™ 2023-03-15 14:24:09 +01:00 committed by GitHub
parent 6c74d41f23
commit b4e60c3b47
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 926 additions and 1291 deletions

View file

@ -1,6 +1,6 @@
import type express from 'express'; import type express from 'express';
import { Container } from 'typedi'; import { Container } from 'typedi';
import type { FindManyOptions, FindOptionsWhere } from 'typeorm'; import type { FindOptionsWhere } from 'typeorm';
import { In } from 'typeorm'; import { In } from 'typeorm';
import { ActiveWorkflowRunner } from '@/ActiveWorkflowRunner'; import { ActiveWorkflowRunner } from '@/ActiveWorkflowRunner';
@ -20,12 +20,11 @@ import {
updateWorkflow, updateWorkflow,
hasStartNode, hasStartNode,
getStartNode, getStartNode,
getWorkflows,
getSharedWorkflows, getSharedWorkflows,
getWorkflowsCount,
createWorkflow, createWorkflow,
getWorkflowIdsViaTags, getWorkflowIdsViaTags,
parseTagNames, parseTagNames,
getWorkflowsAndCount,
} from './workflows.service'; } from './workflows.service';
import { WorkflowsService } from '@/workflows/workflows.services'; import { WorkflowsService } from '@/workflows/workflows.services';
import { InternalHooks } from '@/InternalHooks'; import { InternalHooks } from '@/InternalHooks';
@ -98,28 +97,15 @@ export = {
async (req: WorkflowRequest.GetAll, res: express.Response): Promise<express.Response> => { async (req: WorkflowRequest.GetAll, res: express.Response): Promise<express.Response> => {
const { offset = 0, limit = 100, active = undefined, tags = undefined } = req.query; const { offset = 0, limit = 100, active = undefined, tags = undefined } = req.query;
let workflows: WorkflowEntity[];
let count: number;
const where: FindOptionsWhere<WorkflowEntity> = { const where: FindOptionsWhere<WorkflowEntity> = {
...(active !== undefined && { active }), ...(active !== undefined && { active }),
}; };
const query: FindManyOptions<WorkflowEntity> = {
skip: offset,
take: limit,
where,
...(!config.getEnv('workflowTagsDisabled') && { relations: ['tags'] }),
};
if (isInstanceOwner(req.user)) { if (isInstanceOwner(req.user)) {
if (tags) { if (tags) {
const workflowIds = await getWorkflowIdsViaTags(parseTagNames(tags)); const workflowIds = await getWorkflowIdsViaTags(parseTagNames(tags));
Object.assign(where, { id: In(workflowIds) }); where.id = In(workflowIds);
} }
workflows = await getWorkflows(query);
count = await getWorkflowsCount(query);
} else { } else {
const options: { workflowIds?: string[] } = {}; const options: { workflowIds?: string[] } = {};
@ -137,14 +123,16 @@ export = {
} }
const workflowsIds = sharedWorkflows.map((shareWorkflow) => shareWorkflow.workflowId); const workflowsIds = sharedWorkflows.map((shareWorkflow) => shareWorkflow.workflowId);
where.id = In(workflowsIds);
Object.assign(where, { id: In(workflowsIds) });
workflows = await getWorkflows(query);
count = await getWorkflowsCount(query);
} }
const [workflows, count] = await getWorkflowsAndCount({
skip: offset,
take: limit,
where,
...(!config.getEnv('workflowTagsDisabled') && { relations: ['tags'] }),
});
void Container.get(InternalHooks).onUserRetrievedAllWorkflows({ void Container.get(InternalHooks).onUserRetrievedAllWorkflows({
user_id: req.user.id, user_id: req.user.id,
public_api: true, public_api: true,

View file

@ -109,14 +109,10 @@ export async function deleteWorkflow(workflow: WorkflowEntity): Promise<Workflow
return Db.collections.Workflow.remove(workflow); return Db.collections.Workflow.remove(workflow);
} }
export async function getWorkflows( export async function getWorkflowsAndCount(
options: FindManyOptions<WorkflowEntity>, options: FindManyOptions<WorkflowEntity>,
): Promise<WorkflowEntity[]> { ): Promise<[WorkflowEntity[], number]> {
return Db.collections.Workflow.find(options); return Db.collections.Workflow.findAndCount(options);
}
export async function getWorkflowsCount(options: FindManyOptions<WorkflowEntity>): Promise<number> {
return Db.collections.Workflow.count(options);
} }
export async function updateWorkflow( export async function updateWorkflow(

View file

@ -1,19 +1,25 @@
import express from 'express'; import type { Application } from 'express';
import type { SuperAgentTest } from 'supertest';
import * as Db from '@/Db'; import * as Db from '@/Db';
import config from '@/config'; import config from '@/config';
import { Role } from '@db/entities/Role'; import { Role } from '@db/entities/Role';
import { TagEntity } from '@db/entities/TagEntity'; import { TagEntity } from '@db/entities/TagEntity';
import type { User } from '@db/entities/User';
import { ActiveWorkflowRunner } from '@/ActiveWorkflowRunner'; import { ActiveWorkflowRunner } from '@/ActiveWorkflowRunner';
import { randomApiKey } from '../shared/random'; import { randomApiKey } from '../shared/random';
import * as utils from '../shared/utils'; import * as utils from '../shared/utils';
import * as testDb from '../shared/testDb'; import * as testDb from '../shared/testDb';
let app: express.Application; describe('Workflows Public API', () => {
let app: Application;
let globalOwnerRole: Role; let globalOwnerRole: Role;
let globalMemberRole: Role; let globalMemberRole: Role;
let workflowOwnerRole: Role; let workflowOwnerRole: Role;
let owner: User;
let member: User;
let authOwnerAgent: SuperAgentTest;
let authMemberAgent: SuperAgentTest;
let workflowRunner: ActiveWorkflowRunner; let workflowRunner: ActiveWorkflowRunner;
beforeAll(async () => { beforeAll(async () => {
@ -30,6 +36,16 @@ beforeAll(async () => {
globalMemberRole = fetchedGlobalMemberRole; globalMemberRole = fetchedGlobalMemberRole;
workflowOwnerRole = fetchedWorkflowOwnerRole; workflowOwnerRole = fetchedWorkflowOwnerRole;
owner = await testDb.createUser({
globalRole: globalOwnerRole,
apiKey: randomApiKey(),
});
member = await testDb.createUser({
globalRole: globalMemberRole,
apiKey: randomApiKey(),
});
utils.initConfigFile(); utils.initConfigFile();
await utils.initNodeTypes(); await utils.initNodeTypes();
workflowRunner = await utils.initActiveWorkflowRunner(); workflowRunner = await utils.initActiveWorkflowRunner();
@ -40,11 +56,24 @@ beforeEach(async () => {
'SharedCredentials', 'SharedCredentials',
'SharedWorkflow', 'SharedWorkflow',
'Tag', 'Tag',
'User',
'Workflow', 'Workflow',
'Credentials', 'Credentials',
]); ]);
authOwnerAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: owner,
version: 1,
});
authMemberAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: member,
version: 1,
});
config.set('userManagement.disabled', false); config.set('userManagement.disabled', false);
config.set('userManagement.isInstanceOwnerSetUp', true); config.set('userManagement.isInstanceOwnerSetUp', true);
}); });
@ -57,55 +86,26 @@ afterAll(async () => {
await testDb.terminate(); await testDb.terminate();
}); });
test('GET /workflows should fail due to missing API Key', async () => { const testWithAPIKey =
const owner = await testDb.createUser({ globalRole: globalOwnerRole }); (method: 'get' | 'post' | 'put' | 'delete', url: string, apiKey: string | null) => async () => {
authOwnerAgent.set({ 'X-N8N-API-KEY': apiKey });
const authOwnerAgent = utils.createAgent(app, { const response = await authOwnerAgent[method](url);
apiPath: 'public',
auth: true,
user: owner,
version: 1,
});
const response = await authOwnerAgent.get('/workflows');
expect(response.statusCode).toBe(401); expect(response.statusCode).toBe(401);
}); };
test('GET /workflows should fail due to invalid API Key', async () => { describe('GET /workflows', () => {
const owner = await testDb.createUser({ globalRole: globalOwnerRole, apiKey: randomApiKey() }); test('should fail due to missing API Key', testWithAPIKey('get', '/workflows', null));
owner.apiKey = 'abcXYZ'; test('should fail due to invalid API Key', testWithAPIKey('get', '/workflows', 'abcXYZ'));
const authOwnerAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: owner,
version: 1,
});
const response = await authOwnerAgent.get('/workflows');
expect(response.statusCode).toBe(401);
});
test('GET /workflows should return all owned workflows', async () => {
const member = await testDb.createUser({ globalRole: globalMemberRole, apiKey: randomApiKey() });
const authAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: member,
version: 1,
});
test('should return all owned workflows', async () => {
await Promise.all([ await Promise.all([
testDb.createWorkflow({}, member), testDb.createWorkflow({}, member),
testDb.createWorkflow({}, member), testDb.createWorkflow({}, member),
testDb.createWorkflow({}, member), testDb.createWorkflow({}, member),
]); ]);
const response = await authAgent.get('/workflows'); const response = await authMemberAgent.get('/workflows');
expect(response.statusCode).toBe(200); expect(response.statusCode).toBe(200);
expect(response.body.data.length).toBe(3); expect(response.body.data.length).toBe(3);
@ -138,29 +138,22 @@ test('GET /workflows should return all owned workflows', async () => {
} }
}); });
test('GET /workflows should return all owned workflows with pagination', async () => { test('should return all owned workflows with pagination', async () => {
const member = await testDb.createUser({ globalRole: globalMemberRole, apiKey: randomApiKey() });
const authAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: member,
version: 1,
});
await Promise.all([ await Promise.all([
testDb.createWorkflow({}, member), testDb.createWorkflow({}, member),
testDb.createWorkflow({}, member), testDb.createWorkflow({}, member),
testDb.createWorkflow({}, member), testDb.createWorkflow({}, member),
]); ]);
const response = await authAgent.get('/workflows?limit=1'); const response = await authMemberAgent.get('/workflows?limit=1');
expect(response.statusCode).toBe(200); expect(response.statusCode).toBe(200);
expect(response.body.data.length).toBe(1); expect(response.body.data.length).toBe(1);
expect(response.body.nextCursor).not.toBeNull(); expect(response.body.nextCursor).not.toBeNull();
const response2 = await authAgent.get(`/workflows?limit=1&cursor=${response.body.nextCursor}`); const response2 = await authMemberAgent.get(
`/workflows?limit=1&cursor=${response.body.nextCursor}`,
);
expect(response2.statusCode).toBe(200); expect(response2.statusCode).toBe(200);
expect(response2.body.data.length).toBe(1); expect(response2.body.data.length).toBe(1);
@ -199,16 +192,7 @@ test('GET /workflows should return all owned workflows with pagination', async (
expect(Number(response.body.data[0].id)).toBeLessThan(Number(response2.body.data[0].id)); expect(Number(response.body.data[0].id)).toBeLessThan(Number(response2.body.data[0].id));
}); });
test('GET /workflows should return all owned workflows filtered by tag', async () => { test('should return all owned workflows filtered by tag', async () => {
const member = await testDb.createUser({ globalRole: globalMemberRole, apiKey: randomApiKey() });
const authAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: member,
version: 1,
});
const tag = await testDb.createTag({}); const tag = await testDb.createTag({});
const [workflow] = await Promise.all([ const [workflow] = await Promise.all([
@ -216,7 +200,7 @@ test('GET /workflows should return all owned workflows filtered by tag', async (
testDb.createWorkflow({}, member), testDb.createWorkflow({}, member),
]); ]);
const response = await authAgent.get(`/workflows?tags=${tag.name}`); const response = await authMemberAgent.get(`/workflows?tags=${tag.name}`);
expect(response.statusCode).toBe(200); expect(response.statusCode).toBe(200);
expect(response.body.data.length).toBe(1); expect(response.body.data.length).toBe(1);
@ -248,16 +232,7 @@ test('GET /workflows should return all owned workflows filtered by tag', async (
expect(wfTags[0].id).toBe(tag.id); expect(wfTags[0].id).toBe(tag.id);
}); });
test('GET /workflows should return all owned workflows filtered by tags', async () => { test('should return all owned workflows filtered by tags', async () => {
const member = await testDb.createUser({ globalRole: globalMemberRole, apiKey: randomApiKey() });
const authAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: member,
version: 1,
});
const tags = await Promise.all([await testDb.createTag({}), await testDb.createTag({})]); const tags = await Promise.all([await testDb.createTag({}), await testDb.createTag({})]);
const tagNames = tags.map((tag) => tag.name).join(','); const tagNames = tags.map((tag) => tag.name).join(',');
@ -269,7 +244,7 @@ test('GET /workflows should return all owned workflows filtered by tags', async
testDb.createWorkflow({ tags: [tags[1]] }, member), testDb.createWorkflow({ tags: [tags[1]] }, member),
]); ]);
const response = await authAgent.get(`/workflows?tags=${tagNames}`); const response = await authMemberAgent.get(`/workflows?tags=${tagNames}`);
expect(response.statusCode).toBe(200); expect(response.statusCode).toBe(200);
expect(response.body.data.length).toBe(2); expect(response.body.data.length).toBe(2);
@ -297,17 +272,7 @@ test('GET /workflows should return all owned workflows filtered by tags', async
} }
}); });
test('GET /workflows should return all workflows for owner', async () => { test('should return all workflows for owner', async () => {
const owner = await testDb.createUser({ globalRole: globalOwnerRole, apiKey: randomApiKey() });
const member = await testDb.createUser({ globalRole: globalMemberRole });
const authOwnerAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: owner,
version: 1,
});
await Promise.all([ await Promise.all([
testDb.createWorkflow({}, owner), testDb.createWorkflow({}, owner),
testDb.createWorkflow({}, member), testDb.createWorkflow({}, member),
@ -348,75 +313,38 @@ test('GET /workflows should return all workflows for owner', async () => {
expect(updatedAt).toBeDefined(); expect(updatedAt).toBeDefined();
} }
}); });
test('GET /workflows/:id should fail due to missing API Key', async () => {
const owner = await testDb.createUser({ globalRole: globalOwnerRole });
owner.apiKey = null;
const authOwnerAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: owner,
version: 1,
}); });
describe('GET /workflows/:id', () => {
test('should fail due to missing API Key', testWithAPIKey('get', '/workflows/2', null));
test('should fail due to invalid API Key', testWithAPIKey('get', '/workflows/2', 'abcXYZ'));
test('should fail due to non-existing workflow', async () => {
const response = await authOwnerAgent.get(`/workflows/2`); const response = await authOwnerAgent.get(`/workflows/2`);
expect(response.statusCode).toBe(401);
});
test('GET /workflows/:id should fail due to invalid API Key', async () => {
const owner = await testDb.createUser({ globalRole: globalOwnerRole, apiKey: randomApiKey() });
owner.apiKey = 'abcXYZ';
const authOwnerAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: owner,
version: 1,
});
const response = await authOwnerAgent.get(`/workflows/2`);
expect(response.statusCode).toBe(401);
});
test('GET /workflows/:id should fail due to non-existing workflow', async () => {
const owner = await testDb.createUser({ globalRole: globalOwnerRole, apiKey: randomApiKey() });
const authOwnerAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: owner,
version: 1,
});
const response = await authOwnerAgent.get(`/workflows/2`);
expect(response.statusCode).toBe(404); expect(response.statusCode).toBe(404);
}); });
test('GET /workflows/:id should retrieve workflow', async () => { test('should retrieve workflow', async () => {
const member = await testDb.createUser({ globalRole: globalMemberRole, apiKey: randomApiKey() });
const authAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: member,
version: 1,
});
// create and assign workflow to owner // create and assign workflow to owner
const workflow = await testDb.createWorkflow({}, member); const workflow = await testDb.createWorkflow({}, member);
const response = await authAgent.get(`/workflows/${workflow.id}`); const response = await authMemberAgent.get(`/workflows/${workflow.id}`);
expect(response.statusCode).toBe(200); expect(response.statusCode).toBe(200);
const { id, connections, active, staticData, nodes, settings, name, createdAt, updatedAt, tags } = const {
response.body; id,
connections,
active,
staticData,
nodes,
settings,
name,
createdAt,
updatedAt,
tags,
} = response.body;
expect(id).toEqual(workflow.id); expect(id).toEqual(workflow.id);
expect(name).toEqual(workflow.name); expect(name).toEqual(workflow.name);
@ -430,17 +358,7 @@ test('GET /workflows/:id should retrieve workflow', async () => {
expect(updatedAt).toEqual(workflow.updatedAt.toISOString()); expect(updatedAt).toEqual(workflow.updatedAt.toISOString());
}); });
test('GET /workflows/:id should retrieve non-owned workflow for owner', async () => { test('should retrieve non-owned workflow for owner', async () => {
const owner = await testDb.createUser({ globalRole: globalOwnerRole, apiKey: randomApiKey() });
const member = await testDb.createUser({ globalRole: globalMemberRole });
const authOwnerAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: owner,
version: 1,
});
// create and assign workflow to owner // create and assign workflow to owner
const workflow = await testDb.createWorkflow({}, member); const workflow = await testDb.createWorkflow({}, member);
@ -461,68 +379,23 @@ test('GET /workflows/:id should retrieve non-owned workflow for owner', async ()
expect(createdAt).toEqual(workflow.createdAt.toISOString()); expect(createdAt).toEqual(workflow.createdAt.toISOString());
expect(updatedAt).toEqual(workflow.updatedAt.toISOString()); expect(updatedAt).toEqual(workflow.updatedAt.toISOString());
}); });
test('DELETE /workflows/:id should fail due to missing API Key', async () => {
const owner = await testDb.createUser({ globalRole: globalOwnerRole });
const authOwnerAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: owner,
version: 1,
}); });
describe('DELETE /workflows/:id', () => {
test('should fail due to missing API Key', testWithAPIKey('delete', '/workflows/2', null));
test('should fail due to invalid API Key', testWithAPIKey('delete', '/workflows/2', 'abcXYZ'));
test('should fail due to non-existing workflow', async () => {
const response = await authOwnerAgent.delete(`/workflows/2`); const response = await authOwnerAgent.delete(`/workflows/2`);
expect(response.statusCode).toBe(401);
});
test('DELETE /workflows/:id should fail due to invalid API Key', async () => {
const owner = await testDb.createUser({ globalRole: globalOwnerRole, apiKey: randomApiKey() });
owner.apiKey = 'abcXYZ';
const authOwnerAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: owner,
version: 1,
});
const response = await authOwnerAgent.delete(`/workflows/2`);
expect(response.statusCode).toBe(401);
});
test('DELETE /workflows/:id should fail due to non-existing workflow', async () => {
const owner = await testDb.createUser({ globalRole: globalOwnerRole, apiKey: randomApiKey() });
const authOwnerAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: owner,
version: 1,
});
const response = await authOwnerAgent.delete(`/workflows/2`);
expect(response.statusCode).toBe(404); expect(response.statusCode).toBe(404);
}); });
test('DELETE /workflows/:id should delete the workflow', async () => { test('should delete the workflow', async () => {
const member = await testDb.createUser({ globalRole: globalMemberRole, apiKey: randomApiKey() });
const authAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: member,
version: 1,
});
// create and assign workflow to owner // create and assign workflow to owner
const workflow = await testDb.createWorkflow({}, member); const workflow = await testDb.createWorkflow({}, member);
const response = await authAgent.delete(`/workflows/${workflow.id}`); const response = await authMemberAgent.delete(`/workflows/${workflow.id}`);
expect(response.statusCode).toBe(200); expect(response.statusCode).toBe(200);
@ -547,21 +420,11 @@ test('DELETE /workflows/:id should delete the workflow', async () => {
expect(sharedWorkflow).toBeNull(); expect(sharedWorkflow).toBeNull();
}); });
test('DELETE /workflows/:id should delete non-owned workflow when owner', async () => { test('should delete non-owned workflow when owner', async () => {
const owner = await testDb.createUser({ globalRole: globalOwnerRole, apiKey: randomApiKey() });
const member = await testDb.createUser({ globalRole: globalMemberRole });
const authAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: owner,
version: 1,
});
// create and assign workflow to owner // create and assign workflow to owner
const workflow = await testDb.createWorkflow({}, member); const workflow = await testDb.createWorkflow({}, member);
const response = await authAgent.delete(`/workflows/${workflow.id}`); const response = await authMemberAgent.delete(`/workflows/${workflow.id}`);
expect(response.statusCode).toBe(200); expect(response.statusCode).toBe(200);
@ -585,84 +448,34 @@ test('DELETE /workflows/:id should delete non-owned workflow when owner', async
expect(sharedWorkflow).toBeNull(); expect(sharedWorkflow).toBeNull();
}); });
test('POST /workflows/:id/activate should fail due to missing API Key', async () => {
const owner = await testDb.createUser({ globalRole: globalOwnerRole });
const authOwnerAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: owner,
version: 1,
}); });
describe('POST /workflows/:id/activate', () => {
test(
'should fail due to missing API Key',
testWithAPIKey('post', '/workflows/2/activate', null),
);
test(
'should fail due to invalid API Key',
testWithAPIKey('post', '/workflows/2/activate', 'abcXYZ'),
);
test('should fail due to non-existing workflow', async () => {
const response = await authOwnerAgent.post(`/workflows/2/activate`); const response = await authOwnerAgent.post(`/workflows/2/activate`);
expect(response.statusCode).toBe(401);
});
test('POST /workflows/:id/activate should fail due to invalid API Key', async () => {
const owner = await testDb.createUser({ globalRole: globalOwnerRole, apiKey: randomApiKey() });
owner.apiKey = 'abcXYZ';
const authOwnerAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: owner,
version: 1,
});
const response = await authOwnerAgent.post(`/workflows/2/activate`);
expect(response.statusCode).toBe(401);
});
test('POST /workflows/:id/activate should fail due to non-existing workflow', async () => {
const owner = await testDb.createUser({ globalRole: globalOwnerRole, apiKey: randomApiKey() });
const authOwnerAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: owner,
version: 1,
});
const response = await authOwnerAgent.post(`/workflows/2/activate`);
expect(response.statusCode).toBe(404); expect(response.statusCode).toBe(404);
}); });
test('POST /workflows/:id/activate should fail due to trying to activate a workflow without a trigger', async () => { test('should fail due to trying to activate a workflow without a trigger', async () => {
const owner = await testDb.createUser({ globalRole: globalOwnerRole, apiKey: randomApiKey() });
const authOwnerAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: owner,
version: 1,
});
const workflow = await testDb.createWorkflow({}, owner); const workflow = await testDb.createWorkflow({}, owner);
const response = await authOwnerAgent.post(`/workflows/${workflow.id}/activate`); const response = await authOwnerAgent.post(`/workflows/${workflow.id}/activate`);
expect(response.statusCode).toBe(400); expect(response.statusCode).toBe(400);
}); });
test('POST /workflows/:id/activate should set workflow as active', async () => { test('should set workflow as active', async () => {
const member = await testDb.createUser({ globalRole: globalMemberRole, apiKey: randomApiKey() });
const authAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: member,
version: 1,
});
const workflow = await testDb.createWorkflowWithTrigger({}, member); const workflow = await testDb.createWorkflowWithTrigger({}, member);
const response = await authAgent.post(`/workflows/${workflow.id}/activate`); const response = await authMemberAgent.post(`/workflows/${workflow.id}/activate`);
expect(response.statusCode).toBe(200); expect(response.statusCode).toBe(200);
@ -694,20 +507,10 @@ test('POST /workflows/:id/activate should set workflow as active', async () => {
expect(await workflowRunner.isActive(workflow.id)).toBe(true); expect(await workflowRunner.isActive(workflow.id)).toBe(true);
}); });
test('POST /workflows/:id/activate should set non-owned workflow as active when owner', async () => { test('should set non-owned workflow as active when owner', async () => {
const owner = await testDb.createUser({ globalRole: globalOwnerRole, apiKey: randomApiKey() });
const member = await testDb.createUser({ globalRole: globalMemberRole });
const authAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: owner,
version: 1,
});
const workflow = await testDb.createWorkflowWithTrigger({}, member); const workflow = await testDb.createWorkflowWithTrigger({}, member);
const response = await authAgent.post(`/workflows/${workflow.id}/activate`); const response = await authMemberAgent.post(`/workflows/${workflow.id}/activate`);
expect(response.statusCode).toBe(200); expect(response.statusCode).toBe(200);
@ -747,69 +550,32 @@ test('POST /workflows/:id/activate should set non-owned workflow as active when
// check whether the workflow is on the active workflow runner // check whether the workflow is on the active workflow runner
expect(await workflowRunner.isActive(workflow.id)).toBe(true); expect(await workflowRunner.isActive(workflow.id)).toBe(true);
}); });
test('POST /workflows/:id/deactivate should fail due to missing API Key', async () => {
const owner = await testDb.createUser({ globalRole: globalOwnerRole });
const authOwnerAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: owner,
version: 1,
}); });
describe('POST /workflows/:id/deactivate', () => {
test(
'should fail due to missing API Key',
testWithAPIKey('post', '/workflows/2/deactivate', null),
);
test(
'should fail due to invalid API Key',
testWithAPIKey('post', '/workflows/2/deactivate', 'abcXYZ'),
);
test('should fail due to non-existing workflow', async () => {
const response = await authOwnerAgent.post(`/workflows/2/deactivate`); const response = await authOwnerAgent.post(`/workflows/2/deactivate`);
expect(response.statusCode).toBe(401);
});
test('POST /workflows/:id/deactivate should fail due to invalid API Key', async () => {
const owner = await testDb.createUser({ globalRole: globalOwnerRole, apiKey: randomApiKey() });
owner.apiKey = 'abcXYZ';
const authOwnerAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: owner,
version: 1,
});
const response = await authOwnerAgent.post(`/workflows/2/deactivate`);
expect(response.statusCode).toBe(401);
});
test('POST /workflows/:id/deactivate should fail due to non-existing workflow', async () => {
const owner = await testDb.createUser({ globalRole: globalOwnerRole, apiKey: randomApiKey() });
const authOwnerAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: owner,
version: 1,
});
const response = await authOwnerAgent.post(`/workflows/2/deactivate`);
expect(response.statusCode).toBe(404); expect(response.statusCode).toBe(404);
}); });
test('POST /workflows/:id/deactivate should deactivate workflow', async () => { test('should deactivate workflow', async () => {
const member = await testDb.createUser({ globalRole: globalMemberRole, apiKey: randomApiKey() });
const authAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: member,
version: 1,
});
const workflow = await testDb.createWorkflowWithTrigger({}, member); const workflow = await testDb.createWorkflowWithTrigger({}, member);
await authAgent.post(`/workflows/${workflow.id}/activate`); await authMemberAgent.post(`/workflows/${workflow.id}/activate`);
const workflowDeactivationResponse = await authAgent.post(`/workflows/${workflow.id}/deactivate`); const workflowDeactivationResponse = await authMemberAgent.post(
`/workflows/${workflow.id}/deactivate`,
);
const { id, connections, active, staticData, nodes, settings, name, createdAt, updatedAt } = const { id, connections, active, staticData, nodes, settings, name, createdAt, updatedAt } =
workflowDeactivationResponse.body; workflowDeactivationResponse.body;
@ -839,22 +605,14 @@ test('POST /workflows/:id/deactivate should deactivate workflow', async () => {
expect(await workflowRunner.isActive(workflow.id)).toBe(false); expect(await workflowRunner.isActive(workflow.id)).toBe(false);
}); });
test('POST /workflows/:id/deactivate should deactivate non-owned workflow when owner', async () => { test('should deactivate non-owned workflow when owner', async () => {
const owner = await testDb.createUser({ globalRole: globalOwnerRole, apiKey: randomApiKey() });
const member = await testDb.createUser({ globalRole: globalMemberRole });
const authAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: owner,
version: 1,
});
const workflow = await testDb.createWorkflowWithTrigger({}, member); const workflow = await testDb.createWorkflowWithTrigger({}, member);
await authAgent.post(`/workflows/${workflow.id}/activate`); await authMemberAgent.post(`/workflows/${workflow.id}/activate`);
const workflowDeactivationResponse = await authAgent.post(`/workflows/${workflow.id}/deactivate`); const workflowDeactivationResponse = await authMemberAgent.post(
`/workflows/${workflow.id}/deactivate`,
);
const { id, connections, active, staticData, nodes, settings, name, createdAt, updatedAt } = const { id, connections, active, staticData, nodes, settings, name, createdAt, updatedAt } =
workflowDeactivationResponse.body; workflowDeactivationResponse.body;
@ -891,64 +649,19 @@ test('POST /workflows/:id/deactivate should deactivate non-owned workflow when o
expect(await workflowRunner.isActive(workflow.id)).toBe(false); expect(await workflowRunner.isActive(workflow.id)).toBe(false);
}); });
test('POST /workflows should fail due to missing API Key', async () => {
const owner = await testDb.createUser({ globalRole: globalOwnerRole });
const authOwnerAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: owner,
version: 1,
}); });
const response = await authOwnerAgent.post('/workflows'); describe('POST /workflows', () => {
test('should fail due to missing API Key', testWithAPIKey('post', '/workflows', null));
expect(response.statusCode).toBe(401); test('should fail due to invalid API Key', testWithAPIKey('post', '/workflows', 'abcXYZ'));
});
test('POST /workflows should fail due to invalid API Key', async () => {
const owner = await testDb.createUser({ globalRole: globalOwnerRole, apiKey: randomApiKey() });
owner.apiKey = 'abcXYZ';
const authOwnerAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: owner,
version: 1,
});
const response = await authOwnerAgent.post('/workflows');
expect(response.statusCode).toBe(401);
});
test('POST /workflows should fail due to invalid body', async () => {
const owner = await testDb.createUser({ globalRole: globalOwnerRole, apiKey: randomApiKey() });
const authOwnerAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: owner,
version: 1,
});
test('should fail due to invalid body', async () => {
const response = await authOwnerAgent.post('/workflows').send({}); const response = await authOwnerAgent.post('/workflows').send({});
expect(response.statusCode).toBe(400); expect(response.statusCode).toBe(400);
}); });
test('POST /workflows should create workflow', async () => { test('should create workflow', async () => {
const member = await testDb.createUser({ globalRole: globalMemberRole, apiKey: randomApiKey() });
const authAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: member,
version: 1,
});
const payload = { const payload = {
name: 'testing', name: 'testing',
nodes: [ nodes: [
@ -973,7 +686,7 @@ test('POST /workflows should create workflow', async () => {
}, },
}; };
const response = await authAgent.post('/workflows').send(payload); const response = await authMemberAgent.post('/workflows').send(payload);
expect(response.statusCode).toBe(200); expect(response.statusCode).toBe(200);
@ -1003,49 +716,14 @@ test('POST /workflows should create workflow', async () => {
expect(sharedWorkflow?.workflow.createdAt.toISOString()).toBe(createdAt); expect(sharedWorkflow?.workflow.createdAt.toISOString()).toBe(createdAt);
expect(sharedWorkflow?.role).toEqual(workflowOwnerRole); expect(sharedWorkflow?.role).toEqual(workflowOwnerRole);
}); });
test('PUT /workflows/:id should fail due to missing API Key', async () => {
const owner = await testDb.createUser({ globalRole: globalOwnerRole });
const authOwnerAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: owner,
version: 1,
}); });
const response = await authOwnerAgent.put(`/workflows/1`); describe('PUT /workflows/:id', () => {
test('should fail due to missing API Key', testWithAPIKey('put', '/workflows/1', null));
expect(response.statusCode).toBe(401); test('should fail due to invalid API Key', testWithAPIKey('put', '/workflows/1', 'abcXYZ'));
});
test('PUT /workflows/:id should fail due to invalid API Key', async () => {
const owner = await testDb.createUser({ globalRole: globalOwnerRole, apiKey: randomApiKey() });
owner.apiKey = 'abcXYZ';
const authOwnerAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: owner,
version: 1,
});
const response = await authOwnerAgent.put(`/workflows/1`).send({});
expect(response.statusCode).toBe(401);
});
test('PUT /workflows/:id should fail due to non-existing workflow', async () => {
const owner = await testDb.createUser({ globalRole: globalOwnerRole, apiKey: randomApiKey() });
const authOwnerAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: owner,
version: 1,
});
test('should fail due to non-existing workflow', async () => {
const response = await authOwnerAgent.put(`/workflows/1`).send({ const response = await authOwnerAgent.put(`/workflows/1`).send({
name: 'testing', name: 'testing',
nodes: [ nodes: [
@ -1073,16 +751,7 @@ test('PUT /workflows/:id should fail due to non-existing workflow', async () =>
expect(response.statusCode).toBe(404); expect(response.statusCode).toBe(404);
}); });
test('PUT /workflows/:id should fail due to invalid body', async () => { test('should fail due to invalid body', async () => {
const owner = await testDb.createUser({ globalRole: globalOwnerRole, apiKey: randomApiKey() });
const authOwnerAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: owner,
version: 1,
});
const response = await authOwnerAgent.put(`/workflows/1`).send({ const response = await authOwnerAgent.put(`/workflows/1`).send({
nodes: [ nodes: [
{ {
@ -1109,18 +778,8 @@ test('PUT /workflows/:id should fail due to invalid body', async () => {
expect(response.statusCode).toBe(400); expect(response.statusCode).toBe(400);
}); });
test('PUT /workflows/:id should update workflow', async () => { test('should update workflow', async () => {
const member = await testDb.createUser({ globalRole: globalOwnerRole, apiKey: randomApiKey() });
const workflow = await testDb.createWorkflow({}, member); const workflow = await testDb.createWorkflow({}, member);
const authAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: member,
version: 1,
});
const payload = { const payload = {
name: 'name updated', name: 'name updated',
nodes: [ nodes: [
@ -1153,7 +812,7 @@ test('PUT /workflows/:id should update workflow', async () => {
}, },
}; };
const response = await authAgent.put(`/workflows/${workflow.id}`).send(payload); const response = await authMemberAgent.put(`/workflows/${workflow.id}`).send(payload);
const { id, name, nodes, connections, staticData, active, settings, createdAt, updatedAt } = const { id, name, nodes, connections, staticData, active, settings, createdAt, updatedAt } =
response.body; response.body;
@ -1185,19 +844,9 @@ test('PUT /workflows/:id should update workflow', async () => {
); );
}); });
test('PUT /workflows/:id should update non-owned workflow if owner', async () => { test('should update non-owned workflow if owner', async () => {
const owner = await testDb.createUser({ globalRole: globalOwnerRole, apiKey: randomApiKey() });
const member = await testDb.createUser({ globalRole: globalMemberRole });
const workflow = await testDb.createWorkflow({}, member); const workflow = await testDb.createWorkflow({}, member);
const authAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: owner,
version: 1,
});
const payload = { const payload = {
name: 'name owner updated', name: 'name owner updated',
nodes: [ nodes: [
@ -1230,7 +879,7 @@ test('PUT /workflows/:id should update non-owned workflow if owner', async () =>
}, },
}; };
const response = await authAgent.put(`/workflows/${workflow.id}`).send(payload); const response = await authMemberAgent.put(`/workflows/${workflow.id}`).send(payload);
const { id, name, nodes, connections, staticData, active, settings, createdAt, updatedAt } = const { id, name, nodes, connections, staticData, active, settings, createdAt, updatedAt } =
response.body; response.body;
@ -1271,3 +920,5 @@ test('PUT /workflows/:id should update non-owned workflow if owner', async () =>
); );
expect(sharedWorkflow?.role).toEqual(workflowOwnerRole); expect(sharedWorkflow?.role).toEqual(workflowOwnerRole);
}); });
});
});

View file

@ -172,7 +172,7 @@ export async function createUser(attributes: Partial<User> = {}): Promise<User>
password: await hashPassword(password ?? randomValidPassword()), password: await hashPassword(password ?? randomValidPassword()),
firstName: firstName ?? randomName(), firstName: firstName ?? randomName(),
lastName: lastName ?? randomName(), lastName: lastName ?? randomName(),
globalRole: globalRole ?? (await getGlobalMemberRole()), globalRoleId: (globalRole ?? (await getGlobalMemberRole())).id,
...rest, ...rest,
}; };