Skip to content

Commit

Permalink
chore: CLI Profile Update - Using config file instead of the system k…
Browse files Browse the repository at this point in the history
…eychain (#264)

* chore: CLI Profile Update - Using config file instead of the system keychain

* Added the test cases.

* Addressed the review comments

* Added the missing alignment.

* Addressed the review comments
  • Loading branch information
ravali-rimmalapudi committed Jun 28, 2021
1 parent 981cad5 commit b71fb5e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/commands/profiles/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ class ProfilesCreate extends BaseCommand {
this.logger.debug(error);
throw new TwilioCliError('Could not create an API Key.');
}

await this.removeKeytarKeysByProfileId(this.profileId);
this.userConfig.addProfile(this.profileId, this.accountSid, this.region, apiKey.sid, apiKey.secret);
const configSavedMessage = await this.configFile.save(this.userConfig);

Expand All @@ -239,6 +239,17 @@ class ProfilesCreate extends BaseCommand {
);
this.logger.info(configSavedMessage);
}

async removeKeytarKeysByProfileId(profileId) {
if (this.userConfig.projects.find((p) => p.id === profileId)) {
const removed = await this.secureStorage.removeCredentials(profileId);
if (removed === true) {
this.logger.info('Deleted key from keytar.');
} else {
this.logger.warn(`Could not delete ${profileId} key from keytar.`);
}
}
}
}

ProfilesCreate.aliases = ['profiles:add', 'login'];
Expand Down
41 changes: 40 additions & 1 deletion test/commands/profiles/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ const helpMessages = require('../../../src/services/messaging/help-messages');
describe('commands', () => {
describe('profiles', () => {
describe('create', () => {
const createTest = (commandArgs = [], profileId = 'default') =>
const createTest = (commandArgs = [], { profileId = 'default', addProjects = [], removeCred = true } = {}) =>
test
.twilioFakeProfile(ConfigData)
.do((ctx) => {
ctx.userConfig = new ConfigData();
addProjects.forEach((project) => ctx.userConfig.addProject(project, constants.FAKE_ACCOUNT_SID));
})
.twilioCliEnv(Config)
.twilioCreateCommand(ProfilesCreate, commandArgs)
.stdout()
Expand All @@ -35,6 +39,11 @@ describe('commands', () => {
overwrite: true,
});
ctx.testCmd.inquirer.prompt = fakePrompt;
})
.do((ctx) => {
ctx.testCmd.secureStorage.removeCredentials = () => {
return removeCred;
};
});

const mockSuccess = (api) => {
Expand Down Expand Up @@ -65,6 +74,36 @@ describe('commands', () => {
);
});

createTest([], { profileId: 'profile1', addProjects: ['profile1'] })
.nock('https://api.twilio.com', mockSuccess)
.do((ctx) => ctx.testCmd.run())
.it('runs profiles:create with existing profile in Projects', (ctx) => {
expect(ctx.stdout).to.equal('');
expect(ctx.stderr).to.contain(helpMessages.AUTH_TOKEN_NOT_SAVED);
expect(ctx.stderr).to.contain('Saved profile1.');
expect(ctx.stderr).to.contain('Deleted key from keytar.');
expect(ctx.stderr).to.contain('configuration saved');
expect(ctx.stderr).to.contain(`Created API Key ${constants.FAKE_API_KEY} and stored the secret in Config.`);
expect(ctx.stderr).to.contain(
`See: https://www.twilio.com/console/runtime/api-keys/${constants.FAKE_API_KEY}`,
);
});

createTest([], { profileId: 'profile1', addProjects: ['profile1'], removeCred: false })
.nock('https://api.twilio.com', mockSuccess)
.do((ctx) => ctx.testCmd.run())
.it('runs profiles:create with existing profile in Projects with Keytar remove failed', (ctx) => {
expect(ctx.stdout).to.equal('');
expect(ctx.stderr).to.contain(helpMessages.AUTH_TOKEN_NOT_SAVED);
expect(ctx.stderr).to.contain('Saved profile1.');
expect(ctx.stderr).to.contain('Could not delete profile1 key from keytar.');
expect(ctx.stderr).to.contain('configuration saved');
expect(ctx.stderr).to.contain(`Created API Key ${constants.FAKE_API_KEY} and stored the secret in Config.`);
expect(ctx.stderr).to.contain(
`See: https://www.twilio.com/console/runtime/api-keys/${constants.FAKE_API_KEY}`,
);
});

createTest()
.do((ctx) => {
sinon.stub(os, 'hostname').returns('some_super_long_fake_hostname');
Expand Down

0 comments on commit b71fb5e

Please sign in to comment.