forked from apostrophecms/uploadfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
514 lines (477 loc) · 16 KB
/
test.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
/* jshint node:true */
var uploadfs = require('./uploadfs.js')();
var fs = require('fs');
var request = require('request');
var _ = require('lodash');
var async = require('async');
var localOptions = { storage: 'local', uploadsPath: __dirname + '/test', uploadsUrl: 'http://localhost:3000/test' };
// Supply your own. See s3TestOptions-sample.js
var s3Options = require(__dirname + '/s3TestOptions.js');
var imageSizes = [
{
name: 'small',
width: 320,
height: 320
},
{
name: 'medium',
width: 640,
height: 640
},
{
name: 'large',
width: 1140,
height: 1140
}
];
var tempPath = __dirname + '/temp';
localOptions.imageSizes = imageSizes;
localOptions.tempPath = tempPath;
s3Options.imageSizes = imageSizes;
s3Options.tempPath = tempPath;
localTestStart();
function localTestStart() {
options = localOptions;
console.log('Initializing uploadfs for the ' + options.storage + ' backend');
uploadfs.init(options, function(e) {
if (e) {
console.log('uploadfs.init failed:');
console.log(e);
process.exit(1);
}
testCopyIn();
});
function testCopyIn() {
console.log('testing copyIn');
uploadfs.copyIn('test.txt', '/one/two/three/test.txt', function(e) {
if (e) {
console.log('testCopyIn failed:');
console.log(e);
process.exit(1);
}
var content = fs.readFileSync('test/one/two/three/test.txt', 'utf8');
var original = fs.readFileSync('test.txt', 'utf8');
if (content !== original) {
console.log('testCopyIn did not copy the file faithfully.');
process.exit(1);
}
testCopyOut();
});
}
function testCopyOut() {
console.log('testing copyOut');
uploadfs.copyOut('/one/two/three/test.txt', 'copy-out-test.txt', function(e) {
if (e) {
console.log('testCopyOut failed:');
console.log(e);
process.exit(1);
}
var content = fs.readFileSync('copy-out-test.txt', 'utf8');
var original = fs.readFileSync('test.txt', 'utf8');
if (content !== original) {
console.log('testCopyOut did not copy the file faithfully.');
process.exit(1);
}
// Don't confuse the next test
fs.unlinkSync('copy-out-test.txt');
testDisableAndEnable();
});
}
function testDisableAndEnable() {
console.log('testing disable and enable');
return async.series({
disable: function(callback) {
uploadfs.disable('/one/two/three/test.txt', function(err) {
if (err) {
console.log('uploadfs.disable failed:');
console.log(err);
process.exit(1);
}
return callback(null);
});
},
copyShouldFail: function(callback) {
uploadfs.copyOut('/one/two/three/test.txt', 'copy-out-test.txt', function(e) {
if (!e) {
console.log('uploadfs.disable allowed access when it should not have');
process.exit(1);
}
return callback(null);
});
},
enable: function(callback) {
uploadfs.enable('/one/two/three/test.txt', function(err) {
if (err) {
console.log('uploadfs.enable failed:');
console.log(err);
process.exit(1);
}
return callback(null);
});
},
copyShouldWork: function(callback) {
uploadfs.copyOut('/one/two/three/test.txt', 'copy-out-test.txt', function(e) {
if (e) {
console.log('uploadfs.enable did not restore access');
process.exit(1);
}
var content = fs.readFileSync('copy-out-test.txt', 'utf8');
var original = fs.readFileSync('test.txt', 'utf8');
if (content !== original) {
console.log('testCopyOut did not copy the file faithfully.');
process.exit(1);
}
return callback(null);
});
}
}, function(err) {
if (err) {
console.log('Unexpected error');
process.exit(1);
}
// Don't confuse the next test
fs.unlinkSync('copy-out-test.txt');
testRemove();
});
}
function testRemove() {
console.log('testing remove');
uploadfs.remove('/one/two/three/test.txt', function(e) {
if (e) {
console.log('testRemove failed:');
console.log(e);
process.exit(1);
}
if (fs.existsSync('test/one/two/three/test.txt')) {
console.log('testRemove did not remove the file.');
process.exit(1);
}
testRmdir();
});
}
function testRmdir() {
// This is not an issue with s3
if (options.backend === 's3') {
return testGetUrl();
}
console.log('testing the automatic empty folder cleanup mechanism');
console.log('Waiting for the automatic empty folder cleanup mechanism to finish.');
setTimeout(function() {
if (fs.existsSync('test/one')) {
console.log('testRmdir saw that test/one still existed.');
process.exit(1);
}
testGetUrl();
}, 5000);
}
function testGetUrl() {
console.log('testing getUrl');
var url = uploadfs.getUrl();
if (url + '/one/two/three/test.txt' !== 'http://localhost:3000/test/one/two/three/test.txt') {
console.log('testGetUrl did not return the expected URL.');
process.exit(1);
}
testCopyImageIn();
}
function testCopyImageIn() {
console.log('testing copyImageIn');
// Note copyImageIn adds an extension for us
uploadfs.copyImageIn('test.jpg', '/images/profiles/me', function(e, info) {
if (e) {
console.log('testCopyImageIn failed:');
console.log(e);
process.exit(1);
}
if (info.basePath !== '/images/profiles/me') {
console.log('info.basePath is incorrect');
process.exit(1);
}
console.log('Testing that returned image dimensions are reoriented');
if ((info.width !== 1936) || (info.height !== 2592)) {
console.log('Width and height missing or not reoriented for web use');
console.log(info);
process.exit(1);
}
if ((info.originalWidth !== 2592) || (info.originalHeight !== 1936)) {
console.log('Original width and height missing or incorrect');
console.log(info);
process.exit(1);
}
var stats = fs.statSync('test/images/profiles/me.jpg');
if (!stats.size) {
console.log('Copied image is empty or missing');
process.exit(1);
}
// We already tested remove, just do it to mop up
console.log('Removing files...');
uploadfs.remove('/images/profiles/me.jpg', function(e) { });
_.each(imageSizes, function(size) {
var name = info.basePath + '.' + size.name + '.jpg';
var stats = fs.statSync('test' + name);
if (!stats.size) {
console.log('Scaled and copied image is empty or missing (2)');
process.exit(1);
}
// We already tested remove, just do it to mop up
uploadfs.remove(info.basePath + '.' + size.name + '.jpg', function(e) { });
});
testCopyImageInCrop();
});
}
function testCopyImageInCrop() {
console.log('testing copyImageIn with cropping');
// Note copyImageIn adds an extension for us
// Should grab the flowers
uploadfs.copyImageIn('test.jpg', '/images/profiles/me-cropped', { crop: { top: 830, left: 890, width: 500, height: 500 } }, function(e, info) {
if (e) {
console.log('testCopyImageIn failed:');
console.log(e);
process.exit(1);
}
if (info.basePath !== '/images/profiles/me-cropped') {
console.log('info.basePath is incorrect');
process.exit(1);
}
console.log('Testing that returned image dimensions are reoriented');
if ((info.width !== 500) || (info.height !== 500)) {
console.log('Reported size does not match crop');
console.log(info);
process.exit(1);
}
var stats = fs.statSync('test/images/profiles/me-cropped.jpg');
if (!stats.size) {
console.log('Copied image is empty or missing');
process.exit(1);
}
// We already tested remove, just do it to mop up
console.log('Removing files...');
uploadfs.remove('/images/profiles/me-cropped.jpg', function(e) { });
_.each(imageSizes, function(size) {
var name = info.basePath + '.' + size.name + '.jpg';
var stats = fs.statSync('test' + name);
if (!stats.size) {
console.log('Scaled and copied image is empty or missing (2)');
process.exit(1);
}
// We already tested remove, just do it to mop up
uploadfs.remove(info.basePath + '.' + size.name + '.jpg', function(e) { });
});
success();
});
}
function success() {
console.log('All tests passing.');
s3TestStart();
}
}
function s3TestStart() {
options = s3Options;
console.log('Initializing uploadfs for the ' + options.storage + ' backend');
uploadfs.init(options, function(e) {
if (e) {
console.log('uploadfs.init failed:');
console.log(e);
process.exit(1);
}
testCopyIn();
});
function testCopyIn() {
console.log('testing copyIn');
uploadfs.copyIn('test.txt', '/one/two/three/test.txt', function(e) {
if (e) {
console.log('testCopyIn failed:');
console.log(e);
process.exit(1);
}
var url = uploadfs.getUrl() + '/one/two/three/test.txt';
var original = fs.readFileSync('test.txt', 'utf8');
console.log('Waiting 5 seconds for AWS consistency after PUT');
console.log('(Note: only really required for us-standard region or an update of an existing file)');
setTimeout(function() {
request(url, function(err, response, body) {
if (err || (response.statusCode !== 200)) {
console.log("Did not get 200 status fetching " + url);
process.exit(1);
}
if (response.headers['content-type'] !== 'text/plain') {
console.log("Content type is not text/plain");
process.exit(1);
}
if (response.body !== original) {
console.log("Content not copied faithfully");
process.exit(1);
}
testCopyOut();
});
}, 5000);
});
}
function testCopyOut() {
console.log('testing copyOut');
uploadfs.copyOut('/one/two/three/test.txt', 'copy-out-test.txt', function(e) {
if (e) {
console.log('testCopyOut failed:');
console.log(e);
process.exit(1);
}
var content = fs.readFileSync('copy-out-test.txt', 'utf8');
var original = fs.readFileSync('test.txt', 'utf8');
if (content !== original) {
console.log('testCopyOut did not copy the file faithfully.');
process.exit(1);
}
// Don't confuse the next test
fs.unlinkSync('copy-out-test.txt');
testDisableAndEnable();
});
}
function testDisableAndEnable() {
console.log('s3 test of disable and enable');
return async.series({
disable: function(callback) {
return uploadfs.disable('/one/two/three/test.txt', function(err) {
if (err) {
console.log('uploadfs.disable failed:');
console.log(err);
process.exit(1);
}
return callback(null);
});
},
webShouldFail: function(callback) {
// With s3, copyOut always works, but web access will fail, which is
// all that uploadfs.disable actually promises.
var url = uploadfs.getUrl() + '/one/two/three/test.txt';
return request(url, function(err, response, body) {
if (response.statusCode >= 400) {
return callback(null);
}
console.log('uploadfs.disable: web request should have failed, succeeded with status code ' + response.statusCode);
process.exit(1);
});
},
enable: function(callback) {
uploadfs.enable('/one/two/three/test.txt', function(err) {
if (err) {
console.log('uploadfs.enable failed:');
console.log(err);
process.exit(1);
}
return callback(null);
});
},
webShouldWork: function(callback) {
var url = uploadfs.getUrl() + '/one/two/three/test.txt';
return request(url, function(err, response, body) {
if (response.statusCode >= 400) {
console.log('uploadfs.enable: web request failed with status code ' + response.statusCode);
process.exit(1);
}
var content = response.body;
console.log(content);
var original = fs.readFileSync('test.txt', 'utf8');
if (content !== original) {
console.log('After uploadfs.enable, web request did not copy the file faithfully.');
process.exit(1);
}
if (response.headers['content-type'] !== 'text/plain') {
console.log("Content type is not text/plain");
process.exit(1);
}
return callback(null);
});
}
}, function(err) {
if (err) {
console.log('Unexpected error');
process.exit(1);
}
testRemove();
});
}
function testRemove() {
console.log('testing remove');
uploadfs.remove('/one/two/three/test.txt', function(e) {
if (e) {
console.log('testRemove failed:');
console.log(e);
process.exit(1);
}
console.log('Waiting 5 seconds for AWS consistency after delete');
console.log('(note: this is not immediately consistent in any region)');
setTimeout(function() {
var url = uploadfs.getUrl() + '/one/two/three/test.txt';
var expectedUrl = 'http://' + options.bucket + '.s3.amazonaws.com/one/two/three/test.txt';
if (url !== expectedUrl) {
console.log('URL is not ' + expectedUrl);
process.exit(1);
} else {
console.log('URL is ' + expectedUrl);
}
request(url, function(err, response, body) {
// Amazon currently gives out a 403 rather than a 404 and
// they could conceivably change that, so just make sure
// it's an error code
if (err || (response.statusCode < 400)) {
console.log('testRemove did not remove the file.');
process.exit(1);
} else {
console.log('File removed.');
}
testCopyImageIn();
});
}, 5000);
});
}
function testCopyImageIn() {
console.log('testing copyImageIn');
// Let copyImageIn supply a file extension
uploadfs.copyImageIn('test.jpg', '/images/profiles/me', function(e, info) {
if (e) {
console.log('testCopyImageIn failed:');
console.log(e);
process.exit(1);
}
console.log('Waiting 5 seconds for AWS consistency');
console.log('(Only really necessary in the us-standard region)');
setTimeout(function() {
var path = '/images/profiles/me';
var url = uploadfs.getUrl();
var paths = [ info.basePath + '.jpg' ];
paths.push(info.basePath + '.small.jpg');
paths.push(info.basePath + '.medium.jpg');
paths.push(info.basePath + '.large.jpg');
async.map(paths, function(path, callback) {
request(url + path, function(err, response, body) {
if (err || (response.statusCode !== 200)) {
console.log('testCopyImageIn failed');
console.log(e);
console.log(response);
process.exit(1);
}
// Just mopping up so the next test isn't a false positive
uploadfs.remove(path, function(e) {
if (e) {
console.log('remove failed');
console.log(e);
process.exit(1);
}
callback(null);
});
});
}, function(e) {
if (e) {
console.log('testCopyImageIn failed');
console.log(e);
process.exit(1);
}
success();
});
}, 5000);
});
}
function success() {
console.log('All tests passing.');
process.exit(0);
}
}