-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
reference_e2e.dart
447 lines (389 loc) 路 14 KB
/
reference_e2e.dart
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
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/foundation.dart';
import 'package:drive/drive.dart';
import './test_utils.dart';
void setupReferenceTests() {
group('$Reference', () {
late FirebaseStorage storage;
setUpAll(() async {
storage = FirebaseStorage.instance;
});
group('bucket', () {
test('returns the storage bucket as a string', () async {
expect(
storage.ref('/ok.jpeg').bucket,
storage.app.options.storageBucket,
);
});
});
group('fullPath', () {
test('returns the full path as a string', () async {
expect(
storage.ref('/foo/uploadNope.jpeg').fullPath,
'foo/uploadNope.jpeg',
);
expect(
storage.ref('foo/uploadNope.jpeg').fullPath,
'foo/uploadNope.jpeg',
);
});
});
group('name', () {
test('returns the file name as a string', () async {
Reference ref = storage.ref('/foo/uploadNope.jpeg');
expect(ref.name, 'uploadNope.jpeg');
});
});
group('parent', () {
test('returns the parent directory as a reference', () async {
expect(storage.ref('/foo/uploadNope.jpeg').parent?.fullPath, 'foo');
});
test('returns null if already at root', () async {
Reference ref = storage.ref('/');
expect(ref.parent, isNull);
});
});
group('root', () {
test('returns a reference to the root of the bucket', () async {
expect(storage.ref('/foo/uploadNope.jpeg').root.fullPath, '/');
});
});
group('child()', () {
test('returns a reference to a child path', () async {
Reference parentRef = storage.ref('/foo');
Reference childRef = parentRef.child('someFile.json');
expect(childRef.fullPath, 'foo/someFile.json');
});
});
group('delete()', () {
test('should delete a file', () async {
Reference ref = storage.ref('flutter-tests/deleteMe.jpeg');
await ref.putString('To Be Deleted :)');
await ref.delete();
await expectLater(
() => ref.delete(),
throwsA(
isA<FirebaseException>()
.having((e) => e.code, 'code', 'object-not-found')
.having(
(e) => e.message,
'message',
'No object exists at the desired reference.',
),
),
);
});
test('throws error if file does not exist', () async {
Reference ref = storage.ref('flutter-tests/iDoNotExist.jpeg');
await expectLater(
() => ref.delete(),
throwsA(
isA<FirebaseException>()
.having((e) => e.code, 'code', 'object-not-found')
.having(
(e) => e.message,
'message',
'No object exists at the desired reference.',
),
),
);
});
test('throws error if no write permission', () async {
Reference ref = storage.ref('/uploadNope.jpeg');
await expectLater(
() => ref.delete(),
throwsA(
isA<FirebaseException>()
.having((e) => e.code, 'code', 'unauthorized')
.having(
(e) => e.message,
'message',
'User is not authorized to perform the desired action.',
),
),
);
});
});
group('getDownloadURL', () {
test('gets a download url', () async {
Reference ref = storage.ref('flutter-tests/ok.txt');
await ref.putString('ok');
String downloadUrl = await ref.getDownloadURL();
expect(downloadUrl, isA<String>());
expect(downloadUrl, contains('ok.txt'));
expect(downloadUrl, contains(storage.app.options.projectId));
});
// TODO(ehesp): Emulator rules issue - comment back in once fixed
test('errors if permission denied', () async {
Reference ref = storage.ref('writeOnly.txt');
await expectLater(
() => ref.getDownloadURL(),
throwsA(
isA<FirebaseException>()
.having((e) => e.code, 'code', 'unauthorized')
.having(
(e) => e.message,
'message',
'User is not authorized to perform the desired action.',
),
),
);
});
test('throws error if file does not exist', () async {
Reference ref = storage.ref('flutter-tests/iDoNotExist.jpeg');
await expectLater(
() => ref.getDownloadURL(),
throwsA(
isA<FirebaseException>()
.having((e) => e.code, 'code', 'object-not-found')
.having(
(e) => e.message,
'message',
'No object exists at the desired reference.',
),
),
);
});
});
group('list', () {
test('returns list results', () async {
Reference ref = storage.ref('flutter-tests/list');
ListResult result = await ref.list(const ListOptions(maxResults: 25));
expect(result.items.length, greaterThan(0));
expect(result.prefixes, isA<List<Reference>>());
expect(result.prefixes.length, greaterThan(0));
});
test('errors if maxResults is less than 0 ', () async {
Reference ref = storage.ref('/list');
expect(
() => ref.list(const ListOptions(maxResults: -1)),
throwsAssertionError,
);
});
test('errors if maxResults is 0 ', () async {
Reference ref = storage.ref('/list');
expect(
() => ref.list(const ListOptions(maxResults: 0)),
throwsAssertionError,
);
});
test('errors if maxResults is more than 1000 ', () async {
Reference ref = storage.ref('/list');
expect(
() => ref.list(const ListOptions(maxResults: 1001)),
throwsAssertionError,
);
});
});
test('listAll', () async {
Reference ref = storage.ref('flutter-tests/list');
ListResult result = await ref.listAll();
expect(result.items, isNotNull);
expect(result.items.length, greaterThan(0));
expect(result.nextPageToken, isNull);
expect(result.prefixes, isA<List<Reference>>());
expect(result.prefixes.length, greaterThan(0));
});
group(
'putData',
() {
test('uploads a file with buffer', () async {
List<int> list = utf8.encode(kTestString);
Uint8List data = Uint8List.fromList(list);
final Reference ref =
storage.ref('flutter-tests').child('flt-ok.txt');
final TaskSnapshot complete = await ref.putData(
data,
SettableMetadata(
contentLanguage: 'en',
),
);
expect(complete.metadata?.size, kTestString.length);
// Metadata isn't saved on objects when using the emulator which fails test
// expect(complete.metadata?.contentLanguage, 'en');
});
//TODO(pr-mais): causes the emulator to crash
// test('errors if permission denied', () async {
// List<int> list = utf8.encode('hello world');
// Uint8List data = Uint8List.fromList(list);
// final Reference ref = storage.ref('/uploadNope.jpeg');
// await expectLater(
// () => ref.putData(data),
// throwsA(isA<FirebaseException>()
// .having((e) => e.code, 'code', 'unauthorized')
// .having((e) => e.message, 'message',
// 'User is not authorized to perform the desired action.')));
// });
},
skip: kIsWeb,
);
group('putBlob', () {
test(
'throws [UnimplementedError] for native platforms',
() async {
final File file = await createFile('flt-ok.txt');
final Reference ref =
storage.ref('flutter-tests').child('flt-ok.txt');
await expectLater(
() => ref.putBlob(
file,
SettableMetadata(
contentLanguage: 'en',
customMetadata: <String, String>{'activity': 'test'},
),
),
throwsA(
isA<UnimplementedError>().having(
(e) => e.message,
'message',
'putBlob() is not supported on native platforms. Use [put], [putFile] or [putString] instead.',
),
),
);
// This *must* be skipped in web, the test is intended for native platforms.
},
skip: kIsWeb,
);
});
group(
'putFile',
() {
test('uploads a file', () async {
final File file = await createFile('flt-ok.txt');
final Reference ref =
storage.ref('flutter-tests').child('flt-ok.txt');
final TaskSnapshot complete = await ref.putFile(
file,
SettableMetadata(
contentLanguage: 'en',
customMetadata: <String, String>{'activity': 'test'},
),
);
expect(complete.metadata?.size, kTestString.length);
// Metadata isn't saved on objects when using the emulator which fails test
// expect(complete.metadata?.contentLanguage, 'en');
// expect(complete.metadata?.customMetadata!['activity'], 'test');
});
// TODO(ehesp): Emulator rules issue - comment back in once fixed
// test('errors if permission denied', () async {
// File file = await createFile('flt-ok.txt');
// final Reference ref = storage.ref('uploadNope.jpeg');
// await expectLater(
// () => ref.putFile(file),
// throwsA(isA<FirebaseException>()
// .having((e) => e.code, 'code', 'unauthorized')
// .having((e) => e.message, 'message',
// 'User is not authorized to perform the desired action.')));
// });
// putFile is not supported in web.
},
skip: kIsWeb,
);
group('putString', () {
test('uploads a string', () async {
final Reference ref = storage.ref('flutter-tests').child('flt-ok.txt');
final TaskSnapshot complete = await ref.putString('data');
expect(complete.totalBytes, greaterThan(0));
});
// Emulator continues to make request rather than throw unauthorized exception as expected
// test('errors if permission denied', () async {
// final Reference ref = storage.ref('uploadNope.jpeg');
//
// await expectLater(
// () => ref.putString('data'),
// throwsA(
// isA<FirebaseException>()
// .having((e) => e.code, 'code', 'unauthorized')
// .having(
// (e) => e.message,
// 'message',
// 'User is not authorized to perform the desired action.',
// ),
// ),
// );
// });
});
group('updateMetadata', () {
test('updates metadata', () async {
Reference ref = storage.ref('flutter-tests').child('flt-ok.txt');
FullMetadata fullMetadata = await ref
.updateMetadata(SettableMetadata(customMetadata: {'foo': 'bar'}));
expect(fullMetadata.customMetadata!['foo'], 'bar');
});
test('errors if property does not exist', () async {
Reference ref = storage.ref('flutter-tests/iDoNotExist.jpeg');
await expectLater(
() => ref.updateMetadata(SettableMetadata(contentType: 'unknown')),
throwsA(
isA<FirebaseException>()
.having((e) => e.code, 'code', 'object-not-found')
.having(
(e) => e.message,
'message',
'No object exists at the desired reference.',
),
),
);
});
test(
'errors if permission denied',
() async {
final ref = storage.ref('uploadNope.jpeg');
await expectLater(
() => ref.updateMetadata(SettableMetadata(contentType: 'jpeg')),
throwsA(
isA<FirebaseException>()
.having((e) => e.code, 'code', 'unauthorized')
.having(
(e) => e.message,
'message',
'User is not authorized to perform the desired action.',
),
),
);
},
// TODO(salakar): Firebase storage emulator incorrectly returns `object-not-found` instead of `unauthorized`.
skip: true,
);
});
group(
'writeToFile',
() {
test('downloads a file', () async {
File file = await createFile('ok.jpeg');
TaskSnapshot complete =
await storage.ref('flutter-tests/ok.txt').writeToFile(file);
expect(complete.bytesTransferred, complete.totalBytes);
expect(complete.state, TaskState.success);
});
test('errors if permission denied', () async {
File file = await createFile('not.jpeg');
final Reference ref = storage.ref('/nope.jpeg');
await expectLater(
() => ref.writeToFile(file),
throwsA(
isA<FirebaseException>()
.having((e) => e.code, 'code', 'unauthorized')
.having(
(e) => e.message,
'message',
'User is not authorized to perform the desired action.',
),
),
);
});
// writeToFile is not supported in web
},
skip: kIsWeb,
);
test('toString', () async {
expect(
storage.ref('/uploadNope.jpeg').toString(),
equals('Reference(app: [DEFAULT], fullPath: uploadNope.jpeg)'),
);
});
});
}