Skip to content

Commit

Permalink
fix(credential):ds-438 use auth_type sent from api (#455)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolearagao authored Sep 18, 2024
1 parent 1db100d commit 7dc6e10
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 20 deletions.
3 changes: 3 additions & 0 deletions src/helpers/__tests__/__snapshots__/helpers.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ exports[`getAuthType should return a credential type: credentialTypes 1`] = `
"Username and Password",
"Token",
"SSH Key",
"SSH Key File",
"Unknown credential type",
"Unknown credential type",
]
`;

Expand Down
16 changes: 7 additions & 9 deletions src/helpers/__tests__/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,20 @@ describe('getAuthType', () => {
become_user: undefined,
become_password: undefined,
sources: [],
auth_type: undefined,
...partialCredential
});

it(`should return a credential type`, () => {
expect([
generateAuthType({ username: 'testUser', password: 'testPassword' }),
generateAuthType({ auth_token: 'mockToken' }),
generateAuthType({ ssh_key: 'mockSSH' })
generateAuthType({ auth_type: 'password' }),
generateAuthType({ auth_type: 'auth_token' }),
generateAuthType({ auth_type: 'ssh_key' }),
generateAuthType({ auth_type: 'ssh_keyfile' }),
generateAuthType({ auth_type: 'lorem' }),
generateAuthType({ auth_type: 'ipsum' })
]).toMatchSnapshot('credentialTypes');
});

it('should throw an error when credential has no authentication information', () => {
expect(() => {
generateAuthType({});
}).toThrow('Unknown credential type');
});
});

describe('noopTranslate', () => {
Expand Down
26 changes: 15 additions & 11 deletions src/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ const normalizeTotal = (
enum authType {
UsernameAndPassword = 'Username and Password',
Token = 'Token',
SSHKey = 'SSH Key'
SSHKey = 'SSH Key',
SSHKeyFile = 'SSH Key File',
Unknown = 'Unknown credential type'
}

/**
Expand All @@ -96,17 +98,19 @@ enum authType {
* @param {CredentialType} credential - The CredentialType object representing authentication information.
* @returns {string} - A string indicating the authentication type, e.g., "Username and Password".
*/
const getAuthType = ({ username, password, auth_token, ssh_key }: CredentialType): authType => {
if (username && password) {
return authType.UsernameAndPassword;
const getAuthType = ({ auth_type }: CredentialType): authType => {
switch (auth_type) {
case 'password':
return authType.UsernameAndPassword;
case 'auth_token':
return authType.Token;
case 'ssh_key':
return authType.SSHKey;
case 'ssh_keyfile':
return authType.SSHKeyFile;
default:
return authType.Unknown;
}
if (auth_token) {
return authType.Token;
}
if (ssh_key) {
return authType.SSHKey;
}
throw new Error('Unknown credential type');
};

/**
Expand Down
1 change: 1 addition & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type CredentialType = {
become_user: string;
become_password: string;
sources: SourceType[];
auth_type: string;
};

export type SourceConnectionType = {
Expand Down

0 comments on commit 7dc6e10

Please sign in to comment.