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

ci: Delete unused code in some backend tests (no-changelog) #9456

Merged
merged 1 commit into from
May 17, 2024
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
96 changes: 12 additions & 84 deletions packages/core/test/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@ import { readdirSync, readFileSync } from 'fs';
const BASE_DIR = path.resolve(__dirname, '../../..');

import type {
ICredentialDataDecryptedObject,
IDataObject,
IDeferredPromise,
IExecuteWorkflowInfo,
IHttpRequestHelper,
IHttpRequestOptions,
INode,
INodeCredentialsDetails,
INodeType,
INodeTypes,
IRun,
Expand All @@ -24,66 +18,13 @@ import type {
INodeTypeData,
} from 'n8n-workflow';

import { ApplicationError, ICredentialsHelper, NodeHelpers, WorkflowHooks } from 'n8n-workflow';
import { Credentials } from '@/Credentials';
import { ApplicationError, NodeHelpers, WorkflowHooks } from 'n8n-workflow';

import { predefinedNodesTypes } from './constants';

export class CredentialsHelper extends ICredentialsHelper {
async authenticate(
credentials: ICredentialDataDecryptedObject,
typeName: string,
requestParams: IHttpRequestOptions,
): Promise<IHttpRequestOptions> {
return requestParams;
}

async preAuthentication(
helpers: IHttpRequestHelper,
credentials: ICredentialDataDecryptedObject,
typeName: string,
node: INode,
credentialsExpired: boolean,
): Promise<ICredentialDataDecryptedObject | undefined> {
return undefined;
}

getParentTypes(name: string): string[] {
return [];
}

async getDecrypted(
additionalData: IWorkflowExecuteAdditionalData,
nodeCredentials: INodeCredentialsDetails,
type: string,
): Promise<ICredentialDataDecryptedObject> {
return {};
}

async getCredentials(
nodeCredentials: INodeCredentialsDetails,
type: string,
): Promise<Credentials> {
return new Credentials({ id: null, name: '' }, '', [], '');
}

async updateCredentials(
nodeCredentials: INodeCredentialsDetails,
type: string,
data: ICredentialDataDecryptedObject,
): Promise<void> {}
}
import { mock } from 'jest-mock-extended';

class NodeTypesClass implements INodeTypes {
nodeTypes: INodeTypeData;

constructor(nodeTypes?: INodeTypeData) {
if (nodeTypes) {
this.nodeTypes = nodeTypes;
} else {
this.nodeTypes = predefinedNodesTypes;
}
}
constructor(private nodeTypes: INodeTypeData = predefinedNodesTypes) {}

getByName(nodeType: string): INodeType | IVersionedNodeType {
return this.nodeTypes[nodeType].type;
Expand All @@ -92,11 +33,15 @@ class NodeTypesClass implements INodeTypes {
getByNameAndVersion(nodeType: string, version?: number): INodeType {
return NodeHelpers.getVersionedNodeType(this.nodeTypes[nodeType].type, version);
}

getKnownTypes(): IDataObject {
throw new Error('Method not implemented.');
}
}

let nodeTypesInstance: NodeTypesClass | undefined;

export function NodeTypes(nodeTypes?: INodeTypeData): NodeTypesClass {
export function NodeTypes(nodeTypes?: INodeTypeData): INodeTypes {
if (nodeTypesInstance === undefined || nodeTypes !== undefined) {
nodeTypesInstance = new NodeTypesClass(nodeTypes);
}
Expand All @@ -110,7 +55,7 @@ export function WorkflowExecuteAdditionalData(
): IWorkflowExecuteAdditionalData {
const hookFunctions = {
nodeExecuteAfter: [
async (nodeName: string, data: ITaskData): Promise<void> => {
async (nodeName: string, _data: ITaskData): Promise<void> => {
nodeExecutionOrder.push(nodeName);
},
],
Expand All @@ -121,26 +66,9 @@ export function WorkflowExecuteAdditionalData(
],
};

const workflowData: IWorkflowBase = {
name: '',
createdAt: new Date(),
updatedAt: new Date(),
active: true,
nodes: [],
connections: {},
};

return {
credentialsHelper: new CredentialsHelper(),
hooks: new WorkflowHooks(hookFunctions, 'trigger', '1', workflowData),
executeWorkflow: async (workflowInfo: IExecuteWorkflowInfo) => {},
sendDataToUI: (message: string) => {},
restApiUrl: '',
webhookBaseUrl: 'webhook',
webhookWaitingBaseUrl: 'webhook-waiting',
webhookTestBaseUrl: 'webhook-test',
userId: '123',
};
return mock<IWorkflowExecuteAdditionalData>({
hooks: new WorkflowHooks(hookFunctions, 'trigger', '1', mock()),
});
}

const preparePinData = (pinData: IDataObject) => {
Expand Down
8 changes: 3 additions & 5 deletions packages/nodes-base/test/nodes/ExecuteWorkflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export async function executeWorkflow(testData: WorkflowTestData, nodeTypes: INo
const { baseUrl, mocks } = testData.nock;
const agent = nock(baseUrl);
mocks.forEach(({ method, path, statusCode, responseBody }) =>
// @ts-expect-error
agent[method](path).reply(statusCode, responseBody),
);
}
Expand All @@ -24,18 +25,15 @@ export async function executeWorkflow(testData: WorkflowTestData, nodeTypes: INo
});
const waitPromise = await createDeferredPromise<IRun>();
const nodeExecutionOrder: string[] = [];
const additionalData = Helpers.WorkflowExecuteAdditionalData(
waitPromise,
nodeExecutionOrder,
testData,
);
const additionalData = Helpers.WorkflowExecuteAdditionalData(waitPromise, nodeExecutionOrder);

let executionData: IRun;
const runExecutionData: IRunExecutionData = {
resultData: {
runData: {},
},
executionData: {
metadata: {},
contextData: {},
waitingExecution: {},
waitingExecutionSource: null,
Expand Down
72 changes: 27 additions & 45 deletions packages/nodes-base/test/nodes/Helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { isEmpty } from 'lodash';
import { get } from 'lodash';
import { BinaryDataService, Credentials, constructExecutionMetaData } from 'n8n-core';
import { Container } from 'typedi';
import { mock } from 'jest-mock-extended';
import type {
CredentialLoadingDetails,
ICredentialDataDecryptedObject,
Expand All @@ -15,7 +16,6 @@ import type {
IDataObject,
IDeferredPromise,
IExecuteFunctions,
IExecuteWorkflowInfo,
IGetNodeParameterOptions,
IHttpRequestHelper,
IHttpRequestOptions,
Expand Down Expand Up @@ -87,72 +87,69 @@ class CredentialType implements ICredentialTypes {
return knownCredentials[type]?.supportedNodes ?? [];
}

getParentTypes(typeName: string): string[] {
getParentTypes(_typeName: string): string[] {
return [];
}
}

export class CredentialsHelper extends ICredentialsHelper {
constructor(private credentialTypes: ICredentialTypes) {
super('');
}
const credentialTypes = new CredentialType();

class CredentialsHelper extends ICredentialsHelper {
async authenticate(
credentials: ICredentialDataDecryptedObject,
typeName: string,
requestParams: IHttpRequestOptions,
): Promise<IHttpRequestOptions> {
const credentialType = this.credentialTypes.getByName(typeName);
const credentialType = credentialTypes.getByName(typeName);
if (typeof credentialType.authenticate === 'function') {
return await credentialType.authenticate(credentials, requestParams);
}
return requestParams;
}

async preAuthentication(
helpers: IHttpRequestHelper,
credentials: ICredentialDataDecryptedObject,
typeName: string,
node: INode,
credentialsExpired: boolean,
_helpers: IHttpRequestHelper,
_credentials: ICredentialDataDecryptedObject,
_typeName: string,
_node: INode,
_credentialsExpired: boolean,
): Promise<ICredentialDataDecryptedObject | undefined> {
return undefined;
}

getParentTypes(name: string): string[] {
getParentTypes(_name: string): string[] {
return [];
}

async getDecrypted(
additionalData: IWorkflowExecuteAdditionalData,
_additionalData: IWorkflowExecuteAdditionalData,
nodeCredentials: INodeCredentialsDetails,
type: string,
): Promise<ICredentialDataDecryptedObject> {
return getFakeDecryptedCredentials(nodeCredentials, type, FAKE_CREDENTIALS_DATA);
}

async getCredentials(
nodeCredentials: INodeCredentialsDetails,
type: string,
_nodeCredentials: INodeCredentialsDetails,
_type: string,
): Promise<Credentials> {
return new Credentials({ id: null, name: '' }, '', [], '');
return new Credentials({ id: null, name: '' }, '', '');
}

async updateCredentials(
nodeCredentials: INodeCredentialsDetails,
type: string,
data: ICredentialDataDecryptedObject,
_nodeCredentials: INodeCredentialsDetails,
_type: string,
_data: ICredentialDataDecryptedObject,
): Promise<void> {}
}

export function WorkflowExecuteAdditionalData(
waitPromise: IDeferredPromise<IRun>,
nodeExecutionOrder: string[],
workflowTestData?: WorkflowTestData,
): IWorkflowExecuteAdditionalData {
const hookFunctions = {
nodeExecuteAfter: [
async (nodeName: string, data: ITaskData): Promise<void> => {
async (nodeName: string, _data: ITaskData): Promise<void> => {
nodeExecutionOrder.push(nodeName);
},
],
Expand All @@ -163,27 +160,10 @@ export function WorkflowExecuteAdditionalData(
],
};

const workflowData: IWorkflowBase = {
name: '',
createdAt: new Date(),
updatedAt: new Date(),
active: true,
nodes: [],
connections: {},
};
return {
credentialsHelper: new CredentialsHelper(credentialTypes),
hooks: new WorkflowHooks(hookFunctions, 'trigger', '1', workflowData),
executeWorkflow: async (workflowInfo: IExecuteWorkflowInfo): Promise<any> => {},
sendDataToUI: (message: string) => {},
restApiUrl: '',
webhookBaseUrl: 'webhook',
webhookWaitingBaseUrl: 'webhook-waiting',
webhookTestBaseUrl: 'webhook-test',
userId: '123',
variables: {},
instanceBaseUrl: '',
};
return mock<IWorkflowExecuteAdditionalData>({
credentialsHelper: new CredentialsHelper(),
hooks: new WorkflowHooks(hookFunctions, 'trigger', '1', mock()),
});
}

class NodeTypes implements INodeTypes {
Expand All @@ -209,6 +189,10 @@ class NodeTypes implements INodeTypes {
getByNameAndVersion(nodeType: string, version?: number): INodeType {
return NodeHelpers.getVersionedNodeType(this.nodeTypes[nodeType].type, version);
}

getKnownTypes(): IDataObject {
throw new Error('Method not implemented.');
}
}

export function createTemporaryDir(prefix = 'n8n') {
Expand All @@ -225,8 +209,6 @@ export async function initBinaryDataService(mode: 'default' | 'filesystem' = 'de
Container.set(BinaryDataService, binaryDataService);
}

const credentialTypes = new CredentialType();

export function setup(testData: WorkflowTestData[] | WorkflowTestData) {
if (!Array.isArray(testData)) {
testData = [testData];
Expand Down
22 changes: 12 additions & 10 deletions packages/workflow/src/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2060,21 +2060,23 @@ export const eventNamesAiNodes = [

export type EventNamesAiNodesType = (typeof eventNamesAiNodes)[number];

export interface ExecuteWorkflowOptions {
node?: INode;
parentWorkflowId: string;
inputData?: INodeExecutionData[];
parentExecutionId?: string;
loadedWorkflowData?: IWorkflowBase;
loadedRunData?: any;
parentWorkflowSettings?: IWorkflowSettings;
parentCallbackManager?: CallbackManager;
}

export interface IWorkflowExecuteAdditionalData {
credentialsHelper: ICredentialsHelper;
executeWorkflow: (
workflowInfo: IExecuteWorkflowInfo,
additionalData: IWorkflowExecuteAdditionalData,
options: {
node?: INode;
parentWorkflowId: string;
inputData?: INodeExecutionData[];
parentExecutionId?: string;
loadedWorkflowData?: IWorkflowBase;
loadedRunData?: any;
parentWorkflowSettings?: IWorkflowSettings;
parentCallbackManager?: CallbackManager;
},
options: ExecuteWorkflowOptions,
) => Promise<any>;
executionId?: string;
restartExecutionId?: string;
Expand Down
Loading
Loading