Skip to content

Commit

Permalink
fix: allow login when no user has no username (twilio#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
childish-sambino committed Mar 3, 2020
1 parent 174fd53 commit 7d4b55c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/commands/profiles/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,20 @@ class ProfilesCreate extends BaseCommand {
}

getApiKeyFriendlyName() {
const friendlyName = `twilio-cli for ${os.userInfo().username} on ${os.hostname()}`;
const username = this.getUsername();
const friendlyName = `twilio-cli${username ? ' for ' + username : ''} on ${os.hostname()}`;
return friendlyName.substring(0, 64);
}

getUsername() {
try {
return os.userInfo().username;
} catch (err) {
// Throws a SystemError if a user has no username or homedir.
this.logger.debug(err);
}
}

async saveCredentials() {
const apiKeyFriendlyName = this.getApiKeyFriendlyName();
let apiKey = null;
Expand Down
16 changes: 16 additions & 0 deletions test/commands/profiles/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ describe('commands', () => {
});
};

afterEach(() => {
sinon.restore();
});

createTest()
.nock('https://api.twilio.com', mockSuccess)
.do(ctx => ctx.testCmd.run())
Expand Down Expand Up @@ -75,6 +79,18 @@ describe('commands', () => {
expect(ctx.returned).to.equal('twilio-cli for some_super_long_fake_username on some_super_long_');
});

createTest()
.do(ctx => {
sinon.stub(os, 'hostname').returns('host');
sinon.stub(os, 'userInfo').throws('no username');
return ctx.testCmd.run();
})
.exit(1)
.it('handles bad user info', ctx => {
const friendlyName = ctx.testCmd.getApiKeyFriendlyName();
expect(friendlyName).to.equal('twilio-cli on host');
});

createTest(['not-an-account-sid'])
.do(ctx => {
const fakePrompt = ctx.testCmd.inquirer.prompt;
Expand Down

0 comments on commit 7d4b55c

Please sign in to comment.