Add more auth tests (#2265)

This commit is contained in:
pemontto 2021-10-21 04:35:51 +01:00 committed by GitHub
parent 4b64838db8
commit ba647fbc7b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 125 additions and 0 deletions

View file

@ -1,15 +1,20 @@
import { OptionsWithUri } from 'request';
import {
IHookFunctions,
IWebhookFunctions,
} from 'n8n-core';
import {
ICredentialsDecrypted,
ICredentialTestFunctions,
IDataObject,
ILoadOptionsFunctions,
INodePropertyOptions,
INodeType,
INodeTypeDescription,
IWebhookResponseData,
NodeCredentialTestResult,
} from 'n8n-workflow';
import {
@ -35,6 +40,7 @@ export class BitbucketTrigger implements INodeType {
{
name: 'bitbucketApi',
required: true,
testedBy: 'bitbucketApiTest',
},
],
webhooks: [
@ -166,6 +172,41 @@ export class BitbucketTrigger implements INodeType {
};
methods = {
credentialTest: {
async bitbucketApiTest(this: ICredentialTestFunctions, credential: ICredentialsDecrypted): Promise<NodeCredentialTestResult> {
const credentials = credential.data;
const options: OptionsWithUri = {
method: 'GET',
auth: {
user: credentials!.username as string,
password: credentials!.appPassword as string,
},
uri: 'https://api.bitbucket.org/2.0/user',
json: true,
timeout: 5000,
};
try {
const response = await this.helpers.request(options);
if (!response.username) {
return {
status: 'Error',
message: `Token is not valid: ${response.error}`,
};
}
} catch (error) {
return {
status: 'Error',
message: `Settings are not valid: ${error}`,
};
}
return {
status: 'OK',
message: 'Authentication successful!',
};
},
},
loadOptions: {
// Get all the events to display them to user so that he can
// select them easily

View file

@ -1,12 +1,17 @@
import { OptionsWithUri } from 'request';
import {
IExecuteFunctions,
} from 'n8n-core';
import {
ICredentialsDecrypted,
ICredentialTestFunctions,
IDataObject,
INodeExecutionData,
INodeType,
INodeTypeDescription,
NodeCredentialTestResult,
NodeOperationError,
} from 'n8n-workflow';
@ -39,6 +44,7 @@ export class Github implements INodeType {
{
name: 'githubApi',
required: true,
testedBy: 'githubApiTest',
displayOptions: {
show: {
authentication: [
@ -1698,6 +1704,43 @@ export class Github implements INodeType {
],
};
methods = {
credentialTest: {
async githubApiTest(this: ICredentialTestFunctions, credential: ICredentialsDecrypted): Promise<NodeCredentialTestResult> {
const credentials = credential.data;
const baseUrl = credentials!.server as string || 'https://api.github.com/user';
const options: OptionsWithUri = {
method: 'GET',
headers: {
'User-Agent': 'n8n',
Authorization: `token ${credentials!.accessToken}`,
},
uri: baseUrl,
json: true,
timeout: 5000,
};
try {
const response = await this.helpers.request(options);
if (!response.id) {
return {
status: 'Error',
message: `Token is not valid: ${response.error}`,
};
}
} catch (error) {
return {
status: 'Error',
message: `Settings are not valid: ${error}`,
};
}
return {
status: 'OK',
message: 'Authentication successful!',
};
},
},
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();

View file

@ -3,10 +3,13 @@ import {
} from 'n8n-core';
import {
ICredentialsDecrypted,
ICredentialTestFunctions,
IDataObject,
INodeExecutionData,
INodeType,
INodeTypeDescription,
NodeCredentialTestResult,
} from 'n8n-workflow';
import {
@ -71,6 +74,7 @@ export class HomeAssistant implements INodeType {
{
name: 'homeAssistantApi',
required: true,
testedBy: 'homeAssistantApiTest',
},
],
properties: [
@ -133,6 +137,43 @@ export class HomeAssistant implements INodeType {
],
};
methods = {
credentialTest: {
async homeAssistantApiTest(this: ICredentialTestFunctions, credential: ICredentialsDecrypted): Promise<NodeCredentialTestResult> {
const credentials = credential.data;
const options = {
method: 'GET',
headers: {
Authorization: `Bearer ${credentials!.accessToken}`,
},
uri: `${credentials!.ssl === true ? 'https' : 'http'}://${credentials!.host}:${ credentials!.port || '8123' }/api/`,
json: true,
timeout: 5000,
};
try {
const response = await this.helpers.request(options);
if (!response.message) {
return {
status: 'Error',
message: `Token is not valid: ${response.error}`,
};
}
} catch (error) {
return {
status: 'Error',
message: `${error.statusCode === 401 ? 'Token is' : 'Settings are'} not valid: ${error}`,
};
}
return {
status: 'OK',
message: 'Authentication successful!',
};
},
},
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: IDataObject[] = [];