refactor(core): Update dynamic node parameter endpoints to use DTOs (#12379)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2024-12-30 15:48:44 +01:00
committed by GitHub
parent f08db47077
commit 1674dd0f88
21 changed files with 875 additions and 171 deletions

View File

@@ -3,16 +3,21 @@ import type {
INodeListSearchResult,
IWorkflowExecuteAdditionalData,
ResourceMapperFields,
NodeParameterValueType,
} from 'n8n-workflow';
import { DynamicNodeParametersService } from '@/services/dynamic-node-parameters.service';
import * as AdditionalData from '@/workflow-execute-additional-data';
import { mockInstance } from '@test/mocking';
import { createOwner } from '../shared/db/users';
import type { SuperAgentTest } from '../shared/types';
import { setupTestServer } from '../shared/utils';
describe('DynamicNodeParametersController', () => {
const additionalData = mock<IWorkflowExecuteAdditionalData>();
const service = mockInstance(DynamicNodeParametersService);
const testServer = setupTestServer({ endpointGroups: ['dynamic-node-parameters'] });
let ownerAgent: SuperAgentTest;
@@ -21,62 +26,171 @@ describe('DynamicNodeParametersController', () => {
ownerAgent = testServer.authAgentFor(owner);
});
beforeEach(() => {
jest.clearAllMocks();
jest.spyOn(AdditionalData, 'getBase').mockResolvedValue(additionalData);
});
const commonRequestParams = {
credentials: {},
currentNodeParameters: {},
nodeTypeAndVersion: {},
nodeTypeAndVersion: { name: 'TestNode', version: 1 },
path: 'path',
methodName: 'methodName',
};
describe('POST /dynamic-node-parameters/options', () => {
jest.spyOn(AdditionalData, 'getBase').mockResolvedValue(mock<IWorkflowExecuteAdditionalData>());
it('should take params via body', async () => {
jest
.spyOn(DynamicNodeParametersService.prototype, 'getOptionsViaMethodName')
.mockResolvedValue([]);
service.getOptionsViaMethodName.mockResolvedValue([]);
await ownerAgent
.post('/dynamic-node-parameters/options')
.send({
...commonRequestParams,
loadOptions: {},
methodName: 'testMethod',
})
.expect(200);
});
it('should take params with loadOptions', async () => {
const expectedResult = [{ name: 'Test Option', value: 'test' }];
service.getOptionsViaLoadOptions.mockResolvedValue(expectedResult);
const response = await ownerAgent
.post('/dynamic-node-parameters/options')
.send({
...commonRequestParams,
loadOptions: { type: 'test' },
})
.expect(200);
expect(response.body).toEqual({ data: expectedResult });
});
it('should return empty array when no method or loadOptions provided', async () => {
const response = await ownerAgent
.post('/dynamic-node-parameters/options')
.send({
...commonRequestParams,
})
.expect(200);
expect(response.body).toEqual({ data: [] });
});
});
describe('POST /dynamic-node-parameters/resource-locator-results', () => {
it('should take params via body', async () => {
jest
.spyOn(DynamicNodeParametersService.prototype, 'getResourceLocatorResults')
.mockResolvedValue(mock<INodeListSearchResult>());
it('should return resource locator results', async () => {
const expectedResult: INodeListSearchResult = { results: [] };
service.getResourceLocatorResults.mockResolvedValue(expectedResult);
const response = await ownerAgent
.post('/dynamic-node-parameters/resource-locator-results')
.send({
...commonRequestParams,
methodName: 'testMethod',
filter: 'testFilter',
paginationToken: 'testToken',
})
.expect(200);
expect(response.body).toEqual({ data: expectedResult });
});
it('should handle resource locator results without pagination', async () => {
const mockResults = mock<INodeListSearchResult>();
service.getResourceLocatorResults.mockResolvedValue(mockResults);
await ownerAgent
.post('/dynamic-node-parameters/resource-locator-results')
.send({
methodName: 'testMethod',
...commonRequestParams,
filter: 'filter',
paginationToken: 'paginationToken',
})
.expect(200);
});
it('should return a 400 if methodName is not defined', async () => {
await ownerAgent
.post('/dynamic-node-parameters/resource-locator-results')
.send(commonRequestParams)
.expect(400);
});
});
describe('POST /dynamic-node-parameters/resource-mapper-fields', () => {
it('should take params via body', async () => {
jest
.spyOn(DynamicNodeParametersService.prototype, 'getResourceMappingFields')
.mockResolvedValue(mock<ResourceMapperFields>());
it('should return resource mapper fields', async () => {
const expectedResult: ResourceMapperFields = { fields: [] };
service.getResourceMappingFields.mockResolvedValue(expectedResult);
await ownerAgent
const response = await ownerAgent
.post('/dynamic-node-parameters/resource-mapper-fields')
.send({
...commonRequestParams,
loadOptions: 'loadOptions',
methodName: 'testMethod',
loadOptions: 'testLoadOptions',
})
.expect(200);
expect(response.body).toEqual({ data: expectedResult });
});
it('should return a 400 if methodName is not defined', async () => {
await ownerAgent
.post('/dynamic-node-parameters/resource-mapper-fields')
.send(commonRequestParams)
.expect(400);
});
});
describe('POST /dynamic-node-parameters/local-resource-mapper-fields', () => {
it('should return local resource mapper fields', async () => {
const expectedResult: ResourceMapperFields = { fields: [] };
service.getLocalResourceMappingFields.mockResolvedValue(expectedResult);
const response = await ownerAgent
.post('/dynamic-node-parameters/local-resource-mapper-fields')
.send({
...commonRequestParams,
methodName: 'testMethod',
})
.expect(200);
expect(response.body).toEqual({ data: expectedResult });
});
it('should return a 400 if methodName is not defined', async () => {
await ownerAgent
.post('/dynamic-node-parameters/local-resource-mapper-fields')
.send(commonRequestParams)
.expect(400);
});
});
describe('POST /dynamic-node-parameters/action-result', () => {
it('should return action result with handler', async () => {
const expectedResult: NodeParameterValueType = { test: true };
service.getActionResult.mockResolvedValue(expectedResult);
const response = await ownerAgent
.post('/dynamic-node-parameters/action-result')
.send({
...commonRequestParams,
handler: 'testHandler',
payload: { someData: 'test' },
})
.expect(200);
expect(response.body).toEqual({ data: expectedResult });
});
it('should return a 400 if handler is not defined', async () => {
await ownerAgent
.post('/dynamic-node-parameters/action-result')
.send({
...commonRequestParams,
payload: { someData: 'test' },
})
.expect(400);
});
});
});