Skip to content
This repository was archived by the owner on Mar 11, 2022. It is now read-only.

Commit f05898d

Browse files
authored
Merge pull request #341 from cloudant/340-support-promises-and-vcap
Fix plugin configuration when instantiating a new client using VCAP
2 parents 7141e96 + 83a1128 commit f05898d

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# UNRELEASED
2+
- [FIXED] Don't override `plugins` array when instantiating a new client using VCAP.
3+
14
# 2.4.0 (2018-09-19)
25
- [FIXED] Case where `username` and `password` options were not used if a `url` was supplied.
36
- [FIXED] Case where `vcapServices` was supplied with a basic-auth `url`.

lib/client.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,19 @@ class CloudantClient {
4545
self._usePromises = false;
4646
self.useLegacyPlugin = false;
4747

48-
// build plugin array
48+
// Build plugin array.
4949
var plugins = [];
50-
// If IAM key found in VCAP, add IAM plugin
50+
5151
if (self._cfg.creds && self._cfg.creds.iamApiKey) {
52-
plugins.push({ iamauth: { iamApiKey: self._cfg.creds.iamApiKey } });
53-
} else if (typeof this._cfg.plugin === 'undefined' && typeof this._cfg.plugins === 'undefined') {
54-
plugins = [ 'cookieauth' ]; // default
55-
} else {
52+
// => Found IAM API key in VCAP - Add 'iamauth' plugin.
53+
plugins = [ { iamauth: { iamApiKey: self._cfg.creds.iamApiKey } } ];
54+
} else if (typeof this._cfg.plugins === 'undefined') {
55+
// => No plugins specified - Add 'cookieauth' plugin.
56+
plugins = [ 'cookieauth' ];
57+
}
58+
59+
// Add user specified plugins.
60+
if (typeof this._cfg.plugins !== 'undefined') {
5661
[].concat(self._cfg.plugins).forEach(function(plugin) {
5762
if (typeof plugin !== 'function' || plugin.pluginVersion >= 2) {
5863
plugins.push(plugin);

test/plugins/iamauth.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
const assert = require('assert');
1919
const Client = require('../../lib/client.js');
20+
const Cloudant = require('../../cloudant.js');
2021
const nock = require('../nock.js');
2122
const uuidv4 = require('uuid/v4'); // random
2223

@@ -477,4 +478,39 @@ describe('#db IAMAuth Plugin', function() {
477478
'did not throw with expected message'
478479
);
479480
});
481+
482+
it('supports using vcap with the promise plugin', function(done) {
483+
// NOTE: Use NOCK_OFF=true to test using a real CouchDB instance.
484+
var iamMocks = nock(TOKEN_SERVER)
485+
.post('/identity/token', {
486+
'grant_type': 'urn:ibm:params:oauth:grant-type:apikey',
487+
'response_type': 'cloud_iam',
488+
'apikey': IAM_API_KEY
489+
})
490+
.reply(200, MOCK_IAM_TOKEN_RESPONSE);
491+
492+
var cloudantMocks = nock(SERVER)
493+
.post('/_iam_session', {access_token: MOCK_ACCESS_TOKEN})
494+
.reply(200, {ok: true}, MOCK_SET_IAM_SESSION_HEADER)
495+
.get(DBNAME)
496+
.reply(200, {doc_count: 0});
497+
498+
var cloudant = new Cloudant({
499+
vcapServices: {
500+
cloudantNoSQLDB: [
501+
{ credentials: { apikey: IAM_API_KEY, host: `${ME}.cloudant.com` } }
502+
]
503+
},
504+
plugins: 'promises'
505+
});
506+
507+
cloudant.use(DBNAME.substring(1)).info().then((data) => {
508+
assert.equal(data.doc_count, 0);
509+
iamMocks.done();
510+
cloudantMocks.done();
511+
done();
512+
}).catch(function(err) {
513+
assert.fail(`Unexpected reject: ${err}`);
514+
});
515+
});
480516
});

0 commit comments

Comments
 (0)