Skip to content

Commit a36907b

Browse files
dpopp07anweshan
authored andcommitted
fix bug that prevented reading iam apikey from env (#711)
* fix bug that prevented reading iam apikey from env * add tests for reading iam creds from env and vcap
1 parent 7f6cb8e commit a36907b

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

lib/base_service.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,12 @@ export class BaseService {
125125
options,
126126
_options
127127
);
128-
if (options.iam_apikey || options.iam_access_token) {
128+
129+
if (_options.iam_apikey || _options.iam_access_token) {
129130
this.tokenManager = new IamTokenManagerV1({
130-
iamApikey: options.iam_apikey,
131-
iamAccessToken: options.iam_access_token,
132-
iamUrl: options.iam_url
131+
iamApikey: _options.iam_apikey,
132+
iamAccessToken: _options.iam_access_token,
133+
iamUrl: _options.iam_url
133134
});
134135
} else {
135136
this.tokenManager = null;

test/unit/test.base_service.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ describe('BaseService', function() {
112112
url: 'https://gateway.watsonplatform.net/test/api',
113113
};
114114
assert.deepEqual(actual, expected);
115+
assert.notEqual(instance.tokenManager, null);
115116
});
116117

117118
it('should prefer hard-coded credentials over environment properties', function() {
@@ -218,4 +219,16 @@ describe('BaseService', function() {
218219
instance.setAccessToken('abcd-1234');
219220
assert.notEqual(instance.tokenManager, null);
220221
});
222+
223+
it('should create a token manager instance if env variables specify iam credentials', function() {
224+
process.env.TEST_IAM_APIKEY = 'test1234';
225+
const instance = new TestService();
226+
const actual = instance.getCredentials();
227+
const expected = {
228+
iam_apikey: 'test1234',
229+
url: 'https://gateway.watsonplatform.net/test/api',
230+
};
231+
assert.deepEqual(actual, expected);
232+
assert.notEqual(instance.tokenManager, null);
233+
});
221234
});

0 commit comments

Comments
 (0)