Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): Fix resuming executions on test webhooks from Wait forms #13410

Merged
merged 2 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions packages/cli/src/webhooks/__tests__/waiting-forms.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import type express from 'express';
import { mock } from 'jest-mock-extended';
import { FORM_NODE_TYPE, type Workflow } from 'n8n-workflow';

import type { ExecutionRepository } from '@/databases/repositories/execution.repository';
import { WaitingForms } from '@/webhooks/waiting-forms';

import type { IExecutionResponse } from '../../interfaces';
import type { WaitingWebhookRequest } from '../webhook.types';

describe('WaitingForms', () => {
const executionRepository = mock<ExecutionRepository>();
const waitingWebhooks = new WaitingForms(mock(), mock(), executionRepository, mock());
const waitingForms = new WaitingForms(mock(), mock(), executionRepository, mock());

beforeEach(() => {
jest.restoreAllMocks();
Expand All @@ -27,7 +31,7 @@ describe('WaitingForms', () => {
},
});

const result = waitingWebhooks.findCompletionPage(workflow, {}, 'Form1');
const result = waitingForms.findCompletionPage(workflow, {}, 'Form1');
expect(result).toBe('Form1');
});

Expand All @@ -45,7 +49,7 @@ describe('WaitingForms', () => {
},
});

const result = waitingWebhooks.findCompletionPage(workflow, {}, 'Form1');
const result = waitingForms.findCompletionPage(workflow, {}, 'Form1');
expect(result).toBeUndefined();
});

Expand All @@ -61,7 +65,7 @@ describe('WaitingForms', () => {
},
});

const result = waitingWebhooks.findCompletionPage(workflow, {}, 'NonForm');
const result = waitingForms.findCompletionPage(workflow, {}, 'NonForm');
expect(result).toBeUndefined();
});

Expand All @@ -79,7 +83,7 @@ describe('WaitingForms', () => {
},
});

const result = waitingWebhooks.findCompletionPage(workflow, {}, 'Form1');
const result = waitingForms.findCompletionPage(workflow, {}, 'Form1');
expect(result).toBeUndefined();
});

Expand Down Expand Up @@ -121,7 +125,7 @@ describe('WaitingForms', () => {
Form3: [],
};

const result = waitingWebhooks.findCompletionPage(workflow, runData, 'LastNode');
const result = waitingForms.findCompletionPage(workflow, runData, 'LastNode');
expect(result).toBe('Form3');
});

Expand Down Expand Up @@ -151,7 +155,7 @@ describe('WaitingForms', () => {
},
});

const result = waitingWebhooks.findCompletionPage(workflow, {}, 'LastNode');
const result = waitingForms.findCompletionPage(workflow, {}, 'LastNode');
expect(result).toBeUndefined();
});

Expand Down Expand Up @@ -192,8 +196,29 @@ describe('WaitingForms', () => {
Form2: [],
};

const result = waitingWebhooks.findCompletionPage(workflow, runData, 'LastNode');
const result = waitingForms.findCompletionPage(workflow, runData, 'LastNode');
expect(result).toBe('Form2');
});

it('should mark as test form webhook when execution mode is manual', async () => {
jest
// @ts-expect-error Protected method
.spyOn(waitingForms, 'getWebhookExecutionData')
// @ts-expect-error Protected method
.mockResolvedValue(mock<IWebhookResponseCallbackData>());

const execution = mock<IExecutionResponse>({
finished: false,
mode: 'manual',
data: {
resultData: { lastNodeExecuted: 'someNode', error: undefined },
},
});
executionRepository.findSingleExecution.mockResolvedValue(execution);

await waitingForms.executeWebhook(mock<WaitingWebhookRequest>(), mock<express.Response>());

expect(execution.data.isTestWebhook).toBe(true);
});
});
});
6 changes: 6 additions & 0 deletions packages/cli/src/webhooks/waiting-forms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ export class WaitingForms extends WaitingWebhooks {
}
}

/**
* A manual execution resumed by a webhook call needs to be marked as such
* so workers in scaling mode reuse the existing execution data.
*/
if (execution.mode === 'manual') execution.data.isTestWebhook = true;

return await this.getWebhookExecutionData({
execution,
req,
Expand Down
Loading