From a11b00a0374359f0ba8fe91a1df402f32de61b15 Mon Sep 17 00:00:00 2001 From: Jonathan Bennetts Date: Fri, 8 Apr 2022 11:01:21 +0100 Subject: [PATCH] feat(Magento 2 Node): Add credential tests (#3086) * Implements Magento Auth API Test * Deletes unit tests * Fixed lint issues and changed the URI for the credential test * :zap: Move credential verification to the credential file * :zap: Simplify code Co-authored-by: paolo-rechia Co-authored-by: ricardo Co-authored-by: Jan Oberhauser --- .../credentials/Magento2Api.credentials.ts | 13 +++++++++++++ .../nodes-base/nodes/Magento/GenericFunctions.ts | 6 +----- packages/nodes-base/nodes/Magento/Magento2.node.ts | 8 ++++++++ 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/packages/nodes-base/credentials/Magento2Api.credentials.ts b/packages/nodes-base/credentials/Magento2Api.credentials.ts index 487f68abf6826..0bd4f0d72892f 100644 --- a/packages/nodes-base/credentials/Magento2Api.credentials.ts +++ b/packages/nodes-base/credentials/Magento2Api.credentials.ts @@ -1,4 +1,6 @@ import { + IAuthenticateBearer, + ICredentialTestRequest, ICredentialType, INodeProperties, } from 'n8n-workflow'; @@ -21,4 +23,15 @@ export class Magento2Api implements ICredentialType { default: '', }, ]; + test: ICredentialTestRequest = { + request: { + baseURL: '={{$credentials.host}}', + url: '/rest/default/V1/modules', + }, + }; + + authenticate = { + type: 'bearer', + properties: {}, + } as IAuthenticateBearer; } diff --git a/packages/nodes-base/nodes/Magento/GenericFunctions.ts b/packages/nodes-base/nodes/Magento/GenericFunctions.ts index cf09bba885f72..4a7353a50759a 100644 --- a/packages/nodes-base/nodes/Magento/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Magento/GenericFunctions.ts @@ -28,10 +28,6 @@ export async function magentoApiRequest(this: IWebhookFunctions | IHookFunctions const credentials = await this.getCredentials('magento2Api') as IDataObject; let options: OptionsWithUri = { - headers: { - 'Content-Type': 'application/json', - 'Authorization': `Bearer ${credentials.accessToken}`, - }, method, body, qs, @@ -45,7 +41,7 @@ export async function magentoApiRequest(this: IWebhookFunctions | IHookFunctions delete options.body; } //@ts-ignore - return await this.helpers.request.call(this, options); + return await this.helpers.requestWithAuthentication.call(this, 'magento2Api', options); } catch (error) { throw new NodeApiError(this.getNode(), error); } diff --git a/packages/nodes-base/nodes/Magento/Magento2.node.ts b/packages/nodes-base/nodes/Magento/Magento2.node.ts index 5b0440127e716..b1ac7c941096b 100644 --- a/packages/nodes-base/nodes/Magento/Magento2.node.ts +++ b/packages/nodes-base/nodes/Magento/Magento2.node.ts @@ -1,10 +1,18 @@ +import { + OptionsWithUri, +} from 'request'; + + import { IExecuteFunctions, } from 'n8n-core'; import { + ICredentialsDecrypted, + ICredentialTestFunctions, IDataObject, ILoadOptionsFunctions, + INodeCredentialTestResult, INodeExecutionData, INodePropertyOptions, INodeType,