Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the account-sid flag an add-on flag that custom commands can use #48

Merged
merged 1 commit into from
Jul 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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