2023-03-17 09:24:05 -07:00
|
|
|
import type { Application } from 'express';
|
|
|
|
import type { SuperAgentTest } from 'supertest';
|
2022-11-09 06:25:00 -08:00
|
|
|
import config from '@/config';
|
2023-03-17 09:24:05 -07:00
|
|
|
import type { User } from '@db/entities/User';
|
|
|
|
import type { ActiveWorkflowRunner } from '@/ActiveWorkflowRunner';
|
2022-06-08 11:53:12 -07:00
|
|
|
|
2022-11-09 06:25:00 -08:00
|
|
|
import { randomApiKey } from '../shared/random';
|
2022-06-08 11:53:12 -07:00
|
|
|
import * as utils from '../shared/utils';
|
|
|
|
import * as testDb from '../shared/testDb';
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
let app: Application;
|
|
|
|
let owner: User;
|
|
|
|
let authOwnerAgent: SuperAgentTest;
|
2022-11-09 06:25:00 -08:00
|
|
|
let workflowRunner: ActiveWorkflowRunner;
|
2022-06-08 11:53:12 -07:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
2023-01-26 02:44:50 -08:00
|
|
|
app = await utils.initTestServer({
|
|
|
|
endpointGroups: ['publicApi'],
|
|
|
|
applyAuth: false,
|
|
|
|
enablePublicAPI: true,
|
|
|
|
});
|
2022-06-08 11:53:12 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const globalOwnerRole = await testDb.getGlobalOwnerRole();
|
|
|
|
owner = await testDb.createUser({ globalRole: globalOwnerRole, apiKey: randomApiKey() });
|
2022-06-08 11:53:12 -07:00
|
|
|
|
|
|
|
await utils.initBinaryManager();
|
|
|
|
await utils.initNodeTypes();
|
2022-06-14 09:32:19 -07:00
|
|
|
|
2022-06-08 11:53:12 -07:00
|
|
|
workflowRunner = await utils.initActiveWorkflowRunner();
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
2023-01-13 09:12:22 -08:00
|
|
|
await testDb.truncate([
|
|
|
|
'SharedCredentials',
|
|
|
|
'SharedWorkflow',
|
|
|
|
'Workflow',
|
|
|
|
'Credentials',
|
|
|
|
'Execution',
|
|
|
|
'Settings',
|
|
|
|
]);
|
2022-06-08 11:53:12 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
authOwnerAgent = utils.createAgent(app, {
|
2022-06-08 11:53:12 -07:00
|
|
|
apiPath: 'public',
|
|
|
|
auth: true,
|
|
|
|
user: owner,
|
|
|
|
version: 1,
|
|
|
|
});
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
config.set('userManagement.disabled', false);
|
|
|
|
config.set('userManagement.isInstanceOwnerSetUp', true);
|
2022-06-08 11:53:12 -07:00
|
|
|
});
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
afterEach(async () => {
|
|
|
|
await workflowRunner?.removeAll();
|
2022-06-08 11:53:12 -07:00
|
|
|
});
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
afterAll(async () => {
|
|
|
|
await testDb.terminate();
|
2022-06-08 11:53:12 -07:00
|
|
|
});
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const testWithAPIKey =
|
|
|
|
(method: 'get' | 'post' | 'put' | 'delete', url: string, apiKey: string | null) => async () => {
|
|
|
|
authOwnerAgent.set({ 'X-N8N-API-KEY': apiKey });
|
|
|
|
const response = await authOwnerAgent[method](url);
|
|
|
|
expect(response.statusCode).toBe(401);
|
|
|
|
};
|
2022-06-08 11:53:12 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
describe('GET /executions/:id', () => {
|
|
|
|
test('should fail due to missing API Key', testWithAPIKey('get', '/executions/1', null));
|
2022-06-08 11:53:12 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
test('should fail due to invalid API Key', testWithAPIKey('get', '/executions/1', 'abcXYZ'));
|
2022-06-08 11:53:12 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
test('should get an execution', async () => {
|
|
|
|
const workflow = await testDb.createWorkflow({}, owner);
|
2022-06-08 11:53:12 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const execution = await testDb.createSuccessfulExecution(workflow);
|
2022-06-08 11:53:12 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const response = await authOwnerAgent.get(`/executions/${execution.id}`);
|
2022-06-08 11:53:12 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(response.statusCode).toBe(200);
|
2022-06-08 11:53:12 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const {
|
|
|
|
id,
|
|
|
|
finished,
|
|
|
|
mode,
|
|
|
|
retryOf,
|
|
|
|
retrySuccessId,
|
|
|
|
startedAt,
|
|
|
|
stoppedAt,
|
|
|
|
workflowId,
|
|
|
|
waitTill,
|
|
|
|
} = response.body;
|
2022-06-08 11:53:12 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(id).toBeDefined();
|
|
|
|
expect(finished).toBe(true);
|
|
|
|
expect(mode).toEqual(execution.mode);
|
|
|
|
expect(retrySuccessId).toBeNull();
|
|
|
|
expect(retryOf).toBeNull();
|
|
|
|
expect(startedAt).not.toBeNull();
|
|
|
|
expect(stoppedAt).not.toBeNull();
|
|
|
|
expect(workflowId).toBe(execution.workflowId);
|
|
|
|
expect(waitTill).toBeNull();
|
2022-06-08 11:53:12 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
describe('DELETE /executions/:id', () => {
|
|
|
|
test('should fail due to missing API Key', testWithAPIKey('delete', '/executions/1', null));
|
2022-06-08 11:53:12 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
test('should fail due to invalid API Key', testWithAPIKey('delete', '/executions/1', 'abcXYZ'));
|
2022-06-08 11:53:12 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
test('should delete an execution', async () => {
|
|
|
|
const workflow = await testDb.createWorkflow({}, owner);
|
|
|
|
const execution = await testDb.createSuccessfulExecution(workflow);
|
2022-06-08 11:53:12 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const response = await authOwnerAgent.delete(`/executions/${execution.id}`);
|
2022-06-08 11:53:12 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(response.statusCode).toBe(200);
|
2022-06-08 11:53:12 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const {
|
|
|
|
id,
|
|
|
|
finished,
|
|
|
|
mode,
|
|
|
|
retryOf,
|
|
|
|
retrySuccessId,
|
|
|
|
startedAt,
|
|
|
|
stoppedAt,
|
|
|
|
workflowId,
|
|
|
|
waitTill,
|
|
|
|
} = response.body;
|
2022-06-08 11:53:12 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(id).toBeDefined();
|
|
|
|
expect(finished).toBe(true);
|
|
|
|
expect(mode).toEqual(execution.mode);
|
|
|
|
expect(retrySuccessId).toBeNull();
|
|
|
|
expect(retryOf).toBeNull();
|
|
|
|
expect(startedAt).not.toBeNull();
|
|
|
|
expect(stoppedAt).not.toBeNull();
|
|
|
|
expect(workflowId).toBe(execution.workflowId);
|
|
|
|
expect(waitTill).toBeNull();
|
2022-06-08 11:53:12 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
describe('GET /executions', () => {
|
|
|
|
test('should fail due to missing API Key', testWithAPIKey('get', '/executions', null));
|
2022-08-08 13:15:56 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
test('should fail due to invalid API Key', testWithAPIKey('get', '/executions', 'abcXYZ'));
|
2022-08-08 13:15:56 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
test('should retrieve all successful executions', async () => {
|
|
|
|
const workflow = await testDb.createWorkflow({}, owner);
|
2022-08-08 13:15:56 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const successfulExecution = await testDb.createSuccessfulExecution(workflow);
|
2022-08-08 13:15:56 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
await testDb.createErrorExecution(workflow);
|
2022-08-08 13:15:56 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const response = await authOwnerAgent.get(`/executions`).query({
|
|
|
|
status: 'success',
|
|
|
|
});
|
2022-08-08 13:15:56 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
expect(response.body.data.length).toBe(1);
|
|
|
|
expect(response.body.nextCursor).toBe(null);
|
2022-08-08 13:15:56 -07:00
|
|
|
|
|
|
|
const {
|
|
|
|
id,
|
|
|
|
finished,
|
|
|
|
mode,
|
|
|
|
retryOf,
|
|
|
|
retrySuccessId,
|
|
|
|
startedAt,
|
|
|
|
stoppedAt,
|
|
|
|
workflowId,
|
|
|
|
waitTill,
|
2023-03-17 09:24:05 -07:00
|
|
|
} = response.body.data[0];
|
2022-08-08 13:15:56 -07:00
|
|
|
|
|
|
|
expect(id).toBeDefined();
|
|
|
|
expect(finished).toBe(true);
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(mode).toEqual(successfulExecution.mode);
|
2022-08-08 13:15:56 -07:00
|
|
|
expect(retrySuccessId).toBeNull();
|
|
|
|
expect(retryOf).toBeNull();
|
|
|
|
expect(startedAt).not.toBeNull();
|
|
|
|
expect(stoppedAt).not.toBeNull();
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(workflowId).toBe(successfulExecution.workflowId);
|
2022-08-08 13:15:56 -07:00
|
|
|
expect(waitTill).toBeNull();
|
2022-06-08 11:53:12 -07:00
|
|
|
});
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
// failing on Postgres and MySQL - ref: https://github.com/n8n-io/n8n/pull/3834
|
|
|
|
test.skip('should paginate two executions', async () => {
|
|
|
|
const workflow = await testDb.createWorkflow({}, owner);
|
|
|
|
|
|
|
|
const firstSuccessfulExecution = await testDb.createSuccessfulExecution(workflow);
|
|
|
|
|
|
|
|
const secondSuccessfulExecution = await testDb.createSuccessfulExecution(workflow);
|
|
|
|
|
|
|
|
await testDb.createErrorExecution(workflow);
|
|
|
|
|
|
|
|
const firstExecutionResponse = await authOwnerAgent.get(`/executions`).query({
|
|
|
|
status: 'success',
|
|
|
|
limit: 1,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(firstExecutionResponse.statusCode).toBe(200);
|
|
|
|
expect(firstExecutionResponse.body.data.length).toBe(1);
|
|
|
|
expect(firstExecutionResponse.body.nextCursor).toBeDefined();
|
|
|
|
|
|
|
|
const secondExecutionResponse = await authOwnerAgent.get(`/executions`).query({
|
|
|
|
status: 'success',
|
|
|
|
limit: 1,
|
|
|
|
cursor: firstExecutionResponse.body.nextCursor,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(secondExecutionResponse.statusCode).toBe(200);
|
|
|
|
expect(secondExecutionResponse.body.data.length).toBe(1);
|
|
|
|
expect(secondExecutionResponse.body.nextCursor).toBeNull();
|
|
|
|
|
|
|
|
const successfulExecutions = [firstSuccessfulExecution, secondSuccessfulExecution];
|
|
|
|
const executions = [...firstExecutionResponse.body.data, ...secondExecutionResponse.body.data];
|
|
|
|
|
|
|
|
for (let i = 0; i < executions.length; i++) {
|
|
|
|
const {
|
|
|
|
id,
|
|
|
|
finished,
|
|
|
|
mode,
|
|
|
|
retryOf,
|
|
|
|
retrySuccessId,
|
|
|
|
startedAt,
|
|
|
|
stoppedAt,
|
|
|
|
workflowId,
|
|
|
|
waitTill,
|
|
|
|
} = executions[i];
|
|
|
|
|
|
|
|
expect(id).toBeDefined();
|
|
|
|
expect(finished).toBe(true);
|
|
|
|
expect(mode).toEqual(successfulExecutions[i].mode);
|
|
|
|
expect(retrySuccessId).toBeNull();
|
|
|
|
expect(retryOf).toBeNull();
|
|
|
|
expect(startedAt).not.toBeNull();
|
|
|
|
expect(stoppedAt).not.toBeNull();
|
|
|
|
expect(workflowId).toBe(successfulExecutions[i].workflowId);
|
|
|
|
expect(waitTill).toBeNull();
|
|
|
|
}
|
2022-06-08 11:53:12 -07:00
|
|
|
});
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
test('should retrieve all error executions', async () => {
|
|
|
|
const workflow = await testDb.createWorkflow({}, owner);
|
2022-06-08 11:53:12 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
await testDb.createSuccessfulExecution(workflow);
|
2022-06-08 11:53:12 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const errorExecution = await testDb.createErrorExecution(workflow);
|
2022-06-08 11:53:12 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const response = await authOwnerAgent.get(`/executions`).query({
|
|
|
|
status: 'error',
|
|
|
|
});
|
2022-06-08 11:53:12 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
expect(response.body.data.length).toBe(1);
|
|
|
|
expect(response.body.nextCursor).toBe(null);
|
2022-06-08 11:53:12 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const {
|
|
|
|
id,
|
|
|
|
finished,
|
|
|
|
mode,
|
|
|
|
retryOf,
|
|
|
|
retrySuccessId,
|
|
|
|
startedAt,
|
|
|
|
stoppedAt,
|
|
|
|
workflowId,
|
|
|
|
waitTill,
|
|
|
|
} = response.body.data[0];
|
2022-06-08 11:53:12 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(id).toBeDefined();
|
|
|
|
expect(finished).toBe(false);
|
|
|
|
expect(mode).toEqual(errorExecution.mode);
|
|
|
|
expect(retrySuccessId).toBeNull();
|
|
|
|
expect(retryOf).toBeNull();
|
|
|
|
expect(startedAt).not.toBeNull();
|
|
|
|
expect(stoppedAt).not.toBeNull();
|
|
|
|
expect(workflowId).toBe(errorExecution.workflowId);
|
|
|
|
expect(waitTill).toBeNull();
|
2022-06-08 11:53:12 -07:00
|
|
|
});
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
test('should return all waiting executions', async () => {
|
|
|
|
const workflow = await testDb.createWorkflow({}, owner);
|
2022-06-08 11:53:12 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
await testDb.createSuccessfulExecution(workflow);
|
2022-06-08 11:53:12 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
await testDb.createErrorExecution(workflow);
|
2022-06-08 11:53:12 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const waitingExecution = await testDb.createWaitingExecution(workflow);
|
2022-06-08 11:53:12 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const response = await authOwnerAgent.get(`/executions`).query({
|
|
|
|
status: 'waiting',
|
|
|
|
});
|
2022-06-08 11:53:12 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
expect(response.body.data.length).toBe(1);
|
|
|
|
expect(response.body.nextCursor).toBe(null);
|
2022-06-08 11:53:12 -07:00
|
|
|
|
|
|
|
const {
|
|
|
|
id,
|
|
|
|
finished,
|
|
|
|
mode,
|
|
|
|
retryOf,
|
|
|
|
retrySuccessId,
|
|
|
|
startedAt,
|
|
|
|
stoppedAt,
|
|
|
|
workflowId,
|
|
|
|
waitTill,
|
2023-03-17 09:24:05 -07:00
|
|
|
} = response.body.data[0];
|
2022-06-08 11:53:12 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(id).toBeDefined();
|
|
|
|
expect(finished).toBe(false);
|
|
|
|
expect(mode).toEqual(waitingExecution.mode);
|
2022-06-08 11:53:12 -07:00
|
|
|
expect(retrySuccessId).toBeNull();
|
|
|
|
expect(retryOf).toBeNull();
|
|
|
|
expect(startedAt).not.toBeNull();
|
|
|
|
expect(stoppedAt).not.toBeNull();
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(workflowId).toBe(waitingExecution.workflowId);
|
|
|
|
expect(new Date(waitTill).getTime()).toBeGreaterThan(Date.now() - 1000);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should retrieve all executions of specific workflow', async () => {
|
|
|
|
const [workflow, workflow2] = await testDb.createManyWorkflows(2, {}, owner);
|
|
|
|
|
|
|
|
const savedExecutions = await testDb.createManyExecutions(
|
|
|
|
2,
|
|
|
|
workflow,
|
|
|
|
// @ts-ignore
|
|
|
|
testDb.createSuccessfulExecution,
|
|
|
|
);
|
|
|
|
// @ts-ignore
|
|
|
|
await testDb.createManyExecutions(2, workflow2, testDb.createSuccessfulExecution);
|
|
|
|
|
|
|
|
const response = await authOwnerAgent.get(`/executions`).query({
|
|
|
|
workflowId: workflow.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
expect(response.body.data.length).toBe(2);
|
|
|
|
expect(response.body.nextCursor).toBe(null);
|
|
|
|
|
|
|
|
for (const execution of response.body.data) {
|
|
|
|
const {
|
|
|
|
id,
|
|
|
|
finished,
|
|
|
|
mode,
|
|
|
|
retryOf,
|
|
|
|
retrySuccessId,
|
|
|
|
startedAt,
|
|
|
|
stoppedAt,
|
|
|
|
workflowId,
|
|
|
|
waitTill,
|
|
|
|
} = execution;
|
|
|
|
|
|
|
|
expect(savedExecutions.some((exec) => exec.id === id)).toBe(true);
|
|
|
|
expect(finished).toBe(true);
|
|
|
|
expect(mode).toBeDefined();
|
|
|
|
expect(retrySuccessId).toBeNull();
|
|
|
|
expect(retryOf).toBeNull();
|
|
|
|
expect(startedAt).not.toBeNull();
|
|
|
|
expect(stoppedAt).not.toBeNull();
|
|
|
|
expect(workflowId).toBe(workflow.id);
|
|
|
|
expect(waitTill).toBeNull();
|
|
|
|
}
|
|
|
|
});
|
2022-06-08 11:53:12 -07:00
|
|
|
});
|