This repository has been archived by the owner on Jul 13, 2023. It is now read-only.
/
s3_spec.rb
1665 lines (1419 loc) · 47.3 KB
/
s3_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'
require 'aws-sdk'
describe Paperclip::Storage::S3 do
before do
Aws.config[:stub_responses] = true
end
def aws2_add_region
{ s3_region: 'us-east-1' }
end
context "Parsing S3 credentials" do
before do
@proxy_settings = {host: "127.0.0.1", port: 8888, user: "foo", password: "bar"}
rebuild_model (aws2_add_region).merge storage: :s3,
bucket: "testing",
http_proxy: @proxy_settings,
s3_credentials: {not: :important}
@dummy = Dummy.new
@avatar = @dummy.avatar
end
it "gets the correct credentials when RAILS_ENV is production" do
rails_env("production") do
assert_equal({key: "12345"},
@avatar.parse_credentials('production' => {key: '12345'},
development: {key: "54321"}))
end
end
it "gets the correct credentials when RAILS_ENV is development" do
rails_env("development") do
assert_equal({key: "54321"},
@avatar.parse_credentials('production' => {key: '12345'},
development: {key: "54321"}))
end
end
it "returns the argument if the key does not exist" do
rails_env("not really an env") do
assert_equal({test: "12345"}, @avatar.parse_credentials(test: "12345"))
end
end
it "supports HTTP proxy settings" do
rails_env("development") do
assert_equal(true, @avatar.using_http_proxy?)
assert_equal(@proxy_settings[:host], @avatar.http_proxy_host)
assert_equal(@proxy_settings[:port], @avatar.http_proxy_port)
assert_equal(@proxy_settings[:user], @avatar.http_proxy_user)
assert_equal(@proxy_settings[:password], @avatar.http_proxy_password)
end
end
end
context ":bucket option via :s3_credentials" do
before do
rebuild_model (aws2_add_region).merge storage: :s3,
s3_credentials: {bucket: 'testing'}
@dummy = Dummy.new
end
it "populates #bucket_name" do
assert_equal @dummy.avatar.bucket_name, 'testing'
end
end
context ":bucket option" do
before do
rebuild_model (aws2_add_region).merge storage: :s3,
bucket: "testing", s3_credentials: {}
@dummy = Dummy.new
end
it "populates #bucket_name" do
assert_equal @dummy.avatar.bucket_name, 'testing'
end
end
context "missing :bucket option" do
before do
rebuild_model (aws2_add_region).merge storage: :s3,
http_proxy: @proxy_settings,
s3_credentials: {not: :important}
@dummy = Dummy.new
@dummy.avatar = stringy_file
end
it "raises an argument error" do
expect { @dummy.save }.to raise_error(ArgumentError, /missing required :bucket option/)
end
end
context "" do
before do
rebuild_model (aws2_add_region).merge storage: :s3,
s3_credentials: {},
bucket: "bucket",
path: ":attachment/:basename:dotextension",
url: ":s3_path_url"
@dummy = Dummy.new
@dummy.avatar = stringy_file
@dummy.stubs(:new_record?).returns(false)
end
it "returns a url based on an S3 path" do
assert_match %r{^//s3.amazonaws.com/bucket/avatars/data[^\.]}, @dummy.avatar.url
end
it "uses the correct bucket" do
assert_equal "bucket", @dummy.avatar.s3_bucket.name
end
it "uses the correct key" do
assert_equal "avatars/data", @dummy.avatar.s3_object.key
end
end
context "s3_protocol" do
["http", :http, ""].each do |protocol|
context "as #{protocol.inspect}" do
before do
rebuild_model (aws2_add_region).merge storage: :s3,
s3_protocol: protocol
@dummy = Dummy.new
end
it "returns the s3_protocol in string" do
assert_equal protocol.to_s, @dummy.avatar.s3_protocol
end
end
end
end
context "s3_protocol: 'https'" do
before do
rebuild_model (aws2_add_region).merge storage: :s3,
s3_credentials: {},
s3_protocol: 'https',
bucket: "bucket",
path: ":attachment/:basename:dotextension"
@dummy = Dummy.new
@dummy.avatar = stringy_file
@dummy.stubs(:new_record?).returns(false)
end
it "returns a url based on an S3 path" do
assert_match %r{^https://s3.amazonaws.com/bucket/avatars/data[^\.]}, @dummy.avatar.url
end
end
context "s3_protocol: ''" do
before do
rebuild_model (aws2_add_region).merge storage: :s3,
s3_credentials: {},
s3_protocol: '',
bucket: "bucket",
path: ":attachment/:basename:dotextension"
@dummy = Dummy.new
@dummy.avatar = stringy_file
@dummy.stubs(:new_record?).returns(false)
end
it "returns a protocol-relative URL" do
assert_match %r{^//s3.amazonaws.com/bucket/avatars/data[^\.]}, @dummy.avatar.url
end
end
context "s3_protocol: :https" do
before do
rebuild_model (aws2_add_region).merge storage: :s3,
s3_credentials: {},
s3_protocol: :https,
bucket: "bucket",
path: ":attachment/:basename:dotextension"
@dummy = Dummy.new
@dummy.avatar = stringy_file
@dummy.stubs(:new_record?).returns(false)
end
it "returns a url based on an S3 path" do
assert_match %r{^https://s3.amazonaws.com/bucket/avatars/data[^\.]}, @dummy.avatar.url
end
end
context "s3_protocol: ''" do
before do
rebuild_model (aws2_add_region).merge storage: :s3,
s3_credentials: {},
s3_protocol: '',
bucket: "bucket",
path: ":attachment/:basename:dotextension"
@dummy = Dummy.new
@dummy.avatar = stringy_file
@dummy.stubs(:new_record?).returns(false)
end
it "returns a url based on an S3 path" do
assert_match %r{^//s3.amazonaws.com/bucket/avatars/data[^\.]}, @dummy.avatar.url
end
end
context "An attachment that uses S3 for storage and has the style in the path" do
before do
rebuild_model (aws2_add_region).merge storage: :s3,
bucket: "testing",
path: ":attachment/:style/:basename:dotextension",
styles: {
thumb: "80x80>"
},
s3_credentials: {
'access_key_id' => "12345",
'secret_access_key' => "54321"
}
@dummy = Dummy.new
@dummy.avatar = stringy_file
@avatar = @dummy.avatar
end
it "uses an S3 object based on the correct path for the default style" do
assert_equal("avatars/original/data", @dummy.avatar.s3_object.key)
end
it "uses an S3 object based on the correct path for the custom style" do
assert_equal("avatars/thumb/data", @dummy.avatar.s3_object(:thumb).key)
end
end
# if using aws-sdk-v2, the s3_host_name will be defined by the s3_region
context "s3_host_name" do
before do
rebuild_model storage: :s3,
s3_credentials: {},
bucket: "bucket",
path: ":attachment/:basename:dotextension",
s3_host_name: "s3-ap-northeast-1.amazonaws.com",
s3_region: "ap-northeast-1"
@dummy = Dummy.new
@dummy.avatar = stringy_file
@dummy.stubs(:new_record?).returns(false)
end
it "returns a url based on an :s3_host_name path" do
assert_match %r{^//s3-ap-northeast-1.amazonaws.com/bucket/avatars/data[^\.]}, @dummy.avatar.url
end
it "uses the S3 bucket with the correct host name" do
assert_equal "s3-ap-northeast-1.amazonaws.com",
@dummy.avatar.s3_bucket.client.config.endpoint.host
end
end
context "dynamic s3_host_name" do
before do
rebuild_model (aws2_add_region).merge storage: :s3,
s3_credentials: {},
bucket: "bucket",
path: ":attachment/:basename:dotextension",
s3_host_name: lambda {|a| a.instance.value }
@dummy = Dummy.new
class << @dummy
attr_accessor :value
end
@dummy.avatar = stringy_file
@dummy.stubs(:new_record?).returns(false)
end
it "uses s3_host_name as a proc if available" do
@dummy.value = "s3.something.com"
assert_equal "//s3.something.com/bucket/avatars/data", @dummy.avatar.url(:original, timestamp: false)
end
end
context "use_accelerate_endpoint", if: aws_accelerate_available? do
context "defaults to false" do
before do
rebuild_model(
storage: :s3,
s3_credentials: {},
bucket: "bucket",
path: ":attachment/:basename:dotextension",
s3_host_name: "s3-ap-northeast-1.amazonaws.com",
s3_region: "ap-northeast-1",
)
@dummy = Dummy.new
@dummy.avatar = stringy_file
@dummy.stubs(:new_record?).returns(false)
end
it "returns a url based on an :s3_host_name path" do
assert_match %r{^//s3-ap-northeast-1.amazonaws.com/bucket/avatars/data[^\.]},
@dummy.avatar.url
end
it "uses the S3 client with the use_accelerate_endpoint config is false" do
expect(@dummy.avatar.s3_bucket.client.config.use_accelerate_endpoint).to be(false)
end
end
context "set to true", if: aws_accelerate_available? do
before do
rebuild_model(
storage: :s3,
s3_credentials: {},
bucket: "bucket",
path: ":attachment/:basename:dotextension",
s3_host_name: "s3-accelerate.amazonaws.com",
s3_region: "ap-northeast-1",
use_accelerate_endpoint: true,
)
@dummy = Dummy.new
@dummy.avatar = stringy_file
@dummy.stubs(:new_record?).returns(false)
end
it "returns a url based on an :s3_host_name path" do
assert_match %r{^//s3-accelerate.amazonaws.com/bucket/avatars/data[^\.]},
@dummy.avatar.url
end
it "uses the S3 client with the use_accelerate_endpoint config is true" do
expect(@dummy.avatar.s3_bucket.client.config.use_accelerate_endpoint).to be(true)
end
end
end
context "An attachment that uses S3 for storage and has styles that return different file types" do
before do
rebuild_model (aws2_add_region).merge storage: :s3,
styles: { large: ['500x500#', :jpg] },
bucket: "bucket",
path: ":attachment/:basename:dotextension",
s3_credentials: {
'access_key_id' => "12345",
'secret_access_key' => "54321"
}
File.open(fixture_file('5k.png'), 'rb') do |file|
@dummy = Dummy.new
@dummy.avatar = file
@dummy.stubs(:new_record?).returns(false)
end
end
it "returns a url containing the correct original file mime type" do
assert_match /.+\/5k.png/, @dummy.avatar.url
end
it 'uses the correct key for the original file mime type' do
assert_match /.+\/5k.png/, @dummy.avatar.s3_object.key
end
it "returns a url containing the correct processed file mime type" do
assert_match /.+\/5k.jpg/, @dummy.avatar.url(:large)
end
it "uses the correct key for the processed file mime type" do
assert_match /.+\/5k.jpg/, @dummy.avatar.s3_object(:large).key
end
end
context "An attachment that uses S3 for storage and has a proc for styles" do
before do
rebuild_model (aws2_add_region).merge storage: :s3,
styles: lambda { |attachment| attachment.instance.counter
{thumbnail: { geometry: "50x50#",
s3_headers: {'Cache-Control' => 'max-age=31557600'}} }},
bucket: "bucket",
path: ":attachment/:style/:basename:dotextension",
s3_credentials: {
'access_key_id' => "12345",
'secret_access_key' => "54321"
}
@file = File.new(fixture_file('5k.png'), 'rb')
Dummy.class_eval do
def counter
@counter ||= 0
@counter += 1
@counter
end
end
@dummy = Dummy.new
@dummy.avatar = @file
object = stub
@dummy.avatar.stubs(:s3_object).with(:original).returns(object)
@dummy.avatar.stubs(:s3_object).with(:thumbnail).returns(object)
object.expects(:upload_file)
.with(anything, content_type: 'image/png',
acl: :"public-read")
object.expects(:upload_file)
.with(anything, content_type: 'image/png',
acl: :"public-read",
cache_control: 'max-age=31557600')
@dummy.save
end
after { @file.close }
it "succeeds" do
assert_equal @dummy.counter, 7
end
end
context "An attachment that uses S3 for storage and has styles" do
before do
rebuild_model(
(aws2_add_region).merge(
storage: :s3,
styles: { thumb: ["90x90#", :jpg] },
bucket: "bucket",
s3_credentials: {
"access_key_id" => "12345",
"secret_access_key" => "54321" }
)
)
@file = File.new(fixture_file("5k.png"), "rb")
@dummy = Dummy.new
@dummy.avatar = @file
@dummy.save
end
context "reprocess" do
before do
@object = stub
@dummy.avatar.stubs(:s3_object).with(:original).returns(@object)
@dummy.avatar.stubs(:s3_object).with(:thumb).returns(@object)
@object.stubs(:get).yields(@file.read)
@object.stubs(:exists?).returns(true)
end
it "uploads original" do
@object.expects(:upload_file).with(
anything,
content_type: "image/png",
acl: :"public-read").returns(true)
@object.expects(:upload_file).with(
anything,
content_type: "image/jpeg",
acl: :"public-read").returns(true)
@dummy.avatar.reprocess!
end
it "doesn't upload original" do
@object.expects(:upload_file).with(
anything,
content_type: "image/jpeg",
acl: :"public-read").returns(true)
@dummy.avatar.reprocess!(:thumb)
end
end
after { @file.close }
end
context "An attachment that uses S3 for storage and has spaces in file name" do
before do
rebuild_model(
(aws2_add_region).merge storage: :s3,
styles: { large: ["500x500#", :jpg] },
bucket: "bucket",
s3_credentials: { "access_key_id" => "12345",
"secret_access_key" => "54321" }
)
File.open(fixture_file("spaced file.png"), "rb") do |file|
@dummy = Dummy.new
@dummy.avatar = file
@dummy.stubs(:new_record?).returns(false)
end
end
it "returns a replaced version for path" do
assert_match /.+\/spaced_file\.png/, @dummy.avatar.path
end
it "returns a replaced version for url" do
assert_match /.+\/spaced_file\.png/, @dummy.avatar.url
end
end
context "An attachment that uses S3 for storage and has a question mark in file name" do
before do
rebuild_model (aws2_add_region).merge storage: :s3,
styles: { large: ['500x500#', :jpg] },
bucket: "bucket",
s3_credentials: {
'access_key_id' => "12345",
'secret_access_key' => "54321"
}
stringio = stringy_file
class << stringio
def original_filename
"question?mark.png"
end
end
file = Paperclip.io_adapters.for(stringio, hash_digest: Digest::MD5)
@dummy = Dummy.new
@dummy.avatar = file
@dummy.save
@dummy.stubs(:new_record?).returns(false)
end
it "returns a replaced version for path" do
assert_match /.+\/question_mark\.png/, @dummy.avatar.path
end
it "returns a replaced version for url" do
assert_match /.+\/question_mark\.png/, @dummy.avatar.url
end
end
context "" do
before do
rebuild_model (aws2_add_region).merge storage: :s3,
s3_credentials: {},
bucket: "bucket",
path: ":attachment/:basename:dotextension",
url: ":s3_domain_url"
@dummy = Dummy.new
@dummy.avatar = stringy_file
@dummy.stubs(:new_record?).returns(false)
end
it "returns a url based on an S3 subdomain" do
assert_match %r{^//bucket.s3.amazonaws.com/avatars/data[^\.]}, @dummy.avatar.url
end
end
context "" do
before do
rebuild_model(
(aws2_add_region).merge storage: :s3,
s3_credentials: {
production: { bucket: "prod_bucket" },
development: { bucket: "dev_bucket" }
},
bucket: "bucket",
s3_host_alias: "something.something.com",
path: ":attachment/:basename:dotextension",
url: ":s3_alias_url"
)
@dummy = Dummy.new
@dummy.avatar = stringy_file
@dummy.stubs(:new_record?).returns(false)
end
it "returns a url based on the host_alias" do
assert_match %r{^//something.something.com/avatars/data[^\.]}, @dummy.avatar.url
end
end
context "generating a url with a proc as the host alias" do
before do
rebuild_model (aws2_add_region).merge storage: :s3,
s3_credentials: { bucket: "prod_bucket" },
s3_host_alias: Proc.new{|atch| "cdn#{atch.instance.counter % 4}.example.com"},
path: ":attachment/:basename:dotextension",
url: ":s3_alias_url"
Dummy.class_eval do
def counter
@counter ||= 0
@counter += 1
@counter
end
end
@dummy = Dummy.new
@dummy.avatar = stringy_file
@dummy.stubs(:new_record?).returns(false)
end
it "returns a url based on the host_alias" do
assert_match %r{^//cdn1.example.com/avatars/data[^\.]}, @dummy.avatar.url
assert_match %r{^//cdn2.example.com/avatars/data[^\.]}, @dummy.avatar.url
end
it "still returns the bucket name" do
assert_equal "prod_bucket", @dummy.avatar.bucket_name
end
end
context "" do
before do
rebuild_model (aws2_add_region).merge storage: :s3,
s3_credentials: {},
bucket: "bucket",
path: ":attachment/:basename:dotextension",
url: ":asset_host"
@dummy = Dummy.new
@dummy.avatar = stringy_file
@dummy.stubs(:new_record?).returns(false)
end
it "returns a relative URL for Rails to calculate assets host" do
assert_match %r{^avatars/data[^\.]}, @dummy.avatar.url
end
end
context "Generating a secure url with an expiration" do
before do
@build_model_with_options = lambda {|options|
base_options = {
storage: :s3,
s3_credentials: {
production: { bucket: "prod_bucket" },
development: { bucket: "dev_bucket" }
},
s3_host_alias: "something.something.com",
s3_permissions: "private",
path: ":attachment/:basename:dotextension",
url: ":s3_alias_url"
}
rebuild_model (aws2_add_region).merge base_options.merge(options)
}
end
it "uses default options" do
@build_model_with_options[{}]
rails_env("production") do
@dummy = Dummy.new
@dummy.avatar = stringy_file
object = stub
@dummy.avatar.stubs(:s3_object).returns(object)
object.expects(:presigned_url).with(:get, expires_in: 3600)
@dummy.avatar.expiring_url
end
end
it "allows overriding s3_url_options" do
@build_model_with_options[s3_url_options: { response_content_disposition: "inline" }]
rails_env("production") do
@dummy = Dummy.new
@dummy.avatar = stringy_file
object = stub
@dummy.avatar.stubs(:s3_object).returns(object)
object.expects(:presigned_url)
.with(:get, expires_in: 3600,
response_content_disposition: "inline")
@dummy.avatar.expiring_url
end
end
it "allows overriding s3_object options with a proc" do
@build_model_with_options[s3_url_options: lambda {|attachment| { response_content_type: attachment.avatar_content_type } }]
rails_env("production") do
@dummy = Dummy.new
@file = stringy_file
@file.stubs(:original_filename).returns("5k.png\n\n")
Paperclip.stubs(:run).returns('image/png')
@file.stubs(:content_type).returns("image/png\n\n")
@file.stubs(:to_tempfile).returns(@file)
@dummy.avatar = @file
object = stub
@dummy.avatar.stubs(:s3_object).returns(object)
object.expects(:presigned_url)
.with(:get, expires_in: 3600, response_content_type: "image/png")
@dummy.avatar.expiring_url
end
end
end
context "#expiring_url" do
before { @dummy = Dummy.new }
context "with no attachment" do
before { assert(!@dummy.avatar.exists?) }
it "returns the default URL" do
assert_equal(@dummy.avatar.url, @dummy.avatar.expiring_url)
end
it 'generates a url for a style when a file does not exist' do
assert_equal(@dummy.avatar.url(:thumb), @dummy.avatar.expiring_url(3600, :thumb))
end
end
it "generates the same url when using Times and Integer offsets" do
assert_equal @dummy.avatar.expiring_url(1234), @dummy.avatar.expiring_url(Time.now + 1234)
end
end
context "Generating a url with an expiration for each style" do
before do
rebuild_model (aws2_add_region).merge storage: :s3,
s3_credentials: {
production: { bucket: "prod_bucket" },
development: { bucket: "dev_bucket" }
},
s3_permissions: :private,
s3_host_alias: "something.something.com",
path: ":attachment/:style/:basename:dotextension",
url: ":s3_alias_url"
rails_env("production") do
@dummy = Dummy.new
@dummy.avatar = stringy_file
end
end
it "generates a url for the thumb" do
object = stub
@dummy.avatar.stubs(:s3_object).with(:thumb).returns(object)
object.expects(:presigned_url).with(:get, expires_in: 1800)
@dummy.avatar.expiring_url(1800, :thumb)
end
it "generates a url for the default style" do
object = stub
@dummy.avatar.stubs(:s3_object).with(:original).returns(object)
object.expects(:presigned_url).with(:get, expires_in: 1800)
@dummy.avatar.expiring_url(1800)
end
end
context "Parsing S3 credentials with a bucket in them" do
before do
rebuild_model (aws2_add_region).merge storage: :s3,
s3_credentials: {
production: { bucket: "prod_bucket" },
development: { bucket: "dev_bucket" }
}
@dummy = Dummy.new
end
it "gets the right bucket in production" do
rails_env("production") do
assert_equal "prod_bucket", @dummy.avatar.bucket_name
assert_equal "prod_bucket", @dummy.avatar.s3_bucket.name
end
end
it "gets the right bucket in development" do
rails_env("development") do
assert_equal "dev_bucket", @dummy.avatar.bucket_name
assert_equal "dev_bucket", @dummy.avatar.s3_bucket.name
end
end
end
# for aws-sdk-v2 the bucket.name is determined by the :s3_region
context "Parsing S3 credentials with a s3_host_name in them" do
before do
rebuild_model storage: :s3,
bucket: 'testing',
s3_credentials: {
production: {
s3_region: "world-end",
s3_host_name: "s3-world-end.amazonaws.com" },
development: {
s3_region: "ap-northeast-1",
s3_host_name: "s3-ap-northeast-1.amazonaws.com" },
test: {
s3_region: "" }
}
@dummy = Dummy.new
end
it "gets the right s3_host_name in production" do
rails_env("production") do
assert_match %r{^s3-world-end.amazonaws.com}, @dummy.avatar.s3_host_name
assert_match %r{^s3.world-end.amazonaws.com},
@dummy.avatar.s3_bucket.client.config.endpoint.host
end
end
it "gets the right s3_host_name in development" do
rails_env("development") do
assert_match %r{^s3-ap-northeast-1.amazonaws.com}, @dummy.avatar.s3_host_name
assert_match %r{^s3-ap-northeast-1.amazonaws.com},
@dummy.avatar.s3_bucket.client.config.endpoint.host
end
end
it "gets the right s3_host_name if the key does not exist" do
rails_env("test") do
assert_match %r{^s3.amazonaws.com}, @dummy.avatar.s3_host_name
assert_raises(Aws::Errors::MissingRegionError) do
@dummy.avatar.s3_bucket.client.config.endpoint.host
end
end
end
end
context "An attachment with S3 storage" do
before do
rebuild_model (aws2_add_region).merge storage: :s3,
bucket: "testing",
path: ":attachment/:style/:basename:dotextension",
s3_credentials: {
access_key_id: "12345",
secret_access_key: "54321"
}
end
it "is extended by the S3 module" do
assert Dummy.new.avatar.is_a?(Paperclip::Storage::S3)
end
it "won't be extended by the Filesystem module" do
assert ! Dummy.new.avatar.is_a?(Paperclip::Storage::Filesystem)
end
context "when assigned" do
before do
@file = File.new(fixture_file('5k.png'), 'rb')
@dummy = Dummy.new
@dummy.avatar = @file
@dummy.stubs(:new_record?).returns(false)
end
after { @file.close }
it "does not get a bucket to get a URL" do
@dummy.avatar.expects(:s3).never
@dummy.avatar.expects(:s3_bucket).never
assert_match %r{^//s3\.amazonaws\.com/testing/avatars/original/5k\.png}, @dummy.avatar.url
end
it "is rewound after flush_writes" do
@dummy.avatar.instance_eval "def after_flush_writes; end"
@dummy.avatar.stubs(:s3_object).returns(stub(upload_file: true))
files = @dummy.avatar.queued_for_write.values.each(&:read)
@dummy.save
assert files.none?(&:eof?), "Expect all the files to be rewound."
end
it "is removed after after_flush_writes" do
@dummy.avatar.stubs(:s3_object).returns(stub(upload_file: true))
paths = @dummy.avatar.queued_for_write.values.map(&:path)
@dummy.save
assert paths.none?{ |path| File.exist?(path) },
"Expect all the files to be deleted."
end
it "will retry to save again but back off on SlowDown" do
@dummy.avatar.stubs(:sleep)
Aws::S3::Object.any_instance.stubs(:upload_file).
raises(Aws::S3::Errors::SlowDown.new(stub,
stub(status: 503, body: "")))
expect {@dummy.save}.to raise_error(Aws::S3::Errors::SlowDown)
expect(@dummy.avatar).to have_received(:sleep).with(1)
expect(@dummy.avatar).to have_received(:sleep).with(2)
expect(@dummy.avatar).to have_received(:sleep).with(4)
expect(@dummy.avatar).to have_received(:sleep).with(8)
expect(@dummy.avatar).to have_received(:sleep).with(16)
end
context "and saved" do
before do
object = stub
@dummy.avatar.stubs(:s3_object).returns(object)
object.expects(:upload_file)
.with(anything, content_type: 'image/png', acl: :"public-read")
@dummy.save
end
it "succeeds" do
assert true
end
end
context "and saved without a bucket" do
before do
Aws::S3::Bucket.any_instance.expects(:create)
Aws::S3::Object.any_instance.stubs(:upload_file).
raises(Aws::S3::Errors::NoSuchBucket
.new(stub,
stub(status: 404, body: "<foo/>"))).then.returns(nil)
@dummy.save
end
it "succeeds" do
assert true
end
end
context "and remove" do
before do
Aws::S3::Object.any_instance.stubs(:exists?).returns(true)
Aws::S3::Object.any_instance.stubs(:delete)
@dummy.destroy
end
it "succeeds" do
assert true
end
end
context 'that the file were missing' do
before do
Aws::S3::Object.any_instance.stubs(:exists?)
.raises(Aws::S3::Errors::ServiceError.new("rspec stub raises",
"object exists?"))
end
it 'returns false on exists?' do
assert !@dummy.avatar.exists?
end
end
end
end
context "An attachment with S3 storage and bucket defined as a Proc" do
before do
rebuild_model (aws2_add_region).merge storage: :s3,
bucket: lambda { |attachment| "bucket_#{attachment.instance.other}" },
s3_credentials: {not: :important}
end
it "gets the right bucket name" do
assert "bucket_a", Dummy.new(other: 'a').avatar.bucket_name
assert "bucket_a", Dummy.new(other: 'a').avatar.s3_bucket.name
assert "bucket_b", Dummy.new(other: 'b').avatar.bucket_name
assert "bucket_b", Dummy.new(other: 'b').avatar.s3_bucket.name
end
end
context "An attachment with S3 storage and S3 credentials defined as a Proc" do
before do
rebuild_model (aws2_add_region).merge storage: :s3,
bucket: {not: :important},
s3_credentials: lambda { |attachment|
Hash['access_key_id' => "access#{attachment.instance.other}", 'secret_access_key' => "secret#{attachment.instance.other}"]
}
end
it "gets the right credentials" do
assert "access1234", Dummy.new(other: '1234').avatar.s3_credentials[:access_key_id]
assert "secret1234", Dummy.new(other: '1234').avatar.s3_credentials[:secret_access_key]
end
end
context "An attachment with S3 storage and S3 credentials with a :credential_provider" do
before do
class DummyCredentialProvider; end
rebuild_model (aws2_add_region).merge storage: :s3,
bucket: "testing",
s3_credentials: {
credentials: DummyCredentialProvider.new
}
@dummy = Dummy.new
end
it "sets the credential-provider" do
expect(@dummy.avatar.s3_bucket.client.config.credentials).to be_a DummyCredentialProvider
end
end
context "An attachment with S3 storage and S3 credentials in an unsupported manor" do
before do
rebuild_model (aws2_add_region).merge storage: :s3,
bucket: "testing", s3_credentials: ["unsupported"]
@dummy = Dummy.new
end
it "does not accept the credentials" do
assert_raises(ArgumentError) do
@dummy.avatar.s3_credentials
end
end
end
context "An attachment with S3 storage and S3 credentials not supplied" do
before do
rebuild_model (aws2_add_region).merge storage: :s3, bucket: "testing"
@dummy = Dummy.new
end
it "does not parse any credentials" do
assert_equal({}, @dummy.avatar.s3_credentials)