forked from shrinerb/shrine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shrine_test.rb
537 lines (408 loc) · 15.2 KB
/
shrine_test.rb
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
require "test_helper"
describe Shrine do
before do
@uploader = uploader
@shrine = @uploader.class
end
describe ".inherited" do
it "duplicates options" do
uploader = Class.new(Shrine)
uploader.opts[:foo] = "foo"
subclass = Class.new(uploader)
assert_equal "foo", subclass.opts[:foo]
subclass.opts[:foo] = "bar"
assert_equal "bar", subclass.opts[:foo]
assert_equal "foo", uploader.opts[:foo]
end
it "duplicates collection values of options" do
uploader = Class.new(Shrine)
uploader.opts[:a] = ["a"]
uploader.opts[:b] = {"b" => "b"}
uploader.opts[:c] = ["c"].freeze
subclass = Class.new(uploader)
subclass.opts[:a] << "a"
assert_equal ["a", "a"], subclass.opts[:a]
assert_equal ["a"], uploader.opts[:a]
subclass.opts[:b].update("b" => nil)
assert_equal({"b" => nil}, subclass.opts[:b])
assert_equal({"b" => "b"}, uploader.opts[:b])
assert_equal ["c"], subclass.opts[:c]
end
it "duplicates storages" do
uploader = Class.new(Shrine)
uploader.storages[:foo] = "foo"
subclass = Class.new(uploader)
assert_equal "foo", subclass.storages[:foo]
subclass.storages[:foo] = "bar"
assert_equal "bar", subclass.storages[:foo]
assert_equal "foo", uploader.storages[:foo]
end
it "duplicates core classes" do
uploader = Class.new(Shrine)
refute_equal Shrine::UploadedFile, uploader::UploadedFile
assert_equal uploader, uploader::UploadedFile.shrine_class
refute_equal Shrine::Attachment, uploader::Attachment
assert_equal uploader, uploader::Attachment.shrine_class
refute_equal Shrine::Attacher, uploader::Attacher
assert_equal uploader, uploader::Attacher.shrine_class
end
end
describe ".Attachment" do
it "returns an instance of Attachment" do
uploader = Class.new(Shrine)
assert_instance_of uploader::Attachment, uploader::Attachment(:file)
assert_instance_of uploader::Attachment, uploader.attachment(:file)
assert_instance_of uploader::Attachment, uploader[:file]
end
end
describe ".upload" do
it "uploads the file" do
file = @shrine.upload(fakeio, :store)
assert_instance_of @shrine::UploadedFile, file
end
it "forwads additional options" do
file = @shrine.upload(fakeio, :store, location: "foo")
assert_equal "foo", file.id
end
end
describe ".uploaded_file" do
it "accepts data as stringified Hash" do
uploaded_file = @uploader.upload(fakeio)
retrieved = @shrine.uploaded_file(uploaded_file.data)
assert_equal uploaded_file, retrieved
end
it "accepts data as symbolized Hash" do
uploaded_file = @uploader.upload(fakeio)
data = uploaded_file.data.transform_keys(&:to_sym)
data[:metadata] = data[:metadata].transform_keys(&:to_sym)
retrieved = @shrine.uploaded_file(data)
assert_equal uploaded_file, retrieved
assert_equal uploaded_file.metadata, retrieved.metadata
end
it "accepts data as JSON" do
uploaded_file = @uploader.upload(fakeio)
retrieved = @shrine.uploaded_file(uploaded_file.to_json)
assert_equal uploaded_file, retrieved
end
it "accepts an UploadedFile" do
uploaded_file = @uploader.upload(fakeio)
retrieved = @shrine.uploaded_file(uploaded_file)
assert_equal uploaded_file, retrieved
end
it "raises an error on invalid input" do
assert_raises(ArgumentError) { @shrine.uploaded_file(:foo) }
end
end
describe ".find_storage" do
it "finds by symbol names" do
assert_equal @uploader.storage, @shrine.find_storage(:store)
@shrine.storages["store"] = @shrine.storages.delete(:store)
assert_equal @uploader.storage, @shrine.find_storage(:store)
end
it "finds by string names" do
assert_equal @uploader.storage, @shrine.find_storage("store")
@shrine.storages["store"] = @shrine.storages.delete(:store)
assert_equal @uploader.storage, @shrine.find_storage("store")
end
it "raises an error if storage wasn't found" do
assert_raises(Shrine::Error) { @shrine.find_storage(:foo) }
end
end
describe ".with_file" do
it "yields File objects unchanged" do
file = File.open(__FILE__)
@shrine.with_file(file) do |object|
assert_equal file, object
end
end
it "yields Tempfile objects unchanged" do
tempfile = Tempfile.new
@shrine.with_file(tempfile) do |object|
assert_equal tempfile, object
end
end
it "temporarily downloads Shrine::UploadedFile objects" do
uploaded_file = @uploader.upload(fakeio, location: "file.txt")
tempfile = @shrine.with_file(uploaded_file) do |tempfile|
assert_instance_of Tempfile, tempfile
assert_equal ".txt", File.extname(tempfile.path)
assert_equal uploaded_file.read, tempfile.read
tempfile
end
assert_nil tempfile.path
end
it "temporarily downloads IO-like objects" do
file = @shrine.with_file(fakeio("content")) do |file|
assert_instance_of File, file
assert_equal "content", file.read
file
end
refute File.exist?(file.path)
end
end
describe "#initialize" do
it "accepts symbol storage name" do
uploader = @shrine.new(:store)
assert_equal :store, uploader.storage_key
assert_equal @uploader.storage, uploader.storage
end
it "accepts string storage name" do
uploader = @shrine.new("store")
assert_equal :store, uploader.storage_key
assert_equal @uploader.storage, uploader.storage
end
it "fetches storage lazily" do
uploader = @shrine.new(:store)
@shrine.storages[:store] = Shrine::Storage::Memory.new
assert_equal @shrine.storages[:store], uploader.storage
end
it "raises an error on unknown storage" do
assert_raises(Shrine::Error) { @shrine.new(:foo) }
end
end
it "has #storage_key, #storage and #opts" do
assert_equal :store, @uploader.storage_key
assert_equal @shrine.storages[:store], @uploader.storage
assert_equal Hash.new, @uploader.opts
end
describe "#upload" do
it "uploads the file to storage" do
file = @uploader.upload(fakeio("file"))
assert file.exists?
assert_equal "file", file.read
end
it "raises Shrine::InvalidFile when input is not an IO-like object" do
assert_raises Shrine::InvalidFile do
@uploader.upload(:not_io)
end
end
it "stores basic metadata" do
io = fakeio("file", filename: "nature.jpg", content_type: "image/jpeg")
file = @uploader.upload(io)
assert_equal Hash[
"size" => 4,
"filename" => "nature.jpg",
"mime_type" => "image/jpeg",
], file.metadata
end
it "copies metadata from UploadedFile objects" do
uploaded_file = @uploader.upload(fakeio)
uploaded_file.metadata.clear
file = @uploader.upload(uploaded_file)
assert_equal Hash.new, file.metadata
refute_equal uploaded_file.metadata.object_id, file.metadata.object_id
end
it "accepts additional metadata" do
io = fakeio("file", filename: "nature.jpg", content_type: "image/jpeg")
file = @uploader.upload(io, metadata: { "filename" => "overridden.jpg", "foo" => "bar" })
assert_equal Hash[
"size" => 4,
"filename" => "overridden.jpg",
"mime_type" => "image/jpeg",
"foo" => "bar",
], file.metadata
end
it "skips metadata extraction on metadata: false" do
@uploader.expects(:extract_metadata).never
file = @uploader.upload(fakeio, metadata: false)
assert_equal Hash.new, file.metadata
end
it "forces metadata extraction on metadata: true" do
uploaded_file = @uploader.upload(fakeio)
uploaded_file.metadata.clear
file = @uploader.upload(uploaded_file, metadata: true)
assert_equal %w[filename size mime_type], file.metadata.keys
refute_equal uploaded_file.metadata.object_id, file.metadata.object_id
end
it "forwards options for metadata extraction" do
io = fakeio
@uploader.expects(:extract_metadata).with(io, foo: "bar").returns({})
@uploader.upload(io, foo: "bar")
end
it "uses result of #generate_location for upload location" do
@uploader.instance_eval do
def get_location(io, **)
"foo"
end
end
file = @uploader.upload(fakeio)
assert_equal "foo", file.id
end
it "forwards metadata for location generation" do
@uploader.instance_eval do
def generate_location(io, **options)
options[:metadata].to_s
end
end
file = @uploader.upload(fakeio)
assert_equal file.metadata.to_s, file.id
end
it "forwards options for location generation" do
@uploader.instance_eval do
def generate_location(io, **options)
options[:foo]
end
end
file = @uploader.upload(fakeio, foo: "bar")
assert_equal "bar", file.id
end
it "accepts :location" do
@uploader.expects(:generate_location).never
file = @uploader.upload(fakeio, location: "foo")
assert_equal "foo", file.id
end
it "raises exception when upload location was nil" do
@uploader.instance_eval do
def generate_location(io, **options)
nil
end
end
assert_raises Shrine::Error do
@uploader.upload(fakeio)
end
end
it "forwards metadata to the storage" do
@uploader.storage.expects(:upload).with do |*, o|
o[:shrine_metadata].keys.sort == %w[filename mime_type size]
end
@uploader.upload(fakeio)
end
it "forwards :upload_options to the storage" do
@uploader.storage.expects(:upload).with do |*, o|
o.delete(:shrine_metadata)
o == { foo: "bar" }
end
@uploader.upload(fakeio, upload_options: { foo: "bar" })
end
it "returns instance of correct UploadedFile" do
file = @uploader.upload(fakeio)
assert_instance_of @shrine::UploadedFile, file
end
it "closes the file after uploading" do
@uploader.upload(io = fakeio)
assert_raises(IOError) { io.read }
end
it "accepts :close option" do
@uploader.upload(io = fakeio, close: false)
assert_equal "", io.read
end
it "accepts :delete option" do
@uploader.upload(file = tempfile("file"))
assert File.exist?(file.path)
@uploader.upload(file = tempfile("file"), delete: true)
refute File.exist?(file.path)
@uploader.upload(file = tempfile("file").tap(&File.method(:unlink)), delete: true)
refute File.exist?(file.path)
@uploader.upload(file = File.open(tempfile("file").path), delete: true)
refute File.exist?(file.path)
@uploader.upload(fakeio, delete: true)
refute File.exist?(file.path)
end
it "doesn't error when storage already closed the file" do
@uploader.storage.instance_eval { def upload(io, *); super; io.close; end }
@uploader.upload(fakeio)
end
it "accepts IO objects of unknown size" do
io = StringIO.new
io.instance_eval { undef size }
uploaded_file = @uploader.upload(io)
assert_equal "", uploaded_file.read
io = StringIO.new
io.instance_eval { def size; nil; end }
uploaded_file = @uploader.upload(io)
assert_equal "", uploaded_file.read
end
it "accepts input which satisfies IO interface through #method_missing" do
delegator_class = Class.new do
def initialize(object)
@object = object
end
def method_missing(name, *args, &block)
@object.send(name, *args, &block)
end
def respond_to_missing?(name, include_private = false)
@object.respond_to?(name, include_private) || super
end
end
io = delegator_class.new(fakeio)
@uploader.upload(io)
end
end
describe "#generate_location" do
it "creates a unique location" do
file1 = @uploader.upload(fakeio)
file2 = @uploader.upload(fakeio)
refute file1.id == file2.id
end
it "preserves the extension" do
# Rails file
file = @uploader.upload(fakeio(filename: "avatar.jpg"))
assert_match /\.jpg$/, file.id
# Uploaded file
uploaded_file = @uploader.upload(fakeio, location: "avatar.jpg")
file = @uploader.upload(uploaded_file)
assert_match /\.jpg$/, file.id
# File
file = @uploader.upload(File.open(__FILE__))
assert_match /\.rb$/, file.id
end
it "uses filename metadata" do
file = @uploader.upload(fakeio, metadata: { "filename" => "avatar.jpg" })
assert_match /\.jpg$/, file.id
end
it "downcases the extension" do
file = @uploader.upload(fakeio(filename: "avatar.JPG"))
assert_match /\.jpg$/, file.id
end
it "handles no extension or no filename" do
file = @uploader.upload(fakeio(filename: "avatar"))
assert_match /^[\w-]+$/, file.id
uploaded_file = @uploader.upload(fakeio, location: "avatar")
file = @uploader.upload(uploaded_file)
assert_match /^[\w-]+$/, file.id
file = @uploader.upload(fakeio)
assert_match /^[\w-]+$/, file.id
end
it "gets extension from shrine-url-style id with query params" do
uploaded_file = @uploader.upload(fakeio, location: "http://example.com/path.html?key=value")
file = @uploader.upload(uploaded_file)
assert_match /\.html$/, file.id
uploaded_file = @uploader.upload(fakeio, location: "http://example.com/path?key=value")
file = @uploader.upload(uploaded_file)
refute_match /key=value/, file.id
assert_match /^[\w-]+$/, file.id
end
end
describe "#extract_metadata" do
it "extracts the filename" do
metadata = @uploader.extract_metadata(fakeio(filename: "avatar.jpg"))
assert_equal "avatar.jpg", metadata["filename"]
metadata = @uploader.extract_metadata(File.open("Gemfile"))
assert_equal "Gemfile", metadata["filename"]
metadata = @uploader.extract_metadata(fakeio)
assert_nil metadata["filename"]
end
it "extracts the filesize" do
metadata = @uploader.extract_metadata(fakeio("image"))
assert_equal 5, metadata["size"]
end
it "extracts the mime type" do
metadata = @uploader.extract_metadata(fakeio(content_type: "image/jpeg"))
assert_equal "image/jpeg", metadata["mime_type"]
metadata = @uploader.extract_metadata(fakeio(content_type: "text/plain;charset=utf-8"))
assert_equal "text/plain", metadata["mime_type"]
metadata = @uploader.extract_metadata(fakeio)
assert_nil metadata["mime_type"]
metadata = @uploader.extract_metadata(image)
assert_nil metadata["mime_type"]
end
it "successfully extracts metadata from another UploadedFile" do
io = fakeio("avatar", filename: "foo.jpg", content_type: "image/jpeg")
uploaded_file = @uploader.upload(io)
metadata = @uploader.extract_metadata(uploaded_file)
assert_equal 6, metadata["size"]
assert_equal "foo.jpg", metadata["filename"]
assert_equal "image/jpeg", metadata["mime_type"]
end
end
end