forked from aws/aws-sdk-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.spec.js
583 lines (519 loc) · 18.1 KB
/
config.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
var hasProp = {}.hasOwnProperty;
var helpers = require('./helpers');
var AWS = helpers.AWS;
var iniLoader = AWS.util.iniLoader;
var configure = function(options) {
return new AWS.Config(options);
};
describe('AWS.Config', function() {
describe('constructor', function() {
it('should be able to pass in a Config object as parameter', function() {
var config = new AWS.Config({
sslEnabled: false,
maxRetries: 0
});
var copyConfig = new AWS.Config(config);
expect(copyConfig).not.to.equal(config);
expect(copyConfig.sslEnabled).to.equal(false);
expect(copyConfig.maxRetries).to.equal(0);
});
it('should be able to pass credential values directly', function() {
var config = new AWS.Config({
accessKeyId: 'akid',
secretAccessKey: 'secret',
sessionToken: 'session'
});
expect(config.credentials.accessKeyId).to.equal('akid');
expect(config.credentials.secretAccessKey).to.equal('secret');
expect(config.credentials.sessionToken).to.equal('session');
});
});
describe('region', function() {
var oldEnv = process.env;
beforeEach(function(done) {
process.env = {};
done();
});
afterEach(function() {
process.env = oldEnv;
if (iniLoader) iniLoader.clearCachedFiles();//iniLoader not available in browsers
});
it('defaults to undefined', function() {
expect(configure().region).not.to.exist;
});
if (AWS.util.isNode()) {
it('grabs AWS_REGION from the env', function() {
process.env.AWS_REGION = 'us-west-2';
var config = new AWS.Config();
expect(config.region).to.equal('us-west-2');
});
it('also grabs AMAZON_REGION from the env', function() {
process.env.AMAZON_REGION = 'us-west-1';
var config = new AWS.Config();
expect(config.region).to.equal('us-west-1');
});
it('prefers AWS_REGION to AMAZON_REGION', function() {
process.env.AWS_REGION = 'us-west-2';
process.env.AMAZON_REGION = 'us-west-1';
var config = new AWS.Config();
expect(config.region).to.equal('us-west-2');
});
describe('shared config file', function() {
beforeEach(function() {
var os = require('os');
helpers.spyOn(os, 'homedir').andReturn('/home/user');
});
it('grabs region from shared credentials file if AWS_SDK_LOAD_CONFIG is set', function() {
process.env.AWS_SDK_LOAD_CONFIG = '1';
helpers.spyOn(AWS.util, 'readFileSync').andCallFake(function(path) {
if (path.match(/[\/\\]home[\/\\]user[\/\\].aws[\/\\]credentials/)) {
return '[default]\nregion = us-west-2';
} else {
return '[default]\nregion = eu-east-1';
}
});
var config = new AWS.Config();
expect(config.region).to.equal('us-west-2');
});
it('loads file from path specified in AWS_SHARED_CREDENTIALS_FILE if AWS_SDK_LOAD_CONFIG is set', function() {
process.env.AWS_SDK_LOAD_CONFIG = '1';
process.env.AWS_SHARED_CREDENTIALS_FILE = '/path/to/user/config/file';
helpers.spyOn(AWS.util, 'readFileSync').andCallFake(function(path) {
if (path === '/path/to/user/config/file') {
return '[default]\nregion = us-west-2';
} else {
return '[default]\nregion = eu-east-1';
}
});
var config = new AWS.Config();
expect(config.region).to.equal('us-west-2');
});
it('grabs region from shared config if AWS_SDK_LOAD_CONFIG is set', function() {
process.env.AWS_SDK_LOAD_CONFIG = '1';
helpers.spyOn(AWS.util, 'readFileSync').andCallFake(function(path) {
if (path.match(/[\/\\]home[\/\\]user[\/\\].aws[\/\\]config/)) {
return '[default]\nregion = us-west-2';
} else {
return '[default]\naws_access_key_id = AKIAIOSFODNN7EXAMPLE\naws_secret_access_key = wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY';
}
});
var config = new AWS.Config();
expect(config.region).to.equal('us-west-2');
});
it('loads file from path specified in AWS_CONFIG_FILE if AWS_SDK_LOAD_CONFIG is set', function() {
process.env.AWS_SDK_LOAD_CONFIG = '1';
process.env.AWS_CONFIG_FILE = '/path/to/user/config/file';
helpers.spyOn(AWS.util, 'readFileSync').andCallFake(function(path) {
if (path === '/path/to/user/config/file') {
return '[default]\nregion = us-west-2';
} else {
return '[default]\naws_access_key_id = AKIAIOSFODNN7EXAMPLE\naws_secret_access_key = wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY';
}
});
var config = new AWS.Config();
expect(config.region).to.equal('us-west-2');
});
it('uses the profile specified in AWS_PROFILE', function() {
process.env.AWS_SDK_LOAD_CONFIG = '1';
process.env.AWS_PROFILE = 'foo';
helpers.spyOn(AWS.util, 'readFileSync').andCallFake(function(path) {
if (path.match(/[\/\\]home[\/\\]user[\/\\].aws[\/\\]config/)) {
return '[default]\nregion = us-west-1\n\n[profile foo]\nregion = us-west-2';
} else {
return '[default]\nregion = eu-east-1';
}
});
var config = new AWS.Config();
expect(config.region).to.equal('us-west-2');
});
it('prefers AWS_REGION to the shared config file', function() {
process.env.AWS_REGION = 'us-east-1';
process.env.AWS_SDK_LOAD_CONFIG = '1';
var mock = '[default]\nregion = us-west-2';
helpers.spyOn(AWS.util, 'readFileSync').andReturn(mock);
var config = new AWS.Config();
expect(config.region).to.equal('us-east-1');
});
it('non-existent credentials file returns empty', function() {
process.env.AWS_SDK_LOAD_CONFIG = '1';
var mock = '[default]\nregion = us-west-2';
helpers.spyOn(AWS.util, 'readFileSync').andCallFake(function(path) {
if (path.match(/[\/\\]home[\/\\]user[\/\\].aws[\/\\]config/)) {
return mock;
} else {
throw new Error('File does not exist!');
}
});
var config = new AWS.Config();
expect(config.region).to.equal('us-west-2');
});
});
}
it('can be set to a string', function() {
expect(configure({
region: 'us-west-1'
}).region).to.equal('us-west-1');
});
});
describe('logger', function() {
var oldEnv = process.env;
beforeEach(function(done) {
process.env = {};
done();
});
afterEach(function() {
process.env = oldEnv;
});
it('defaults to null', function() {
expect(configure().logger).not.to.exist;
});
it('can be set to an object', function() {
var myLogger = {log: function() {}};
expect(configure({
logger: myLogger
}).logger).to.equal(myLogger);
});
if (AWS.util.isNode()) {
it('grabs AWSJS_DEBUG from the env', function() {
process.env.AWSJS_DEBUG = '1';
var config = new AWS.Config();
expect(config.logger).to.equal(console);
});
it('should prefer loggers supplied in code', function() {
process.env.AWSJS_DEBUG = '1';
var myLogger = {log: function() {}};
expect(configure({
logger: myLogger
}).logger).to.equal(myLogger);
});
}
});
describe('maxRetries', function() {
it('defaults to unefined', function() {
expect(configure().maxRetries).to.equal(void 0);
});
it('can be set to an integer', function() {
expect(configure({
maxRetries: 2
}).maxRetries).to.equal(2);
});
});
describe('retryDelayOptions', function() {
it('can set "base" to an integer', function() {
expect(configure({
retryDelayOptions: {
base: 30
}
}).retryDelayOptions).to.eql({
base: 30
});
});
});
describe('paramValidation', function() {
it('defaults to true', function() {
expect(configure().paramValidation).to.equal(true);
});
});
describe('computeChecksums', function() {
it('defaults to true', function() {
expect(configure().computeChecksums).to.equal(true);
});
});
describe('sslEnabled', function() {
it('defaults to true', function() {
expect(configure().sslEnabled).to.equal(true);
});
it('can be set to false', function() {
expect(configure({
sslEnabled: false
}).sslEnabled).to.equal(false);
});
});
describe('httpOptions', function() {
it('defaults to {timeout:120000}', function() {
expect(configure().httpOptions).to.eql({
timeout: 120000
});
});
});
describe('systemClockOffset', function() {
it('defaults to 0', function() {
expect(configure().systemClockOffset).to.equal(0);
});
});
describe('correctClockSkew', function() {
it('defaults to false', function() {
expect(configure().correctClockSkew).to.equal(false);
});
});
describe('customUserAgent', function() {
it('defaults to null', function() {
expect(configure().customUserAgent).to.equal(null);
});
});
describe('useAccelerateEndpoint', function() {
it('defaults to false', function() {
expect(configure().useAccelerateEndpoint).to.equal(false);
});
});
describe('s3DisableBodySigning', function() {
it('defaults to true', function() {
expect(configure().s3DisableBodySigning).to.equal(true);
});
});
describe('set', function() {
it('should set a default value for a key', function() {
var config = new AWS.Config();
config.set('maxRetries', void 0, 'DEFAULT');
expect(config.maxRetries).to.equal('DEFAULT');
});
it('should execute default value if it is a function', function() {
var mock = helpers.createSpy();
var config = new AWS.Config();
config.set('maxRetries', void 0, mock);
expect(mock.calls.length).not.to.equal(0);
});
it('should not expand default value function if value is present', function() {
var mock = helpers.createSpy();
var config = new AWS.Config();
config.set('maxRetries', 'VALUE', mock);
expect(mock.calls.length).to.equal(0);
});
});
describe('clear', function() {
it('should be able to clear all key values from a config object', function() {
var config = new AWS.Config({
credentials: {},
maxRetries: 300,
sslEnabled: 'foo'
});
expect(config.maxRetries).to.equal(300);
expect(config.sslEnabled).to.equal('foo');
expect(config.credentials).not.to.equal(void 0);
config.clear();
expect(config.maxRetries).to.equal(void 0);
expect(config.sslEnabled).to.equal(void 0);
expect(config.credentials).not.to.equal(void 0);
expect(config.credentialProvider).not.to.equal(void 0);
});
});
describe('update', function() {
it('should be able to update keyed values', function() {
var config = new AWS.Config();
expect(config.maxRetries).to.equal(void 0);
config.update({
maxRetries: 10
});
expect(config.maxRetries).to.equal(10);
});
it('should ignore non-keyed values', function() {
var config = new AWS.Config();
config.update({
foo: 10
});
expect(config.foo).to.equal(void 0);
});
describe('should allow', function() {
var allServices = require('../clients/all');
var results = [];
for (var className in allServices) {
if (!hasProp.call(allServices, className)) continue;
var ctor = allServices[className];
var serviceIdentifier = className.toLowerCase();
results.push((function(id) {
it(id + ' to be set', function() {
var config = new AWS.Config();
var params = {};
params[id] = {
endpoint: 'localhost'
};
config.update(params);
expect(config[id]).to.eql({
endpoint: 'localhost'
});
});
})(serviceIdentifier));
}
});
it('allows unknown keys if allowUnknownKeys is set', function() {
var config = new AWS.Config();
config.update({
foo: 10
}, true);
expect(config.foo).to.equal(10);
});
it('should be able to update literal credentials', function() {
var config = new AWS.Config();
config.update({
accessKeyId: 'akid',
secretAccessKey: 'secret',
sessionToken: 'session'
});
expect(config.credentials.accessKeyId).to.equal('akid');
expect(config.credentials.secretAccessKey).to.equal('secret');
expect(config.credentials.sessionToken).to.equal('session');
});
it('should deep merge httpOptions', function() {
var config = new AWS.Config();
config.update({
httpOptions: {
timeout: 1
}
});
config.update({
httpOptions: {
xhrSync: true
}
});
expect(config.httpOptions.timeout).to.equal(1);
expect(config.httpOptions.xhrSync).to.equal(true);
});
});
return describe('getCredentials', function() {
var spy = null;
var config = null;
beforeEach(function(done) {
spy = helpers.createSpy('getCredentials callback');
done();
});
var expectValid = function(options, key) {
if (options instanceof AWS.Config) {
config = options;
} else {
config = new AWS.Config(options);
}
config.getCredentials(spy);
expect(spy.calls.length).not.to.equal(0);
expect(spy.calls[0]['arguments'][0]).not.to.exist;
if (key) {
expect(config.credentials.accessKeyId).to.equal(key);
}
};
var expectError = function(options, message) {
if (options instanceof AWS.Config) {
config = options;
} else {
config = new AWS.Config(options);
}
config.getCredentials(spy);
expect(spy.calls.length).not.to.equal(0);
expect(spy.calls[0]['arguments'][0].code).to.equal('CredentialsError');
expect(spy.calls[0]['arguments'][0].name).to.equal('CredentialsError');
expect(spy.calls[0]['arguments'][0].message).to.equal(message);
};
it('should check credentials for static object first', function() {
expectValid({
credentials: {
accessKeyId: '123',
secretAccessKey: '456'
}
});
});
it('should error if static credentials are not available', function() {
expectError({
credentials: {}
}, 'Missing credentials');
});
it('should check credentials for async get() method', function() {
expectValid({
credentials: {
get: function(cb) {
return cb();
}
}
});
});
it('should error if credentials.get() cannot resolve', function() {
var error = new Error('Error!');
error.code = 'FooError';
error.name = 'BarError';
var options = {
credentials: {
constructor: {
name: 'CustomCredentials'
},
get: function(cb) {
return cb(error, null);
}
}
};
expectError(options, 'Could not load credentials from CustomCredentials');
});
it('should check credentialProvider if no credentials', function() {
expectValid({
credentials: null,
credentialProvider: {
resolve: function(cb) {
return cb(null, {
accessKeyId: 'key',
secretAccessKey: 'secret'
});
}
}
});
});
it('should error if credentialProvider fails to resolve', function() {
var error = new Error('Error!');
error.code = 'FooError';
error.name = 'BarError';
var options = {
credentials: null,
credentialProvider: {
resolve: function(cb) {
return cb(error, null);
}
}
};
expectError(options, 'Could not load credentials from any providers');
});
it('should error if no credentials or credentialProvider', function() {
var options = {
credentials: null,
credentialProvider: null
};
expectError(options, 'No credentials to load');
});
});
});
describe('AWS.config', function() {
it('should be a default Config object', function() {
expect(AWS.config.sslEnabled).to.equal(true);
expect(AWS.config.maxRetries).to.equal(void 0);
});
it('can set default config to an object literal', function() {
var oldConfig = AWS.config;
AWS.config = {};
expect(AWS.config).to.eql({});
AWS.config = oldConfig;
});
describe('setPromisesDependency', function() {
it('updates promise support on requests', function() {
var utilSpy = helpers.spyOn(AWS.util, 'addPromises');
AWS.config.setPromisesDependency(function() {});
expect(utilSpy.calls.length).to.equal(1);
expect(Array.isArray(utilSpy.calls[0]['arguments'][0])).to.be['true'];
expect(utilSpy.calls[0]['arguments'][0].length).to.equal(5);
});
if (typeof Promise !== 'undefined') {
it('reverts to native promises when null is passed', function() {
var P = function() {};
var utilSpy = helpers.spyOn(AWS.util, 'addPromises');
AWS.config.setPromisesDependency(P);
expect(utilSpy.calls[0]['arguments'][1] === P).to.be['true'];
AWS.config.setPromisesDependency(null);
expect(utilSpy.calls[1]['arguments'][1] === Promise).to.be['true'];
expect(utilSpy.calls[1]['arguments'][1] === P).to.be['false'];
});
}
});
describe('getPromisesDependency', function() {
it('returns PromisesDependency if set', function() {
AWS.config.setPromisesDependency();
expect(AWS.config.getPromisesDependency()).to.be.undefined;
var P = function() {};
AWS.config.setPromisesDependency(P);
var dep = AWS.config.getPromisesDependency();
expect(dep).to.equal(P);
});
});
});