-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
StorageReference.e2e.js
614 lines (547 loc) 路 22.2 KB
/
StorageReference.e2e.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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/*
* Copyright (c) 2016-present Invertase Limited & Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this library except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
const { PATH, seed, WRITE_ONLY_NAME } = require('./helpers');
describe('storage() -> StorageReference', function () {
describe('toString()', function () {
it('returns the correct bucket path to the file', function () {
const app = firebase.app();
firebase
.storage()
.ref('/uploadNope.jpeg')
.toString()
.should.equal(`gs://${app.options.storageBucket}/uploadNope.jpeg`);
});
});
describe('properties', function () {
describe('fullPath', function () {
it('returns the full path as a string', function () {
firebase.storage().ref('/foo/uploadNope.jpeg').fullPath.should.equal('foo/uploadNope.jpeg');
firebase.storage().ref('foo/uploadNope.jpeg').fullPath.should.equal('foo/uploadNope.jpeg');
});
});
describe('storage', function () {
it('returns the instance of storage', function () {
firebase.storage().ref('/foo/uploadNope.jpeg').storage.ref.should.be.a.Function();
});
});
describe('bucket', function () {
it('returns the storage bucket as a string', function () {
const app = firebase.app();
firebase
.storage()
.ref('/foo/uploadNope.jpeg')
.bucket.should.equal(app.options.storageBucket);
});
});
describe('name', function () {
it('returns the file name as a string', function () {
const ref = firebase.storage().ref('/foo/uploadNope.jpeg');
ref.name.should.equal('uploadNope.jpeg');
});
});
describe('parent', function () {
it('returns the parent directory as a reference', function () {
firebase.storage().ref('/foo/uploadNope.jpeg').parent.fullPath.should.equal('foo');
});
it('returns null if already at root', function () {
const ref = firebase.storage().ref('/');
should.equal(ref.parent, null);
});
});
describe('root', function () {
it('returns a reference to the root of the bucket', function () {
firebase.storage().ref('/foo/uploadNope.jpeg').root.fullPath.should.equal('/');
});
});
});
describe('child()', function () {
it('returns a reference to a child path', function () {
const parentRef = firebase.storage().ref('/foo');
const childRef = parentRef.child('someFile.json');
childRef.fullPath.should.equal('foo/someFile.json');
});
});
describe('delete()', function () {
it('should delete a file', async function () {
const storageReference = firebase.storage().ref(`${PATH}/deleteMe.txt`);
await storageReference.putString('Delete File');
await storageReference.delete();
try {
await storageReference.getMetadata();
return Promise.reject(new Error('Did not throw'));
} catch (error) {
error.code.should.equal('storage/object-not-found');
error.message.should.equal(
'[storage/object-not-found] No object exists at the desired reference.',
);
return Promise.resolve();
}
});
it('throws error if file does not exist', async function () {
const storageReference = firebase.storage().ref(`${PATH}/iDoNotExist.txt`);
try {
await storageReference.delete();
return Promise.reject(new Error('Did not throw'));
} catch (error) {
error.code.should.equal('storage/object-not-found');
error.message.should.equal(
'[storage/object-not-found] No object exists at the desired reference.',
);
return Promise.resolve();
}
});
it('throws error if no write permission', async function () {
const storageReference = firebase.storage().ref('/uploadNope.jpeg');
try {
await storageReference.delete();
return Promise.reject(new Error('Did not throw'));
} catch (error) {
error.code.should.equal('storage/unauthorized');
error.message.should.equal(
'[storage/unauthorized] User is not authorized to perform the desired action.',
);
return Promise.resolve();
}
});
});
describe('getDownloadURL', function () {
before(async function () {
await seed(PATH);
});
it('should return a download url for a file', async function () {
const storageReference = firebase.storage().ref(`${PATH}/list/file1.txt`);
const downloadUrl = await storageReference.getDownloadURL();
downloadUrl.should.be.a.String();
downloadUrl.should.containEql('file1.txt');
downloadUrl.should.containEql(firebase.app().options.projectId);
});
it('throws error if file does not exist', async function () {
const storageReference = firebase.storage().ref(`${PATH}/iDoNotExist.txt`);
try {
await storageReference.getDownloadURL();
return Promise.reject(new Error('Did not throw'));
} catch (error) {
error.code.should.equal('storage/object-not-found');
error.message.should.equal(
'[storage/object-not-found] No object exists at the desired reference.',
);
return Promise.resolve();
}
});
// Not throwing against the storage emulator on android?
ios.it('throws error if no read permission', async function () {
const storageReference = firebase.storage().ref(WRITE_ONLY_NAME);
try {
await storageReference.getDownloadURL();
return Promise.reject(new Error('Did not throw'));
} catch (error) {
error.code.should.equal('storage/unauthorized');
error.message.should.equal(
'[storage/unauthorized] User is not authorized to perform the desired action.',
);
return Promise.resolve();
}
});
});
describe('getMetadata', function () {
it('should return a metadata for a file', async function () {
const storageReference = firebase.storage().ref(`${PATH}/list/file1.txt`);
const metadata = await storageReference.getMetadata();
metadata.generation.should.be.a.String();
metadata.fullPath.should.equal(`${PATH}/list/file1.txt`);
if (device.getPlatform() === 'android') {
// FIXME - iOS on emulator this is fullPath not name ?
metadata.name.should.equal('file1.txt');
}
metadata.size.should.be.a.Number();
should.equal(metadata.size > 0, true);
metadata.updated.should.be.a.String();
metadata.timeCreated.should.be.a.String();
metadata.contentEncoding.should.be.a.String();
metadata.contentDisposition.should.be.a.String();
metadata.contentType.should.equal('text/plain');
metadata.bucket.should.equal(`${firebase.app().options.projectId}.appspot.com`);
metadata.metageneration.should.be.a.String();
metadata.md5Hash.should.be.a.String();
should.equal(metadata.cacheControl, null);
should.equal(metadata.contentLanguage, null);
should.equal(metadata.customMetadata, null);
});
});
describe('list', function () {
it('should return list results', async function () {
const storageReference = firebase.storage().ref(`${PATH}/list`);
const result = await storageReference.list();
result.constructor.name.should.eql('StorageListResult');
result.should.have.property('nextPageToken');
result.items.should.be.Array();
result.items.length.should.be.greaterThan(0);
result.items[0].constructor.name.should.eql('StorageReference');
result.prefixes.should.be.Array();
result.prefixes.length.should.be.greaterThan(0);
result.prefixes[0].constructor.name.should.eql('StorageReference');
});
it('throws if options is not an object', function () {
try {
const storageReference = firebase.storage().ref(`${PATH}/ok.jpeg`);
storageReference.list(123);
return Promise.reject(new Error('Did not throw'));
} catch (error) {
error.message.should.containEql("'options' expected an object value");
return Promise.resolve();
}
});
describe('maxResults', function () {
it('should limit with maxResults are passed', async function () {
const storageReference = firebase.storage().ref(`${PATH}/list`);
const result = await storageReference.list({
maxResults: 1,
});
result.nextPageToken.should.be.String();
result.items.should.be.Array();
result.items.length.should.eql(1);
result.items[0].constructor.name.should.eql('StorageReference');
result.prefixes.should.be.Array();
// todo length?
});
it('throws if maxResults is not a number', function () {
try {
const storageReference = firebase.storage().ref(`${PATH}/list`);
storageReference.list({
maxResults: '123',
});
return Promise.reject(new Error('Did not throw'));
} catch (error) {
error.message.should.containEql("'options.maxResults' expected a number value");
return Promise.resolve();
}
});
it('throws if maxResults is not a valid number', function () {
try {
const storageReference = firebase.storage().ref(`${PATH}/list`);
storageReference.list({
maxResults: 2000,
});
return Promise.reject(new Error('Did not throw'));
} catch (error) {
error.message.should.containEql(
"'options.maxResults' expected a number value between 1-1000",
);
return Promise.resolve();
}
});
});
describe('pageToken', function () {
it('throws if pageToken is not a string', function () {
try {
const storageReference = firebase.storage().ref(`${PATH}/list`);
storageReference.list({
pageToken: 123,
});
return Promise.reject(new Error('Did not throw'));
} catch (error) {
error.message.should.containEql("'options.pageToken' expected a string value");
return Promise.resolve();
}
});
it('should return and use a page token', async function () {
const storageReference = firebase.storage().ref(`${PATH}/list`);
const result1 = await storageReference.list({
maxResults: 1,
});
const item1 = result1.items[0].fullPath;
const result2 = await storageReference.list({
maxResults: 1,
pageToken: result1.nextPageToken,
});
const item2 = result2.items[0].fullPath;
if (item1 === item2) {
throw new Error('Expected item results to be different.');
}
});
});
});
describe('listAll', function () {
it('should return all results', async function () {
const storageReference = firebase.storage().ref(`${PATH}/list`);
const result = await storageReference.listAll();
should.equal(result.nextPageToken, null);
result.items.should.be.Array();
result.items.length.should.be.greaterThan(0);
result.items[0].constructor.name.should.eql('StorageReference');
result.prefixes.should.be.Array();
result.prefixes.length.should.be.greaterThan(0);
result.prefixes[0].constructor.name.should.eql('StorageReference');
});
it('should not crash if the user is not allowed to list the directory', async function () {
const storageReference = firebase.storage().ref('/forbidden');
try {
await storageReference.listAll();
return Promise.reject(new Error('listAll on a forbidden directory succeeded'));
} catch (error) {
error.code.should.equal('storage/unauthorized');
error.message.should.equal(
'[storage/unauthorized] User is not authorized to perform the desired action.',
);
return Promise.resolve();
}
});
});
describe('updateMetadata', function () {
it('should return the updated metadata for a file', async function () {
const storageReference = firebase.storage().ref(WRITE_ONLY_NAME);
const metadata = await storageReference.updateMetadata({
contentType: 'image/jpeg',
cacheControl: 'true',
contentDisposition: 'disposed',
contentEncoding: 'encoded',
contentLanguage: 'martian',
customMetadata: {
hello: 'world',
},
});
metadata.customMetadata.hello.should.equal('world');
metadata.generation.should.be.a.String();
metadata.fullPath.should.equal(WRITE_ONLY_NAME);
metadata.name.should.equal(WRITE_ONLY_NAME);
metadata.size.should.be.a.Number();
should.equal(metadata.size > 0, true);
metadata.updated.should.be.a.String();
metadata.timeCreated.should.be.a.String();
metadata.contentEncoding.should.be.a.String();
metadata.contentDisposition.should.be.a.String();
if (device.getPlatform() === 'android') {
// FIXME on iOS this is 'application/octet-stream'?
metadata.contentType.should.equal('image/jpeg');
}
metadata.bucket.should.equal(`${firebase.app().options.projectId}.appspot.com`);
metadata.metageneration.should.be.a.String();
metadata.md5Hash.should.be.a.String();
if (device.getPlatform() === 'android') {
// FIXME on iOS this comes back null ?
should.equal(metadata.cacheControl, 'true');
}
if (device.getPlatform() === 'android') {
// FIXME on iOS this comes back null ?
should.equal(metadata.contentLanguage, 'martian');
}
metadata.customMetadata.should.be.an.Object();
});
// FIXME not working against android on emulator? it returns the string 'null' for the cleared customMetadata value
ios.it('should set removed customMetadata properties to null', async function () {
const storageReference = firebase.storage().ref(WRITE_ONLY_NAME);
const metadata = await storageReference.updateMetadata({
contentType: 'text/plain',
customMetadata: {
removeMe: 'please',
},
});
metadata.customMetadata.removeMe.should.equal('please');
const metadataAfterRemove = await storageReference.updateMetadata({
contentType: 'text/plain',
customMetadata: {
removeMe: null,
},
});
// FIXME this is failing the part that fails
should.equal(metadataAfterRemove.customMetadata.removeMe, undefined);
});
it('should error if updateMetadata includes md5hash', async function () {
const storageReference = firebase.storage().ref(WRITE_ONLY_NAME);
try {
await storageReference.updateMetadata({
md5hash: '0xDEADBEEF',
});
return Promise.reject(new Error('Did not throw on invalid updateMetadata'));
} catch (e) {
e.message.should.containEql('md5hash may only be set on upload, not on updateMetadata');
return Promise.resolve();
}
});
});
describe('putFile', function () {
it('errors if file path is not a string', async function () {
const storageReference = firebase.storage().ref(`${PATH}/ok.jpeg`);
try {
storageReference.putFile(1337);
return Promise.reject(new Error('Did not error!'));
} catch (error) {
error.message.should.containEql('expects a string value');
return Promise.resolve();
}
});
it('errors if metadata is not an object', async function () {
const storageReference = firebase.storage().ref(`${PATH}/ok.jpeg`);
try {
storageReference.putFile('foo', 123);
return Promise.reject(new Error('Did not error!'));
} catch (error) {
error.message.should.containEql('must be an object value');
return Promise.resolve();
}
});
it('errors if metadata contains an unsupported property', async function () {
const storageReference = firebase.storage().ref(`${PATH}/ok.jpeg`);
try {
storageReference.putFile('foo', { foo: true });
return Promise.reject(new Error('Did not error!'));
} catch (error) {
error.message.should.containEql("unknown property 'foo'");
return Promise.resolve();
}
});
it('errors if metadata property value is not a string or null value', async function () {
const storageReference = firebase.storage().ref(`${PATH}/ok.jpeg`);
try {
storageReference.putFile('foo', { contentType: true });
return Promise.reject(new Error('Did not error!'));
} catch (error) {
error.message.should.containEql('should be a string or null value');
return Promise.resolve();
}
});
it('errors if metadata.customMetadata is not an object', async function () {
const storageReference = firebase.storage().ref(`${PATH}/ok.jpeg`);
try {
storageReference.putFile('foo', { customMetadata: true });
return Promise.reject(new Error('Did not error!'));
} catch (error) {
error.message.should.containEql(
'customMetadata must be an object of keys and string values',
);
return Promise.resolve();
}
});
// TODO check an metaData:md5hash property passes through correcty on putFile
});
describe('putString', function () {
it('errors if metadata is not an object', async function () {
const storageReference = firebase.storage().ref(`${PATH}/ok.jpeg`);
try {
storageReference.putString('foo', 'raw', 123);
return Promise.reject(new Error('Did not error!'));
} catch (error) {
error.message.should.containEql('must be an object value');
return Promise.resolve();
}
});
it('errors if metadata contains an unsupported property', async function () {
const storageReference = firebase.storage().ref(`${PATH}/ok.jpeg`);
try {
storageReference.putString('foo', 'raw', { foo: true });
return Promise.reject(new Error('Did not error!'));
} catch (error) {
error.message.should.containEql("unknown property 'foo'");
return Promise.resolve();
}
});
it('errors if metadata property value is not a string or null value', async function () {
const storageReference = firebase.storage().ref(`${PATH}/ok.jpeg`);
try {
storageReference.putString('foo', 'raw', { contentType: true });
return Promise.reject(new Error('Did not error!'));
} catch (error) {
error.message.should.containEql('should be a string or null value');
return Promise.resolve();
}
});
it('errors if metadata.customMetadata is not an object', async function () {
const storageReference = firebase.storage().ref(`${PATH}/ok.jpeg`);
try {
storageReference.putString('foo', 'raw', { customMetadata: true });
return Promise.reject(new Error('Did not error!'));
} catch (error) {
error.message.should.containEql(
'customMetadata must be an object of keys and string values',
);
return Promise.resolve();
}
});
it('allows valid metadata properties for upload', async function () {
const storageReference = firebase.storage().ref(`${PATH}/metadataTest.txt`);
await storageReference.putString('foo', 'raw', {
contentType: 'text/plain',
md5hash: '123412341234',
cacheControl: 'true',
contentDisposition: 'disposed',
contentEncoding: 'encoded',
contentLanguage: 'martian',
customMetadata: {
customMetadata1: 'metadata1value',
},
});
});
});
describe('put', function () {
it('errors if metadata is not an object', async function () {
const storageReference = firebase.storage().ref(`${PATH}/ok.jpeg`);
try {
storageReference.put(new jet.context.window.ArrayBuffer(), 123);
return Promise.reject(new Error('Did not error!'));
} catch (error) {
error.message.should.containEql('must be an object value');
return Promise.resolve();
}
});
it('errors if metadata contains an unsupported property', async function () {
const storageReference = firebase.storage().ref(`${PATH}/ok.jpeg`);
try {
storageReference.put(new jet.context.window.ArrayBuffer(), { foo: true });
return Promise.reject(new Error('Did not error!'));
} catch (error) {
error.message.should.containEql("unknown property 'foo'");
return Promise.resolve();
}
});
it('errors if metadata property value is not a string or null value', async function () {
const storageReference = firebase.storage().ref(`${PATH}/ok.jpeg`);
try {
storageReference.put(new jet.context.window.ArrayBuffer(), { contentType: true });
return Promise.reject(new Error('Did not error!'));
} catch (error) {
error.message.should.containEql('should be a string or null value');
return Promise.resolve();
}
});
it('errors if metadata.customMetadata is not an object', async function () {
const storageReference = firebase.storage().ref(`${PATH}/ok.jpeg`);
try {
storageReference.put(new jet.context.window.ArrayBuffer(), { customMetadata: true });
return Promise.reject(new Error('Did not error!'));
} catch (error) {
error.message.should.containEql(
'customMetadata must be an object of keys and string values',
);
return Promise.resolve();
}
});
it('allows valid metadata properties for upload', async function () {
const storageReference = firebase.storage().ref(`${PATH}/metadataTest.jpeg`);
await storageReference.put(new jet.context.window.ArrayBuffer(), {
contentType: 'image/jpg',
md5hash: '123412341234',
cacheControl: 'true',
contentDisposition: 'disposed',
contentEncoding: 'encoded',
contentLanguage: 'martian',
customMetadata: {
customMetadata1: 'metadata1value',
},
});
});
});
});