Skip to content

Commit

Permalink
feat: update the mechanics of the login command (twilio#156)
Browse files Browse the repository at this point in the history
* change 'Twilio Project' to 'Twilio Account or Subaccount'
* trim leading/trailing whitespace from profile ID
* move the profile ID entry to last (after account and auth token)
  • Loading branch information
childish-sambino committed Mar 4, 2020
1 parent 7d4b55c commit a3f8e02
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
24 changes: 13 additions & 11 deletions src/commands/profiles/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,13 @@ class ProfilesCreate extends BaseCommand {
await this.secureStorage.loadKeytar();

this.loadArguments();
await this.promptForProfileId();

if (!(await this.confirmProfileAndEnvVars()) || !(await this.confirmOverwrite())) {
this.cancel();
}

this.loadAccountSid();
this.loadAuthToken();
await this.promptForCredentials();

if (await this.validateCredentials()) {
await this.loadProfileId();
await this.saveCredentials();
this.logger.info(`Saved ${this.profileId}.`);
} else {
Expand All @@ -53,22 +49,28 @@ class ProfilesCreate extends BaseCommand {
}

loadArguments() {
this.profileId = this.flags.profile;
this.force = this.flags.force;
this.region = this.flags.region;
}

async promptForProfileId() {
async loadProfileId() {
this.profileId = this.flags.profile;
if (!this.profileId) {
const answer = await this.inquirer.prompt([
{
name: 'profileId',
message: this.getPromptMessage(ProfilesCreate.flags.profile.description),
validate: input => Boolean(input)
validate: input => Boolean(input.trim())
}
]);
this.profileId = answer.profileId;
}

this.profileId = this.profileId.trim();

if (!(await this.confirmProfileAndEnvVars()) || !(await this.confirmOverwrite())) {
this.cancel();
}
}

loadAccountSid() {
Expand Down Expand Up @@ -249,12 +251,12 @@ class ProfilesCreate extends BaseCommand {
}

ProfilesCreate.aliases = ['profiles:add', 'login'];
ProfilesCreate.description = 'create a new profile to store Twilio Project credentials and configuration';
ProfilesCreate.description = 'create a new profile to store Twilio Account credentials and configuration';

ProfilesCreate.flags = Object.assign(
{
'auth-token': flags.string({
description: 'Your Twilio Auth Token for your Twilio Project.'
description: 'Your Twilio Auth Token for your Twilio Account or Subaccount.'
}),
force: flags.boolean({
char: 'f',
Expand All @@ -274,7 +276,7 @@ ProfilesCreate.flags = Object.assign(
ProfilesCreate.args = [
{
name: 'account-sid',
description: 'The Account SID for your Twilio Project.'
description: 'The Account SID for your Twilio Account or Subaccount.'
}
];

Expand Down
2 changes: 1 addition & 1 deletion src/services/messaging/help-messages.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const WHERE_TO_FIND_ACCOUNT_SID = 'You can find your Account SID and Auth Token at https://www.twilio.com/console';
const AUTH_TOKEN_NOT_SAVED =
'Your Auth Token will be used once to create an API Key for future CLI access to your Twilio Project, and then forgotten.';
'Your Auth Token will be used once to create an API Key for future CLI access to your Twilio Account or Subaccount, and then forgotten.';

module.exports = {
WHERE_TO_FIND_ACCOUNT_SID,
Expand Down
16 changes: 8 additions & 8 deletions test/commands/profiles/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ describe('commands', () => {
fakePrompt
.onFirstCall()
.resolves({
profileId
accountSid: constants.FAKE_ACCOUNT_SID,
authToken: '0'.repeat(32)
})
.onSecondCall()
.resolves({
overwrite: true
profileId
})
.onThirdCall()
.resolves({
accountSid: constants.FAKE_ACCOUNT_SID,
authToken: '0'.repeat(32)
overwrite: true
});
ctx.testCmd.inquirer.prompt = fakePrompt;
ctx.testCmd.secureStorage.loadKeytar = sinon.fake.resolves(true);
Expand Down Expand Up @@ -94,7 +94,7 @@ describe('commands', () => {
createTest(['not-an-account-sid'])
.do(ctx => {
const fakePrompt = ctx.testCmd.inquirer.prompt;
fakePrompt.onThirdCall().resolves({
fakePrompt.onFirstCall().resolves({
authToken: constants.FAKE_API_SECRET
});

Expand All @@ -106,7 +106,7 @@ describe('commands', () => {
createTest()
.do(ctx => {
const fakePrompt = ctx.testCmd.inquirer.prompt;
fakePrompt.onThirdCall().resolves({
fakePrompt.onFirstCall().resolves({
accountSid: constants.FAKE_ACCOUNT_SID,
authToken: '0'
});
Expand All @@ -120,7 +120,7 @@ describe('commands', () => {
.nock('https://api.twilio.com', mockSuccess)
.do(ctx => {
const fakePrompt = ctx.testCmd.inquirer.prompt;
fakePrompt.onThirdCall().resolves({
fakePrompt.onFirstCall().resolves({
accountSid: constants.FAKE_ACCOUNT_SID,
authToken: 'blurgh'
});
Expand All @@ -139,7 +139,7 @@ describe('commands', () => {
process.env.TWILIO_API_SECRET = constants.FAKE_API_SECRET;

const fakePrompt = ctx.testCmd.inquirer.prompt;
fakePrompt.onSecondCall().resolves({
fakePrompt.onThirdCall().resolves({
affirmative: false
});

Expand Down

0 comments on commit a3f8e02

Please sign in to comment.