Skip to content

Commit

Permalink
Make the account-sid flag an add-on flag that custom commands can use (
Browse files Browse the repository at this point in the history
  • Loading branch information
childish-sambino authored Jul 31, 2019
1 parent 15db53c commit 130c62a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
14 changes: 10 additions & 4 deletions src/base-commands/twilio-client-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const { TwilioApiClient } = require('../services/twilio-api');
const { TwilioCliError } = require('../services/error');
const { HELP_ENVIRONMENT_VARIABLES, UNEXPECTED_ERROR } = require('../services/messaging/help-messages');

// 'account-sid' is a special snowflake
const ACCOUNT_SID = 'account-sid';

class TwilioClientCommand extends BaseCommand {
constructor(argv, config, secureStorage) {
super(argv, config, secureStorage);
Expand Down Expand Up @@ -129,7 +132,7 @@ class TwilioClientCommand extends BaseCommand {

buildClient(ClientClass) {
return new ClientClass(this.currentProject.apiKey, this.currentProject.apiSecret, {
accountSid: this.flags['sub-account-sid'] || this.currentProject.accountSid,
accountSid: this.flags[ACCOUNT_SID] || this.currentProject.accountSid,
region: this.currentProject.region,
httpClient: this.httpClient
});
Expand All @@ -141,12 +144,15 @@ TwilioClientCommand.flags = Object.assign(
project: flags.string({
char: 'p',
description: 'Shorthand identifier for your Twilio project.'
}),
'sub-account-sid': flags.string({
description: 'access resources for the specified sub-account'
})
},
BaseCommand.flags
);

TwilioClientCommand.accountSidFlag = {
[ACCOUNT_SID]: flags.string({
description: 'Access resources for the specified account.'
})
};

module.exports = TwilioClientCommand;
14 changes: 10 additions & 4 deletions test/base-commands/twilio-client-command.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@ describe('base-commands', () => {
}
}

TestClientCommand.flags = TwilioClientCommand.flags;

class ThrowingClientCommand extends TwilioClientCommand {
async runCommand() {
throw new Error('We were so wrong!');
}
}

class AccountSidClientCommand extends TwilioClientCommand {
async runCommand() {
// no-op
}
}

TestClientCommand.flags = TwilioClientCommand.flags;
ThrowingClientCommand.flags = TwilioClientCommand.flags;
AccountSidClientCommand.flags = Object.assign({}, TwilioClientCommand.flags, TwilioClientCommand.accountSidFlag);

const setUpTest = (
args = [],
Expand Down Expand Up @@ -67,8 +73,8 @@ describe('base-commands', () => {
expect(ctx.testCmd.twilioClient.region).to.equal(undefined);
});

setUpTest(['-l', 'debug', '--sub-account-sid', 'ACbaccbaccbaccbaccbaccbaccbaccbacc']).it(
'should create a client for the active project with a subaccount',
setUpTest(['-l', 'debug', '--account-sid', 'ACbaccbaccbaccbaccbaccbaccbaccbacc'], { commandClass: AccountSidClientCommand }).it(
'should create a client for the active project with a different account SID',
async ctx => {
expect(ctx.stderr).to.contain('MyFirstProject');
expect(ctx.testCmd.twilioClient.accountSid).to.equal('ACbaccbaccbaccbaccbaccbaccbaccbacc');
Expand Down

0 comments on commit 130c62a

Please sign in to comment.