Skip to content

Commit

Permalink
Use VCAP cloudant env vars if available. Closes #3.
Browse files Browse the repository at this point in the history
  • Loading branch information
JonHoughton committed Aug 16, 2016
1 parent e89ae80 commit d5f6eba
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/lib/cloudant.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function buildEnv() {
// Build information for Cloudant API (obtain account from endpoint env var)
// The account is derived by stripping the protocol:// and the :port from the endpoint.
// Then .cloudant.com is stripped from the end.
var endpoint = process.env.HUBOT_CLOUDANT_ENDPOINT;
var endpoint = process.env.VCAP_SERVICES_CLOUDANTNOSQLDB_0_CREDENTIALS_HOST || process.env.HUBOT_CLOUDANT_ENDPOINT;
var account = endpoint;
if (account) {
var protocolSepIndex = account.indexOf('://');
Expand All @@ -42,12 +42,12 @@ function buildEnv() {
// environment variables are the default one associated with the account) and the
// user name to use on all Cloudant API calls (if it is the same as the account,
// then it should not be specified).
var actualusername = process.env.HUBOT_CLOUDANT_KEY || account;
var actualusername = process.env.VCAP_SERVICES_CLOUDANTNOSQLDB_0_CREDENTIALS_USERNAME || process.env.HUBOT_CLOUDANT_KEY || account;
var username;
if (actualusername && account && actualusername !== account) username = actualusername;

// Build information for Cloudant API (obtain password from password env var).
var password = process.env.HUBOT_CLOUDANT_PASSWORD;
var password = process.env.VCAP_SERVICES_CLOUDANTNOSQLDB_0_CREDENTIALS_PASSWORD || process.env.HUBOT_CLOUDANT_PASSWORD;

// Store api credential info
retEnv.api = {
Expand Down
85 changes: 85 additions & 0 deletions test/cloudant.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -515,3 +515,88 @@ describe('Interacting with Cloudant through regular expression interface', funct
});

});

// Passing arrow functions to mocha is discouraged: https://mochajs.org/#arrow-functions
// return promises from mocha tests rather than calling done() - http://tobyho.com/2015/12/16/mocha-with-promises/
describe('Test with various env variable settings', function() {

let room;
let saveEndpoint;


before(function() {
saveEndpoint = process.env.HUBOT_CLOUDANT_ENDPOINT;
mockUtils.setupMockery();
});

after(function() {
mockUtils.destroyMockery();
process.env.HUBOT_CLOUDANT_ENDPOINT = saveEndpoint;
const cloudant = require('../src/lib/cloudant').cloudant;
cloudant.clearEnv();
});

beforeEach(function() {
delete process.env.VCAP_SERVICES_CLOUDANTNOSQLDB_0_CREDENTIALS_HOST;
delete process.env.VCAP_SERVICES_CLOUDANTNOSQLDB_0_CREDENTIALS_USERNAME;
delete process.env.VCAP_SERVICES_CLOUDANTNOSQLDB_0_CREDENTIALS_PASSWORD;
process.env.HUBOT_CLOUDANT_ENDPOINT = saveEndpoint;
const cloudant = require('../src/lib/cloudant').cloudant;
cloudant.clearEnv();
room = helper.createRoom();
// Force all emits into a reply.
room.robot.on('ibmcloud.formatter', function(event) {
if (event.message) {
event.response.reply(event.message);
}
else {
event.response.send({attachments: event.attachments});
}
});
});

afterEach(function() {
room.destroy();
});

context('user calls `cloudant list databases`: use VCAP vars', function(done) {
it('should send list databases properly', function(done) {
process.env.VCAP_SERVICES_CLOUDANTNOSQLDB_0_CREDENTIALS_HOST = process.env.HUBOT_CLOUDANT_ENDPOINT.replace('https://', '');
process.env.VCAP_SERVICES_CLOUDANTNOSQLDB_0_CREDENTIALS_USERNAME = process.env.HUBOT_CLOUDANT_KEY;
process.env.VCAP_SERVICES_CLOUDANTNOSQLDB_0_CREDENTIALS_PASSWORD = process.env.HUBOT_CLOUDANT_PASSWORD;
room.user.say('mimiron', '@hubot cloudant list databases').then(() => {
return waitForMessageQueue(room, 3);
}).then(() => {
const dbs = mockUtils.RESOURCES.DATABASES.join('\n');
expect(room.messages[1]).to.eql(['hubot', '@mimiron ' + i18n.__('cloudant.listdatabases')]);
let event = room.messages[2][1];
expect(event.attachments.length).to.eql(1);
expect(event.attachments[0].title).to.eql(i18n.__('cloudant.listdatabases.title'));
expect(event.attachments[0].text).to.eql(dbs);
done();
}).catch((error) => {
done(error);
});
});
});

context('user calls `cloudant list databases`: add port to endpoint', function(done) {
it('should send list databases properly', function(done) {
process.env.HUBOT_CLOUDANT_ENDPOINT += ':443';
room.user.say('mimiron', '@hubot cloudant list databases').then(() => {
return waitForMessageQueue(room, 3);
}).then(() => {
const dbs = mockUtils.RESOURCES.DATABASES.join('\n');
expect(room.messages[1]).to.eql(['hubot', '@mimiron ' + i18n.__('cloudant.listdatabases')]);
let event = room.messages[2][1];
expect(event.attachments.length).to.eql(1);
expect(event.attachments[0].title).to.eql(i18n.__('cloudant.listdatabases.title'));
expect(event.attachments[0].text).to.eql(dbs);
done();
}).catch((error) => {
done(error);
});
});
});

});

0 comments on commit d5f6eba

Please sign in to comment.