/
mount_multiple_spec.rb
1142 lines (891 loc) · 35 KB
/
mount_multiple_spec.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
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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require 'spec_helper'
describe CarrierWave::Mount do
let(:klass) { Class.new.send(:extend, CarrierWave::Mount) }
let(:uploader) { Class.new(CarrierWave::Uploader::Base) }
let(:instance) { klass.new }
let(:test_file_name) { 'test.jpg' }
let(:new_file_name) { 'new.jpeg' }
let(:test_file_stub) { stub_file(test_file_name) }
let(:new_file_stub) { stub_file(new_file_name) }
after { FileUtils.rm_rf(public_path) }
describe '.mount_uploaders' do
before { klass.mount_uploaders(:images, uploader) }
describe "super behavior" do
before do
klass.class_eval do
def images_uploader
super
end
def images=(val)
super
end
end
instance.images = [stub_file(test_file_name)]
end
it "maintains the ability to super" do
expect(instance.images[0]).to be_an_instance_of(uploader)
end
end
describe "upload inheritance" do
let(:subclass) { Class.new(klass) }
let(:subclass_instance) { subclass.new }
before { subclass_instance.images = [test_file_stub, new_file_stub] }
it "inherits uploaders to subclasses" do
expect(subclass_instance.images[0]).to be_an_instance_of(uploader)
expect(subclass_instance.images[1]).to be_an_instance_of(uploader)
end
end
describe "upload marshalling" do
before do
Object.const_set("MyClass#{klass.object_id}".gsub('-', '_'), klass)
Object.const_set("Uploader#{uploader.object_id}".gsub('-', '_'), uploader)
uploader.class_eval do
def rotate
end
end
uploader.version :thumb do
process :rotate
end
instance.images = [test_file_stub]
end
it "allows marshalling uploaders and versions" do
expect { Marshal.dump(instance.images) }.not_to raise_error
end
end
describe "behavior of subclassed uploaders" do
let(:uploader_1) do
Class.new(CarrierWave::Uploader::Base) do
[:rotate, :compress, :encrypt, :shrink].each { |m| define_method(m) {} }
end.tap do |uploader|
uploader.process :rotate
uploader.version :thumb do
process :compress
end
end
end
let(:uploader_2) do
Class.new(uploader_1).tap do |uploader|
uploader.process :shrink
uploader.version :secret do
process :encrypt
end
end
end
let(:instance) do
klass.new.tap do |instance|
instance.images1 = [test_file_stub]
instance.images2 = [test_file_stub]
end
end
before do
klass.mount_uploaders(:images1, uploader_1)
klass.mount_uploaders(:images2, uploader_2)
end
context "defined version inheritance works" do
it { expect(instance.images1[0]).to respond_to(:thumb) }
it { expect(instance.images2[0]).to respond_to(:thumb) }
end
context "version inheritance defined in subclasses works" do
it { expect(instance.images1[0]).not_to respond_to(:secret) }
it { expect(instance.images2[0]).to respond_to(:secret) }
end
context "defined processors inheritance works" do
it { expect(uploader_1.processors).to eq([[:rotate, [], nil, nil]]) }
it { expect(uploader_2.processors).to eq([[:rotate, [], nil, nil], [:shrink, [], nil, nil]]) }
it { expect(uploader_1.new.versions[:thumb].processors).to eq([[:compress, [], nil, nil]]) }
it { expect(uploader_2.new.versions[:thumb].processors).to eq([[:compress, [], nil, nil]]) }
it { expect(uploader_2.new.versions[:secret].processors).to eq([[:encrypt, [], nil, nil]]) }
end
end
describe '#images' do
context "return an empty array when nothing has been assigned" do
before do
allow(instance).to receive(:read_uploader).with(:images).and_return(nil)
end
it { expect(instance.images).to eq [] }
end
context "returns an empty array when an empty string has been assigned" do
before do
allow(instance).to receive(:read_uploader).with(:images).and_return('')
end
it { expect(instance.images).to eq [] }
end
context "retrieves a file from the storage if a value is stored in the database" do
subject(:images) { instance.images }
before do
allow(instance).to receive(:read_uploader).with(:images).at_least(:once).and_return([test_file_name, new_file_name])
end
it { expect(images[0]).to be_an_instance_of(uploader) }
it { expect(images[1]).to be_an_instance_of(uploader) }
end
context "sets the path to the store dir" do
subject(:image) { instance.images.first }
before do
allow(instance).to receive(:read_uploader).with(:images).at_least(:once).and_return(test_file_name)
end
it { expect(image.current_path).to eq(public_path("uploads/#{test_file_name}")) }
end
end
describe '#images=' do
let(:old_image_stub) { stub_file('old.jpeg') }
let(:text_file_stub) { stub_file('bork.txt') }
context "caching images" do
before do
instance.images = [test_file_stub, old_image_stub]
end
it { expect(instance.images[0]).to be_an_instance_of(uploader) }
it { expect(instance.images[1]).to be_an_instance_of(uploader) }
it "copies files into the cache directory" do
expect(instance.images[0].current_path).to match(/^#{public_path('uploads/tmp')}/)
end
it "marks the uploader as staged" do
expect(instance.images[0].staged).to be true
expect(instance.images[1].staged).to be true
end
end
it "does nothing when nil is assigned" do
expect(instance).not_to receive(:write_uploader)
instance.images = nil
end
it "does nothing when an empty string is assigned" do
expect(instance).not_to receive(:write_uploader)
instance.images = ''
end
it "accepts another uploader instances" do
instance.images = [test_file_stub, old_image_stub]
instance.store_images!
another = klass.new
another.images = instance.images
expect(another.images).to all(be_an_instance_of(uploader))
expect(another.images.map(&:cached?)).to all(be true)
end
context "if the images fails an allowlist integrity check" do
before do
uploader.class_eval do
def extension_allowlist
%w(txt)
end
end
end
it "fails silently" do
expect { instance.images = [test_file_stub] }.not_to raise_error
end
it "keeps files which passed the check" do
instance.images = [test_file_stub, text_file_stub]
expect(instance.images.map(&:identifier)).to eq ['bork.txt']
end
end
describe "if the images fails a denylist integrity check" do
before do
uploader.class_eval do
def extension_allowlist
%w(png txt)
end
end
end
it "fails silently" do
expect { instance.images = [test_file_stub] }.not_to raise_error
end
it "keeps files which passed the check" do
instance.images = [test_file_stub, text_file_stub]
expect(instance.images.map(&:identifier)).to eq ['bork.txt']
end
end
describe "if the images fails to be processed" do
before do
uploader.class_eval do
process :monkey
def monkey
raise CarrierWave::ProcessingError, "Ohh noez!" if file.path =~ /test\.jpg/
end
end
end
it "fails silently" do
expect { instance.images = [test_file_stub] }.not_to raise_error
end
it "keeps files which was processed successfully" do
instance.images = [test_file_stub, text_file_stub]
expect(instance.images.map(&:identifier)).to eq ['bork.txt']
end
end
describe "when caching files of the same filename" do
before { FileUtils.cp(file_path('bork.json'), tmp_path('bork.txt')) }
after { FileUtils.rm(tmp_path('bork.txt')) }
it "accepts them without confusion" do
instance.images = [text_file_stub, File.open(tmp_path('bork.txt'))]
expect(instance.images[0].cache_path).not_to eq instance.images[1].cache_path
expect(instance.images[0].read).not_to eq instance.images[1].read
end
end
describe "with cached files" do
before do
instance.images = [text_file_stub, test_file_stub]
end
let(:cache_names) { instance.images.map(&:cache_name) }
it "accepts cache name and retrieves from cache" do
instance.images = [cache_names[1]]
expect(instance.images.map { |u| u.file.filename }).to eq ['test.jpg']
end
context "when adding a file which has the same filename with the existing one" do
before { FileUtils.cp(file_path('bork.json'), tmp_path('bork.txt')) }
after { FileUtils.rm(tmp_path('bork.txt')) }
it "accepts it without confusion" do
instance.images = [instance.images[0].cache_name, File.open(tmp_path('bork.txt'))]
expect(instance.images[0].cache_path).not_to eq instance.images[1].cache_path
expect(instance.images[0].read).not_to eq instance.images[1].read
end
end
end
describe "with stored files" do
before do
instance.images = [text_file_stub, test_file_stub]
instance.store_images!
end
let(:identifiers) { instance.images.map(&:identifier) }
it "writes over a previously stored file" do
instance.images = [old_image_stub]
instance.store_images!
expect(instance.images.map(&:identifier)).to eq ['old.jpeg']
end
it "preserves existing image of given identifier" do
instance.images = [identifiers[0], old_image_stub]
instance.store_images!
expect(instance.images.map(&:identifier)).to eq ['bork.txt', 'old.jpeg']
end
it "reorders existing image" do
instance.images = identifiers.reverse
instance.store_images!
expect(instance.images.map(&:identifier)).to eq ['test.jpg', 'bork.txt']
end
it "allows uploading and reordering at once" do
instance.images = [identifiers[1], old_image_stub, identifiers[0]]
instance.store_images!
expect(instance.images.map(&:identifier)).to eq ['test.jpg', 'old.jpeg', 'bork.txt']
end
it "allows repeating the same identifiers" do
instance.images = ['bork.txt', 'test.jpg', 'bork.txt']
instance.store_images!
expect(instance.images.map(&:identifier)).to eq ['bork.txt', 'test.jpg', 'bork.txt']
end
it "removes image which is unused" do
@image_paths = instance.images.map(&:current_path)
instance.images = [identifiers[0]]
instance.store_images!
instance.send(:_mounter, :images).remove_previous
expect(instance.images.map(&:identifier)).to eq ['bork.txt']
expect(File.exist?(@image_paths[0])).to be_truthy
expect(File.exist?(@image_paths[1])).to be_falsey
end
it "ignores unknown identifier" do
instance.images = ['unknown.txt']
expect { instance.store_images! }.not_to raise_error
expect(instance.images.map(&:identifier)).to be_empty
end
it "allows deleting all files" do
instance.images = []
expect(instance.images.map(&:identifier)).to be_empty
end
it "allows assignment of uploader instances" do
instance.images = [instance.images[0]]
expect(instance.images.map(&:identifier)).to eq ['bork.txt']
end
context "when adding a file which has the same filename with the existing one" do
before { FileUtils.cp(file_path('bork.json'), tmp_path('bork.txt')) }
after { FileUtils.rm(tmp_path('bork.txt')) }
it "renames the latter file to avoid filename duplication" do
instance.images = ['bork.txt', File.open(tmp_path('bork.txt'))]
instance.write_images_identifier
instance.store_images!
expect(instance.images.map(&:identifier)).to eq ['bork.txt', 'bork(2).txt']
expect(instance.images[0].read).not_to eq instance.images[1].read
end
end
end
end
describe '#images?' do
subject { instance.images? }
context "false when nothing has been assigned" do
before { instance.images = nil }
it { is_expected.to be_falsey }
end
context "false when an empty string has been assigned" do
before { instance.images = '' }
it { is_expected.to be_falsey }
end
context "true when a file has been cached" do
before { instance.images = [test_file_stub] }
it { is_expected.to be_truthy }
end
end
describe '#images_urls' do
subject(:images_urls) { instance.images_urls }
describe "returns nil when nothing has been assigned" do
before do
allow(instance).to receive(:read_uploader).with(:images).and_return(nil)
end
it { is_expected.to be_empty }
end
describe "should return nil when an empty string has been assigned" do
before do
allow(instance).to receive(:read_uploader).with(:images).and_return('')
end
it { is_expected.to be_empty }
end
describe "gets the url from a retrieved file" do
before do
allow(instance).to receive(:read_uploader).at_least(:once).with(:images).and_return(test_file_name)
end
it { expect(images_urls.first).to eq("/uploads/#{test_file_name}") }
end
describe "gets the url from a cached file" do
before { instance.images = [test_file_stub] }
it { expect(images_urls[0]).to match(%r{uploads/tmp/[\d\-]+/test.jpg}) }
end
describe "gets the url from a cached file's version" do
before do
uploader.version(:thumb)
instance.images = [test_file_stub]
end
it { expect(instance.images_urls(:thumb)[0]).to match(%r{uploads/tmp/[\d\-]+/thumb_test.jpg}) }
end
end
describe '#images_cache' do
before do
allow(instance).to receive(:write_uploader)
allow(instance).to receive(:read_uploader).and_return(nil)
end
context "when nothing has been assigned" do
it { expect(instance.images_cache).to be_nil }
end
context "when a file has been stored" do
before do
instance.images = [test_file_stub]
instance.store_images!
end
it { expect(instance.images_cache).to be_nil }
end
context "when a file has been cached" do
let(:json_response) { JSON.parse(instance.images_cache) }
before do
instance.images = [test_file_stub, stub_file('old.jpeg')]
end
it { expect(json_response[0]).to match(%r(^[\d]+\-[\d]+\-[\d]{4}\-[\d]{4}/test\.jpg$)) }
it { expect(json_response[1]).to match(%r(^[\d]+\-[\d]+\-[\d]{4}\-[\d]{4}/old\.jpeg$)) }
end
end
describe '#images_cache=' do
before do
allow(instance).to receive(:write_uploader)
allow(instance).to receive(:read_uploader).and_return(nil)
CarrierWave::SanitizedFile.new(test_file_stub).copy_to(public_path('uploads/tmp/1369894322-123-0123-1234/test.jpg'))
end
context "does nothing when nil is assigned" do
before { instance.images_cache = nil }
it { expect(instance.images).to be_empty }
end
context "does nothing when an empty string is assigned" do
before { instance.images_cache = '' }
it { expect(instance.images).to be_empty }
end
context "retrieve from cache when a cache name is assigned" do
before { instance.images_cache = ['1369894322-123-0123-1234/test.jpg'].to_json }
it { expect(instance.images[0].current_path).to eq(public_path('uploads/tmp/1369894322-123-0123-1234/test.jpg')) }
it "marks the uploader as staged" do
expect(instance.images[0].staged).to be true
end
end
context "writes over a previously stored file" do
before do
instance.images = [test_file_stub]
instance.store_images!
instance.images_cache = ['1369894322-123-0123-1234/monkey.jpg'].to_json
end
it { expect(instance.images[0].current_path).to match(/monkey.jpg$/) }
end
context "doesn't write over a previously assigned file" do
before do
instance.images = [test_file_stub]
instance.images_cache = ['1369894322-123-0123-1234/monkey.jpg'].to_json
end
it { expect(instance.images[0].current_path).to match(/test.jpg$/) }
end
context "when valid and invalid cache names are assigned" do
before { instance.images_cache = ['1369894322-123-0123-1234/test.jpg', 'invalid'].to_json }
it "retrieves valid file only from cache" do
expect(instance.images.map(&:cache_name)).to eq(['1369894322-123-0123-1234/test.jpg'])
end
end
context "when an empty string is assigned" do
before do
instance.images = [test_file_stub]
instance.store_images!
instance.images_cache = [''].to_json
end
it "does not write over a previously stored file" do
expect(instance.images[0].current_path).to match(/test.jpg$/)
end
end
end
describe "#remote_images_urls" do
subject { instance.remote_images_urls }
before { stub_request(:get, "http://www.example.com/#{test_file_name}").to_return(body: File.read(test_file_stub)) }
context "returns nil" do
it { is_expected.to be_nil }
end
context "returns previously cached URL" do
before { instance.remote_images_urls = ["http://www.example.com/test.jpg"] }
it { is_expected.to eq(["http://www.example.com/test.jpg"]) }
end
end
describe "#remote_images_urls=" do
subject(:images) { instance.images }
before do
stub_request(:get, "http://www.example.com/#{test_file_name}").to_return(body: File.read(test_file_stub))
stub_request(:get, "http://www.example.com/test.txt").to_return(status: 404)
instance.remote_images_urls = remote_images_url
end
context "does nothing when nil is assigned" do
let(:remote_images_url) { nil }
it { is_expected.to be_empty }
end
context "does nothing when an empty string is assigned" do
let(:remote_images_url) { '' }
it { is_expected.to be_empty }
end
context "retrieves from cache when a cache name is assigned" do
subject { images[0].current_path }
let(:remote_images_url) { ["http://www.example.com/test.jpg"] }
it { is_expected.to match(/test.jpg$/) }
it "marks the uploader as staged" do
expect(instance.images[0].staged).to be true
end
end
context "writes over a previously stored file" do
subject { images[0].current_path }
let(:remote_images_url) { ["http://www.example.com/test.jpg"] }
before do
instance.images = [stub_file("portrait.jpg")]
instance.store_images!
instance.remote_images_urls = remote_images_url
end
it { is_expected.to match(/test.jpg$/) }
end
context "does not write over a previously assigned file" do
subject { images[0].current_path }
let(:remote_images_url) { ["http://www.example.com/test.jpg"] }
before do
instance.images = [stub_file("portrait.jpg")]
instance.remote_images_urls = remote_images_url
end
it { is_expected.to match(/portrait.jpg$/) }
end
context "when an empty string is assigned" do
subject { images[0].current_path }
let(:remote_images_url) { [""] }
before do
instance.images = [stub_file("portrait.jpg")]
instance.store_images!
instance.remote_images_urls = remote_images_url
end
it "does not write over a previously stored file" do
is_expected.to match(/portrait.jpg$/)
end
end
context "if a file fails to be downloaded" do
let(:remote_images_url) { ["http://www.example.com/test.txt", "http://www.example.com/test.jpg"] }
it "keeps files which was downloaded successfully" do
expect(instance.images.map(&:identifier)).to eq ['test.jpg']
end
end
context "clears the unsaved remote urls when nil is assigned" do
subject { instance.remote_images_urls }
let(:remote_images_url) { ['invalid'] }
before do
instance.remote_images_urls = nil
end
it { is_expected.to be_empty }
end
end
describe '#store_images!' do
before do
allow(instance).to receive(:write_uploader)
allow(instance).to receive(:read_uploader).and_return(nil)
end
context "does nothing when no file has been uploaded" do
before { instance.store_images! }
it { expect(instance.images).to be_empty }
end
context "stores an assigned file" do
let(:images) { [test_file_stub] }
before do
instance.images = images
instance.store_images!
end
it { expect(instance.images[0].current_path).to eq(public_path("uploads/#{test_file_name}")) }
it "marks the uploader as unstaged" do
expect(instance.images[0].staged).to be false
end
end
end
describe '#remove_images!' do
before do
allow(instance).to receive(:write_uploader)
allow(instance).to receive(:read_uploader).and_return(nil)
end
context "does nothing when no file has been uploaded" do
before { instance.remove_images! }
it { expect(instance.images).to be_empty }
end
context "removes an uploaded file" do
before do
instance.images = [test_file_stub]
@image_path = instance.images[0].current_path
instance.remove_images!
end
it { expect(instance.images).to be_empty }
it { expect(File.exist?(@image_path)).to be_falsey }
end
end
describe '#remove_images' do
before { instance.remove_images = true }
it "stores a value" do
expect(instance.remove_images).to be_truthy
end
end
describe '#remove_images?' do
subject { instance.remove_images? }
let(:remove_images) { true }
before { instance.remove_images = remove_images }
it "when value is true" do
is_expected.to be_truthy
end
context "when value is false" do
let(:remove_images) { false }
it { is_expected.to be_falsey }
end
context "when value is ''" do
let(:remove_images) { '' }
it { is_expected.to be_falsey }
end
context "when value is '0'" do
let(:remove_images) { "0" }
it { is_expected.to be_falsey }
end
context "when value is 'false'" do
let(:remove_images) { 'false' }
it { is_expected.to be_falsey }
end
context "when value is 0" do
let(:remove_images) { 0 }
it { is_expected.to be_falsey }
end
end
describe '#images_integrity_errors' do
subject(:images_integrity_errors) { instance.images_integrity_errors }
describe "default behaviour" do
it { is_expected.to be_empty }
end
context "when a file is cached" do
before { instance.images = test_file_stub }
it { is_expected.to be_empty }
end
describe "when an integrity check fails" do
before do
uploader.class_eval do
def extension_allowlist
%w(txt)
end
end
end
context "when file is cached" do
before { instance.images = [test_file_stub] }
it { is_expected.to include(a_kind_of(CarrierWave::IntegrityError)) }
it "has an error message" do
expect(images_integrity_errors[0].message.lines.grep(/^You are not allowed to upload/)).to be_truthy
end
end
context "when file was downloaded" do
before do
stub_request(:get, "http://www.example.com/#{test_file_name}").to_return(body: File.read(test_file_stub))
instance.remote_images_urls = ["http://www.example.com/#{test_file_name}"]
end
it { is_expected.to include(a_kind_of(CarrierWave::IntegrityError)) }
it "has an error message" do
expect(images_integrity_errors[0].message.lines.grep(/^You are not allowed to upload/)).to be_truthy
end
end
context "when file is assigned and remote_images_url is blank" do
before do
instance.images = [test_file_stub]
instance.remote_images_urls = ""
end
it { is_expected.to include(a_kind_of(CarrierWave::IntegrityError)) }
it "has an error message" do
expect(images_integrity_errors[0].message.lines.grep(/^You are not allowed to upload/)).to be_truthy
end
end
end
end
describe '#images_processing_errors' do
subject(:images_processing_errors) { instance.images_processing_errors }
describe "default behavior" do
it { is_expected.to be_empty }
end
context "when file is cached" do
before { instance.images = [test_file_stub] }
it { is_expected.to be_empty }
end
describe "when an processing error occurs" do
before do
uploader.class_eval do
process :monkey
def monkey
raise CarrierWave::ProcessingError, "Ohh noez!"
end
end
end
context "when file is cached" do
before { instance.images = [test_file_stub] }
it { is_expected.to include(a_kind_of(CarrierWave::ProcessingError)) }
end
context "when file was downloaded" do
before do
stub_request(:get, "http://www.example.com/#{test_file_name}").to_return(body: File.read(test_file_stub))
instance.remote_images_urls = ["http://www.example.com/#{test_file_name}"]
end
it { is_expected.to include(a_kind_of(CarrierWave::ProcessingError)) }
end
end
end
describe '#images_download_errors' do
subject(:images_download_errors) { instance.images_download_errors }
before do
stub_request(:get, "http://www.example.com/#{test_file_name}").to_return(body: File.read(test_file_stub))
stub_request(:get, "http://www.example.com/missing.jpg").to_return(status: 404)
end
describe "default behaviour" do
it { expect(instance.images_download_errors).to be_empty }
end
context "when file download was successful" do
before { instance.remote_images_urls = ["http://www.example.com/#{test_file_name}"] }
it { is_expected.to be_empty }
end
context "when file couldn't be found" do
before { instance.remote_images_urls = ["http://www.example.com/missing.jpg"] }
it { is_expected.to include(a_kind_of(CarrierWave::DownloadError)) }
end
end
describe '#write_images_identifier' do
after { instance.write_images_identifier }
it "writes to the column" do
instance.images = [test_file_stub]
expect(instance).to receive(:write_uploader).with(:images, [test_file_name]).at_least(:once)
instance.write_images_identifier
end
context "when remove_images is true" do
before do
instance.images = [test_file_name]
instance.store_images!
instance.remove_images = true
end
it "clears existing uploaders" do
expect(instance.images).to be_empty
end
it "removes from the column" do
expect(instance).to receive(:write_uploader).with(:images, nil)
end
end
end
describe '#images_identifiers' do
it "returns the identifier from the mounted column" do
expect(instance).to receive(:read_uploader).with(:images).and_return(test_file_name)
expect(instance.images_identifiers).to eq([test_file_name])
end
end
end
describe '#mount_uploaders without an uploader' do
let(:klass) do
Class.new.tap do |k|
k.send(:extend, CarrierWave::Mount)
k.mount_uploaders(:images)
end
end
let(:instance) { klass.new }
describe '#images' do
before do
allow(instance).to receive(:read_uploader).and_return(test_file_name)
end
it "returns an instance of a subclass of CarrierWave::Uploader::Base" do
expect(instance.images[0]).to be_a(CarrierWave::Uploader::Base)
end
it "sets the path to the store dir" do
expect(instance.images[0].current_path).to eq(public_path("uploads/#{test_file_name}"))
end
end
end
describe '#mount_uploaders with a block' do
describe 'and no uploader given' do
subject(:last_image) { instance.images[0] }
let(:klass) do
Class.new do |k|
k.send(:extend, CarrierWave::Mount)
k.mount_uploaders(:images) do
def monkey
'blah'
end
end
end
end
before { instance.images = [test_file_stub] }
it "returns an instance of a subclass of CarrierWave::Uploader::Base" do
is_expected.to be_a(CarrierWave::Uploader::Base)
end
it "applies any custom modifications" do
expect(last_image.monkey).to eq("blah")
end
end
describe 'and an uploader given' do
let!(:uploader) do
Class.new(CarrierWave::Uploader::Base).tap do |u|
u.version :thumb do
version :mini
version :maxi
end
end
end
let(:klass) do
Class.new.tap do |k|
k.send(:extend, CarrierWave::Mount)
k.mount_uploaders(:images, uploader) do
def fish
'blub'
end
end
end
end
let(:first_image) { instance.images[0] }
before { instance.images = [test_file_stub] }
it "returns an instance of the uploader specified" do
expect(first_image).to be_a_kind_of(uploader)
end
it "applies any custom modifications to the instance" do