mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-14 08:34:07 -08:00
679a443a0c
* update type * add id to new nodes * update paste/import behavior * update duplicate/copy * update duplicate workflow * update import functions + templates * add instance id on copy * on download add instance id * simplify for testing * update telemetry events * add ids to nodegraph * not if same instance * update spacing * fix tests * update tests * add uuid * fix tests update tests add uuid fix ts issue * fix telemetry event * update workflow import * update public api * add sqlit migration * on workflow update * add psql migration * add mysql migration * revert to title * fix telemetry bug * remove console log * remove migration logs * fix copy/paste bug * replace node index with node id * remove console log * address PR feedback * address comment * fix type issue * fix select * update schema * fix ts issue * update tel helpers * fix eslint issues
249 lines
5.6 KiB
TypeScript
249 lines
5.6 KiB
TypeScript
import { CredentialsHelper, CredentialTypes } from '../../src';
|
|
import * as Helpers from './Helpers';
|
|
import {
|
|
IAuthenticateGeneric,
|
|
ICredentialDataDecryptedObject,
|
|
ICredentialType,
|
|
ICredentialTypeData,
|
|
IHttpRequestOptions,
|
|
INode,
|
|
INodeProperties,
|
|
Workflow,
|
|
} from 'n8n-workflow';
|
|
|
|
const TEST_ENCRYPTION_KEY = 'test';
|
|
|
|
describe('CredentialsHelper', () => {
|
|
describe('authenticate', () => {
|
|
const tests: Array<{
|
|
description: string;
|
|
input: {
|
|
credentials: ICredentialDataDecryptedObject;
|
|
credentialType: ICredentialType;
|
|
};
|
|
output: IHttpRequestOptions;
|
|
}> = [
|
|
{
|
|
description: 'basicAuth, default property names',
|
|
input: {
|
|
credentials: {
|
|
user: 'user1',
|
|
password: 'password1',
|
|
},
|
|
credentialType: new (class TestApi implements ICredentialType {
|
|
name = 'testApi';
|
|
displayName = 'Test API';
|
|
properties: INodeProperties[] = [
|
|
{
|
|
displayName: 'User',
|
|
name: 'user',
|
|
type: 'string',
|
|
default: '',
|
|
},
|
|
{
|
|
displayName: 'Password',
|
|
name: 'password',
|
|
type: 'string',
|
|
default: '',
|
|
},
|
|
];
|
|
|
|
authenticate: IAuthenticateGeneric = {
|
|
type: 'generic',
|
|
properties: {
|
|
auth: {
|
|
username: '={{$credentials.user}}',
|
|
password: '={{$credentials.password}}',
|
|
},
|
|
},
|
|
};
|
|
})(),
|
|
},
|
|
output: {
|
|
url: '',
|
|
headers: {},
|
|
auth: { username: 'user1', password: 'password1' },
|
|
qs: {},
|
|
},
|
|
},
|
|
{
|
|
description: 'headerAuth',
|
|
input: {
|
|
credentials: {
|
|
accessToken: 'test',
|
|
},
|
|
credentialType: new (class TestApi implements ICredentialType {
|
|
name = 'testApi';
|
|
displayName = 'Test API';
|
|
properties: INodeProperties[] = [
|
|
{
|
|
displayName: 'Access Token',
|
|
name: 'accessToken',
|
|
type: 'string',
|
|
default: '',
|
|
},
|
|
];
|
|
|
|
authenticate: IAuthenticateGeneric = {
|
|
type: 'generic',
|
|
properties: {
|
|
headers: {
|
|
Authorization: '=Bearer {{$credentials.accessToken}}',
|
|
},
|
|
},
|
|
};
|
|
})(),
|
|
},
|
|
output: { url: '', headers: { Authorization: 'Bearer test' }, qs: {} },
|
|
},
|
|
{
|
|
description: 'headerAuth, key and value expressions',
|
|
input: {
|
|
credentials: {
|
|
accessToken: 'test',
|
|
},
|
|
credentialType: new (class TestApi implements ICredentialType {
|
|
name = 'testApi';
|
|
displayName = 'Test API';
|
|
properties: INodeProperties[] = [
|
|
{
|
|
displayName: 'Access Token',
|
|
name: 'accessToken',
|
|
type: 'string',
|
|
default: '',
|
|
},
|
|
];
|
|
|
|
authenticate: IAuthenticateGeneric = {
|
|
type: 'generic',
|
|
properties: {
|
|
headers: {
|
|
'={{$credentials.accessToken}}': '=Bearer {{$credentials.accessToken}}',
|
|
},
|
|
},
|
|
};
|
|
})(),
|
|
},
|
|
output: { url: '', headers: { test: 'Bearer test' }, qs: {} },
|
|
},
|
|
{
|
|
description: 'queryAuth',
|
|
input: {
|
|
credentials: {
|
|
accessToken: 'test',
|
|
},
|
|
credentialType: new (class TestApi implements ICredentialType {
|
|
name = 'testApi';
|
|
displayName = 'Test API';
|
|
properties: INodeProperties[] = [
|
|
{
|
|
displayName: 'Access Token',
|
|
name: 'accessToken',
|
|
type: 'string',
|
|
default: '',
|
|
},
|
|
];
|
|
|
|
authenticate = {
|
|
type: 'generic',
|
|
properties: {
|
|
qs: {
|
|
accessToken: '={{$credentials.accessToken}}',
|
|
},
|
|
},
|
|
} as IAuthenticateGeneric;
|
|
})(),
|
|
},
|
|
output: { url: '', headers: {}, qs: { accessToken: 'test' } },
|
|
},
|
|
{
|
|
description: 'custom authentication',
|
|
input: {
|
|
credentials: {
|
|
accessToken: 'test',
|
|
user: 'testUser',
|
|
},
|
|
credentialType: new (class TestApi implements ICredentialType {
|
|
name = 'testApi';
|
|
displayName = 'Test API';
|
|
properties: INodeProperties[] = [
|
|
{
|
|
displayName: 'My Token',
|
|
name: 'myToken',
|
|
type: 'string',
|
|
default: '',
|
|
},
|
|
];
|
|
|
|
async authenticate(
|
|
credentials: ICredentialDataDecryptedObject,
|
|
requestOptions: IHttpRequestOptions,
|
|
): Promise<IHttpRequestOptions> {
|
|
requestOptions.headers!['Authorization'] = `Bearer ${credentials.accessToken}`;
|
|
requestOptions.qs!['user'] = credentials.user;
|
|
return requestOptions;
|
|
}
|
|
})(),
|
|
},
|
|
output: {
|
|
url: '',
|
|
headers: { Authorization: 'Bearer test' },
|
|
qs: { user: 'testUser' },
|
|
},
|
|
},
|
|
];
|
|
|
|
const node: INode = {
|
|
id: 'uuid-1',
|
|
parameters: {},
|
|
name: 'test',
|
|
type: 'test.set',
|
|
typeVersion: 1,
|
|
position: [0, 0],
|
|
};
|
|
|
|
const incomingRequestOptions = {
|
|
url: '',
|
|
headers: {},
|
|
qs: {},
|
|
};
|
|
|
|
const nodeTypes = Helpers.NodeTypes();
|
|
|
|
const workflow = new Workflow({
|
|
nodes: [node],
|
|
connections: {},
|
|
active: false,
|
|
nodeTypes,
|
|
});
|
|
|
|
const timezone = 'America/New_York';
|
|
|
|
for (const testData of tests) {
|
|
test(testData.description, async () => {
|
|
const credentialTypes: ICredentialTypeData = {
|
|
[testData.input.credentialType.name]: {
|
|
type: testData.input.credentialType,
|
|
sourcePath: '',
|
|
},
|
|
};
|
|
|
|
await CredentialTypes().init(credentialTypes);
|
|
|
|
const credentialsHelper = new CredentialsHelper(TEST_ENCRYPTION_KEY);
|
|
|
|
const result = await credentialsHelper.authenticate(
|
|
testData.input.credentials,
|
|
testData.input.credentialType.name,
|
|
JSON.parse(JSON.stringify(incomingRequestOptions)),
|
|
workflow,
|
|
node,
|
|
timezone,
|
|
);
|
|
|
|
expect(result).toEqual(testData.output);
|
|
});
|
|
}
|
|
});
|
|
});
|