first commit
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled

This commit is contained in:
2026-03-17 16:22:57 +03:30
commit 3d5eaf9445
15349 changed files with 2847338 additions and 0 deletions
@@ -0,0 +1,289 @@
import { mock } from 'jest-mock-extended';
import type { IBinaryData, IExecuteFunctions, INodeExecutionData } from 'n8n-workflow';
import { BINARY_ENCODING } from 'n8n-workflow';
import { type WorkSheet, utils as xlsxUtils, write as xlsxWrite } from 'xlsx';
import {
convertJsonToSpreadsheetBinary,
extractDataFromPDF,
prepareBinariesDataList,
} from '@utils/binary';
jest.mock('xlsx', () => ({
utils: {
json_to_sheet: jest.fn(),
},
write: jest.fn(),
}));
jest.mock('pdfjs-dist/legacy/build/pdf.mjs', () => ({
getDocument: jest.fn(),
version: '5.3.31',
}));
describe('convertJsonToSpreadsheetBinary', () => {
const helpers = mock<IExecuteFunctions['helpers']>();
const executeFunctions = mock<IExecuteFunctions>({ helpers });
const items = [
{ json: { key1: 'value1', key2: 'value2' } },
{ json: { key1: 'value3', key2: 'value4' } },
] as INodeExecutionData[];
const mockSheet = mock<WorkSheet>();
const workBook = {
SheetNames: ['Sheet'],
Sheets: {
Sheet: mockSheet,
},
};
const mockBuffer = mock<Buffer>();
const mockBinaryData = mock<IBinaryData>({ id: 'binaryId' });
beforeEach(() => {
jest.clearAllMocks();
(xlsxUtils.json_to_sheet as jest.Mock).mockReturnValue(mockSheet);
(xlsxWrite as jest.Mock).mockReturnValue(mockBuffer);
helpers.prepareBinaryData.mockResolvedValue(mockBinaryData);
});
describe('for fileFormat xlsx', () => {
it('should convert from JSON', async () => {
const result = await convertJsonToSpreadsheetBinary.call(executeFunctions, items, 'xlsx', {});
expect(result).toEqual(mockBinaryData);
expect(xlsxUtils.json_to_sheet).toHaveBeenCalledWith(
items.map((item) => item.json),
undefined,
);
expect(xlsxWrite).toHaveBeenCalledWith(workBook, {
bookType: 'xlsx',
bookSST: false,
type: 'buffer',
});
expect(helpers.prepareBinaryData).toHaveBeenCalledWith(mockBuffer, 'spreadsheet.xlsx');
});
});
describe('for fileFormat csv', () => {
it('should convert from JSON', async () => {
const result = await convertJsonToSpreadsheetBinary.call(executeFunctions, items, 'csv', {});
expect(result).toEqual(mockBinaryData);
expect(xlsxUtils.json_to_sheet).toHaveBeenCalledWith(
items.map((item) => item.json),
undefined,
);
expect(xlsxWrite).toHaveBeenCalledWith(workBook, {
bookType: 'csv',
bookSST: false,
type: 'buffer',
});
expect(helpers.prepareBinaryData).toHaveBeenCalledWith(mockBuffer, 'spreadsheet.csv');
});
it('should handle custom delimiter', async () => {
const result = await convertJsonToSpreadsheetBinary.call(executeFunctions, items, 'csv', {
delimiter: ';',
});
expect(result).toEqual(mockBinaryData);
expect(xlsxUtils.json_to_sheet).toHaveBeenCalledWith(
items.map((item) => item.json),
undefined,
);
expect(xlsxWrite).toHaveBeenCalledWith(workBook, {
bookType: 'csv',
bookSST: false,
type: 'buffer',
FS: ';',
});
expect(helpers.prepareBinaryData).toHaveBeenCalledWith(mockBuffer, 'spreadsheet.csv');
});
});
});
describe('extractDataFromPDF', () => {
const helpers = mock<IExecuteFunctions['helpers']>();
const executeFunctions = mock<IExecuteFunctions>({ helpers });
const originalDOMMatrix = globalThis.DOMMatrix;
beforeEach(() => {
jest.clearAllMocks();
});
afterEach(() => {
if (originalDOMMatrix) {
globalThis.DOMMatrix = originalDOMMatrix;
} else {
// @ts-expect-error - Intentionally deleting for test cleanup
delete globalThis.DOMMatrix;
}
});
describe('DOMMatrix polyfill', () => {
it('should polyfill DOMMatrix when it is undefined', async () => {
// @ts-expect-error - Intentionally deleting for test
delete globalThis.DOMMatrix;
expect(globalThis.DOMMatrix).toBeUndefined();
const mockPage = {
getTextContent: jest.fn().mockResolvedValue({ items: [] }),
};
const mockDocument = {
numPages: 1,
getMetadata: jest.fn().mockResolvedValue({ info: {}, metadata: null }),
getPage: jest.fn().mockResolvedValue(mockPage),
};
const { getDocument } = await import('pdfjs-dist/legacy/build/pdf.mjs');
(getDocument as jest.Mock).mockReturnValue({
promise: Promise.resolve(mockDocument),
});
const mockBinaryData = {
data: Buffer.from('fake pdf content').toString(BINARY_ENCODING),
mimeType: 'application/pdf',
};
helpers.assertBinaryData.mockReturnValue(mockBinaryData);
await extractDataFromPDF.call(executeFunctions, 'data', undefined, undefined, true, 0);
expect(globalThis.DOMMatrix).toBeDefined();
});
it('should not re-polyfill DOMMatrix when it is already defined', async () => {
const mockDOMMatrix = class MockDOMMatrix {};
globalThis.DOMMatrix = mockDOMMatrix as unknown as typeof DOMMatrix;
const mockPage = {
getTextContent: jest.fn().mockResolvedValue({ items: [] }),
};
const mockDocument = {
numPages: 1,
getMetadata: jest.fn().mockResolvedValue({ info: {}, metadata: null }),
getPage: jest.fn().mockResolvedValue(mockPage),
};
const { getDocument } = await import('pdfjs-dist/legacy/build/pdf.mjs');
(getDocument as jest.Mock).mockReturnValue({
promise: Promise.resolve(mockDocument),
});
const mockBinaryData = {
data: Buffer.from('fake pdf content').toString(BINARY_ENCODING),
mimeType: 'application/pdf',
};
helpers.assertBinaryData.mockReturnValue(mockBinaryData);
await extractDataFromPDF.call(executeFunctions, 'data', undefined, undefined, true, 0);
expect(globalThis.DOMMatrix).toBe(mockDOMMatrix);
});
});
});
describe('prepareBinariesDataList', () => {
describe('string input', () => {
it('should split comma-separated string with spaces', () => {
const input = 'file1, file2, file3';
const result = prepareBinariesDataList(input);
expect(result).toEqual(['file1', 'file2', 'file3']);
});
it('should split comma-separated string without spaces', () => {
const input = 'file1,file2,file3';
const result = prepareBinariesDataList(input);
expect(result).toEqual(['file1', 'file2', 'file3']);
});
it('should trim whitespace from property names', () => {
const input = 'file1 , file2 , file3';
const result = prepareBinariesDataList(input);
expect(result).toEqual(['file1', 'file2', 'file3']);
});
it('should return single item as array', () => {
const input = 'singleFile';
const result = prepareBinariesDataList(input);
expect(result).toEqual(['singleFile']);
});
it('should return array with empty string for empty input', () => {
const input = '';
const result = prepareBinariesDataList(input);
expect(result).toEqual(['']);
});
it('should handle trailing comma', () => {
const input = 'file1, file2,';
const result = prepareBinariesDataList(input);
expect(result).toEqual(['file1', 'file2', '']);
});
});
describe('array input', () => {
it('should return string array as-is', () => {
const input = ['file1', 'file2', 'file3'];
const result = prepareBinariesDataList(input);
expect(result).toEqual(['file1', 'file2', 'file3']);
});
it('should return empty array as-is', () => {
const input: string[] = [];
const result = prepareBinariesDataList(input);
expect(result).toEqual([]);
});
it('should return IBinaryData array as-is', () => {
const input = [
{ data: 'data1', mimeType: 'text/plain' },
{ data: 'data2', mimeType: 'text/plain' },
];
const result = prepareBinariesDataList(input);
expect(result).toEqual(input);
});
it('should return IBinaryData array with multiple items', () => {
const input = [
{ data: 'data1', mimeType: 'text/plain', fileName: 'file1.txt' },
{ data: 'data2', mimeType: 'image/png', fileName: 'file2.png' },
{ data: 'data3', mimeType: 'application/pdf', fileName: 'file3.pdf' },
];
const result = prepareBinariesDataList(input);
expect(result).toEqual(input);
expect(result).toHaveLength(3);
});
it('should return single-item IBinaryData array as-is', () => {
const input = [{ data: 'data1', mimeType: 'text/plain', fileName: 'single.txt' }];
const result = prepareBinariesDataList(input);
expect(result).toEqual(input);
expect(result).toHaveLength(1);
});
});
describe('object input', () => {
it('should wrap single IBinaryData object in array', () => {
const input = { data: 'data1', mimeType: 'text/plain' };
const result = prepareBinariesDataList(input);
expect(result).toEqual([input]);
});
it('should wrap IBinaryData object with fileName property in array', () => {
const input = { data: 'data1', mimeType: 'text/plain', fileName: 'test.txt' };
const result = prepareBinariesDataList(input);
expect(result).toEqual([input]);
expect(result).toHaveLength(1);
});
it('should wrap IBinaryData object with all properties in array', () => {
const input = {
data: 'base64data',
mimeType: 'application/pdf',
fileName: 'document.pdf',
fileExtension: 'pdf',
};
const result = prepareBinariesDataList(input);
expect(result).toEqual([input]);
expect(result[0]).toMatchObject(input);
});
});
});
@@ -0,0 +1,268 @@
import { mock } from 'jest-mock-extended';
import { OperationalError, type Logger } from 'n8n-workflow';
import { ConnectionPoolManager } from '@utils/connection-pool-manager';
const ttl = 5 * 60 * 1000;
const cleanUpInterval = 60 * 1000;
const logger = mock<Logger>();
let cpm: ConnectionPoolManager;
beforeAll(() => {
jest.useFakeTimers();
cpm = ConnectionPoolManager.getInstance(logger);
});
beforeEach(async () => {
cpm.purgeConnections();
});
afterAll(() => {
cpm.purgeConnections();
});
test('getInstance returns a singleton', () => {
const instance1 = ConnectionPoolManager.getInstance(logger);
const instance2 = ConnectionPoolManager.getInstance(logger);
expect(instance1).toBe(instance2);
});
describe('getConnection', () => {
test('calls fallBackHandler only once and returns the first value', async () => {
// ARRANGE
const connectionType = {};
const fallBackHandler = jest.fn(async () => {
return connectionType;
});
const options = {
credentials: {},
nodeType: 'example',
nodeVersion: '1',
fallBackHandler,
wasUsed: jest.fn(),
};
// ACT 1
const connection = await cpm.getConnection(options);
// ASSERT 1
expect(fallBackHandler).toHaveBeenCalledTimes(1);
expect(connection).toBe(connectionType);
// ACT 2
const connection2 = await cpm.getConnection(options);
// ASSERT 2
expect(fallBackHandler).toHaveBeenCalledTimes(1);
expect(connection2).toBe(connectionType);
});
test('creates different pools for different node versions', async () => {
// ARRANGE
const connectionType1 = {};
const fallBackHandler1 = jest.fn(async () => {
return connectionType1;
});
const connectionType2 = {};
const fallBackHandler2 = jest.fn(async () => {
return connectionType2;
});
// ACT 1
const connection1 = await cpm.getConnection({
credentials: {},
nodeType: 'example',
nodeVersion: '1',
fallBackHandler: fallBackHandler1,
wasUsed: jest.fn(),
});
const connection2 = await cpm.getConnection({
credentials: {},
nodeType: 'example',
nodeVersion: '2',
fallBackHandler: fallBackHandler2,
wasUsed: jest.fn(),
});
// ASSERT
expect(fallBackHandler1).toHaveBeenCalledTimes(1);
expect(connection1).toBe(connectionType1);
expect(fallBackHandler2).toHaveBeenCalledTimes(1);
expect(connection2).toBe(connectionType2);
expect(connection1).not.toBe(connection2);
});
test('calls cleanUpHandler after TTL expires', async () => {
// ARRANGE
const connectionType = {};
let abortController: AbortController | undefined;
const fallBackHandler = jest.fn(async (ac: AbortController) => {
abortController = ac;
return connectionType;
});
await cpm.getConnection({
credentials: {},
nodeType: 'example',
nodeVersion: '1',
fallBackHandler,
wasUsed: jest.fn(),
});
// ACT
jest.advanceTimersByTime(ttl + cleanUpInterval * 2);
// ASSERT
if (abortController === undefined) {
fail("abortController haven't been initialized");
}
expect(abortController.signal.aborted).toBe(true);
});
test('throws OperationsError if the fallBackHandler aborts during connection initialization', async () => {
// ARRANGE
const connectionType = {};
const fallBackHandler = jest.fn(async (ac: AbortController) => {
ac.abort();
return connectionType;
});
// ACT
const connectionPromise = cpm.getConnection({
credentials: {},
nodeType: 'example',
nodeVersion: '1',
fallBackHandler,
wasUsed: jest.fn(),
});
// ASSERT
await expect(connectionPromise).rejects.toThrow(OperationalError);
await expect(connectionPromise).rejects.toThrow(
'Could not create pool. Connection attempt was aborted.',
);
});
});
describe('onShutdown', () => {
test('calls all clean up handlers', async () => {
// ARRANGE
const connectionType1 = {};
let abortController1: AbortController | undefined;
const fallBackHandler1 = jest.fn(async (ac: AbortController) => {
abortController1 = ac;
return connectionType1;
});
await cpm.getConnection({
credentials: {},
nodeType: 'example',
nodeVersion: '1',
fallBackHandler: fallBackHandler1,
wasUsed: jest.fn(),
});
const connectionType2 = {};
let abortController2: AbortController | undefined;
const fallBackHandler2 = jest.fn(async (ac: AbortController) => {
abortController2 = ac;
return connectionType2;
});
await cpm.getConnection({
credentials: {},
nodeType: 'example',
nodeVersion: '2',
fallBackHandler: fallBackHandler2,
wasUsed: jest.fn(),
});
// ACT
cpm.purgeConnections();
// ASSERT
if (abortController1 === undefined || abortController2 === undefined) {
fail("abortController haven't been initialized");
}
expect(abortController1.signal.aborted).toBe(true);
expect(abortController2.signal.aborted).toBe(true);
});
test('calls all clean up handlers when `exit` is emitted on process', async () => {
// ARRANGE
const connectionType1 = {};
let abortController1: AbortController | undefined;
const fallBackHandler1 = jest.fn(async (ac: AbortController) => {
abortController1 = ac;
return connectionType1;
});
await cpm.getConnection({
credentials: {},
nodeType: 'example',
nodeVersion: '1',
fallBackHandler: fallBackHandler1,
wasUsed: jest.fn(),
});
const connectionType2 = {};
let abortController2: AbortController | undefined;
const fallBackHandler2 = jest.fn(async (ac: AbortController) => {
abortController2 = ac;
return connectionType2;
});
await cpm.getConnection({
credentials: {},
nodeType: 'example',
nodeVersion: '2',
fallBackHandler: fallBackHandler2,
wasUsed: jest.fn(),
});
// ACT
// @ts-expect-error we're not supposed to emit `exit` so it's missing from
// the type definition
process.emit('exit');
// ASSERT
if (abortController1 === undefined || abortController2 === undefined) {
fail("abortController haven't been initialized");
}
expect(abortController1.signal.aborted).toBe(true);
expect(abortController2.signal.aborted).toBe(true);
});
});
describe('wasUsed', () => {
test('is called for every successive `getConnection` call', async () => {
// ARRANGE
const connectionType = {};
const fallBackHandler = jest.fn(async () => {
return connectionType;
});
const wasUsed = jest.fn();
const options = {
credentials: {},
nodeType: 'example',
nodeVersion: '1',
fallBackHandler,
wasUsed,
};
// ACT 1
await cpm.getConnection(options);
// ASSERT 1
expect(wasUsed).toHaveBeenCalledTimes(0);
// ACT 2
await cpm.getConnection(options);
// ASSERT 2
expect(wasUsed).toHaveBeenCalledTimes(1);
});
});
@@ -0,0 +1,414 @@
import { mock } from 'jest-mock-extended';
import type { INode, IExecuteFunctions, INodeExecutionData } from 'n8n-workflow';
import { MYSQL_NODE_TYPE, POSTGRES_NODE_TYPE } from 'n8n-workflow';
import {
addExecutionHints,
compareItems,
flattenKeys,
fuzzyCompare,
getResolvables,
keysToLowercase,
removeTrailingSlash,
shuffleArray,
sortItemKeysByPriorityList,
wrapData,
} from '@utils/utilities';
//most test cases for fuzzyCompare are done in Compare Datasets node tests
describe('Test fuzzyCompare', () => {
it('should do strict comparison', () => {
const compareFunction = fuzzyCompare(false);
expect(compareFunction(1, '1')).toEqual(false);
});
it('should do fuzzy comparison', () => {
const compareFunction = fuzzyCompare(true);
expect(compareFunction(1, '1')).toEqual(true);
});
it('should treat null, 0 and "0" as equal', () => {
const compareFunction = fuzzyCompare(true, 2);
expect(compareFunction(null, null)).toEqual(true);
expect(compareFunction(null, 0)).toEqual(true);
expect(compareFunction(null, '0')).toEqual(true);
});
it('should not treat null, 0 and "0" as equal', () => {
const compareFunction = fuzzyCompare(true);
expect(compareFunction(null, 0)).toEqual(false);
expect(compareFunction(null, '0')).toEqual(false);
});
});
describe('Test wrapData', () => {
it('should wrap object in json', () => {
const data = {
id: 1,
name: 'Name',
};
const wrappedData = wrapData(data);
expect(wrappedData).toBeDefined();
expect(wrappedData).toEqual([{ json: data }]);
});
it('should wrap each object in array in json', () => {
const data = [
{
id: 1,
name: 'Name',
},
{
id: 2,
name: 'Name 2',
},
];
const wrappedData = wrapData(data);
expect(wrappedData).toBeDefined();
expect(wrappedData).toEqual([{ json: data[0] }, { json: data[1] }]);
});
it('json key from source should be inside json', () => {
const data = {
json: {
id: 1,
name: 'Name',
},
};
const wrappedData = wrapData(data);
expect(wrappedData).toBeDefined();
expect(wrappedData).toEqual([{ json: data }]);
expect(Object.keys(wrappedData[0].json)).toContain('json');
});
});
describe('Test keysToLowercase', () => {
it('should convert keys to lowercase', () => {
const headers = {
'Content-Type': 'application/json',
'X-Test-Header': 'Test',
Accept: 'application/json',
};
const newHeaders = keysToLowercase(headers);
expect(newHeaders).toEqual({
'content-type': 'application/json',
'x-test-header': 'Test',
accept: 'application/json',
});
});
it('should return original value if it is not an object', () => {
const test1 = keysToLowercase(['hello']);
const test2 = keysToLowercase('test');
const test3 = keysToLowercase(1);
const test4 = keysToLowercase(true);
const test5 = keysToLowercase(null);
const test6 = keysToLowercase(undefined);
expect(test1).toEqual(['hello']);
expect(test2).toEqual('test');
expect(test3).toEqual(1);
expect(test4).toEqual(true);
expect(test5).toEqual(null);
expect(test6).toEqual(undefined);
});
});
describe('Test getResolvables', () => {
it('should return empty array when there are no resolvables', () => {
expect(getResolvables('Plain String, no resolvables here.')).toEqual([]);
});
it('should properly handle resovables in SQL query', () => {
expect(getResolvables('SELECT * FROM {{ $json.db }}.{{ $json.table }};')).toEqual([
'{{ $json.db }}',
'{{ $json.table }}',
]);
});
it('should properly handle resovables in HTML string', () => {
expect(
getResolvables(
`
<!DOCTYPE html>
<html>
<head><title>{{ $json.pageTitle }}</title></head>
<body><h1>{{ $json.heading }}</h1></body>
<html>
<style>
body { height: {{ $json.pageHeight }}; }
</style>
<script>
console.log('{{ $json.welcomeMessage }}');
</script>
`,
),
).toEqual([
'{{ $json.pageTitle }}',
'{{ $json.heading }}',
'{{ $json.pageHeight }}',
'{{ $json.welcomeMessage }}',
]);
});
});
describe('shuffleArray', () => {
it('should shuffle array', () => {
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const toShuffle = [...array];
shuffleArray(toShuffle);
expect(toShuffle).not.toEqual(array);
expect(toShuffle).toHaveLength(array.length);
expect(toShuffle).toEqual(expect.arrayContaining(array));
});
});
describe('flattenKeys', () => {
const name = 'Lisa';
const city1 = 'Berlin';
const city2 = 'Schoenwald';
const withNestedObject = {
name,
address: { city: city1 },
};
const withNestedArrays = {
name,
addresses: [{ city: city1 }, { city: city2 }],
};
it('should handle empty object', () => {
const flattenedObj = flattenKeys({});
expect(flattenedObj).toEqual({});
});
it('should flatten object with nested object', () => {
const flattenedObj = flattenKeys(withNestedObject);
expect(flattenedObj).toEqual({
name,
'address.city': city1,
});
});
it('should handle object with nested arrays', () => {
const flattenedObj = flattenKeys(withNestedArrays);
expect(flattenedObj).toEqual({
name,
'addresses.0.city': city1,
'addresses.1.city': city2,
});
});
it('should flatten object with nested object and specified prefix', () => {
const flattenedObj = flattenKeys(withNestedObject, ['test']);
expect(flattenedObj).toEqual({
'test.name': name,
'test.address.city': city1,
});
});
it('should handle object with nested arrays and specified prefix', () => {
const flattenedObj = flattenKeys(withNestedArrays, ['test']);
expect(flattenedObj).toEqual({
'test.name': name,
'test.addresses.0.city': city1,
'test.addresses.1.city': city2,
});
});
});
describe('compareItems', () => {
it('should return true if all values of specified keys are equal', () => {
const obj1 = { json: { a: 1, b: 2, c: 3 } };
const obj2 = { json: { a: 1, b: 2, c: 3 } };
const keys = ['a', 'b', 'c'];
const result = compareItems(obj1, obj2, keys);
expect(result).toBe(true);
});
it('should return false if any values of specified keys are not equal', () => {
const obj1 = { json: { a: 1, b: 2, c: 3 } };
const obj2 = { json: { a: 1, b: 2, c: 4 } };
const keys = ['a', 'b', 'c'];
const result = compareItems(obj1, obj2, keys);
expect(result).toBe(false);
});
it('should return true if all values of specified keys are equal using dot notation', () => {
const obj1 = { json: { a: { b: { c: 1 } } } };
const obj2 = { json: { a: { b: { c: 1 } } } };
const keys = ['a.b.c'];
const result = compareItems(obj1, obj2, keys);
expect(result).toBe(true);
});
it('should return false if any values of specified keys are not equal using dot notation', () => {
const obj1 = { json: { a: { b: { c: 1 } } } };
const obj2 = { json: { a: { b: { c: 2 } } } };
const keys = ['a.b.c'];
const result = compareItems(obj1, obj2, keys);
expect(result).toBe(false);
});
it('should return true if all values of specified keys are equal using bracket notation', () => {
const obj1 = { json: { 'a.b': { 'c.d': 1 } } };
const obj2 = { json: { 'a.b': { 'c.d': 1 } } };
const keys = ['a.b.c.d'];
const result = compareItems(obj1, obj2, keys, true);
expect(result).toBe(true);
});
});
describe('sortItemKeysByPriorityList', () => {
it('should reorder keys based on priority list', () => {
const data: INodeExecutionData[] = [{ json: { c: 3, a: 1, b: 2 } }];
const priorityList = ['b', 'a'];
const result = sortItemKeysByPriorityList(data, priorityList);
expect(Object.keys(result[0].json)).toEqual(['b', 'a', 'c']);
});
it('should sort keys not in the priority list alphabetically', () => {
const data: INodeExecutionData[] = [{ json: { c: 3, a: 1, b: 2, d: 4 } }];
const priorityList = ['b', 'a'];
const result = sortItemKeysByPriorityList(data, priorityList);
expect(Object.keys(result[0].json)).toEqual(['b', 'a', 'c', 'd']);
});
it('should sort all keys alphabetically when priority list is empty', () => {
const data: INodeExecutionData[] = [{ json: { c: 3, a: 1, b: 2 } }];
const priorityList: string[] = [];
const result = sortItemKeysByPriorityList(data, priorityList);
expect(Object.keys(result[0].json)).toEqual(['a', 'b', 'c']);
});
it('should handle an empty data array', () => {
const data: INodeExecutionData[] = [];
const priorityList = ['b', 'a'];
const result = sortItemKeysByPriorityList(data, priorityList);
// Expect an empty array since there is no data
expect(result).toEqual([]);
});
it('should handle a single object in the data array', () => {
const data: INodeExecutionData[] = [{ json: { d: 4, b: 2, a: 1 } }];
const priorityList = ['a', 'b', 'c'];
const result = sortItemKeysByPriorityList(data, priorityList);
expect(Object.keys(result[0].json)).toEqual(['a', 'b', 'd']);
});
it('should handle duplicate keys in the priority list gracefully', () => {
const data: INodeExecutionData[] = [{ json: { d: 4, b: 2, a: 1 } }];
const priorityList = ['a', 'b', 'a'];
const result = sortItemKeysByPriorityList(data, priorityList);
expect(Object.keys(result[0].json)).toEqual(['a', 'b', 'd']);
});
});
describe('removeTrailingSlash', () => {
it('removes trailing slash', () => {
expect(removeTrailingSlash('https://example.com/')).toBe('https://example.com');
});
it('does not change a URL without trailing slash', () => {
expect(removeTrailingSlash('https://example.com')).toBe('https://example.com');
});
});
describe('addExecutionHints', () => {
const executeQueryOperationContext = {
getNodeParameter: (parameterName: string) => {
if (parameterName === 'options.queryBatching') {
return 'single';
}
if (parameterName === 'query') {
return 'INSERT INTO my_test_table VALUES (`{{ $json.name }}`)';
}
},
addExecutionHints: jest.fn(),
} as unknown as IExecuteFunctions;
const insertHint = {
message:
"Inserts were batched for performance. If you need to preserve item matching, consider changing 'Query batching' to 'Independent' in the options.",
location: 'outputPane',
};
const selectHint = {
location: 'outputPane',
message:
"This node ran 2 times, once for each input item. To run for the first item only, enable 'execute once' in the node settings",
};
it('should add batching insert hint to Postgres executeQuery operation', () => {
addExecutionHints(
executeQueryOperationContext,
mock<INode>({
type: POSTGRES_NODE_TYPE,
}),
[{ json: {} }, { json: {} }],
'executeQuery',
false,
);
expect(executeQueryOperationContext.addExecutionHints).toHaveBeenCalledWith(insertHint);
});
it('should add batching insert hint to MySql executeQuery operation', () => {
addExecutionHints(
executeQueryOperationContext,
mock<INode>({
type: MYSQL_NODE_TYPE,
}),
[{ json: {} }, { json: {} }],
'executeQuery',
false,
);
expect(executeQueryOperationContext.addExecutionHints).toHaveBeenCalledWith(insertHint);
});
it('should add run per item hint to Postgres select operation ', () => {
const context = {
addExecutionHints: jest.fn(),
} as unknown as IExecuteFunctions;
addExecutionHints(
context,
mock<INode>({
type: POSTGRES_NODE_TYPE,
}),
[{ json: {} }, { json: {} }],
'select',
false,
);
expect(context.addExecutionHints).toHaveBeenCalledWith(selectHint);
});
it('should add run per item hint to MySQL select operation', () => {
const context = {
addExecutionHints: jest.fn(),
} as unknown as IExecuteFunctions;
addExecutionHints(
context,
mock<INode>({
type: MYSQL_NODE_TYPE,
}),
[{ json: {} }, { json: {} }],
'select',
false,
);
expect(context.addExecutionHints).toHaveBeenCalledWith(selectHint);
});
});
@@ -0,0 +1,268 @@
import { timingSafeEqual } from 'crypto';
import { verifySignature } from '../webhook-signature-verification';
jest.mock('crypto', () => ({
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
...jest.requireActual('crypto'),
timingSafeEqual: jest.fn(),
}));
describe('webhook-signature-verification', () => {
beforeEach(() => {
jest.clearAllMocks();
});
describe('verifySignature', () => {
it('should return true when signatures match', () => {
const expectedSignature = 'sha256=abc123';
const actualSignature = 'sha256=abc123';
(timingSafeEqual as jest.Mock).mockReturnValue(true);
const result = verifySignature({
getExpectedSignature: () => expectedSignature,
getActualSignature: () => actualSignature,
});
expect(result).toBe(true);
expect(timingSafeEqual).toHaveBeenCalled();
});
it('should return false when signatures do not match', () => {
const expectedSignature = 'sha256=abc123';
const actualSignature = 'sha256=xyz789';
(timingSafeEqual as jest.Mock).mockReturnValue(false);
const result = verifySignature({
getExpectedSignature: () => expectedSignature,
getActualSignature: () => actualSignature,
});
expect(result).toBe(false);
expect(timingSafeEqual).toHaveBeenCalled();
});
it('should verify signature even when timestamp is skipped', () => {
const expectedSignature = 'sha256=abc123';
const actualSignature = 'sha256=xyz789';
(timingSafeEqual as jest.Mock).mockReturnValue(false);
const result = verifySignature({
getExpectedSignature: () => expectedSignature,
getActualSignature: () => actualSignature,
getTimestamp: () => null,
skipIfNoTimestamp: true,
});
expect(result).toBe(false);
expect(timingSafeEqual).toHaveBeenCalled();
});
it('should return false when signatures have different lengths', () => {
const expectedSignature = 'sha256=abc123';
const actualSignature = 'sha256=abc1234';
const result = verifySignature({
getExpectedSignature: () => expectedSignature,
getActualSignature: () => actualSignature,
});
expect(result).toBe(false);
expect(timingSafeEqual).not.toHaveBeenCalled();
});
it('should return false when expected signature is missing', () => {
const result = verifySignature({
getExpectedSignature: () => null,
getActualSignature: () => 'sha256=abc123',
});
expect(result).toBe(false);
});
it('should return false when expected signature is undefined', () => {
const result = verifySignature({
getExpectedSignature: () => undefined as unknown as string,
getActualSignature: () => 'sha256=abc123',
});
expect(result).toBe(false);
});
it('should return false when actual signature is missing', () => {
const result = verifySignature({
getExpectedSignature: () => 'sha256=abc123',
getActualSignature: () => null,
});
expect(result).toBe(false);
});
it('should return false when actual signature is undefined', () => {
const result = verifySignature({
getExpectedSignature: () => 'sha256=abc123',
getActualSignature: () => undefined as unknown as string,
});
expect(result).toBe(false);
});
it('should return true when skipIfNoExpectedSignature is true and no expected signature', () => {
const result = verifySignature({
getExpectedSignature: () => null,
getActualSignature: () => 'sha256=abc123',
skipIfNoExpectedSignature: true,
});
expect(result).toBe(true);
});
it('should validate timestamp when provided and within window', () => {
const currentTimeSec = Math.floor(Date.now() / 1000);
const recentTimestamp = currentTimeSec - 60; // 1 minute ago
(timingSafeEqual as jest.Mock).mockReturnValue(true);
const result = verifySignature({
getExpectedSignature: () => 'sha256=abc123',
getActualSignature: () => 'sha256=abc123',
getTimestamp: () => recentTimestamp,
});
expect(result).toBe(true);
});
it('should return false when timestamp is too old', () => {
const currentTimeSec = Math.floor(Date.now() / 1000);
const oldTimestamp = currentTimeSec - 400; // More than 5 minutes ago
const result = verifySignature({
getExpectedSignature: () => 'sha256=abc123',
getActualSignature: () => 'sha256=abc123',
getTimestamp: () => oldTimestamp,
});
expect(result).toBe(false);
});
it('should return false when timestamp is too far in future', () => {
const currentTimeSec = Math.floor(Date.now() / 1000);
const futureTimestamp = currentTimeSec + 400; // More than 5 minutes in future
const result = verifySignature({
getExpectedSignature: () => 'sha256=abc123',
getActualSignature: () => 'sha256=abc123',
getTimestamp: () => futureTimestamp,
});
expect(result).toBe(false);
});
it('should handle timestamp as string', () => {
const currentTimeSec = Math.floor(Date.now() / 1000);
const recentTimestamp = String(currentTimeSec - 60);
(timingSafeEqual as jest.Mock).mockReturnValue(true);
const result = verifySignature({
getExpectedSignature: () => 'sha256=abc123',
getActualSignature: () => 'sha256=abc123',
getTimestamp: () => recentTimestamp,
});
expect(result).toBe(true);
});
it('should convert milliseconds timestamp to seconds', () => {
const currentTimeMs = Date.now();
const recentTimestampMs = currentTimeMs - 60 * 1000; // 1 minute ago in ms
(timingSafeEqual as jest.Mock).mockReturnValue(true);
const result = verifySignature({
getExpectedSignature: () => 'sha256=abc123',
getActualSignature: () => 'sha256=abc123',
getTimestamp: () => recentTimestampMs,
});
expect(result).toBe(true);
});
it('should use custom maxTimestampAgeSeconds', () => {
const currentTimeSec = Math.floor(Date.now() / 1000);
const timestamp = currentTimeSec - 120; // 2 minutes ago
(timingSafeEqual as jest.Mock).mockReturnValue(true);
const result = verifySignature({
getExpectedSignature: () => 'sha256=abc123',
getActualSignature: () => 'sha256=abc123',
getTimestamp: () => timestamp,
maxTimestampAgeSeconds: 60, // 1 minute window
});
expect(result).toBe(false);
});
it('should return true when skipIfNoTimestamp is true and timestamp is null', () => {
(timingSafeEqual as jest.Mock).mockReturnValue(true);
const result = verifySignature({
getExpectedSignature: () => 'sha256=abc123',
getActualSignature: () => 'sha256=abc123',
getTimestamp: () => null,
skipIfNoTimestamp: true,
});
expect(result).toBe(true);
});
it('should return false when timestamp is null and skipIfNoTimestamp is false', () => {
const result = verifySignature({
getExpectedSignature: () => 'sha256=abc123',
getActualSignature: () => 'sha256=abc123',
getTimestamp: () => null,
skipIfNoTimestamp: false,
});
expect(result).toBe(false);
});
it('should return false when timestamp is invalid (NaN)', () => {
const result = verifySignature({
getExpectedSignature: () => 'sha256=abc123',
getActualSignature: () => 'sha256=abc123',
getTimestamp: () => 'invalid-timestamp',
});
expect(result).toBe(false);
});
it('should handle errors gracefully and return false', () => {
const result = verifySignature({
getExpectedSignature: () => {
throw new Error('Test error');
},
getActualSignature: () => 'sha256=abc123',
});
expect(result).toBe(false);
});
it('should handle timingSafeEqual errors gracefully', () => {
(timingSafeEqual as jest.Mock).mockImplementation(() => {
throw new Error('Buffer length mismatch');
});
const result = verifySignature({
getExpectedSignature: () => 'sha256=abc123',
getActualSignature: () => 'sha256=abc123',
});
expect(result).toBe(false);
});
});
});