-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathindex-test.js
More file actions
361 lines (305 loc) · 10.5 KB
/
index-test.js
File metadata and controls
361 lines (305 loc) · 10.5 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
/*eslint-env node*/
'use strict';
var chai = require('chai');
var chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);
var assert = chai.assert;
var RSVP = require('rsvp');
describe('s3 plugin', function() {
var subject;
var mockUi;
var context;
before(function() {
subject = require('../index');
});
beforeEach(function() {
mockUi = {
verbose: true,
messages: [],
write: function() {},
writeLine: function(message) {
this.messages.push(message);
}
};
context = {
distDir: process.cwd() + '/tests/fixtures/dist',
distFiles: ['app.css', 'app.js'],
ui: mockUi,
uploadClient: {
upload: function(/* options */) {
return RSVP.resolve(['app.css', 'app.js']);
}
},
config: {
s3: {
accessKeyId: 'aaaa',
secretAccessKey: 'bbbb',
sessionToken: 'eeee',
bucket: 'cccc',
region: 'dddd',
profile: 'ffff',
filePattern: '*.{css,js}',
acl: 'authenticated-read',
prefix: '',
proxy: 'http://user:password@internal.proxy.com',
distDir: function(context) {
return context.distDir;
},
distFiles: function(context) {
return context.distFiles || [];
},
gzippedFiles: function(context) {
return context.gzippedFiles || []; // e.g. from ember-cli-deploy-gzip
},
brotliCompressedFiles: function(context) {
return context.brotliCompressedFiles || []; // e.g. from ember-cli-deploy-gzip
},
manifestPath: function(context) {
return context.manifestPath; // e.g. from ember-cli-deploy-manifest
},
uploadClient: function(context) {
return context.uploadClient; // if you want to provide your own upload client to be used instead of one from this addon
},
s3Client: function(context) {
return context.s3Client; // if you want to provide your own s3 client to be used instead of one from aws-sdk
}
}
}
};
});
it('has a name', function() {
var plugin = subject.createDeployPlugin({
name: 's3'
});
assert.equal(plugin.name, 's3');
});
it('implements the correct hooks', function() {
var plugin = subject.createDeployPlugin({
name: 's3'
});
assert.typeOf(plugin.configure, 'function');
assert.typeOf(plugin.upload, 'function');
});
describe('configure hook', function() {
it('does not throw if config is ok', function() {
var plugin = subject.createDeployPlugin({
name: 's3'
});
plugin.beforeHook(context);
plugin.configure(context);
assert.ok(true); // it didn't throw
});
it('throws if config is not valid', function() {
var plugin = subject.createDeployPlugin({
name: 's3'
});
context.config.s3 = {};
plugin.beforeHook(context);
assert.throws(function(){
plugin.configure(context);
});
});
it('warns about missing optional config', function() {
delete context.config.s3.filePattern;
delete context.config.s3.prefix;
var plugin = subject.createDeployPlugin({
name: 's3'
});
plugin.beforeHook(context);
plugin.configure(context);
var messages = mockUi.messages.reduce(function(previous, current) {
if (/- Missing config:\s.*, using default:\s/.test(current)) {
previous.push(current);
}
return previous;
}, []);
assert.equal(messages.length, 9);
});
describe('required config', function() {
it('warns about missing bucket', function() {
delete context.config.s3.bucket;
var plugin = subject.createDeployPlugin({
name: 's3'
});
plugin.beforeHook(context);
assert.throws(function(/* error */){
plugin.configure(context);
});
var messages = mockUi.messages.reduce(function(previous, current) {
if (/- Missing required config: `bucket`/.test(current)) {
previous.push(current);
}
return previous;
}, []);
assert.equal(messages.length, 1);
});
it('warns about missing region', function() {
delete context.config.s3.region;
var plugin = subject.createDeployPlugin({
name: 's3'
});
plugin.beforeHook(context);
assert.throws(function(/* error */){
plugin.configure(context);
});
var messages = mockUi.messages.reduce(function(previous, current) {
if (/- Missing required config: `region`/.test(current)) {
previous.push(current);
}
return previous;
}, []);
assert.equal(messages.length, 1);
});
});
it('adds default config to the config object', function() {
delete context.config.s3.filePattern;
delete context.config.s3.prefix;
delete context.config.s3.cacheControl;
delete context.config.s3.expires;
assert.isUndefined(context.config.s3.filePattern);
assert.isUndefined(context.config.s3.prefix);
assert.isUndefined(context.config.s3.cacheControl);
assert.isUndefined(context.config.s3.expires);
var plugin = subject.createDeployPlugin({
name: 's3'
});
plugin.beforeHook(context);
plugin.configure(context);
assert.equal(context.config.s3.filePattern, '**/*.{js,css,png,gif,ico,jpg,webp,map,xml,txt,svg,swf,eot,ttf,woff,woff2,otf,wasm,json}');
assert.equal(context.config.s3.prefix, '');
assert.equal(context.config.s3.cacheControl, 'max-age=63072000, public, immutable');
assert.equal(new Date(context.config.s3.expires).getTime(), new Date('Tue, 01 Jan 2030 00:00:00 GMT').getTime());
});
});
describe('#upload hook', function() {
it('prints the begin message', function() {
var plugin = subject.createDeployPlugin({
name: 's3'
});
plugin.beforeHook(context);
return assert.isFulfilled(plugin.upload(context))
.then(function() {
assert.equal(mockUi.messages.length, 2);
assert.match(mockUi.messages[0], /preparing to upload to S3 bucket `cccc`/);
});
});
it('prints success message when files successully uploaded', function() {
var plugin = subject.createDeployPlugin({
name: 's3'
});
plugin.beforeHook(context);
return assert.isFulfilled(plugin.upload(context))
.then(function() {
assert.equal(mockUi.messages.length, 2);
var messages = mockUi.messages.reduce(function(previous, current) {
if (/- uploaded 2 files ok/.test(current)) {
previous.push(current);
}
return previous;
}, []);
assert.equal(messages.length, 1);
});
});
it('filters out ignored files via fileIgnorePattern', function() {
var plugin = subject.createDeployPlugin({
name: 's3'
});
context.config.s3.fileIgnorePattern = '*.css';
context.gzippedFiles = ['app.css', 'app.js'];
context.brotliCompressedFiles = ['app.css', 'app.js'];
context.config.s3.fileIgnorePattern = '*.css';
context.uploadClient.upload = function(options) {
assert.equal(options.filePaths.length, 1, 'file paths are filtered');
assert.equal(options.gzippedFilePaths.length, 1, 'gzipped file paths are filtered');
assert.equal(options.brotliCompressedFilePaths.length, 1, 'brotli compressed file paths are filtered');
return RSVP.resolve(options.filePaths);
};
plugin.beforeHook(context);
return assert.isFulfilled(plugin.upload(context))
.then(function() {
assert.equal(mockUi.messages.length, 2);
var messages = mockUi.messages.reduce(function(previous, current) {
if (/- uploaded 1 files ok/.test(current)) {
previous.push(current);
}
return previous;
}, []);
assert.equal(messages.length, 1);
});
});
it('prints an error message if the upload errors', function() {
var plugin = subject.createDeployPlugin({
name: 's3'
});
context.uploadClient = {
upload: function(/* opts */) {
return RSVP.reject(new Error('something bad went wrong'));
}
};
plugin.beforeHook(context);
return assert.isRejected(plugin.upload(context))
.then(function() {
assert.equal(mockUi.messages.length, 3);
assert.match(mockUi.messages[1], /- Error: something bad went wrong/);
});
});
it('calls proxy agent if a proxy is specified', function() {
var plugin = subject.createDeployPlugin({
name: 's3'
});
var assertionCount = 0;
context.proxyAgent = function(/* proxy */) {
assertionCount++;
};
plugin.beforeHook(context);
plugin.configure(context);
assert.isFulfilled(plugin.upload(context)).then(function(){
assert.equal(assertionCount, 1);
});
});
it('sets the appropriate header if the file is included in gzippedFiles list', function(done) {
var plugin = subject.createDeployPlugin({
name: 's3'
});
context.gzippedFiles = ['app.css'];
var assertionCount = 0;
context.uploadClient = null;
context.s3Client = {
putObject: function(params, cb) {
if (params.Key === 'app.css') {
assert.equal(params.ContentEncoding, 'gzip');
assertionCount++;
} else {
assert.isUndefined(params.ContentEncoding);
assertionCount++;
}
cb();
},
getObject: function(params, cb){
cb(new Error("File not found"));
}
};
plugin.beforeHook(context);
assert.isFulfilled(plugin.upload(context)).then(function(){
assert.equal(assertionCount, 2);
done();
}).catch(function(reason){
done(reason);
});
});
it('does not add an ACL', function() {
context.config.s3.acl = false
var plugin = subject.createDeployPlugin({
name: 's3'
});
context.uploadClient.upload = function(options) {
return RSVP.resolve(options);
};
plugin.beforeHook(context);
return assert.isFulfilled(plugin.upload(context))
.then(function(options) {
assert.equal(options.acl, undefined, 'acl is not present');
});
});
});
});