From fbdc36ba99be611dcaf5d08e8697a42e2256b59e Mon Sep 17 00:00:00 2001 From: kridai Date: Thu, 29 Jul 2021 20:26:36 +0530 Subject: [PATCH] feat: Added support to make profile input mandatory based on config property (#135) * feat: Added support to make profile input mandatory based on config property * removing duplicate test --- src/base-commands/twilio-client-command.js | 6 +++++ src/services/config.js | 3 +++ .../twilio-client-command.test.js | 23 +++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/src/base-commands/twilio-client-command.js b/src/base-commands/twilio-client-command.js index 3b51c0b4..ca8fb654 100644 --- a/src/base-commands/twilio-client-command.js +++ b/src/base-commands/twilio-client-command.js @@ -24,6 +24,12 @@ class TwilioClientCommand extends BaseCommand { async run() { await super.run(); + // check if profile flag is required as per the config + if (this.userConfig.requireProfileInput && !this.flags.profile) { + throw new TwilioCliError( + `Error: Missing required flag:\n -p, --profile PROFILE ${TwilioClientCommand.flags.profile.description} To disable this check run:\n\n twilio config:set --no-require-profile-input`, + ); + } this.currentProfile = this.userConfig.getProfileById(this.flags.profile); let keytarFlag = false; diff --git a/src/services/config.js b/src/services/config.js index 7c5ac37d..b679f339 100644 --- a/src/services/config.js +++ b/src/services/config.js @@ -32,6 +32,7 @@ class ConfigData { this.projects = []; this.activeProfile = null; this.profiles = {}; + this.requireProfileInput = undefined; } getProfileFromEnvironment() { @@ -182,6 +183,7 @@ class ConfigData { loadFromObject(configObj) { this.edge = configObj.edge; this.email = configObj.email || {}; + this.requireProfileInput = configObj.requireProfileInput; this.prompts = configObj.prompts || {}; // Note the historical 'projects' naming. configObj.projects = configObj.projects || []; @@ -217,6 +219,7 @@ class Config { configData = { edge: configData.edge, email: configData.email, + requireProfileInput: configData.requireProfileInput, prompts: configData.prompts, // Note the historical 'projects' naming. projects: configData.projects, diff --git a/test/base-commands/twilio-client-command.test.js b/test/base-commands/twilio-client-command.test.js index 4cff13b4..184479ad 100644 --- a/test/base-commands/twilio-client-command.test.js +++ b/test/base-commands/twilio-client-command.test.js @@ -42,12 +42,14 @@ describe('base-commands', () => { envEdge, configRegion = 'configRegion', configEdge, + configRequireProfileInput, } = {}, ) => { return test .do((ctx) => { ctx.userConfig = new ConfigData(); ctx.userConfig.edge = configEdge; + ctx.userConfig.requireProfileInput = configRequireProfileInput; if (envRegion) { process.env.TWILIO_REGION = envRegion; @@ -89,6 +91,27 @@ describe('base-commands', () => { }); }; + setUpTest([], { configRequireProfileInput: true }) + .exit(1) + .it('should fail if requireProfileInput attribute in config is set but flag is not passed', (ctx) => { + expect(ctx.stderr).to.contain('Error: Missing required flag:'); + expect(ctx.stderr).to.contain('-p, --profile PROFILE'); + }); + + setUpTest(['-p', ''], { configRequireProfileInput: true }) + .exit(1) + .it('should fail if requireProfileInput attribute in config is set but flag is passed as empty string', (ctx) => { + expect(ctx.stderr).to.contain('Error: Missing required flag:'); + expect(ctx.stderr).to.contain('-p, --profile PROFILE'); + }); + + setUpTest(['-p', 'region-edge-testing'], { configRequireProfileInput: true }).it( + 'should use the profile passed, when requireProfileInput flag is set in config and valid profile is passed', + (ctx) => { + expect(ctx.testCmd.currentProfile.id).to.equal('region-edge-testing'); + }, + ); + setUpTest(['-l', 'debug']).it('should create a client for the active profile', (ctx) => { expect(ctx.stderr).to.contain('MyFirstProfile'); expect(ctx.testCmd.twilioClient.accountSid).to.equal(constants.FAKE_ACCOUNT_SID);