Skip to content

Commit

Permalink
test credentials in a specific order
Browse files Browse the repository at this point in the history
  • Loading branch information
netroy committed Nov 29, 2022
1 parent f3668f0 commit 11c5496
Showing 1 changed file with 61 additions and 50 deletions.
111 changes: 61 additions & 50 deletions packages/cli/src/CredentialsHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,70 +465,81 @@ export class CredentialsHelper extends ICredentialsHelper {
await Db.collections.Credentials.update(findQuery, newCredentialsData);
}

getCredentialTestFunction(
private getCredentialTestFunction(
credentialType: string,
nodeToTestWith?: string,
): ICredentialTestFunction | ICredentialTestRequestData | undefined {
const nodeTypesToTestWith = nodeToTestWith
? [nodeToTestWith]
: this.credentialTypes.getNodeTypesToTestWith(credentialType);
// Check if a more specific test is defined on the requested nodeType
if (nodeToTestWith) {
const testFunction = this.getTestFunctionFromNode(nodeToTestWith, credentialType);
if (testFunction) return testFunction;
}

// Check if test is defined on credentials
const type = this.credentialTypes.getByName(credentialType);
if (type.test) {
return {
testRequest: type.test,
};
}

const nodeTypesToTestWith = this.credentialTypes.getNodeTypesToTestWith(credentialType);
for (const nodeName of nodeTypesToTestWith) {
const node = this.nodeTypes.getByName(nodeName);

// Always set to an array even if node is not versioned to not having
// to duplicate the logic
const allNodeTypes: INodeType[] = [];
if (node instanceof VersionedNodeType) {
// Node is versioned
allNodeTypes.push(...Object.values(node.nodeVersions));
} else {
// Node is not versioned
allNodeTypes.push(node as INodeType);
}
const testFunction = this.getTestFunctionFromNode(nodeName, credentialType);
if (testFunction) return testFunction;
}

return undefined;
}

// Check each of the node versions for credential tests
for (const nodeType of allNodeTypes) {
// Check each of teh credentials
for (const credential of nodeType.description.credentials ?? []) {
if (credential.name === credentialType && !!credential.testedBy) {
if (typeof credential.testedBy === 'string') {
if (node instanceof VersionedNodeType) {
// The node is versioned. So check all versions for test function
// starting with the latest
const versions = Object.keys(node.nodeVersions).sort().reverse();
for (const version of versions) {
const versionedNode = node.nodeVersions[parseInt(version, 10)];
if (
versionedNode.methods?.credentialTest &&
versionedNode.methods?.credentialTest[credential.testedBy]
) {
return versionedNode.methods?.credentialTest[credential.testedBy];
}
private getTestFunctionFromNode(
nodeName: string,
credentialType: string,
): ICredentialTestFunction | ICredentialTestRequestData | undefined {
const node = this.nodeTypes.getByName(nodeName);

// Always set to an array even if node is not versioned to not having
// to duplicate the logic
const allNodeTypes: INodeType[] = [];
if (node instanceof VersionedNodeType) {
// Node is versioned
allNodeTypes.push(...Object.values(node.nodeVersions));
} else {
// Node is not versioned
allNodeTypes.push(node as INodeType);
}

// Check each of the node versions for credential tests
for (const nodeType of allNodeTypes) {
// Check each of teh credentials
for (const { name, testedBy } of nodeType.description.credentials ?? []) {
if (name === credentialType && !!testedBy) {
if (typeof testedBy === 'string') {
if (node instanceof VersionedNodeType) {
// The node is versioned. So check all versions for test function
// starting with the latest
const versions = Object.keys(node.nodeVersions).sort().reverse();
for (const version of versions) {
const versionedNode = node.nodeVersions[parseInt(version, 10)];
const credentialTest = versionedNode.methods?.credentialTest;
if (credentialTest && testedBy in credentialTest) {
return credentialTest[testedBy];
}
}
// Test is defined as string which links to a function
return (node as unknown as INodeType).methods?.credentialTest![credential.testedBy];
}

// Test is defined as JSON with a definition for the request to make
return {
nodeType,
testRequest: credential.testedBy,
};
// Test is defined as string which links to a function
return (node as unknown as INodeType).methods?.credentialTest![testedBy];
}

// Test is defined as JSON with a definition for the request to make
return {
nodeType,
testRequest: testedBy,
};
}
}
}

// Check if test is defined on credentials
const type = this.credentialTypes.getByName(credentialType);
if (type.test) {
return {
testRequest: type.test,
};
}

return undefined;
}

Expand Down

0 comments on commit 11c5496

Please sign in to comment.