mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
fix(Wait Node): Add validation for wait amount and unit (#18239)
This commit is contained in:
@@ -14,6 +14,7 @@ import {
|
|||||||
NodeOperationError,
|
NodeOperationError,
|
||||||
} from 'n8n-workflow';
|
} from 'n8n-workflow';
|
||||||
|
|
||||||
|
import { validateWaitAmount, validateWaitUnit } from './validation';
|
||||||
import { updateDisplayOptions } from '../../utils/utilities';
|
import { updateDisplayOptions } from '../../utils/utilities';
|
||||||
import {
|
import {
|
||||||
formDescription,
|
formDescription,
|
||||||
@@ -47,6 +48,7 @@ const toWaitAmount: INodeProperties = {
|
|||||||
},
|
},
|
||||||
default: 1,
|
default: 1,
|
||||||
description: 'The time to wait',
|
description: 'The time to wait',
|
||||||
|
validateType: 'number',
|
||||||
};
|
};
|
||||||
|
|
||||||
const unitSelector: INodeProperties = {
|
const unitSelector: INodeProperties = {
|
||||||
@@ -487,9 +489,24 @@ export class Wait extends Webhook {
|
|||||||
|
|
||||||
let waitTill: Date;
|
let waitTill: Date;
|
||||||
if (resume === 'timeInterval') {
|
if (resume === 'timeInterval') {
|
||||||
const unit = context.getNodeParameter('unit', 0) as string;
|
const unit = context.getNodeParameter('unit', 0);
|
||||||
|
|
||||||
|
if (!validateWaitUnit(unit)) {
|
||||||
|
throw new NodeOperationError(
|
||||||
|
context.getNode(),
|
||||||
|
"Invalid wait unit. Valid units are 'seconds', 'minutes', 'hours', or 'days'.",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let waitAmount = context.getNodeParameter('amount', 0);
|
||||||
|
|
||||||
|
if (!validateWaitAmount(waitAmount)) {
|
||||||
|
throw new NodeOperationError(
|
||||||
|
context.getNode(),
|
||||||
|
'Invalid wait amount. It must be a positive number.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
let waitAmount = context.getNodeParameter('amount', 0) as number;
|
|
||||||
if (unit === 'minutes') {
|
if (unit === 'minutes') {
|
||||||
waitAmount *= 60;
|
waitAmount *= 60;
|
||||||
}
|
}
|
||||||
@@ -514,7 +531,7 @@ export class Wait extends Webhook {
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw new NodeOperationError(
|
throw new NodeOperationError(
|
||||||
context.getNode(),
|
context.getNode(),
|
||||||
'[Wait node] Cannot put execution to wait because `dateTime` parameter is not a valid date. Please pick a specific date and time to wait until.',
|
'Cannot put execution to wait because `dateTime` parameter is not a valid date. Please pick a specific date and time to wait until.',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ describe('Execute Wait Node', () => {
|
|||||||
|
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
timer = setInterval(() => jest.advanceTimersByTime(1000), 10);
|
timer = setInterval(() => jest.advanceTimersByTime(1000), 10);
|
||||||
jest.useFakeTimers();
|
jest.useFakeTimers().setSystemTime(new Date('2025-01-01'));
|
||||||
});
|
});
|
||||||
|
|
||||||
afterAll(() => {
|
afterAll(() => {
|
||||||
@@ -67,5 +67,71 @@ describe('Execute Wait Node', () => {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
describe('Validation', () => {
|
||||||
|
describe('Time interval', () => {
|
||||||
|
it.each([
|
||||||
|
{
|
||||||
|
unit: 'seconds',
|
||||||
|
amount: 300,
|
||||||
|
expectedWaitTill: () => DateTime.now().plus({ seconds: 300 }).toJSDate(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
unit: 'minutes',
|
||||||
|
amount: 2,
|
||||||
|
expectedWaitTill: () => DateTime.now().plus({ minutes: 2 }).toJSDate(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
unit: 'hours',
|
||||||
|
amount: 1,
|
||||||
|
expectedWaitTill: () => DateTime.now().plus({ hours: 1 }).toJSDate(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
unit: 'days',
|
||||||
|
amount: 10,
|
||||||
|
expectedWaitTill: () => DateTime.now().plus({ days: 10 }).toJSDate(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
unit: 'seconds',
|
||||||
|
amount: -10,
|
||||||
|
error: 'Invalid wait amount. It must be a positive number.',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
unit: 'years',
|
||||||
|
amount: 10,
|
||||||
|
error: "Invalid wait unit. Valid units are 'seconds', 'minutes', 'hours', or 'days'.",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
unit: 'minutes',
|
||||||
|
amount: 'test',
|
||||||
|
error: 'Invalid wait amount. It must be a positive number.',
|
||||||
|
},
|
||||||
|
])(
|
||||||
|
'Validate wait unit: $unit, amount: $amount',
|
||||||
|
async ({ unit, amount, expectedWaitTill, error }) => {
|
||||||
|
const putExecutionToWaitSpy = jest.fn();
|
||||||
|
const waitNode = new Wait();
|
||||||
|
const executeFunctionsMock = mock<IExecuteFunctions>({
|
||||||
|
getNodeParameter: jest.fn().mockImplementation((paramName: string) => {
|
||||||
|
if (paramName === 'resume') return 'timeInterval';
|
||||||
|
if (paramName === 'amount') return amount;
|
||||||
|
if (paramName === 'unit') return unit;
|
||||||
|
}),
|
||||||
|
getTimezone: jest.fn().mockReturnValue('UTC'),
|
||||||
|
putExecutionToWait: putExecutionToWaitSpy,
|
||||||
|
getInputData: jest.fn(),
|
||||||
|
getNode: jest.fn(),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!error) {
|
||||||
|
await expect(waitNode.execute(executeFunctionsMock)).resolves.not.toThrow();
|
||||||
|
expect(putExecutionToWaitSpy).toHaveBeenCalledWith(expectedWaitTill?.());
|
||||||
|
} else {
|
||||||
|
await expect(waitNode.execute(executeFunctionsMock)).rejects.toThrowError(error);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
new NodeTestHarness().setupTests();
|
new NodeTestHarness().setupTests();
|
||||||
});
|
});
|
||||||
|
|||||||
8
packages/nodes-base/nodes/Wait/validation.ts
Normal file
8
packages/nodes-base/nodes/Wait/validation.ts
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
export function validateWaitAmount(amount: unknown): amount is number {
|
||||||
|
return typeof amount === 'number' && amount > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type WaitUnit = 'seconds' | 'minutes' | 'hours' | 'days';
|
||||||
|
export function validateWaitUnit(unit: unknown): unit is WaitUnit {
|
||||||
|
return typeof unit === 'string' && ['seconds', 'minutes', 'hours', 'days'].includes(unit);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user