From 4ce8bd84e607b63ac5e2accd92cfba357070595d Mon Sep 17 00:00:00 2001 From: kridai Date: Tue, 27 Jul 2021 10:00:35 +0530 Subject: [PATCH 1/2] feat: Added support to make profile input mandatory based on config property --- src/base-commands/twilio-client-command.js | 6 ++++ src/services/config.js | 3 ++ .../twilio-client-command.test.js | 30 +++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/src/base-commands/twilio-client-command.js b/src/base-commands/twilio-client-command.js index 99247cc6..df2cf0a0 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); const reportUnconfigured = (verb, message = '', commandName = 'create') => { 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..2f7ac8f5 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,34 @@ 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', ''], { 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); From b4f698fca62c5cbc940e65fd7504d60dbc784451 Mon Sep 17 00:00:00 2001 From: kridai Date: Tue, 27 Jul 2021 10:59:07 +0530 Subject: [PATCH 2/2] removing duplicate test --- test/base-commands/twilio-client-command.test.js | 7 ------- 1 file changed, 7 deletions(-) diff --git a/test/base-commands/twilio-client-command.test.js b/test/base-commands/twilio-client-command.test.js index 2f7ac8f5..184479ad 100644 --- a/test/base-commands/twilio-client-command.test.js +++ b/test/base-commands/twilio-client-command.test.js @@ -105,13 +105,6 @@ describe('base-commands', () => { 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) => {