forked from rockcarver/frodo-lib
-
Notifications
You must be signed in to change notification settings - Fork 3
Add alias support #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
devintrivir
wants to merge
3
commits into
main
Choose a base branch
from
feature/aliases
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -8,9 +8,21 @@ | |
| */ | ||
| import fs from 'fs'; | ||
| import { homedir } from 'os'; | ||
| import { state } from '../index'; | ||
| import { FrodoError, state } from '../index'; | ||
| import * as ConnectionProfileOps from './ConnectionProfileOps'; | ||
| import Constants from '../shared/Constants'; | ||
| import { CONNECTOR_TYPE } from './ConnectorOps'; | ||
|
|
||
| const exampleHost = 'https://openam-tenant-name.forgeblocks.com/am'; | ||
| const exampleUsername = '[email protected]'; | ||
| const examplePassword = 'G@nd@lfTheW153'; | ||
| const exampleConnectionProfile = { | ||
| tenant: exampleHost, | ||
| username: exampleUsername, | ||
| encodedPassword: examplePassword, | ||
| allowInsecureConnection: false, | ||
| deploymentType: 'classic', | ||
| }; | ||
|
|
||
| describe('ConnectionProfileOps', () => { | ||
| const connectionProfilePath1 = `${homedir()}/connections1.json`; | ||
|
|
@@ -36,6 +48,12 @@ describe('ConnectionProfileOps', () => { | |
| } | ||
| }); | ||
|
|
||
| beforeEach(() => { | ||
| fs.writeFileSync(connectionProfilePath1, JSON.stringify({})); | ||
| fs.writeFileSync(connectionProfilePath2, JSON.stringify({})); | ||
| fs.writeFileSync(connectionProfilePath3, JSON.stringify({})); | ||
| }); | ||
|
|
||
| // clean up all connection profile files after running the tests | ||
| afterAll(() => { | ||
| try { | ||
|
|
@@ -55,11 +73,81 @@ describe('ConnectionProfileOps', () => { | |
| } | ||
| }); | ||
|
|
||
| describe('findConnectionProfiles()', () => { | ||
| test.only('1: Find connection profile by alias', async () => { | ||
| const tenant = exampleHost | ||
| const alias = 'unique-alias'; | ||
| const host = alias; | ||
| const connectionProfiles = { | ||
| [tenant]: { | ||
| ...exampleConnectionProfile, | ||
| alias, | ||
| }, | ||
| }; | ||
| fs.writeFileSync( | ||
| connectionProfilePath1, | ||
| JSON.stringify(connectionProfiles, null, 2) | ||
| ); | ||
|
|
||
| const connections = ConnectionProfileOps.findConnectionProfiles({ | ||
| connectionProfiles, | ||
| host, | ||
| state, | ||
| }); | ||
| expect(connections).toHaveLength(1); | ||
| expect(connections[0].tenant).toBe(tenant); | ||
| expect(connections[0].alias).toBe(alias); | ||
| }); | ||
|
|
||
| test('2: Default to substring matching after failing to find connection profile by alias', async () => { | ||
| const tenant = exampleHost; | ||
| const host = 'name'; | ||
| const connectionProfiles = { | ||
| [tenant]: { | ||
| ...exampleConnectionProfile, | ||
| }, | ||
| }; | ||
| fs.writeFileSync( | ||
| connectionProfilePath1, | ||
| JSON.stringify(connectionProfiles, null, 2) | ||
| ); | ||
|
|
||
| const connections = ConnectionProfileOps.findConnectionProfiles({ | ||
| connectionProfiles, | ||
| host, | ||
| state, | ||
| }); | ||
| expect(connections).toHaveLength(1); | ||
| expect(connections[0].tenant).toBe(tenant); | ||
| }); | ||
|
|
||
| test('3: Fail to find a match by alias or substring', async () => { | ||
| const host = 'nonexistent' | ||
| const tenant = exampleHost; | ||
| const connectionProfiles = { | ||
| [tenant]: { | ||
| ...exampleConnectionProfile, | ||
| }, | ||
| }; | ||
| fs.writeFileSync( | ||
| connectionProfilePath1, | ||
| JSON.stringify(connectionProfiles, null, 2) | ||
| ); | ||
|
|
||
| const connections = ConnectionProfileOps.findConnectionProfiles({ | ||
| connectionProfiles, | ||
| host, | ||
| state, | ||
| }); | ||
| expect(connections).toHaveLength(0); | ||
| }); | ||
| }); | ||
|
|
||
| describe('saveConnectionProfile()', () => { | ||
| test('1: Create connection profiles in location from state field', async () => { | ||
| const host = 'https://openam-tenant-name.forgeblocks.com/am'; | ||
| const user = '[email protected]'; | ||
| const password = 'G@nd@lfTheW153'; | ||
| const host = exampleHost; | ||
| const user = exampleUsername; | ||
| const password = examplePassword; | ||
|
|
||
| state.setHost(host); | ||
| state.setDeploymentType(Constants.FORGEOPS_DEPLOYMENT_TYPE_KEY); | ||
|
|
@@ -81,9 +169,9 @@ describe('ConnectionProfileOps', () => { | |
| }); | ||
|
|
||
| test(`2: Create connection profiles in location from env ${Constants.FRODO_MASTER_KEY_PATH_KEY}`, async () => { | ||
| const host = 'https://openam-tenant-name.forgeblocks.com/am'; | ||
| const user = '[email protected]'; | ||
| const password = 'G@nd@lfTheW153'; | ||
| const host = exampleHost; | ||
| const user = exampleUsername; | ||
| const password = examplePassword; | ||
| // set the hard-coded master key | ||
| process.env[Constants.FRODO_CONNECTION_PROFILES_PATH_KEY] = | ||
| connectionProfilePath2; | ||
|
|
@@ -110,9 +198,9 @@ describe('ConnectionProfileOps', () => { | |
| }); | ||
|
|
||
| test(`3: Use Master Key from env ${Constants.FRODO_MASTER_KEY_KEY}`, async () => { | ||
| const host = 'https://openam-tenant-name.forgeblocks.com/am'; | ||
| const user = '[email protected]'; | ||
| const password = 'G@nd@lfTheW153'; | ||
| const host = exampleHost; | ||
| const user = exampleUsername; | ||
| const password = examplePassword; | ||
| const masterKey = 'bxnQlhcU5VfyDs+BBPhRhK09yHaNtdIIk85HUMKBnqg='; | ||
| // set the hard-coded master key | ||
| process.env[Constants.FRODO_MASTER_KEY_KEY] = masterKey; | ||
|
|
@@ -135,5 +223,199 @@ describe('ConnectionProfileOps', () => { | |
| expect(connections[host].username).toEqual(user); | ||
| expect(connections[host].encodedPassword).toBeTruthy(); | ||
| }); | ||
|
|
||
| test(`4: Save a new connection with a unique alias`, async () => { | ||
| const host = exampleHost; | ||
| const alias = 'unique-alias'; | ||
| const user = exampleUsername; | ||
| const password = examplePassword; | ||
|
|
||
| state.setConnectionProfilesPath(connectionProfilePath1); | ||
| state.setHost(host); | ||
| state.setAlias(alias); | ||
| state.setUsername(user); | ||
| state.setPassword(password); | ||
| state.setDeploymentType(Constants.CLASSIC_DEPLOYMENT_TYPE_KEY); | ||
| await ConnectionProfileOps.saveConnectionProfile({ host, state }); | ||
| const connections = JSON.parse( | ||
| fs.readFileSync(connectionProfilePath1, 'utf8') | ||
| ); | ||
| expect(connections[host]).toBeTruthy(); | ||
| expect(connections[host].alias).toEqual(alias); | ||
| expect(connections[host].username).toEqual(user); | ||
| expect(connections[host].encodedPassword).toBeTruthy(); | ||
| }); | ||
|
|
||
| test(`5: Fail to save a new connection with a conflicting alias`, async () => { | ||
| const host1 = 'https://openam-tenant1.forgeblocks.com/am'; | ||
| const host2 = 'https://openam-tenant2.forgeblocks.com/am'; | ||
| const alias = 'conflicting-alias'; | ||
| const user = exampleUsername; | ||
| const password = examplePassword; | ||
|
|
||
| state.setConnectionProfilesPath(connectionProfilePath1); | ||
| state.setHost(host1); | ||
| state.setAlias(alias); | ||
| state.setUsername(user); | ||
| state.setPassword(password); | ||
| state.setDeploymentType(Constants.CLASSIC_DEPLOYMENT_TYPE_KEY); | ||
| await ConnectionProfileOps.saveConnectionProfile({ host: host1, state }); | ||
|
|
||
| state.setHost(host2); | ||
| state.setAlias(alias); | ||
| state.setUsername(user); | ||
| state.setPassword(password); | ||
| state.setDeploymentType(Constants.CLASSIC_DEPLOYMENT_TYPE_KEY); | ||
|
|
||
| try { | ||
| await ConnectionProfileOps.saveConnectionProfile({ | ||
| host: host2, | ||
| state, | ||
| }); | ||
| } catch (e) { | ||
| expect(e).toBeInstanceOf(FrodoError); | ||
| const error = e as FrodoError; | ||
| expect(error.message).toBe('Error saving connection profile'); | ||
| expect(error.originalErrors[0]?.message).toBe( | ||
| `Alias '${alias}' is already in use by connection profile '${host1}'. Please use a unique alias.` | ||
| ); | ||
| } | ||
| expect.assertions(3); | ||
| }); | ||
| }); | ||
|
|
||
| describe('setConnectionProfileAlias()', () => { | ||
| test(`1: Set unique alias for existing connection profile`, async () => { | ||
| const host = exampleHost; | ||
| const alias = 'unique-alias'; | ||
| const connectionProfile = { | ||
| [host]: { | ||
| ...exampleConnectionProfile, | ||
| }, | ||
| }; | ||
| fs.writeFileSync( | ||
| connectionProfilePath1, | ||
| JSON.stringify(connectionProfile, null, 2) | ||
| ); | ||
|
|
||
| state.setConnectionProfilesPath(connectionProfilePath1); | ||
| state.setHost(host); | ||
| state.setAlias(alias); | ||
| await ConnectionProfileOps.setConnectionProfileAlias({ | ||
| host, | ||
| alias, | ||
| state, | ||
| }); | ||
| const connections = JSON.parse( | ||
| fs.readFileSync(connectionProfilePath1, 'utf8') | ||
| ); | ||
| expect(connections[host]).toBeTruthy(); | ||
| expect(connections[host].alias).toEqual(alias); | ||
| }); | ||
|
|
||
| test(`2: Fail to set a conflicting alias to an existing connection profile`, async () => { | ||
| const host1 = 'https://openam-tenant1.forgeblocks.com/am'; | ||
| const host2 = 'https://openam-tenant2.forgeblocks.com/am'; | ||
| const alias = 'conflicting-alias'; | ||
|
|
||
| const connectionProfiles = { | ||
| [host1]: { | ||
| ...exampleConnectionProfile, | ||
| tenant: host1, | ||
| }, | ||
| [host2]: { | ||
| ...exampleConnectionProfile, | ||
| tenant: host2, | ||
| }, | ||
| }; | ||
| fs.writeFileSync( | ||
| connectionProfilePath1, | ||
| JSON.stringify(connectionProfiles, null, 2) | ||
| ); | ||
|
|
||
| state.setConnectionProfilesPath(connectionProfilePath1); | ||
| state.setHost(host1); | ||
| state.setAlias(alias); | ||
| await ConnectionProfileOps.setConnectionProfileAlias({ | ||
| host: host1, | ||
| alias, | ||
| state, | ||
| }); | ||
|
|
||
| state.setHost(host2); | ||
| try { | ||
| await ConnectionProfileOps.setConnectionProfileAlias({ | ||
| host: host2, | ||
| alias, | ||
| state, | ||
| }); | ||
| } catch (e) { | ||
| expect(e).toBeInstanceOf(FrodoError); | ||
| const error = e as FrodoError; | ||
| expect(error.message).toBe( | ||
| `Alias '${alias}' is already in use by connection profile '${host1}'. Please use a unique alias.` | ||
| ); | ||
| } | ||
| expect.assertions(2); | ||
| }); | ||
| }); | ||
|
|
||
| describe('deleteConnectionProfileAlias()', () => { | ||
| test(`1: Delete the alias of an existing connection profile`, async () => { | ||
| const host = exampleHost; | ||
| const alias = 'unique-alias'; | ||
|
|
||
| const connectionProfile = { | ||
| [host]: { | ||
| ...exampleConnectionProfile, | ||
| alias: alias, | ||
| }, | ||
| }; | ||
| fs.writeFileSync( | ||
| connectionProfilePath1, | ||
| JSON.stringify(connectionProfile, null, 2) | ||
| ); | ||
|
|
||
| state.setConnectionProfilesPath(connectionProfilePath1); | ||
| state.setHost(host); | ||
| await ConnectionProfileOps.deleteConnectionProfileAlias({ | ||
| host, | ||
| state, | ||
| }); | ||
| const connections = JSON.parse( | ||
| fs.readFileSync(connectionProfilePath1, 'utf8') | ||
| ); | ||
| expect(connections[host]).toBeTruthy(); | ||
| expect(connections[host].alias).toBeUndefined(); | ||
| }); | ||
|
|
||
| test(`2: Fail to delete the nonexistent alias of an exisiting connection profile`, async () => { | ||
| const host = exampleHost; | ||
| const connectionProfile = { | ||
| [host]: { | ||
| ...exampleConnectionProfile, | ||
| }, | ||
| }; | ||
| fs.writeFileSync( | ||
| connectionProfilePath1, | ||
| JSON.stringify(connectionProfile, null, 2) | ||
| ); | ||
|
|
||
| state.setConnectionProfilesPath(connectionProfilePath1); | ||
| state.setHost(host); | ||
| try { | ||
| await ConnectionProfileOps.deleteConnectionProfileAlias({ | ||
| host, | ||
| state, | ||
| }); | ||
| } catch (e) { | ||
| expect(e).toBeInstanceOf(FrodoError); | ||
| const error = e as FrodoError; | ||
| expect(error.message).toBe( | ||
| `No alias is set for connection profile '${host}'` | ||
| ); | ||
| } | ||
| expect.assertions(2); | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.