|
1 | 1 | const { expect, test, constants } = require('@twilio/cli-test');
|
2 | 2 | const TwilioClientCommand = require('../../src/base-commands/twilio-client-command');
|
3 | 3 | const { Config, ConfigData } = require('../../src/services/config');
|
4 |
| - |
5 |
| -const ORIGINAL_ENV = process.env; |
| 4 | +const { TwilioCliError } = require('../../src/services/error'); |
6 | 5 |
|
7 | 6 | describe('base-commands', () => {
|
8 | 7 | describe('twilio-client-command', () => {
|
9 | 8 | class TestClientCommand extends TwilioClientCommand {
|
10 | 9 | }
|
11 | 10 |
|
12 |
| - class ThrowingClientCommand extends TwilioClientCommand { |
| 11 | + class ThrowingUnknownClientCommand extends TwilioClientCommand { |
13 | 12 | async run() {
|
14 | 13 | await super.run();
|
15 | 14 |
|
16 | 15 | throw new Error('We were so wrong!');
|
17 | 16 | }
|
18 | 17 | }
|
19 | 18 |
|
| 19 | + class Throwing20003ClientCommand extends TwilioClientCommand { |
| 20 | + async run() { |
| 21 | + await super.run(); |
| 22 | + |
| 23 | + throw new TwilioCliError('Access Denied!', 20003); |
| 24 | + } |
| 25 | + } |
| 26 | + |
20 | 27 | class AccountSidClientCommand extends TwilioClientCommand {
|
21 | 28 | }
|
22 | 29 |
|
23 | 30 | TestClientCommand.flags = TwilioClientCommand.flags;
|
24 |
| - ThrowingClientCommand.flags = TwilioClientCommand.flags; |
| 31 | + ThrowingUnknownClientCommand.flags = TwilioClientCommand.flags; |
| 32 | + Throwing20003ClientCommand.flags = TwilioClientCommand.flags; |
25 | 33 | AccountSidClientCommand.flags = Object.assign({}, TwilioClientCommand.flags, TwilioClientCommand.accountSidFlag);
|
26 | 34 |
|
27 | 35 | const setUpTest = (
|
28 | 36 | args = [],
|
29 |
| - { setUpUserConfig = undefined, mockSecureStorage = true, commandClass: CommandClass = TestClientCommand } = {} |
| 37 | + { |
| 38 | + setUpUserConfig = undefined, |
| 39 | + mockSecureStorage = true, |
| 40 | + commandClass: CommandClass = TestClientCommand, |
| 41 | + envRegion, envEdge, configRegion = 'configRegion', configEdge |
| 42 | + } = {} |
30 | 43 | ) => {
|
31 | 44 | return test
|
32 | 45 | .do(ctx => {
|
33 | 46 | ctx.userConfig = new ConfigData();
|
| 47 | + ctx.userConfig.edge = configEdge; |
| 48 | + |
| 49 | + if (envRegion) { |
| 50 | + process.env.TWILIO_REGION = envRegion; |
| 51 | + process.env.TWILIO_ACCOUNT_SID = constants.FAKE_ACCOUNT_SID; |
| 52 | + process.env.TWILIO_AUTH_TOKEN = constants.FAKE_API_SECRET; |
| 53 | + } |
| 54 | + |
| 55 | + if (envEdge) { |
| 56 | + process.env.TWILIO_EDGE = envEdge; |
| 57 | + } |
| 58 | + |
34 | 59 | if (setUpUserConfig) {
|
35 | 60 | setUpUserConfig(ctx.userConfig);
|
36 | 61 | } else {
|
37 | 62 | ctx.userConfig.addProfile('MyFirstProfile', constants.FAKE_ACCOUNT_SID);
|
38 |
| - ctx.userConfig.addProfile('twilio-cli-unit-testing', constants.FAKE_ACCOUNT_SID, 'stage'); |
| 63 | + ctx.userConfig.addProfile('region-edge-testing', constants.FAKE_ACCOUNT_SID, configRegion); |
39 | 64 | }
|
40 | 65 | })
|
41 | 66 | .twilioCliEnv(Config)
|
@@ -100,27 +125,41 @@ describe('base-commands', () => {
|
100 | 125 | expect(ctx.stderr).to.contain('TWILIO_ACCOUNT_SID');
|
101 | 126 | });
|
102 | 127 |
|
103 |
| - setUpTest(['-p', 'twilio-cli-unit-testing']).it('should create a client for a non-default profile', ctx => { |
| 128 | + setUpTest(['-p', 'region-edge-testing']).it('should create a client for a non-default profile', ctx => { |
104 | 129 | expect(ctx.testCmd.twilioClient.accountSid).to.equal(constants.FAKE_ACCOUNT_SID);
|
105 | 130 | expect(ctx.testCmd.twilioClient.username).to.equal(constants.FAKE_API_KEY);
|
106 |
| - expect(ctx.testCmd.twilioClient.password).to.equal(constants.FAKE_API_SECRET + 'twilio-cli-unit-testing'); |
107 |
| - expect(ctx.testCmd.twilioClient.region).to.equal('stage'); |
| 131 | + expect(ctx.testCmd.twilioClient.password).to.equal(constants.FAKE_API_SECRET + 'region-edge-testing'); |
| 132 | + expect(ctx.testCmd.twilioClient.region).to.equal('configRegion'); |
108 | 133 | });
|
109 | 134 |
|
110 |
| - setUpTest(['-p', 'twilio-cli-unit-testing'], { mockSecureStorage: false }) |
| 135 | + setUpTest(['-p', 'region-edge-testing'], { mockSecureStorage: false }) |
111 | 136 | .exit(1)
|
112 | 137 | .it('should handle a secure storage error', ctx => {
|
113 |
| - expect(ctx.stderr).to.contain('Could not get credentials for profile "twilio-cli-unit-testing"'); |
| 138 | + expect(ctx.stderr).to.contain('Could not get credentials for profile "region-edge-testing"'); |
114 | 139 | expect(ctx.stderr).to.contain('To reconfigure the profile, run:');
|
115 |
| - expect(ctx.stderr).to.contain('twilio profiles:create --profile "twilio-cli-unit-testing"'); |
| 140 | + expect(ctx.stderr).to.contain('twilio profiles:create --profile "region-edge-testing"'); |
116 | 141 | });
|
117 | 142 |
|
118 |
| - setUpTest([], { commandClass: ThrowingClientCommand }) |
| 143 | + setUpTest([], { commandClass: ThrowingUnknownClientCommand }) |
119 | 144 | .exit(1)
|
120 | 145 | .it('should catch unhandled errors', ctx => {
|
121 | 146 | expect(ctx.stderr).to.contain('unexpected error');
|
122 | 147 | });
|
123 | 148 |
|
| 149 | + setUpTest([], { commandClass: Throwing20003ClientCommand }) |
| 150 | + .exit(20003) |
| 151 | + .it('should catch access denied errors and enhance the message', ctx => { |
| 152 | + expect(ctx.stderr).to.contain('Access Denied'); |
| 153 | + expect(ctx.stderr).to.contain('Standard API Keys'); |
| 154 | + }); |
| 155 | + |
| 156 | + setUpTest([], { commandClass: Throwing20003ClientCommand, envRegion: 'region' }) |
| 157 | + .exit(20003) |
| 158 | + .it('should catch access denied errors but not enhance the message when using env var auth', ctx => { |
| 159 | + expect(ctx.stderr).to.contain('Access Denied'); |
| 160 | + expect(ctx.stderr).to.not.contain('Standard API Keys'); |
| 161 | + }); |
| 162 | + |
124 | 163 | describe('parseProperties', () => {
|
125 | 164 | setUpTest().it('should ignore empty PropertyFlags', ctx => {
|
126 | 165 | const updatedProperties = ctx.testCmd.parseProperties();
|
@@ -236,69 +275,26 @@ describe('base-commands', () => {
|
236 | 275 | });
|
237 | 276 |
|
238 | 277 | describe('regional and edge support', () => {
|
239 |
| - const envTest = ( |
240 |
| - args = [], |
241 |
| - { envRegion, envEdge, configRegion = 'configRegion', configEdge } = {} |
242 |
| - ) => { |
243 |
| - return test |
244 |
| - .do(ctx => { |
245 |
| - ctx.userConfig = new ConfigData(); |
246 |
| - ctx.userConfig.edge = configEdge; |
247 |
| - |
248 |
| - if (envRegion) { |
249 |
| - process.env.TWILIO_REGION = envRegion; |
250 |
| - process.env.TWILIO_ACCOUNT_SID = constants.FAKE_ACCOUNT_SID; |
251 |
| - process.env.TWILIO_AUTH_TOKEN = constants.FAKE_API_SECRET; |
252 |
| - } |
253 |
| - if (envEdge) { |
254 |
| - process.env.TWILIO_EDGE = envEdge; |
255 |
| - } |
256 |
| - |
257 |
| - ctx.userConfig.addProfile('default-profile', constants.FAKE_ACCOUNT_SID); |
258 |
| - ctx.userConfig.addProfile('region-edge-testing', constants.FAKE_ACCOUNT_SID, configRegion); |
259 |
| - }) |
260 |
| - .twilioCliEnv(Config) |
261 |
| - .do(async ctx => { |
262 |
| - ctx.testCmd = new TwilioClientCommand(args, ctx.fakeConfig); |
263 |
| - ctx.testCmd.secureStorage = |
264 |
| - { |
265 |
| - async getCredentials(profileId) { |
266 |
| - return { |
267 |
| - apiKey: constants.FAKE_API_KEY, |
268 |
| - apiSecret: constants.FAKE_API_SECRET + profileId |
269 |
| - }; |
270 |
| - } |
271 |
| - }; |
272 |
| - |
273 |
| - // This is essentially what oclif does behind the scenes. |
274 |
| - try { |
275 |
| - await ctx.testCmd.run(); |
276 |
| - } catch (error) { |
277 |
| - await ctx.testCmd.catch(error); |
278 |
| - } |
279 |
| - process.env = ORIGINAL_ENV; |
280 |
| - }); |
281 |
| - }; |
282 |
| - |
283 |
| - envTest([], { configEdge: 'edge' }).it('should use the config edge when defined', ctx => { |
| 278 | + setUpTest([], { configEdge: 'edge' }).it('should use the config edge when defined', ctx => { |
284 | 279 | expect(ctx.testCmd.twilioApiClient.edge).to.equal('edge');
|
285 | 280 | expect(ctx.testCmd.twilioApiClient.region).to.be.undefined;
|
286 | 281 | });
|
287 | 282 |
|
288 |
| - envTest(['-p', 'region-edge-testing']).it('should use the config region when defined', ctx => { |
| 283 | + setUpTest(['-p', 'region-edge-testing']).it('should use the config region when defined', ctx => { |
289 | 284 | expect(ctx.testCmd.twilioApiClient.region).to.equal('configRegion');
|
290 | 285 | expect(ctx.testCmd.twilioApiClient.edge).to.be.undefined;
|
291 | 286 | });
|
292 | 287 |
|
293 |
| - envTest([], { envRegion: 'region' }).it('should use the env region over a config region', ctx => { |
| 288 | + setUpTest([], { envRegion: 'region' }).it('should use the env region over a config region', ctx => { |
294 | 289 | expect(ctx.testCmd.twilioApiClient.region).to.equal('region');
|
295 | 290 | expect(ctx.testCmd.twilioApiClient.edge).to.be.undefined;
|
296 | 291 | });
|
297 | 292 |
|
298 |
| - envTest([], { configEdge: 'configEdge', envEdge: 'edge', envRegion: 'region' }).it('should use the env edge over a config edge', ctx => { |
299 |
| - expect(ctx.testCmd.twilioApiClient.edge).to.equal('edge'); |
300 |
| - expect(ctx.testCmd.twilioApiClient.region).to.equal('region'); |
301 |
| - }); |
| 293 | + setUpTest([], { configEdge: 'configEdge', envEdge: 'edge', envRegion: 'region' }) |
| 294 | + .it('should use the env edge over a config edge', ctx => { |
| 295 | + expect(ctx.testCmd.twilioApiClient.edge).to.equal('edge'); |
| 296 | + expect(ctx.testCmd.twilioApiClient.region).to.equal('region'); |
| 297 | + }); |
302 | 298 | });
|
303 | 299 | });
|
304 | 300 | });
|
0 commit comments