-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more information about the account to auth contexts (#168)
- Loading branch information
1 parent
073ceec
commit 02b91c7
Showing
18 changed files
with
6,604 additions
and
720 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
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const { sendHttpRequest } = require('../../api/helper'); | ||
const _ = require('lodash'); | ||
const debug = require('debug')('codefresh:logic:auth:whoami'); | ||
|
||
const whoami = async (context) => { | ||
debug(`context is ${context} for ${context.name}`); | ||
const userOptions = { | ||
url: '/api/user/', | ||
method: 'GET', | ||
}; | ||
const user = await sendHttpRequest(userOptions, context, true); | ||
const accounts = _.get(user, 'account', {}); | ||
const accountInfo = _.chain(accounts) | ||
.filter(account => account.name === user.activeAccountName) | ||
.get('[0]', {}) | ||
.pick('name', 'runtimeEnvironment') | ||
.value(); | ||
|
||
debug(`account info ${JSON.stringify(accountInfo)}`); | ||
debug(`current account name is : ${JSON.stringify(_.get(user, 'activeAccountName'))}`); | ||
|
||
return accountInfo; | ||
}; | ||
|
||
module.exports = { whoami }; |
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 |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* eslint-disable */ | ||
|
||
const context = require('../index'); // eslint-disable-line | ||
const { whoami } = require('./whoami'); | ||
const { APIKeyContext } = require('./index'); | ||
|
||
jest.mock('../../api/helper', () => { // eslint-disable-line | ||
const apiHelper = require.requireActual('../../api/helper'); | ||
|
||
let response; | ||
let validateParams; | ||
|
||
const __setResponse = (x) => { | ||
response = x; | ||
}; | ||
|
||
const __setValidateParams = (x) => { | ||
validateParams = x; | ||
}; | ||
|
||
const __reset = () => { | ||
response = null; | ||
validateParams = null; | ||
}; | ||
|
||
const sendHttpRequest = (userOptions, context, flag) => { // eslint-disable-line | ||
if (validateParams) { | ||
validateParams(userOptions, context, flag); | ||
} | ||
|
||
if (response) { | ||
return response(); | ||
} else { // eslint-disable-line | ||
return apiHelper.sendHttpRequest(userOptions, context, flag); | ||
} | ||
}; | ||
|
||
return { | ||
__setResponse, | ||
__setValidateParams, | ||
__reset, | ||
sendHttpRequest, | ||
}; | ||
}); | ||
|
||
const apiHelper = require('../../api/helper'); | ||
|
||
beforeEach(() => { | ||
apiHelper.__reset(); | ||
}); | ||
|
||
describe('whoami tests', () => { | ||
|
||
describe('api key context', () => { | ||
|
||
describe('positive', () => { | ||
|
||
it('should return account information in case context is valid', () => { | ||
const context = new APIKeyContext({ | ||
name: 'test-context', | ||
url: 'http://test', | ||
token: 'test-token', | ||
}); | ||
|
||
apiHelper.__setValidateParams((userOptions, context, flag) => { | ||
expect(userOptions) | ||
.toEqual({ | ||
'method': 'GET', | ||
'url': '/api/user/', | ||
}); | ||
expect(flag) | ||
.toEqual(true); | ||
}); | ||
|
||
apiHelper.__setResponse(() => { | ||
return Promise.resolve({ | ||
activeAccountName: 'account-name-1', | ||
account: [ | ||
{ | ||
name: 'account-name-1', | ||
runtimeEnvironment: 'runtime-environment-1', | ||
}, | ||
{ | ||
name: 'account-name-2', | ||
runtimeEnvironment: 'runtime-environment-2', | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
return whoami(context) | ||
.then((accountInfo) => { | ||
expect(accountInfo) | ||
.toEqual({ | ||
name: 'account-name-1', | ||
runtimeEnvironment: 'runtime-environment-1', | ||
}); | ||
}); | ||
|
||
}); | ||
|
||
}); | ||
|
||
describe('negative', () => { | ||
|
||
it('should fail in case context is not valid', () => { | ||
const context = new APIKeyContext({ | ||
name: 'test-context', | ||
url: 'http://test', | ||
token: 'test-token', | ||
}); | ||
|
||
apiHelper.__setResponse(() => { | ||
return Promise.reject(new Error('http request error')); | ||
}); | ||
|
||
return whoami(context) | ||
.then(() => { | ||
throw new Error('should have failed'); | ||
}, (err) => { | ||
expect(err.toString()) | ||
.toEqual('Error: http request error'); | ||
}); | ||
|
||
}); | ||
|
||
}); | ||
}); | ||
|
||
}); |
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.