File size: 3,841 Bytes
aec3094 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | import { mock } from 'jest-mock-extended';
import get from 'lodash/get';
import set from 'lodash/set';
import type { EventService } from '@/events/event.service';
import type { NodeTypes } from '@/node-types';
import type { Task } from '@/task-runners/task-managers/task-requester';
import { TaskRequester } from '@/task-runners/task-managers/task-requester';
class TestTaskRequester extends TaskRequester {
sentMessages: unknown[] = [];
sendMessage(message: unknown) {
this.sentMessages.push(message);
}
}
describe('TaskRequester', () => {
let instance: TestTaskRequester;
const mockNodeTypes = mock<NodeTypes>();
const mockEventService = mock<EventService>();
beforeEach(() => {
instance = new TestTaskRequester(mockNodeTypes, mockEventService);
});
describe('handleRpc', () => {
test.each([
['logNodeOutput', ['hello world']],
['helpers.assertBinaryData', [0, 'propertyName']],
['helpers.getBinaryDataBuffer', [0, 'propertyName']],
['helpers.prepareBinaryData', [Buffer.from('data').toJSON(), 'filename', 'mimetype']],
['helpers.setBinaryDataBuffer', [{ data: '123' }, Buffer.from('data').toJSON()]],
['helpers.binaryToString', [Buffer.from('data').toJSON(), 'utf8']],
['helpers.httpRequest', [{ url: 'http://localhost' }]],
['helpers.request', [{ url: 'http://localhost' }]],
])('should handle %s rpc call', async (methodName, args) => {
const executeFunctions = set({}, methodName.split('.'), jest.fn());
const mockTask = mock<Task>({
taskId: 'taskId',
data: {
executeFunctions,
},
});
instance.tasks.set('taskId', mockTask);
await instance.handleRpc('taskId', 'callId', methodName, args);
expect(instance.sentMessages).toEqual([
{
callId: 'callId',
data: undefined,
status: 'success',
taskId: 'taskId',
type: 'requester:rpcresponse',
},
]);
expect(get(executeFunctions, methodName.split('.'))).toHaveBeenCalledWith(...args);
});
it('converts any serialized buffer arguments into buffers', async () => {
const mockPrepareBinaryData = jest.fn().mockResolvedValue(undefined);
const mockTask = mock<Task>({
taskId: 'taskId',
data: {
executeFunctions: {
helpers: {
prepareBinaryData: mockPrepareBinaryData,
},
},
},
});
instance.tasks.set('taskId', mockTask);
await instance.handleRpc('taskId', 'callId', 'helpers.prepareBinaryData', [
Buffer.from('data').toJSON(),
'filename',
'mimetype',
]);
expect(mockPrepareBinaryData).toHaveBeenCalledWith(
Buffer.from('data'),
'filename',
'mimetype',
);
});
describe('errors', () => {
it('sends method not allowed error if method is not in the allow list', async () => {
const mockTask = mock<Task>({
taskId: 'taskId',
data: {
executeFunctions: {},
},
});
instance.tasks.set('taskId', mockTask);
await instance.handleRpc('taskId', 'callId', 'notAllowedMethod', []);
expect(instance.sentMessages).toEqual([
{
callId: 'callId',
data: 'Method not allowed',
status: 'error',
taskId: 'taskId',
type: 'requester:rpcresponse',
},
]);
});
it('sends error if method throws', async () => {
const error = new Error('Test error');
const mockTask = mock<Task>({
taskId: 'taskId',
data: {
executeFunctions: {
helpers: {
assertBinaryData: jest.fn().mockRejectedValue(error),
},
},
},
});
instance.tasks.set('taskId', mockTask);
await instance.handleRpc('taskId', 'callId', 'helpers.assertBinaryData', []);
expect(instance.sentMessages).toEqual([
{
callId: 'callId',
data: error,
status: 'error',
taskId: 'taskId',
type: 'requester:rpcresponse',
},
]);
});
});
});
});
|