-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy paths3-test.js
More file actions
473 lines (396 loc) · 14.3 KB
/
s3-test.js
File metadata and controls
473 lines (396 loc) · 14.3 KB
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
/*eslint-env node*/
var os = require('os');
var chai = require('chai');
var chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);
var assert = chai.assert;
describe('s3', function() {
var S3, mockUi, s3Client, plugin, subject;
before(function() {
S3 = require('../../lib/s3');
});
beforeEach(function() {
s3Client = {
putObject: function(params, cb) {
cb();
},
getObject: function(params, cb) {
cb(new Error("File not found"));
}
};
mockUi = {
messages: [],
write: function() {},
writeLine: function(message) {
this.messages.push(message);
}
};
plugin = {
ui: mockUi,
readConfig: function(propertyName) {
if (propertyName === 's3Client') {
return s3Client;
}
},
log: function(message/*, opts */) {
this.ui.write('| ');
this.ui.writeLine('- ' + message);
}
};
subject = new S3({
plugin: plugin
});
});
describe('#upload', function() {
it('resolves if all uploads succeed', function() {
var options = {
filePaths: ['app.js', 'app.css'],
cwd: process.cwd() + '/tests/fixtures/dist',
prefix: 'js-app'
};
var promises = subject.upload(options);
return assert.isFulfilled(promises)
.then(function() {
assert.equal(mockUi.messages.length, 2);
var messages = mockUi.messages.reduce(function(previous, current) {
if (/- ✔ {2}js-app\/app\.[js|css]/.test(current)) {
previous.push(current);
}
return previous;
}, []);
assert.equal(messages.length, 2);
});
});
it('rejects if an upload fails', function() {
s3Client.putObject = function(params, cb) {
cb('error uploading');
};
var options = {
filePaths: ['app.js', 'app.css'],
cwd: process.cwd() + '/tests/fixtures/dist',
prefix: 'js-app'
};
var promises = subject.upload(options);
return assert.isRejected(promises)
.then(function() {
});
});
describe('sending the object to s3', function() {
it('sends the correct params', function() {
var s3Params;
s3Client.putObject = function(params, cb) {
s3Params = params;
cb();
};
var options = {
filePaths: ['app.css'],
cwd: process.cwd() + '/tests/fixtures/dist',
prefix: 'js-app',
acl: 'public-read',
bucket: 'some-bucket',
cacheControl: 'max-age=1234, public',
expires: '2010'
};
var promises = subject.upload(options);
return assert.isFulfilled(promises)
.then(function() {
assert.equal(s3Params.Bucket, 'some-bucket');
assert.equal(s3Params.ACL, 'public-read');
assert.equal(s3Params.Body.toString(), 'body: {}' + os.EOL);
assert.equal(s3Params.ContentType, 'text/css; charset=utf-8');
assert.equal(s3Params.Key, 'js-app/app.css');
assert.equal(s3Params.CacheControl, 'max-age=1234, public');
assert.equal(s3Params.Expires, '2010');
assert.isUndefined(s3Params.ContentEncoding);
assert.isUndefined(s3Params.ServerSideEncryption);
});
});
it('sets ServerSideEncryption using serverSideEncryption', function() {
var s3Params;
s3Client.putObject = function(params, cb) {
s3Params = params;
cb();
};
var options = {
filePaths: ['app.css'],
cwd: process.cwd() + '/tests/fixtures/dist',
prefix: 'js-app',
acl: 'public-read',
bucket: 'some-bucket',
cacheControl: 'max-age=1234, public',
expires: '2010',
serverSideEncryption: 'AES256'
};
var promise = subject.upload(options);
return assert.isFulfilled(promise)
.then(function() {
assert.equal(s3Params.ServerSideEncryption, 'AES256', 'ServerSideEncryption passed correctly');
});
});
it('sets Metadata using metadata', function() {
var s3Params;
s3Client.putObject = function(params, cb) {
s3Params = params;
cb();
};
var options = {
filePaths: ['app.css'],
cwd: process.cwd() + '/tests/fixtures/dist',
prefix: 'js-app',
metadata: { 'test-key': 'test-value' }
};
return assert.isFulfilled(subject.upload(options))
.then(function() {
assert.deepEqual(s3Params.Metadata, { 'test-key': 'test-value' });
});
});
it('sends the correct content type params for gzipped files with .gz extension', function() {
var s3Params;
s3Client.putObject = function(params, cb) {
s3Params = params;
cb();
};
var options = {
filePaths: ['app.css', 'app.css.gz'],
gzippedFilePaths: ['app.css.gz'],
cwd: process.cwd() + '/tests/fixtures/dist',
prefix: 'js-app',
acl: 'public-read',
bucket: 'some-bucket',
cacheControl: 'max-age=1234, public',
expires: '2010'
};
var promises = subject.upload(options);
return assert.isFulfilled(promises)
.then(function() {
assert.equal(s3Params.ContentType, 'text/css; charset=utf-8');
assert.equal(s3Params.Key, 'js-app/app.css.gz');
assert.equal(s3Params.ContentEncoding, 'gzip');
assert.equal(s3Params.CacheControl, 'max-age=1234, public');
assert.equal(s3Params.Expires, '2010');
});
});
it('sends the correct content type params for brotli-compressed files with .br extension', function () {
var s3Params;
s3Client.putObject = function (params, cb) {
s3Params = params;
cb();
};
var options = {
filePaths: ['app.css', 'app.css.br'],
brotliCompressedFilePaths: ['app.css.br'],
cwd: process.cwd() + '/tests/fixtures/dist',
prefix: 'js-app',
acl: 'public-read',
bucket: 'some-bucket',
cacheControl: 'max-age=1234, public',
expires: '2010'
};
var promises = subject.upload(options);
return assert.isFulfilled(promises)
.then(function () {
assert.equal(s3Params.ContentType, 'text/css; charset=utf-8');
assert.equal(s3Params.Key, 'js-app/app.css.br');
assert.equal(s3Params.ContentEncoding, 'br');
assert.equal(s3Params.CacheControl, 'max-age=1234, public');
assert.equal(s3Params.Expires, '2010');
});
});
it('sets the content type using defaultMimeType', function() {
var s3Params;
s3Client.putObject = function(params, cb) {
s3Params = params;
cb();
};
var options = {
filePaths: ['index'],
cwd: process.cwd() + '/tests/fixtures/dist',
defaultMimeType: 'text/html'
};
var promises = subject.upload(options);
return assert.isFulfilled(promises)
.then(function() {
assert.equal(s3Params.ContentType, 'text/html; charset=utf-8');
});
});
it('sets the content type to the default', function() {
var s3Params;
s3Client.putObject = function(params, cb) {
s3Params = params;
cb();
};
var options = {
filePaths: ['index'],
cwd: process.cwd() + '/tests/fixtures/dist'
};
var promises = subject.upload(options);
return assert.isFulfilled(promises)
.then(function() {
assert.equal(s3Params.ContentType, 'application/octet-stream');
});
});
it('returns a promise with an array of the files uploaded', function() {
s3Client.putObject = function(params, cb) {
cb();
};
var options = {
filePaths: ['app.js', 'app.css'],
cwd: process.cwd() + '/tests/fixtures/dist',
prefix: 'js-app'
};
var promises = subject.upload(options);
return assert.isFulfilled(promises)
.then(function(filesUploaded) {
assert.deepEqual(filesUploaded, ['app.js', 'app.css']);
});
});
});
describe('with a manifestPath specified', function () {
it('uploads all files when manifest is missing from server', function () {
var options = {
filePaths: ['app.js', 'app.css'],
cwd: process.cwd() + '/tests/fixtures/dist',
prefix: 'js-app',
manifestPath: 'manifest.txt'
};
var promise = subject.upload(options);
return assert.isFulfilled(promise)
.then(function(filesUploaded) {
assert.equal(mockUi.messages.length, 5);
assert.match(mockUi.messages[0], /- Downloading manifest for differential deploy.../);
assert.match(mockUi.messages[1], /- Manifest not found. Disabling differential deploy\./);
assert.match(mockUi.messages[2], /- ✔ {2}js-app\/app\.js/);
assert.match(mockUi.messages[3], /- ✔ {2}js-app\/app\.css/);
assert.match(mockUi.messages[4], /- ✔ {2}js-app\/manifest\.txt/);
assert.deepEqual(filesUploaded, ['app.js', 'app.css', 'manifest.txt']);
});
});
it('only uploads missing files when manifest is present on server', function () {
s3Client.getObject = function(params, cb) {
cb(undefined, {
Body: "app.js"
});
};
var options = {
filePaths: ['app.js', 'app.css'],
cwd: process.cwd() + '/tests/fixtures/dist',
prefix: 'js-app',
manifestPath: 'manifest.txt'
};
var promise = subject.upload(options);
return assert.isFulfilled(promise)
.then(function(filesUploaded) {
assert.equal(mockUi.messages.length, 4);
assert.match(mockUi.messages[0], /- Downloading manifest for differential deploy.../);
assert.match(mockUi.messages[1], /- Manifest found. Differential deploy will be applied\./);
assert.match(mockUi.messages[2], /- ✔ {2}js-app\/app\.css/);
assert.match(mockUi.messages[3], /- ✔ {2}js-app\/manifest\.txt/);
assert.deepEqual(filesUploaded, ['app.css', 'manifest.txt']);
});
});
it('does not upload manifest.txt when one of the files does not succeed uploading', function() {
s3Client.putObject = function(params, cb) {
if (params.Key === 'js-app/app.css') {
cb('error uploading');
} else {
cb();
}
};
var options = {
filePaths: ['app.js', 'app.css'],
cwd: process.cwd() + '/tests/fixtures/dist',
prefix: 'js-app',
manifestPath: 'manifest.txt'
};
var promise = subject.upload(options);
return assert.isRejected(promise)
.then(function() {
assert.equal(mockUi.messages.length, 3);
assert.match(mockUi.messages[0], /- Downloading manifest for differential deploy.../);
assert.match(mockUi.messages[1], /- Manifest not found. Disabling differential deploy\./);
assert.match(mockUi.messages[2], /- ✔ {2}js-app\/app\.js/);
});
});
});
describe('with an integer batchSize specified', function () {
it('uploads all files', function () {
var options = {
filePaths: ['app.js', 'app.css'],
cwd: process.cwd() + '/tests/fixtures/dist',
prefix: 'js-app',
batchSize: 10
};
var promises = subject.upload(options);
return assert.isFulfilled(promises)
.then(function() {
assert.equal(mockUi.messages.length, 2);
var messages = mockUi.messages.reduce(function(previous, current) {
if (/- ✔ {2}js-app\/app\.[js|css]/.test(current)) {
previous.push(current);
}
return previous;
}, []);
assert.equal(messages.length, 2);
});
});
it('returns a promise with an array of the files uploaded', function() {
s3Client.putObject = function(params, cb) {
cb();
};
var options = {
filePaths: ['app.js', 'app.css'],
cwd: process.cwd() + '/tests/fixtures/dist',
prefix: 'js-app',
batchSize: 10
};
var promises = subject.upload(options);
return assert.isFulfilled(promises)
.then(function(filesUploaded) {
assert.deepEqual(filesUploaded, ['app.js', 'app.css']);
});
});
it('uploads the correct number of batches', function () {
var requests = 0;
s3Client.putObject = function (params, cb) {
requests++;
cb();
};
var oldPutObjectsBatch = subject._putObjectsBatch.bind(subject);
var called = false;
//Spy on _putObjectsBatch, making sure that after it has executed a batch it has created 2 requests
subject._putObjectsBatch = function (paths, options) {
if (!called) {
called = true;
return oldPutObjectsBatch(paths, options);
}
assert.equal(requests, 2);
subject._putObjectsBatch = oldPutObjectsBatch;
return subject._putObjectsBatch(paths, options);
};
var options = {
filePaths: ['app.js', 'app.css', 'app.css.gz', 'manifest.txt'],
cwd: process.cwd() + '/tests/fixtures/dist',
prefix: 'js-app',
batchSize: 2
};
return subject.upload(options)
.then(function () {
assert.equal(requests, 4);
});
});
it('rejects if an upload fails', function () {
s3Client.putObject = function(params, cb) {
cb('error uploading');
};
var options = {
filePaths: ['app.js', 'app.css'],
cwd: process.cwd() + '/tests/fixtures/dist',
prefix: 'js-app',
batchSize: 10
};
var promises = subject.upload(options);
return assert.isRejected(promises);
});
});
});
});