forked from n8n-io/n8n
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'n8n-io:master' into master
- Loading branch information
Showing
74 changed files
with
2,545 additions
and
1,329 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,121 @@ | ||
import { Container } from 'typedi'; | ||
import { mock } from 'jest-mock-extended'; | ||
import { UnrecognizedCredentialTypeError } from 'n8n-core'; | ||
import type { ICredentialType, LoadedClass } from 'n8n-workflow'; | ||
|
||
import { CredentialTypes } from '@/credential-types'; | ||
import { LoadNodesAndCredentials } from '@/load-nodes-and-credentials'; | ||
import { mockInstance } from '@test/mocking'; | ||
import type { LoadNodesAndCredentials } from '@/load-nodes-and-credentials'; | ||
|
||
describe('CredentialTypes', () => { | ||
const mockNodesAndCredentials = mockInstance(LoadNodesAndCredentials, { | ||
loadedCredentials: { | ||
fakeFirstCredential: { | ||
type: { | ||
name: 'fakeFirstCredential', | ||
displayName: 'Fake First Credential', | ||
properties: [], | ||
}, | ||
sourcePath: '', | ||
}, | ||
fakeSecondCredential: { | ||
type: { | ||
name: 'fakeSecondCredential', | ||
displayName: 'Fake Second Credential', | ||
properties: [], | ||
}, | ||
sourcePath: '', | ||
}, | ||
}, | ||
const loadNodesAndCredentials = mock<LoadNodesAndCredentials>(); | ||
|
||
const credentialTypes = new CredentialTypes(loadNodesAndCredentials); | ||
|
||
const testCredential: LoadedClass<ICredentialType> = { | ||
sourcePath: '', | ||
type: mock(), | ||
}; | ||
|
||
loadNodesAndCredentials.getCredential.mockImplementation((credentialType) => { | ||
if (credentialType === 'testCredential') return testCredential; | ||
throw new UnrecognizedCredentialTypeError(credentialType); | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('getByName', () => { | ||
test('Should throw error when calling invalid credential name', () => { | ||
expect(() => credentialTypes.getByName('unknownCredential')).toThrowError('c'); | ||
}); | ||
|
||
test('Should return correct credential type for valid name', () => { | ||
expect(credentialTypes.getByName('testCredential')).toStrictEqual(testCredential.type); | ||
}); | ||
}); | ||
|
||
describe('recognizes', () => { | ||
test('Should recognize credential type that exists in knownCredentials', () => { | ||
const credentialTypes = new CredentialTypes( | ||
mock<LoadNodesAndCredentials>({ | ||
loadedCredentials: {}, | ||
knownCredentials: { testCredential: mock({ supportedNodes: [] }) }, | ||
}), | ||
); | ||
|
||
expect(credentialTypes.recognizes('testCredential')).toBe(true); | ||
}); | ||
|
||
test('Should recognize credential type that exists in loadedCredentials', () => { | ||
const credentialTypes = new CredentialTypes( | ||
mock<LoadNodesAndCredentials>({ | ||
loadedCredentials: { testCredential }, | ||
knownCredentials: {}, | ||
}), | ||
); | ||
|
||
expect(credentialTypes.recognizes('testCredential')).toBe(true); | ||
}); | ||
|
||
test('Should not recognize unknown credential type', () => { | ||
expect(credentialTypes.recognizes('unknownCredential')).toBe(false); | ||
}); | ||
}); | ||
|
||
const credentialTypes = Container.get(CredentialTypes); | ||
describe('getSupportedNodes', () => { | ||
test('Should return supported nodes for known credential type', () => { | ||
const supportedNodes = ['node1', 'node2']; | ||
const credentialTypes = new CredentialTypes( | ||
mock<LoadNodesAndCredentials>({ | ||
knownCredentials: { testCredential: mock({ supportedNodes }) }, | ||
}), | ||
); | ||
|
||
expect(credentialTypes.getSupportedNodes('testCredential')).toEqual(supportedNodes); | ||
}); | ||
|
||
test('Should throw error when calling invalid credential name', () => { | ||
expect(() => credentialTypes.getByName('fakeThirdCredential')).toThrowError(); | ||
test('Should return empty array for unknown credential type supported nodes', () => { | ||
expect(credentialTypes.getSupportedNodes('unknownCredential')).toBeEmptyArray(); | ||
}); | ||
}); | ||
|
||
test('Should return correct credential type for valid name', () => { | ||
const mockedCredentialTypes = mockNodesAndCredentials.loadedCredentials; | ||
expect(credentialTypes.getByName('fakeFirstCredential')).toStrictEqual( | ||
mockedCredentialTypes.fakeFirstCredential.type, | ||
); | ||
describe('getParentTypes', () => { | ||
test('Should return parent types for credential type with extends', () => { | ||
const credentialTypes = new CredentialTypes( | ||
mock<LoadNodesAndCredentials>({ | ||
knownCredentials: { | ||
childType: { extends: ['parentType1', 'parentType2'] }, | ||
parentType1: { extends: ['grandparentType'] }, | ||
parentType2: { extends: [] }, | ||
grandparentType: { extends: [] }, | ||
}, | ||
}), | ||
); | ||
|
||
const parentTypes = credentialTypes.getParentTypes('childType'); | ||
expect(parentTypes).toContain('parentType1'); | ||
expect(parentTypes).toContain('parentType2'); | ||
expect(parentTypes).toContain('grandparentType'); | ||
}); | ||
|
||
test('Should return empty array for credential type without extends', () => { | ||
const credentialTypes = new CredentialTypes( | ||
mock<LoadNodesAndCredentials>({ | ||
knownCredentials: { testCredential: { extends: [] } }, | ||
}), | ||
); | ||
|
||
expect(credentialTypes.getParentTypes('testCredential')).toBeEmptyArray(); | ||
}); | ||
|
||
test('Should return empty array for unknown credential type parent types', () => { | ||
const credentialTypes = new CredentialTypes( | ||
mock<LoadNodesAndCredentials>({ | ||
knownCredentials: {}, | ||
}), | ||
); | ||
|
||
expect(credentialTypes.getParentTypes('unknownCredential')).toBeEmptyArray(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.