kt-paperclip 5.4.0 → 6.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (150) hide show
  1. checksums.yaml +4 -4
  2. data/.github/issue_template.md +3 -0
  3. data/.hound.yml +27 -32
  4. data/.travis.yml +23 -2
  5. data/Appraisals +17 -0
  6. data/Gemfile +9 -7
  7. data/NEWS +21 -0
  8. data/README.md +27 -37
  9. data/Rakefile +29 -21
  10. data/UPGRADING +3 -3
  11. data/features/basic_integration.feature +4 -0
  12. data/features/migration.feature +10 -51
  13. data/features/step_definitions/attachment_steps.rb +12 -12
  14. data/features/step_definitions/html_steps.rb +5 -5
  15. data/features/step_definitions/rails_steps.rb +29 -9
  16. data/features/step_definitions/s3_steps.rb +3 -3
  17. data/features/step_definitions/web_steps.rb +5 -6
  18. data/features/support/env.rb +4 -4
  19. data/features/support/fakeweb.rb +3 -5
  20. data/features/support/file_helpers.rb +2 -2
  21. data/features/support/paths.rb +4 -4
  22. data/features/support/rails.rb +7 -7
  23. data/features/support/selectors.rb +1 -1
  24. data/gemfiles/4.2.gemfile +7 -4
  25. data/gemfiles/5.0.gemfile +7 -4
  26. data/gemfiles/5.1.gemfile +20 -0
  27. data/gemfiles/5.2.gemfile +20 -0
  28. data/gemfiles/6.0.gemfile +20 -0
  29. data/lib/generators/paperclip/paperclip_generator.rb +6 -8
  30. data/lib/paperclip/attachment.rb +102 -104
  31. data/lib/paperclip/attachment_registry.rb +2 -2
  32. data/lib/paperclip/file_command_content_type_detector.rb +1 -3
  33. data/lib/paperclip/filename_cleaner.rb +0 -1
  34. data/lib/paperclip/geometry.rb +18 -19
  35. data/lib/paperclip/geometry_detector_factory.rb +13 -16
  36. data/lib/paperclip/geometry_parser_factory.rb +5 -5
  37. data/lib/paperclip/glue.rb +3 -3
  38. data/lib/paperclip/has_attached_file.rb +5 -4
  39. data/lib/paperclip/helpers.rb +3 -3
  40. data/lib/paperclip/interpolations.rb +42 -38
  41. data/lib/paperclip/io_adapters/abstract_adapter.rb +16 -14
  42. data/lib/paperclip/io_adapters/attachment_adapter.rb +12 -6
  43. data/lib/paperclip/io_adapters/data_uri_adapter.rb +1 -1
  44. data/lib/paperclip/io_adapters/file_adapter.rb +1 -3
  45. data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +3 -3
  46. data/lib/paperclip/io_adapters/identity_adapter.rb +1 -2
  47. data/lib/paperclip/io_adapters/registry.rb +1 -1
  48. data/lib/paperclip/io_adapters/stringio_adapter.rb +1 -1
  49. data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +6 -8
  50. data/lib/paperclip/io_adapters/uri_adapter.rb +9 -7
  51. data/lib/paperclip/logger.rb +1 -1
  52. data/lib/paperclip/matchers/have_attached_file_matcher.rb +4 -4
  53. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +19 -18
  54. data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +4 -4
  55. data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +11 -10
  56. data/lib/paperclip/matchers.rb +4 -4
  57. data/lib/paperclip/media_type_spoof_detector.rb +13 -13
  58. data/lib/paperclip/missing_attachment_styles.rb +11 -6
  59. data/lib/paperclip/processor.rb +13 -6
  60. data/lib/paperclip/processor_helpers.rb +3 -1
  61. data/lib/paperclip/rails_environment.rb +1 -5
  62. data/lib/paperclip/railtie.rb +5 -5
  63. data/lib/paperclip/schema.rb +18 -14
  64. data/lib/paperclip/storage/filesystem.rb +5 -7
  65. data/lib/paperclip/storage/fog.rb +36 -32
  66. data/lib/paperclip/storage/s3.rb +67 -75
  67. data/lib/paperclip/style.rb +3 -6
  68. data/lib/paperclip/tempfile.rb +4 -5
  69. data/lib/paperclip/tempfile_factory.rb +0 -1
  70. data/lib/paperclip/thumbnail.rb +11 -11
  71. data/lib/paperclip/url_generator.rb +5 -12
  72. data/lib/paperclip/validators/attachment_content_type_validator.rb +3 -3
  73. data/lib/paperclip/validators/attachment_file_name_validator.rb +5 -10
  74. data/lib/paperclip/validators/attachment_file_type_ignorance_validator.rb +1 -2
  75. data/lib/paperclip/validators/attachment_presence_validator.rb +3 -5
  76. data/lib/paperclip/validators/attachment_size_validator.rb +8 -8
  77. data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +3 -1
  78. data/lib/paperclip/validators.rb +12 -13
  79. data/lib/paperclip/version.rb +1 -3
  80. data/lib/paperclip.rb +49 -48
  81. data/lib/tasks/paperclip.rake +23 -24
  82. data/paperclip.gemspec +29 -33
  83. data/shoulda_macros/paperclip.rb +16 -16
  84. data/spec/paperclip/attachment_definitions_spec.rb +5 -5
  85. data/spec/paperclip/attachment_processing_spec.rb +22 -23
  86. data/spec/paperclip/attachment_registry_spec.rb +15 -15
  87. data/spec/paperclip/attachment_spec.rb +238 -196
  88. data/spec/paperclip/content_type_detector_spec.rb +11 -12
  89. data/spec/paperclip/file_command_content_type_detector_spec.rb +10 -10
  90. data/spec/paperclip/filename_cleaner_spec.rb +3 -4
  91. data/spec/paperclip/geometry_detector_spec.rb +7 -8
  92. data/spec/paperclip/geometry_parser_spec.rb +31 -31
  93. data/spec/paperclip/geometry_spec.rb +24 -24
  94. data/spec/paperclip/glue_spec.rb +3 -5
  95. data/spec/paperclip/has_attached_file_spec.rb +46 -126
  96. data/spec/paperclip/integration_spec.rb +111 -77
  97. data/spec/paperclip/interpolations_spec.rb +101 -93
  98. data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +41 -13
  99. data/spec/paperclip/io_adapters/attachment_adapter_spec.rb +8 -10
  100. data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +13 -14
  101. data/spec/paperclip/io_adapters/empty_string_adapter_spec.rb +4 -4
  102. data/spec/paperclip/io_adapters/file_adapter_spec.rb +12 -12
  103. data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +42 -26
  104. data/spec/paperclip/io_adapters/identity_adapter_spec.rb +1 -1
  105. data/spec/paperclip/io_adapters/nil_adapter_spec.rb +2 -2
  106. data/spec/paperclip/io_adapters/registry_spec.rb +4 -4
  107. data/spec/paperclip/io_adapters/stringio_adapter_spec.rb +10 -10
  108. data/spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb +6 -6
  109. data/spec/paperclip/io_adapters/uri_adapter_spec.rb +80 -31
  110. data/spec/paperclip/matchers/have_attached_file_matcher_spec.rb +3 -3
  111. data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +4 -5
  112. data/spec/paperclip/matchers/validate_attachment_presence_matcher_spec.rb +4 -4
  113. data/spec/paperclip/matchers/validate_attachment_size_matcher_spec.rb +4 -4
  114. data/spec/paperclip/media_type_spoof_detector_spec.rb +50 -24
  115. data/spec/paperclip/meta_class_spec.rb +3 -3
  116. data/spec/paperclip/paperclip_missing_attachment_styles_spec.rb +28 -24
  117. data/spec/paperclip/paperclip_spec.rb +15 -11
  118. data/spec/paperclip/plural_cache_spec.rb +8 -8
  119. data/spec/paperclip/processor_helpers_spec.rb +35 -35
  120. data/spec/paperclip/processor_spec.rb +8 -8
  121. data/spec/paperclip/rails_environment_spec.rb +7 -10
  122. data/spec/paperclip/rake_spec.rb +39 -39
  123. data/spec/paperclip/schema_spec.rb +57 -53
  124. data/spec/paperclip/storage/filesystem_spec.rb +6 -6
  125. data/spec/paperclip/storage/fog_spec.rb +76 -82
  126. data/spec/paperclip/storage/s3_live_spec.rb +22 -22
  127. data/spec/paperclip/storage/s3_spec.rb +585 -583
  128. data/spec/paperclip/style_spec.rb +67 -71
  129. data/spec/paperclip/tempfile_factory_spec.rb +5 -5
  130. data/spec/paperclip/thumbnail_spec.rb +68 -67
  131. data/spec/paperclip/url_generator_spec.rb +18 -29
  132. data/spec/paperclip/validators/attachment_content_type_validator_spec.rb +27 -27
  133. data/spec/paperclip/validators/attachment_file_name_validator_spec.rb +15 -16
  134. data/spec/paperclip/validators/attachment_presence_validator_spec.rb +5 -5
  135. data/spec/paperclip/validators/attachment_size_validator_spec.rb +21 -21
  136. data/spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb +9 -13
  137. data/spec/paperclip/validators_spec.rb +40 -40
  138. data/spec/spec_helper.rb +21 -23
  139. data/spec/support/assertions.rb +8 -6
  140. data/spec/support/fake_model.rb +1 -2
  141. data/spec/support/fake_rails.rb +1 -1
  142. data/spec/support/matchers/exist.rb +1 -1
  143. data/spec/support/matchers/have_column.rb +1 -1
  144. data/spec/support/mock_url_generator_builder.rb +2 -3
  145. data/spec/support/model_reconstruction.rb +16 -12
  146. data/spec/support/reporting.rb +1 -1
  147. data/spec/support/test_data.rb +2 -2
  148. metadata +49 -105
  149. data/lib/kt-paperclip.rb +0 -1
  150. data/spec/support/conditional_filter_helper.rb +0 -5
@@ -1,5 +1,5 @@
1
- require 'spec_helper'
2
- require 'aws-sdk'
1
+ require "spec_helper"
2
+ require "aws-sdk-s3"
3
3
 
4
4
  describe Paperclip::Storage::S3 do
5
5
  before do
@@ -7,39 +7,39 @@ describe Paperclip::Storage::S3 do
7
7
  end
8
8
 
9
9
  def aws2_add_region
10
- { s3_region: 'us-east-1' }
10
+ { s3_region: "us-east-1" }
11
11
  end
12
12
 
13
13
  context "Parsing S3 credentials" do
14
14
  before do
15
- @proxy_settings = {host: "127.0.0.1", port: 8888, user: "foo", password: "bar"}
16
- rebuild_model (aws2_add_region).merge storage: :s3,
17
- bucket: "testing",
18
- http_proxy: @proxy_settings,
19
- s3_credentials: {not: :important}
15
+ @proxy_settings = { host: "127.0.0.1", port: 8888, user: "foo", password: "bar" }
16
+ rebuild_model aws2_add_region.merge storage: :s3,
17
+ bucket: "testing",
18
+ http_proxy: @proxy_settings,
19
+ s3_credentials: { not: :important }
20
20
  @dummy = Dummy.new
21
21
  @avatar = @dummy.avatar
22
22
  end
23
23
 
24
24
  it "gets the correct credentials when RAILS_ENV is production" do
25
25
  rails_env("production") do
26
- assert_equal({key: "12345"},
27
- @avatar.parse_credentials('production' => {key: '12345'},
28
- development: {key: "54321"}))
26
+ assert_equal({ key: "12345" },
27
+ @avatar.parse_credentials("production" => { key: "12345" },
28
+ development: { key: "54321" }))
29
29
  end
30
30
  end
31
31
 
32
32
  it "gets the correct credentials when RAILS_ENV is development" do
33
33
  rails_env("development") do
34
- assert_equal({key: "54321"},
35
- @avatar.parse_credentials('production' => {key: '12345'},
36
- development: {key: "54321"}))
34
+ assert_equal({ key: "54321" },
35
+ @avatar.parse_credentials("production" => { key: "12345" },
36
+ development: { key: "54321" }))
37
37
  end
38
38
  end
39
39
 
40
40
  it "returns the argument if the key does not exist" do
41
41
  rails_env("not really an env") do
42
- assert_equal({test: "12345"}, @avatar.parse_credentials(test: "12345"))
42
+ assert_equal({ test: "12345" }, @avatar.parse_credentials(test: "12345"))
43
43
  end
44
44
  end
45
45
 
@@ -52,65 +52,57 @@ describe Paperclip::Storage::S3 do
52
52
  assert_equal(@proxy_settings[:password], @avatar.http_proxy_password)
53
53
  end
54
54
  end
55
-
56
55
  end
57
56
 
58
57
  context ":bucket option via :s3_credentials" do
59
-
60
58
  before do
61
- rebuild_model (aws2_add_region).merge storage: :s3,
62
- s3_credentials: {bucket: 'testing'}
59
+ rebuild_model aws2_add_region.merge storage: :s3,
60
+ s3_credentials: { bucket: "testing" }
63
61
  @dummy = Dummy.new
64
62
  end
65
63
 
66
64
  it "populates #bucket_name" do
67
- assert_equal @dummy.avatar.bucket_name, 'testing'
65
+ assert_equal @dummy.avatar.bucket_name, "testing"
68
66
  end
69
-
70
67
  end
71
68
 
72
69
  context ":bucket option" do
73
-
74
70
  before do
75
- rebuild_model (aws2_add_region).merge storage: :s3,
76
- bucket: "testing", s3_credentials: {}
71
+ rebuild_model aws2_add_region.merge storage: :s3,
72
+ bucket: "testing", s3_credentials: {}
77
73
  @dummy = Dummy.new
78
74
  end
79
75
 
80
76
  it "populates #bucket_name" do
81
- assert_equal @dummy.avatar.bucket_name, 'testing'
77
+ assert_equal @dummy.avatar.bucket_name, "testing"
82
78
  end
83
-
84
79
  end
85
80
 
86
81
  context "missing :bucket option" do
87
-
88
82
  before do
89
- rebuild_model (aws2_add_region).merge storage: :s3,
90
- http_proxy: @proxy_settings,
91
- s3_credentials: {not: :important}
83
+ rebuild_model aws2_add_region.merge storage: :s3,
84
+ http_proxy: @proxy_settings,
85
+ s3_credentials: { not: :important }
92
86
 
93
87
  @dummy = Dummy.new
94
88
  @dummy.avatar = stringy_file
95
-
96
89
  end
97
90
 
98
91
  it "raises an argument error" do
99
92
  expect { @dummy.save }.to raise_error(ArgumentError, /missing required :bucket option/)
100
93
  end
101
-
102
94
  end
103
95
 
104
96
  context "" do
105
97
  before do
106
- rebuild_model (aws2_add_region).merge storage: :s3,
107
- s3_credentials: {},
108
- bucket: "bucket",
109
- path: ":attachment/:basename:dotextension",
110
- url: ":s3_path_url"
98
+ rebuild_model aws2_add_region.merge storage: :s3,
99
+ s3_credentials: {},
100
+ bucket: "bucket",
101
+ path: ":attachment/:basename:dotextension",
102
+ url: ":s3_path_url"
111
103
  @dummy = Dummy.new
112
104
  @dummy.avatar = stringy_file
113
- @dummy.stubs(:new_record?).returns(false)
105
+ allow(@dummy).to receive(:new_record?).and_return(false)
114
106
  end
115
107
 
116
108
  it "returns a url based on an S3 path" do
@@ -130,8 +122,8 @@ describe Paperclip::Storage::S3 do
130
122
  ["http", :http, ""].each do |protocol|
131
123
  context "as #{protocol.inspect}" do
132
124
  before do
133
- rebuild_model (aws2_add_region).merge storage: :s3,
134
- s3_protocol: protocol
125
+ rebuild_model aws2_add_region.merge storage: :s3,
126
+ s3_protocol: protocol
135
127
  @dummy = Dummy.new
136
128
  end
137
129
 
@@ -144,14 +136,14 @@ describe Paperclip::Storage::S3 do
144
136
 
145
137
  context "s3_protocol: 'https'" do
146
138
  before do
147
- rebuild_model (aws2_add_region).merge storage: :s3,
148
- s3_credentials: {},
149
- s3_protocol: 'https',
150
- bucket: "bucket",
151
- path: ":attachment/:basename:dotextension"
139
+ rebuild_model aws2_add_region.merge storage: :s3,
140
+ s3_credentials: {},
141
+ s3_protocol: "https",
142
+ bucket: "bucket",
143
+ path: ":attachment/:basename:dotextension"
152
144
  @dummy = Dummy.new
153
145
  @dummy.avatar = stringy_file
154
- @dummy.stubs(:new_record?).returns(false)
146
+ allow(@dummy).to receive(:new_record?).and_return(false)
155
147
  end
156
148
 
157
149
  it "returns a url based on an S3 path" do
@@ -161,14 +153,14 @@ describe Paperclip::Storage::S3 do
161
153
 
162
154
  context "s3_protocol: ''" do
163
155
  before do
164
- rebuild_model (aws2_add_region).merge storage: :s3,
165
- s3_credentials: {},
166
- s3_protocol: '',
167
- bucket: "bucket",
168
- path: ":attachment/:basename:dotextension"
156
+ rebuild_model aws2_add_region.merge storage: :s3,
157
+ s3_credentials: {},
158
+ s3_protocol: "",
159
+ bucket: "bucket",
160
+ path: ":attachment/:basename:dotextension"
169
161
  @dummy = Dummy.new
170
162
  @dummy.avatar = stringy_file
171
- @dummy.stubs(:new_record?).returns(false)
163
+ allow(@dummy).to receive(:new_record?).and_return(false)
172
164
  end
173
165
 
174
166
  it "returns a protocol-relative URL" do
@@ -178,14 +170,14 @@ describe Paperclip::Storage::S3 do
178
170
 
179
171
  context "s3_protocol: :https" do
180
172
  before do
181
- rebuild_model (aws2_add_region).merge storage: :s3,
182
- s3_credentials: {},
183
- s3_protocol: :https,
184
- bucket: "bucket",
185
- path: ":attachment/:basename:dotextension"
173
+ rebuild_model aws2_add_region.merge storage: :s3,
174
+ s3_credentials: {},
175
+ s3_protocol: :https,
176
+ bucket: "bucket",
177
+ path: ":attachment/:basename:dotextension"
186
178
  @dummy = Dummy.new
187
179
  @dummy.avatar = stringy_file
188
- @dummy.stubs(:new_record?).returns(false)
180
+ allow(@dummy).to receive(:new_record?).and_return(false)
189
181
  end
190
182
 
191
183
  it "returns a url based on an S3 path" do
@@ -195,14 +187,14 @@ describe Paperclip::Storage::S3 do
195
187
 
196
188
  context "s3_protocol: ''" do
197
189
  before do
198
- rebuild_model (aws2_add_region).merge storage: :s3,
199
- s3_credentials: {},
200
- s3_protocol: '',
201
- bucket: "bucket",
202
- path: ":attachment/:basename:dotextension"
190
+ rebuild_model aws2_add_region.merge storage: :s3,
191
+ s3_credentials: {},
192
+ s3_protocol: "",
193
+ bucket: "bucket",
194
+ path: ":attachment/:basename:dotextension"
203
195
  @dummy = Dummy.new
204
196
  @dummy.avatar = stringy_file
205
- @dummy.stubs(:new_record?).returns(false)
197
+ allow(@dummy).to receive(:new_record?).and_return(false)
206
198
  end
207
199
 
208
200
  it "returns a url based on an S3 path" do
@@ -212,20 +204,20 @@ describe Paperclip::Storage::S3 do
212
204
 
213
205
  context "An attachment that uses S3 for storage and has the style in the path" do
214
206
  before do
215
- rebuild_model (aws2_add_region).merge storage: :s3,
216
- bucket: "testing",
217
- path: ":attachment/:style/:basename:dotextension",
218
- styles: {
219
- thumb: "80x80>"
220
- },
221
- s3_credentials: {
222
- 'access_key_id' => "12345",
223
- 'secret_access_key' => "54321"
224
- }
207
+ rebuild_model aws2_add_region.merge storage: :s3,
208
+ bucket: "testing",
209
+ path: ":attachment/:style/:basename:dotextension",
210
+ styles: {
211
+ thumb: "80x80>"
212
+ },
213
+ s3_credentials: {
214
+ "access_key_id" => "12345",
215
+ "secret_access_key" => "54321"
216
+ }
225
217
 
226
- @dummy = Dummy.new
227
- @dummy.avatar = stringy_file
228
- @avatar = @dummy.avatar
218
+ @dummy = Dummy.new
219
+ @dummy.avatar = stringy_file
220
+ @avatar = @dummy.avatar
229
221
  end
230
222
 
231
223
  it "uses an S3 object based on the correct path for the default style" do
@@ -237,18 +229,18 @@ describe Paperclip::Storage::S3 do
237
229
  end
238
230
  end
239
231
 
240
- # if using aws-sdk-v2, the s3_host_name will be defined by the s3_region
232
+ # the s3_host_name will be defined by the s3_region
241
233
  context "s3_host_name" do
242
234
  before do
243
235
  rebuild_model storage: :s3,
244
- s3_credentials: {},
245
- bucket: "bucket",
246
- path: ":attachment/:basename:dotextension",
247
- s3_host_name: "s3-ap-northeast-1.amazonaws.com",
248
- s3_region: "ap-northeast-1"
236
+ s3_credentials: {},
237
+ bucket: "bucket",
238
+ path: ":attachment/:basename:dotextension",
239
+ s3_host_name: "s3-ap-northeast-1.amazonaws.com",
240
+ s3_region: "ap-northeast-1"
249
241
  @dummy = Dummy.new
250
242
  @dummy.avatar = stringy_file
251
- @dummy.stubs(:new_record?).returns(false)
243
+ allow(@dummy).to receive(:new_record?).and_return(false)
252
244
  end
253
245
 
254
246
  it "returns a url based on an :s3_host_name path" do
@@ -257,23 +249,23 @@ describe Paperclip::Storage::S3 do
257
249
 
258
250
  it "uses the S3 bucket with the correct host name" do
259
251
  assert_equal "s3.ap-northeast-1.amazonaws.com",
260
- @dummy.avatar.s3_bucket.client.config.endpoint.host
252
+ @dummy.avatar.s3_bucket.client.config.endpoint.host
261
253
  end
262
254
  end
263
255
 
264
256
  context "dynamic s3_host_name" do
265
257
  before do
266
- rebuild_model (aws2_add_region).merge storage: :s3,
267
- s3_credentials: {},
268
- bucket: "bucket",
269
- path: ":attachment/:basename:dotextension",
270
- s3_host_name: lambda {|a| a.instance.value }
258
+ rebuild_model aws2_add_region.merge storage: :s3,
259
+ s3_credentials: {},
260
+ bucket: "bucket",
261
+ path: ":attachment/:basename:dotextension",
262
+ s3_host_name: lambda { |a| a.instance.value }
271
263
  @dummy = Dummy.new
272
264
  class << @dummy
273
265
  attr_accessor :value
274
266
  end
275
267
  @dummy.avatar = stringy_file
276
- @dummy.stubs(:new_record?).returns(false)
268
+ allow(@dummy).to receive(:new_record?).and_return(false)
277
269
  end
278
270
 
279
271
  it "uses s3_host_name as a proc if available" do
@@ -282,7 +274,7 @@ describe Paperclip::Storage::S3 do
282
274
  end
283
275
  end
284
276
 
285
- context "use_accelerate_endpoint", if: aws_accelerate_available? do
277
+ context "use_accelerate_endpoint" do
286
278
  context "defaults to false" do
287
279
  before do
288
280
  rebuild_model(
@@ -291,16 +283,16 @@ describe Paperclip::Storage::S3 do
291
283
  bucket: "bucket",
292
284
  path: ":attachment/:basename:dotextension",
293
285
  s3_host_name: "s3-ap-northeast-1.amazonaws.com",
294
- s3_region: "ap-northeast-1",
286
+ s3_region: "ap-northeast-1"
295
287
  )
296
288
  @dummy = Dummy.new
297
289
  @dummy.avatar = stringy_file
298
- @dummy.stubs(:new_record?).returns(false)
290
+ allow(@dummy).to receive(:new_record?).and_return(false)
299
291
  end
300
292
 
301
293
  it "returns a url based on an :s3_host_name path" do
302
294
  assert_match %r{^//s3-ap-northeast-1.amazonaws.com/bucket/avatars/data[^\.]},
303
- @dummy.avatar.url
295
+ @dummy.avatar.url
304
296
  end
305
297
 
306
298
  it "uses the S3 client with the use_accelerate_endpoint config is false" do
@@ -308,7 +300,7 @@ describe Paperclip::Storage::S3 do
308
300
  end
309
301
  end
310
302
 
311
- context "set to true", if: aws_accelerate_available? do
303
+ context "set to true" do
312
304
  before do
313
305
  rebuild_model(
314
306
  storage: :s3,
@@ -317,16 +309,16 @@ describe Paperclip::Storage::S3 do
317
309
  path: ":attachment/:basename:dotextension",
318
310
  s3_host_name: "s3-accelerate.amazonaws.com",
319
311
  s3_region: "ap-northeast-1",
320
- use_accelerate_endpoint: true,
312
+ use_accelerate_endpoint: true
321
313
  )
322
314
  @dummy = Dummy.new
323
315
  @dummy.avatar = stringy_file
324
- @dummy.stubs(:new_record?).returns(false)
316
+ allow(@dummy).to receive(:new_record?).and_return(false)
325
317
  end
326
318
 
327
319
  it "returns a url based on an :s3_host_name path" do
328
320
  assert_match %r{^//s3-accelerate.amazonaws.com/bucket/avatars/data[^\.]},
329
- @dummy.avatar.url
321
+ @dummy.avatar.url
330
322
  end
331
323
 
332
324
  it "uses the S3 client with the use_accelerate_endpoint config is true" do
@@ -337,27 +329,27 @@ describe Paperclip::Storage::S3 do
337
329
 
338
330
  context "An attachment that uses S3 for storage and has styles that return different file types" do
339
331
  before do
340
- rebuild_model (aws2_add_region).merge storage: :s3,
341
- styles: { large: ['500x500#', :jpg] },
342
- bucket: "bucket",
343
- path: ":attachment/:basename:dotextension",
344
- s3_credentials: {
345
- 'access_key_id' => "12345",
346
- 'secret_access_key' => "54321"
347
- }
348
-
349
- File.open(fixture_file('5k.png'), 'rb') do |file|
350
- @dummy = Dummy.new
351
- @dummy.avatar = file
352
- @dummy.stubs(:new_record?).returns(false)
353
- end
332
+ rebuild_model aws2_add_region.merge storage: :s3,
333
+ styles: { large: ["500x500#", :jpg] },
334
+ bucket: "bucket",
335
+ path: ":attachment/:basename:dotextension",
336
+ s3_credentials: {
337
+ "access_key_id" => "12345",
338
+ "secret_access_key" => "54321"
339
+ }
340
+
341
+ File.open(fixture_file("5k.png"), "rb") do |file|
342
+ @dummy = Dummy.new
343
+ @dummy.avatar = file
344
+ allow(@dummy).to receive(:new_record?).and_return(false)
345
+ end
354
346
  end
355
347
 
356
348
  it "returns a url containing the correct original file mime type" do
357
349
  assert_match /.+\/5k.png/, @dummy.avatar.url
358
350
  end
359
351
 
360
- it 'uses the correct key for the original file mime type' do
352
+ it "uses the correct key for the original file mime type" do
361
353
  assert_match /.+\/5k.png/, @dummy.avatar.s3_object.key
362
354
  end
363
355
 
@@ -372,42 +364,44 @@ describe Paperclip::Storage::S3 do
372
364
 
373
365
  context "An attachment that uses S3 for storage and has a proc for styles" do
374
366
  before do
375
- rebuild_model (aws2_add_region).merge storage: :s3,
376
- styles: lambda { |attachment| attachment.instance.counter
377
- {thumbnail: { geometry: "50x50#",
378
- s3_headers: {'Cache-Control' => 'max-age=31557600'}} }},
379
- bucket: "bucket",
380
- path: ":attachment/:style/:basename:dotextension",
381
- s3_credentials: {
382
- 'access_key_id' => "12345",
383
- 'secret_access_key' => "54321"
384
- }
367
+ rebuild_model aws2_add_region.merge storage: :s3,
368
+ styles: lambda { |attachment|
369
+ attachment.instance.counter
370
+ { thumbnail: { geometry: "50x50#",
371
+ s3_headers: { "Cache-Control" => "max-age=31557600" } } }
372
+ },
373
+ bucket: "bucket",
374
+ path: ":attachment/:style/:basename:dotextension",
375
+ s3_credentials: {
376
+ "access_key_id" => "12345",
377
+ "secret_access_key" => "54321"
378
+ }
385
379
 
386
- @file = File.new(fixture_file('5k.png'), 'rb')
380
+ @file = File.new(fixture_file("5k.png"), "rb")
387
381
 
388
- Dummy.class_eval do
389
- def counter
390
- @counter ||= 0
391
- @counter += 1
392
- @counter
393
- end
382
+ Dummy.class_eval do
383
+ def counter
384
+ @counter ||= 0
385
+ @counter += 1
386
+ @counter
394
387
  end
388
+ end
395
389
 
396
- @dummy = Dummy.new
397
- @dummy.avatar = @file
390
+ @dummy = Dummy.new
391
+ @dummy.avatar = @file
398
392
 
399
- object = stub
400
- @dummy.avatar.stubs(:s3_object).with(:original).returns(object)
401
- @dummy.avatar.stubs(:s3_object).with(:thumbnail).returns(object)
402
-
403
- object.expects(:upload_file)
404
- .with(anything, content_type: 'image/png',
405
- acl: :"public-read")
406
- object.expects(:upload_file)
407
- .with(anything, content_type: 'image/png',
408
- acl: :"public-read",
409
- cache_control: 'max-age=31557600')
410
- @dummy.save
393
+ object = double
394
+ allow(@dummy.avatar).to receive(:s3_object).with(:original).and_return(object)
395
+ allow(@dummy.avatar).to receive(:s3_object).with(:thumbnail).and_return(object)
396
+
397
+ expect(object).to receive(:upload_file).
398
+ with(anything, content_type: "image/png",
399
+ acl: :"public-read")
400
+ expect(object).to receive(:upload_file).
401
+ with(anything, content_type: "image/png",
402
+ acl: :"public-read",
403
+ cache_control: "max-age=31557600")
404
+ @dummy.save
411
405
  end
412
406
 
413
407
  after { @file.close }
@@ -420,13 +414,14 @@ describe Paperclip::Storage::S3 do
420
414
  context "An attachment that uses S3 for storage and has styles" do
421
415
  before do
422
416
  rebuild_model(
423
- (aws2_add_region).merge(
417
+ aws2_add_region.merge(
424
418
  storage: :s3,
425
419
  styles: { thumb: ["90x90#", :jpg] },
426
420
  bucket: "bucket",
427
421
  s3_credentials: {
428
422
  "access_key_id" => "12345",
429
- "secret_access_key" => "54321" }
423
+ "secret_access_key" => "54321"
424
+ }
430
425
  )
431
426
  )
432
427
 
@@ -438,31 +433,36 @@ describe Paperclip::Storage::S3 do
438
433
 
439
434
  context "reprocess" do
440
435
  before do
441
- @object = stub
442
- @dummy.avatar.stubs(:s3_object).with(:original).returns(@object)
443
- @dummy.avatar.stubs(:s3_object).with(:thumb).returns(@object)
444
- @object.stubs(:get).yields(@file.read)
445
- @object.stubs(:exists?).returns(true)
436
+ @object = double
437
+ allow(@dummy.avatar).to receive(:s3_object).with(:original).and_return(@object)
438
+ allow(@dummy.avatar).to receive(:s3_object).with(:thumb).and_return(@object)
439
+ allow(@object).to receive(:get).and_yield(@file.read)
440
+ allow(@object).to receive(:exists?).and_return(true)
441
+ allow(@object).to receive(:download_file).with(anything)
446
442
  end
447
443
 
448
444
  it "uploads original" do
449
- @object.expects(:upload_file).with(
445
+ expect(@object).to receive(:upload_file).with(
450
446
  anything,
451
447
  content_type: "image/png",
452
- acl: :"public-read").returns(true)
453
- @object.expects(:upload_file).with(
448
+ acl: :"public-read"
449
+ ).and_return(true)
450
+ @dummy.avatar.reprocess!
451
+ expect(@object).to receive(:upload_file).with(
454
452
  anything,
455
- content_type: "image/jpeg",
456
- acl: :"public-read").returns(true)
453
+ content_type: "image/png",
454
+ acl: :"public-read"
455
+ ).and_return(true)
457
456
  @dummy.avatar.reprocess!
458
457
  end
459
458
 
460
459
  it "doesn't upload original" do
461
- @object.expects(:upload_file).with(
460
+ expect(@object).to receive(:upload_file).with(
462
461
  anything,
463
- content_type: "image/jpeg",
464
- acl: :"public-read").returns(true)
465
- @dummy.avatar.reprocess!(:thumb)
462
+ content_type: "image/png",
463
+ acl: :"public-read"
464
+ ).and_return(true)
465
+ @dummy.avatar.reprocess!
466
466
  end
467
467
  end
468
468
 
@@ -472,17 +472,17 @@ describe Paperclip::Storage::S3 do
472
472
  context "An attachment that uses S3 for storage and has spaces in file name" do
473
473
  before do
474
474
  rebuild_model(
475
- (aws2_add_region).merge storage: :s3,
476
- styles: { large: ["500x500#", :jpg] },
477
- bucket: "bucket",
478
- s3_credentials: { "access_key_id" => "12345",
479
- "secret_access_key" => "54321" }
480
- )
475
+ aws2_add_region.merge(storage: :s3,
476
+ styles: { large: ["500x500#", :jpg] },
477
+ bucket: "bucket",
478
+ s3_credentials: { "access_key_id" => "12345",
479
+ "secret_access_key" => "54321" })
480
+ )
481
481
 
482
482
  File.open(fixture_file("spaced file.png"), "rb") do |file|
483
483
  @dummy = Dummy.new
484
484
  @dummy.avatar = file
485
- @dummy.stubs(:new_record?).returns(false)
485
+ allow(@dummy).to receive(:new_record?).and_return(false)
486
486
  end
487
487
  end
488
488
 
@@ -497,13 +497,13 @@ describe Paperclip::Storage::S3 do
497
497
 
498
498
  context "An attachment that uses S3 for storage and has a question mark in file name" do
499
499
  before do
500
- rebuild_model (aws2_add_region).merge storage: :s3,
501
- styles: { large: ['500x500#', :jpg] },
502
- bucket: "bucket",
503
- s3_credentials: {
504
- 'access_key_id' => "12345",
505
- 'secret_access_key' => "54321"
506
- }
500
+ rebuild_model aws2_add_region.merge storage: :s3,
501
+ styles: { large: ["500x500#", :jpg] },
502
+ bucket: "bucket",
503
+ s3_credentials: {
504
+ "access_key_id" => "12345",
505
+ "secret_access_key" => "54321"
506
+ }
507
507
 
508
508
  stringio = stringy_file
509
509
  class << stringio
@@ -515,7 +515,7 @@ describe Paperclip::Storage::S3 do
515
515
  @dummy = Dummy.new
516
516
  @dummy.avatar = file
517
517
  @dummy.save
518
- @dummy.stubs(:new_record?).returns(false)
518
+ allow(@dummy).to receive(:new_record?).and_return(false)
519
519
  end
520
520
 
521
521
  it "returns a replaced version for path" do
@@ -529,14 +529,14 @@ describe Paperclip::Storage::S3 do
529
529
 
530
530
  context "" do
531
531
  before do
532
- rebuild_model (aws2_add_region).merge storage: :s3,
533
- s3_credentials: {},
534
- bucket: "bucket",
535
- path: ":attachment/:basename:dotextension",
536
- url: ":s3_domain_url"
532
+ rebuild_model aws2_add_region.merge storage: :s3,
533
+ s3_credentials: {},
534
+ bucket: "bucket",
535
+ path: ":attachment/:basename:dotextension",
536
+ url: ":s3_domain_url"
537
537
  @dummy = Dummy.new
538
538
  @dummy.avatar = stringy_file
539
- @dummy.stubs(:new_record?).returns(false)
539
+ allow(@dummy).to receive(:new_record?).and_return(false)
540
540
  end
541
541
 
542
542
  it "returns a url based on an S3 subdomain" do
@@ -547,19 +547,19 @@ describe Paperclip::Storage::S3 do
547
547
  context "" do
548
548
  before do
549
549
  rebuild_model(
550
- (aws2_add_region).merge storage: :s3,
551
- s3_credentials: {
552
- production: { bucket: "prod_bucket" },
553
- development: { bucket: "dev_bucket" }
554
- },
555
- bucket: "bucket",
556
- s3_host_alias: "something.something.com",
557
- path: ":attachment/:basename:dotextension",
558
- url: ":s3_alias_url"
559
- )
550
+ aws2_add_region.merge(storage: :s3,
551
+ s3_credentials: {
552
+ production: { bucket: "prod_bucket" },
553
+ development: { bucket: "dev_bucket" }
554
+ },
555
+ bucket: "bucket",
556
+ s3_host_alias: "something.something.com",
557
+ path: ":attachment/:basename:dotextension",
558
+ url: ":s3_alias_url")
559
+ )
560
560
  @dummy = Dummy.new
561
561
  @dummy.avatar = stringy_file
562
- @dummy.stubs(:new_record?).returns(false)
562
+ allow(@dummy).to receive(:new_record?).and_return(false)
563
563
  end
564
564
 
565
565
  it "returns a url based on the host_alias" do
@@ -580,12 +580,12 @@ describe Paperclip::Storage::S3 do
580
580
  s3_host_alias: "something.something.com",
581
581
  s3_prefixes_in_alias: 2,
582
582
  path: "prefix1/prefix2/:attachment/:basename:dotextension",
583
- url: ":s3_alias_url",
583
+ url: ":s3_alias_url"
584
584
  )
585
585
  )
586
586
  @dummy = Dummy.new
587
587
  @dummy.avatar = stringy_file
588
- @dummy.stubs(:new_record?).returns(false)
588
+ allow(@dummy).to receive(:new_record?).and_return(false)
589
589
  end
590
590
 
591
591
  it "returns a url with the prefixes removed" do
@@ -596,11 +596,11 @@ describe Paperclip::Storage::S3 do
596
596
 
597
597
  context "generating a url with a proc as the host alias" do
598
598
  before do
599
- rebuild_model (aws2_add_region).merge storage: :s3,
600
- s3_credentials: { bucket: "prod_bucket" },
601
- s3_host_alias: Proc.new{|atch| "cdn#{atch.instance.counter % 4}.example.com"},
602
- path: ":attachment/:basename:dotextension",
603
- url: ":s3_alias_url"
599
+ rebuild_model aws2_add_region.merge storage: :s3,
600
+ s3_credentials: { bucket: "prod_bucket" },
601
+ s3_host_alias: Proc.new { |atch| "cdn#{atch.instance.counter % 4}.example.com" },
602
+ path: ":attachment/:basename:dotextension",
603
+ url: ":s3_alias_url"
604
604
  Dummy.class_eval do
605
605
  def counter
606
606
  @counter ||= 0
@@ -610,7 +610,7 @@ describe Paperclip::Storage::S3 do
610
610
  end
611
611
  @dummy = Dummy.new
612
612
  @dummy.avatar = stringy_file
613
- @dummy.stubs(:new_record?).returns(false)
613
+ allow(@dummy).to receive(:new_record?).and_return(false)
614
614
  end
615
615
 
616
616
  it "returns a url based on the host_alias" do
@@ -621,30 +621,28 @@ describe Paperclip::Storage::S3 do
621
621
  it "still returns the bucket name" do
622
622
  assert_equal "prod_bucket", @dummy.avatar.bucket_name
623
623
  end
624
-
625
624
  end
626
625
 
627
626
  context "" do
628
627
  before do
629
- rebuild_model (aws2_add_region).merge storage: :s3,
630
- s3_credentials: {},
631
- bucket: "bucket",
632
- path: ":attachment/:basename:dotextension",
633
- url: ":asset_host"
628
+ rebuild_model aws2_add_region.merge storage: :s3,
629
+ s3_credentials: {},
630
+ bucket: "bucket",
631
+ path: ":attachment/:basename:dotextension",
632
+ url: ":asset_host"
634
633
  @dummy = Dummy.new
635
634
  @dummy.avatar = stringy_file
636
- @dummy.stubs(:new_record?).returns(false)
635
+ allow(@dummy).to receive(:new_record?).and_return(false)
637
636
  end
638
637
 
639
638
  it "returns a relative URL for Rails to calculate assets host" do
640
639
  assert_match %r{^avatars/data[^\.]}, @dummy.avatar.url
641
640
  end
642
-
643
641
  end
644
642
 
645
643
  context "Generating a secure url with an expiration" do
646
644
  before do
647
- @build_model_with_options = lambda {|options|
645
+ @build_model_with_options = lambda { |options|
648
646
  base_options = {
649
647
  storage: :s3,
650
648
  s3_credentials: {
@@ -657,7 +655,7 @@ describe Paperclip::Storage::S3 do
657
655
  url: ":s3_alias_url"
658
656
  }
659
657
 
660
- rebuild_model (aws2_add_region).merge base_options.merge(options)
658
+ rebuild_model aws2_add_region.merge base_options.merge(options)
661
659
  }
662
660
  end
663
661
 
@@ -668,10 +666,10 @@ describe Paperclip::Storage::S3 do
668
666
  @dummy = Dummy.new
669
667
  @dummy.avatar = stringy_file
670
668
 
671
- object = stub
672
- @dummy.avatar.stubs(:s3_object).returns(object)
669
+ object = double
670
+ allow(@dummy.avatar).to receive(:s3_object).and_return(object)
673
671
 
674
- object.expects(:presigned_url).with(:get, expires_in: 3600)
672
+ expect(object).to receive(:presigned_url).with(:get, expires_in: 3600)
675
673
  @dummy.avatar.expiring_url
676
674
  end
677
675
  end
@@ -683,33 +681,33 @@ describe Paperclip::Storage::S3 do
683
681
  @dummy = Dummy.new
684
682
  @dummy.avatar = stringy_file
685
683
 
686
- object = stub
687
- @dummy.avatar.stubs(:s3_object).returns(object)
688
- object.expects(:presigned_url)
689
- .with(:get, expires_in: 3600,
690
- response_content_disposition: "inline")
684
+ object = double
685
+ allow(@dummy.avatar).to receive(:s3_object).and_return(object)
686
+ expect(object).to receive(:presigned_url).
687
+ with(:get, expires_in: 3600,
688
+ response_content_disposition: "inline")
691
689
  @dummy.avatar.expiring_url
692
690
  end
693
691
  end
694
692
 
695
693
  it "allows overriding s3_object options with a proc" do
696
- @build_model_with_options[s3_url_options: lambda {|attachment| { response_content_type: attachment.avatar_content_type } }]
694
+ @build_model_with_options[s3_url_options: lambda { |attachment| { response_content_type: attachment.avatar_content_type } }]
697
695
 
698
696
  rails_env("production") do
699
697
  @dummy = Dummy.new
700
698
 
701
699
  @file = stringy_file
702
- @file.stubs(:original_filename).returns("5k.png\n\n")
703
- Paperclip.stubs(:run).returns('image/png')
704
- @file.stubs(:content_type).returns("image/png\n\n")
705
- @file.stubs(:to_tempfile).returns(@file)
700
+ allow(@file).to receive(:original_filename).and_return("5k.png\n\n")
701
+ allow(Paperclip).to receive(:run).and_return("image/png")
702
+ allow(@file).to receive(:content_type).and_return("image/png\n\n")
703
+ allow(@file).to receive(:to_tempfile).and_return(@file)
706
704
 
707
705
  @dummy.avatar = @file
708
706
 
709
- object = stub
710
- @dummy.avatar.stubs(:s3_object).returns(object)
711
- object.expects(:presigned_url)
712
- .with(:get, expires_in: 3600, response_content_type: "image/png")
707
+ object = double
708
+ allow(@dummy.avatar).to receive(:s3_object).and_return(object)
709
+ expect(object).to receive(:presigned_url).
710
+ with(:get, expires_in: 3600, response_content_type: "image/png")
713
711
  @dummy.avatar.expiring_url
714
712
  end
715
713
  end
@@ -725,7 +723,7 @@ describe Paperclip::Storage::S3 do
725
723
  assert_equal(@dummy.avatar.url, @dummy.avatar.expiring_url)
726
724
  end
727
725
 
728
- it 'generates a url for a style when a file does not exist' do
726
+ it "generates a url for a style when a file does not exist" do
729
727
  assert_equal(@dummy.avatar.url(:thumb), @dummy.avatar.expiring_url(3600, :thumb))
730
728
  end
731
729
  end
@@ -737,15 +735,15 @@ describe Paperclip::Storage::S3 do
737
735
 
738
736
  context "Generating a url with an expiration for each style" do
739
737
  before do
740
- rebuild_model (aws2_add_region).merge storage: :s3,
741
- s3_credentials: {
742
- production: { bucket: "prod_bucket" },
743
- development: { bucket: "dev_bucket" }
744
- },
745
- s3_permissions: :private,
746
- s3_host_alias: "something.something.com",
747
- path: ":attachment/:style/:basename:dotextension",
748
- url: ":s3_alias_url"
738
+ rebuild_model aws2_add_region.merge storage: :s3,
739
+ s3_credentials: {
740
+ production: { bucket: "prod_bucket" },
741
+ development: { bucket: "dev_bucket" }
742
+ },
743
+ s3_permissions: :private,
744
+ s3_host_alias: "something.something.com",
745
+ path: ":attachment/:style/:basename:dotextension",
746
+ url: ":s3_alias_url"
749
747
 
750
748
  rails_env("production") do
751
749
  @dummy = Dummy.new
@@ -754,27 +752,27 @@ describe Paperclip::Storage::S3 do
754
752
  end
755
753
 
756
754
  it "generates a url for the thumb" do
757
- object = stub
758
- @dummy.avatar.stubs(:s3_object).with(:thumb).returns(object)
759
- object.expects(:presigned_url).with(:get, expires_in: 1800)
755
+ object = double
756
+ allow(@dummy.avatar).to receive(:s3_object).with(:thumb).and_return(object)
757
+ expect(object).to receive(:presigned_url).with(:get, expires_in: 1800)
760
758
  @dummy.avatar.expiring_url(1800, :thumb)
761
759
  end
762
760
 
763
761
  it "generates a url for the default style" do
764
- object = stub
765
- @dummy.avatar.stubs(:s3_object).with(:original).returns(object)
766
- object.expects(:presigned_url).with(:get, expires_in: 1800)
762
+ object = double
763
+ allow(@dummy.avatar).to receive(:s3_object).with(:original).and_return(object)
764
+ expect(object).to receive(:presigned_url).with(:get, expires_in: 1800)
767
765
  @dummy.avatar.expiring_url(1800)
768
766
  end
769
767
  end
770
768
 
771
769
  context "Parsing S3 credentials with a bucket in them" do
772
770
  before do
773
- rebuild_model (aws2_add_region).merge storage: :s3,
774
- s3_credentials: {
775
- production: { bucket: "prod_bucket" },
776
- development: { bucket: "dev_bucket" }
777
- }
771
+ rebuild_model aws2_add_region.merge storage: :s3,
772
+ s3_credentials: {
773
+ production: { bucket: "prod_bucket" },
774
+ development: { bucket: "dev_bucket" }
775
+ }
778
776
  @dummy = Dummy.new
779
777
  end
780
778
 
@@ -793,21 +791,24 @@ describe Paperclip::Storage::S3 do
793
791
  end
794
792
  end
795
793
 
796
- # for aws-sdk-v2 the bucket.name is determined by the :s3_region
794
+ # the bucket.name is determined by the :s3_region
797
795
  context "Parsing S3 credentials with a s3_host_name in them" do
798
796
  before do
799
797
  rebuild_model storage: :s3,
800
- bucket: 'testing',
801
- s3_credentials: {
802
- production: {
803
- s3_region: "world-end",
804
- s3_host_name: "s3-world-end.amazonaws.com" },
805
- development: {
806
- s3_region: "ap-northeast-1",
807
- s3_host_name: "s3-ap-northeast-1.amazonaws.com" },
808
- test: {
809
- s3_region: "" }
810
- }
798
+ bucket: "testing",
799
+ s3_credentials: {
800
+ production: {
801
+ s3_region: "world-end",
802
+ s3_host_name: "s3-world-end.amazonaws.com"
803
+ },
804
+ development: {
805
+ s3_region: "ap-northeast-1",
806
+ s3_host_name: "s3-ap-northeast-1.amazonaws.com"
807
+ },
808
+ test: {
809
+ s3_region: ""
810
+ }
811
+ }
811
812
  @dummy = Dummy.new
812
813
  end
813
814
 
@@ -815,16 +816,16 @@ describe Paperclip::Storage::S3 do
815
816
  rails_env("production") do
816
817
  assert_match %r{^s3-world-end.amazonaws.com}, @dummy.avatar.s3_host_name
817
818
  assert_match %r{^s3.world-end.amazonaws.com},
818
- @dummy.avatar.s3_bucket.client.config.endpoint.host
819
+ @dummy.avatar.s3_bucket.client.config.endpoint.host
819
820
  end
820
821
  end
821
822
 
822
823
  it "gets the right s3_host_name in development" do
823
824
  rails_env("development") do
824
825
  assert_match %r{^s3.ap-northeast-1.amazonaws.com},
825
- @dummy.avatar.s3_host_name
826
+ @dummy.avatar.s3_host_name
826
827
  assert_match %r{^s3.ap-northeast-1.amazonaws.com},
827
- @dummy.avatar.s3_bucket.client.config.endpoint.host
828
+ @dummy.avatar.s3_bucket.client.config.endpoint.host
828
829
  end
829
830
  end
830
831
 
@@ -840,13 +841,13 @@ describe Paperclip::Storage::S3 do
840
841
 
841
842
  context "An attachment with S3 storage" do
842
843
  before do
843
- rebuild_model (aws2_add_region).merge storage: :s3,
844
- bucket: "testing",
845
- path: ":attachment/:style/:basename:dotextension",
846
- s3_credentials: {
847
- access_key_id: "12345",
848
- secret_access_key: "54321"
849
- }
844
+ rebuild_model aws2_add_region.merge storage: :s3,
845
+ bucket: "testing",
846
+ path: ":attachment/:style/:basename:dotextension",
847
+ s3_credentials: {
848
+ access_key_id: "12345",
849
+ secret_access_key: "54321"
850
+ }
850
851
  end
851
852
 
852
853
  it "is extended by the S3 module" do
@@ -854,47 +855,47 @@ describe Paperclip::Storage::S3 do
854
855
  end
855
856
 
856
857
  it "won't be extended by the Filesystem module" do
857
- assert ! Dummy.new.avatar.is_a?(Paperclip::Storage::Filesystem)
858
+ assert !Dummy.new.avatar.is_a?(Paperclip::Storage::Filesystem)
858
859
  end
859
860
 
860
861
  context "when assigned" do
861
862
  before do
862
- @file = File.new(fixture_file('5k.png'), 'rb')
863
+ @file = File.new(fixture_file("5k.png"), "rb")
863
864
  @dummy = Dummy.new
864
865
  @dummy.avatar = @file
865
- @dummy.stubs(:new_record?).returns(false)
866
+ allow(@dummy).to receive(:new_record?).and_return(false)
866
867
  end
867
868
 
868
869
  after { @file.close }
869
870
 
870
871
  it "does not get a bucket to get a URL" do
871
- @dummy.avatar.expects(:s3).never
872
- @dummy.avatar.expects(:s3_bucket).never
872
+ expect(@dummy.avatar).to_not receive(:s3)
873
+ expect(@dummy.avatar).to_not receive(:s3_bucket)
873
874
  assert_match %r{^//s3\.amazonaws\.com/testing/avatars/original/5k\.png}, @dummy.avatar.url
874
875
  end
875
876
 
876
877
  it "is rewound after flush_writes" do
877
878
  @dummy.avatar.instance_eval "def after_flush_writes; end"
878
- @dummy.avatar.stubs(:s3_object).returns(stub(upload_file: true))
879
+ allow(@dummy.avatar).to receive(:s3_object).and_return(double(upload_file: true))
879
880
  files = @dummy.avatar.queued_for_write.values.each(&:read)
880
881
  @dummy.save
881
882
  assert files.none?(&:eof?), "Expect all the files to be rewound."
882
883
  end
883
884
 
884
885
  it "is removed after after_flush_writes" do
885
- @dummy.avatar.stubs(:s3_object).returns(stub(upload_file: true))
886
+ allow(@dummy.avatar).to receive(:s3_object).and_return(double(upload_file: true))
886
887
  paths = @dummy.avatar.queued_for_write.values.map(&:path)
887
888
  @dummy.save
888
- assert paths.none?{ |path| File.exist?(path) },
889
- "Expect all the files to be deleted."
889
+ assert paths.none? { |path| File.exist?(path) },
890
+ "Expect all the files to be deleted."
890
891
  end
891
892
 
892
893
  it "will retry to save again but back off on SlowDown" do
893
- @dummy.avatar.stubs(:sleep)
894
- Aws::S3::Object.any_instance.stubs(:upload_file).
895
- raises(Aws::S3::Errors::SlowDown.new(stub,
896
- stub(status: 503, body: "")))
897
- expect {@dummy.save}.to raise_error(Aws::S3::Errors::SlowDown)
894
+ allow(@dummy.avatar).to receive(:sleep)
895
+ allow_any_instance_of(Aws::S3::Object).to receive(:upload_file).
896
+ and_raise(Aws::S3::Errors::SlowDown.new(spy,
897
+ spy(status: 503, body: "")))
898
+ expect { @dummy.save }.to raise_error(Aws::S3::Errors::SlowDown)
898
899
  expect(@dummy.avatar).to have_received(:sleep).with(1)
899
900
  expect(@dummy.avatar).to have_received(:sleep).with(2)
900
901
  expect(@dummy.avatar).to have_received(:sleep).with(4)
@@ -904,10 +905,10 @@ describe Paperclip::Storage::S3 do
904
905
 
905
906
  context "and saved" do
906
907
  before do
907
- object = stub
908
- @dummy.avatar.stubs(:s3_object).returns(object)
909
- object.expects(:upload_file)
910
- .with(anything, content_type: 'image/png', acl: :"public-read")
908
+ object = double
909
+ allow(@dummy.avatar).to receive(:s3_object).and_return(object)
910
+ expect(object).to receive(:upload_file).
911
+ with(anything, content_type: "image/png", acl: :"public-read")
911
912
  @dummy.save
912
913
  end
913
914
 
@@ -918,11 +919,9 @@ describe Paperclip::Storage::S3 do
918
919
 
919
920
  context "and saved without a bucket" do
920
921
  before do
921
- Aws::S3::Bucket.any_instance.expects(:create)
922
- Aws::S3::Object.any_instance.stubs(:upload_file).
923
- raises(Aws::S3::Errors::NoSuchBucket
924
- .new(stub,
925
- stub(status: 404, body: "<foo/>"))).then.returns(nil)
922
+ allow_any_instance_of(Aws::S3::Object).to receive(:upload_file).
923
+ and_raise(Aws::S3::Errors::NoSuchBucket.new(double, double(status: 404, body: "<foo/>", empty?: false)))
924
+ allow_any_instance_of(Aws::S3::Object).to receive(:upload_file).and_return(nil)
926
925
  @dummy.save
927
926
  end
928
927
 
@@ -933,8 +932,8 @@ describe Paperclip::Storage::S3 do
933
932
 
934
933
  context "and remove" do
935
934
  before do
936
- Aws::S3::Object.any_instance.stubs(:exists?).returns(true)
937
- Aws::S3::Object.any_instance.stubs(:delete)
935
+ allow_any_instance_of(Aws::S3::Object).to receive(:exists?).and_return(true)
936
+ allow_any_instance_of(Aws::S3::Object).to receive(:delete)
938
937
  @dummy.destroy
939
938
  end
940
939
 
@@ -943,14 +942,14 @@ describe Paperclip::Storage::S3 do
943
942
  end
944
943
  end
945
944
 
946
- context 'that the file were missing' do
945
+ context "that the file were missing" do
947
946
  before do
948
- Aws::S3::Object.any_instance.stubs(:exists?)
949
- .raises(Aws::S3::Errors::ServiceError.new("rspec stub raises",
950
- "object exists?"))
947
+ allow_any_instance_of(Aws::S3::Object).to receive(:exists?).
948
+ and_raise(Aws::S3::Errors::ServiceError.new("rspec stub raises",
949
+ "object exists?"))
951
950
  end
952
951
 
953
- it 'returns false on exists?' do
952
+ it "returns false on exists?" do
954
953
  assert !@dummy.avatar.exists?
955
954
  end
956
955
  end
@@ -959,31 +958,31 @@ describe Paperclip::Storage::S3 do
959
958
 
960
959
  context "An attachment with S3 storage and bucket defined as a Proc" do
961
960
  before do
962
- rebuild_model (aws2_add_region).merge storage: :s3,
963
- bucket: lambda { |attachment| "bucket_#{attachment.instance.other}" },
964
- s3_credentials: {not: :important}
961
+ rebuild_model aws2_add_region.merge storage: :s3,
962
+ bucket: lambda { |attachment| "bucket_#{attachment.instance.other}" },
963
+ s3_credentials: { not: :important }
965
964
  end
966
965
 
967
966
  it "gets the right bucket name" do
968
- assert "bucket_a", Dummy.new(other: 'a').avatar.bucket_name
969
- assert "bucket_a", Dummy.new(other: 'a').avatar.s3_bucket.name
970
- assert "bucket_b", Dummy.new(other: 'b').avatar.bucket_name
971
- assert "bucket_b", Dummy.new(other: 'b').avatar.s3_bucket.name
967
+ assert "bucket_a", Dummy.new(other: "a").avatar.bucket_name
968
+ assert "bucket_a", Dummy.new(other: "a").avatar.s3_bucket.name
969
+ assert "bucket_b", Dummy.new(other: "b").avatar.bucket_name
970
+ assert "bucket_b", Dummy.new(other: "b").avatar.s3_bucket.name
972
971
  end
973
972
  end
974
973
 
975
974
  context "An attachment with S3 storage and S3 credentials defined as a Proc" do
976
975
  before do
977
- rebuild_model (aws2_add_region).merge storage: :s3,
978
- bucket: {not: :important},
979
- s3_credentials: lambda { |attachment|
980
- Hash['access_key_id' => "access#{attachment.instance.other}", 'secret_access_key' => "secret#{attachment.instance.other}"]
981
- }
976
+ rebuild_model aws2_add_region.merge storage: :s3,
977
+ bucket: { not: :important },
978
+ s3_credentials: lambda { |attachment|
979
+ Hash["access_key_id" => "access#{attachment.instance.other}", "secret_access_key" => "secret#{attachment.instance.other}"]
980
+ }
982
981
  end
983
982
 
984
983
  it "gets the right credentials" do
985
- assert "access1234", Dummy.new(other: '1234').avatar.s3_credentials[:access_key_id]
986
- assert "secret1234", Dummy.new(other: '1234').avatar.s3_credentials[:secret_access_key]
984
+ assert "access1234", Dummy.new(other: "1234").avatar.s3_credentials[:access_key_id]
985
+ assert "secret1234", Dummy.new(other: "1234").avatar.s3_credentials[:secret_access_key]
987
986
  end
988
987
  end
989
988
 
@@ -991,11 +990,11 @@ describe Paperclip::Storage::S3 do
991
990
  before do
992
991
  class DummyCredentialProvider; end
993
992
 
994
- rebuild_model (aws2_add_region).merge storage: :s3,
995
- bucket: "testing",
996
- s3_credentials: {
997
- credentials: DummyCredentialProvider.new
998
- }
993
+ rebuild_model aws2_add_region.merge storage: :s3,
994
+ bucket: "testing",
995
+ s3_credentials: {
996
+ credentials: DummyCredentialProvider.new
997
+ }
999
998
  @dummy = Dummy.new
1000
999
  end
1001
1000
 
@@ -1006,8 +1005,8 @@ describe Paperclip::Storage::S3 do
1006
1005
 
1007
1006
  context "An attachment with S3 storage and S3 credentials in an unsupported manor" do
1008
1007
  before do
1009
- rebuild_model (aws2_add_region).merge storage: :s3,
1010
- bucket: "testing", s3_credentials: ["unsupported"]
1008
+ rebuild_model aws2_add_region.merge storage: :s3,
1009
+ bucket: "testing", s3_credentials: ["unsupported"]
1011
1010
  @dummy = Dummy.new
1012
1011
  end
1013
1012
 
@@ -1020,7 +1019,7 @@ describe Paperclip::Storage::S3 do
1020
1019
 
1021
1020
  context "An attachment with S3 storage and S3 credentials not supplied" do
1022
1021
  before do
1023
- rebuild_model (aws2_add_region).merge storage: :s3, bucket: "testing"
1022
+ rebuild_model aws2_add_region.merge storage: :s3, bucket: "testing"
1024
1023
  @dummy = Dummy.new
1025
1024
  end
1026
1025
 
@@ -1031,19 +1030,19 @@ describe Paperclip::Storage::S3 do
1031
1030
 
1032
1031
  context "An attachment with S3 storage and specific s3 headers set" do
1033
1032
  before do
1034
- rebuild_model (aws2_add_region).merge storage: :s3,
1035
- bucket: "testing",
1036
- path: ":attachment/:style/:basename:dotextension",
1037
- s3_credentials: {
1038
- 'access_key_id' => "12345",
1039
- 'secret_access_key' => "54321"
1040
- },
1041
- s3_headers: {'Cache-Control' => 'max-age=31557600'}
1033
+ rebuild_model aws2_add_region.merge storage: :s3,
1034
+ bucket: "testing",
1035
+ path: ":attachment/:style/:basename:dotextension",
1036
+ s3_credentials: {
1037
+ "access_key_id" => "12345",
1038
+ "secret_access_key" => "54321"
1039
+ },
1040
+ s3_headers: { "Cache-Control" => "max-age=31557600" }
1042
1041
  end
1043
1042
 
1044
1043
  context "when assigned" do
1045
1044
  before do
1046
- @file = File.new(fixture_file('5k.png'), 'rb')
1045
+ @file = File.new(fixture_file("5k.png"), "rb")
1047
1046
  @dummy = Dummy.new
1048
1047
  @dummy.avatar = @file
1049
1048
  end
@@ -1052,14 +1051,14 @@ describe Paperclip::Storage::S3 do
1052
1051
 
1053
1052
  context "and saved" do
1054
1053
  before do
1055
- object = stub
1056
- @dummy.avatar.stubs(:s3_object).returns(object)
1057
-
1058
- object.expects(:upload_file)
1059
- .with(anything,
1060
- content_type: 'image/png',
1061
- acl: :"public-read",
1062
- cache_control: 'max-age=31557600')
1054
+ object = double
1055
+ allow(@dummy.avatar).to receive(:s3_object).and_return(object)
1056
+
1057
+ expect(object).to receive(:upload_file).
1058
+ with(anything,
1059
+ content_type: "image/png",
1060
+ acl: :"public-read",
1061
+ cache_control: "max-age=31557600")
1063
1062
  @dummy.save
1064
1063
  end
1065
1064
 
@@ -1072,19 +1071,19 @@ describe Paperclip::Storage::S3 do
1072
1071
 
1073
1072
  context "An attachment with S3 storage and metadata set using header names" do
1074
1073
  before do
1075
- rebuild_model (aws2_add_region).merge storage: :s3,
1076
- bucket: "testing",
1077
- path: ":attachment/:style/:basename:dotextension",
1078
- s3_credentials: {
1079
- 'access_key_id' => "12345",
1080
- 'secret_access_key' => "54321"
1081
- },
1082
- s3_headers: {'x-amz-meta-color' => 'red'}
1074
+ rebuild_model aws2_add_region.merge storage: :s3,
1075
+ bucket: "testing",
1076
+ path: ":attachment/:style/:basename:dotextension",
1077
+ s3_credentials: {
1078
+ "access_key_id" => "12345",
1079
+ "secret_access_key" => "54321"
1080
+ },
1081
+ s3_headers: { "x-amz-meta-color" => "red" }
1083
1082
  end
1084
1083
 
1085
1084
  context "when assigned" do
1086
1085
  before do
1087
- @file = File.new(fixture_file('5k.png'), 'rb')
1086
+ @file = File.new(fixture_file("5k.png"), "rb")
1088
1087
  @dummy = Dummy.new
1089
1088
  @dummy.avatar = @file
1090
1089
  end
@@ -1093,14 +1092,14 @@ describe Paperclip::Storage::S3 do
1093
1092
 
1094
1093
  context "and saved" do
1095
1094
  before do
1096
- object = stub
1097
- @dummy.avatar.stubs(:s3_object).returns(object)
1098
-
1099
- object.expects(:upload_file)
1100
- .with(anything,
1101
- content_type: 'image/png',
1102
- acl: :"public-read",
1103
- metadata: { "color" => "red" })
1095
+ object = double
1096
+ allow(@dummy.avatar).to receive(:s3_object).and_return(object)
1097
+
1098
+ expect(object).to receive(:upload_file).
1099
+ with(anything,
1100
+ content_type: "image/png",
1101
+ acl: :"public-read",
1102
+ metadata: { "color" => "red" })
1104
1103
  @dummy.save
1105
1104
  end
1106
1105
 
@@ -1113,19 +1112,19 @@ describe Paperclip::Storage::S3 do
1113
1112
 
1114
1113
  context "An attachment with S3 storage and metadata set using the :s3_metadata option" do
1115
1114
  before do
1116
- rebuild_model (aws2_add_region).merge storage: :s3,
1117
- bucket: "testing",
1118
- path: ":attachment/:style/:basename:dotextension",
1119
- s3_credentials: {
1120
- 'access_key_id' => "12345",
1121
- 'secret_access_key' => "54321"
1122
- },
1123
- s3_metadata: { "color" => "red" }
1115
+ rebuild_model aws2_add_region.merge storage: :s3,
1116
+ bucket: "testing",
1117
+ path: ":attachment/:style/:basename:dotextension",
1118
+ s3_credentials: {
1119
+ "access_key_id" => "12345",
1120
+ "secret_access_key" => "54321"
1121
+ },
1122
+ s3_metadata: { "color" => "red" }
1124
1123
  end
1125
1124
 
1126
1125
  context "when assigned" do
1127
1126
  before do
1128
- @file = File.new(fixture_file('5k.png'), 'rb')
1127
+ @file = File.new(fixture_file("5k.png"), "rb")
1129
1128
  @dummy = Dummy.new
1130
1129
  @dummy.avatar = @file
1131
1130
  end
@@ -1134,14 +1133,14 @@ describe Paperclip::Storage::S3 do
1134
1133
 
1135
1134
  context "and saved" do
1136
1135
  before do
1137
- object = stub
1138
- @dummy.avatar.stubs(:s3_object).returns(object)
1139
-
1140
- object.expects(:upload_file)
1141
- .with(anything,
1142
- content_type: 'image/png',
1143
- acl: :"public-read",
1144
- metadata: { "color" => "red" })
1136
+ object = double
1137
+ allow(@dummy.avatar).to receive(:s3_object).and_return(object)
1138
+
1139
+ expect(object).to receive(:upload_file).
1140
+ with(anything,
1141
+ content_type: "image/png",
1142
+ acl: :"public-read",
1143
+ metadata: { "color" => "red" })
1145
1144
  @dummy.save
1146
1145
  end
1147
1146
 
@@ -1155,19 +1154,19 @@ describe Paperclip::Storage::S3 do
1155
1154
  context "An attachment with S3 storage and storage class set" do
1156
1155
  context "using the header name" do
1157
1156
  before do
1158
- rebuild_model (aws2_add_region).merge storage: :s3,
1159
- bucket: "testing",
1160
- path: ":attachment/:style/:basename:dotextension",
1161
- s3_credentials: {
1162
- 'access_key_id' => "12345",
1163
- 'secret_access_key' => "54321"
1164
- },
1165
- s3_headers: { "x-amz-storage-class" => "reduced_redundancy" }
1157
+ rebuild_model aws2_add_region.merge storage: :s3,
1158
+ bucket: "testing",
1159
+ path: ":attachment/:style/:basename:dotextension",
1160
+ s3_credentials: {
1161
+ "access_key_id" => "12345",
1162
+ "secret_access_key" => "54321"
1163
+ },
1164
+ s3_headers: { "x-amz-storage-class" => "reduced_redundancy" }
1166
1165
  end
1167
1166
 
1168
1167
  context "when assigned" do
1169
1168
  before do
1170
- @file = File.new(fixture_file('5k.png'), 'rb')
1169
+ @file = File.new(fixture_file("5k.png"), "rb")
1171
1170
  @dummy = Dummy.new
1172
1171
  @dummy.avatar = @file
1173
1172
  end
@@ -1176,14 +1175,14 @@ describe Paperclip::Storage::S3 do
1176
1175
 
1177
1176
  context "and saved" do
1178
1177
  before do
1179
- object = stub
1180
- @dummy.avatar.stubs(:s3_object).returns(object)
1181
-
1182
- object.expects(:upload_file)
1183
- .with(anything,
1184
- content_type: 'image/png',
1185
- acl: :"public-read",
1186
- storage_class: "reduced_redundancy")
1178
+ object = double
1179
+ allow(@dummy.avatar).to receive(:s3_object).and_return(object)
1180
+
1181
+ expect(object).to receive(:upload_file).
1182
+ with(anything,
1183
+ content_type: "image/png",
1184
+ acl: :"public-read",
1185
+ storage_class: "reduced_redundancy")
1187
1186
  @dummy.save
1188
1187
  end
1189
1188
 
@@ -1196,24 +1195,24 @@ describe Paperclip::Storage::S3 do
1196
1195
 
1197
1196
  context "using per style hash" do
1198
1197
  before do
1199
- rebuild_model (aws2_add_region).merge :storage => :s3,
1200
- :bucket => "testing",
1201
- :path => ":attachment/:style/:basename.:extension",
1202
- :styles => {
1203
- :thumb => "80x80>"
1204
- },
1205
- :s3_credentials => {
1206
- 'access_key_id' => "12345",
1207
- 'secret_access_key' => "54321"
1208
- },
1209
- :s3_storage_class => {
1210
- :thumb => :reduced_redundancy
1211
- }
1198
+ rebuild_model aws2_add_region.merge storage: :s3,
1199
+ bucket: "testing",
1200
+ path: ":attachment/:style/:basename.:extension",
1201
+ styles: {
1202
+ thumb: "80x80>"
1203
+ },
1204
+ s3_credentials: {
1205
+ "access_key_id" => "12345",
1206
+ "secret_access_key" => "54321"
1207
+ },
1208
+ s3_storage_class: {
1209
+ thumb: :reduced_redundancy
1210
+ }
1212
1211
  end
1213
1212
 
1214
1213
  context "when assigned" do
1215
1214
  before do
1216
- @file = File.new(fixture_file('5k.png'), 'rb')
1215
+ @file = File.new(fixture_file("5k.png"), "rb")
1217
1216
  @dummy = Dummy.new
1218
1217
  @dummy.avatar = @file
1219
1218
  end
@@ -1222,18 +1221,18 @@ describe Paperclip::Storage::S3 do
1222
1221
 
1223
1222
  context "and saved" do
1224
1223
  before do
1225
- object = stub
1224
+ object = double
1226
1225
  [:thumb, :original].each do |style|
1227
- @dummy.avatar.stubs(:s3_object).with(style).returns(object)
1226
+ allow(@dummy.avatar).to receive(:s3_object).with(style).and_return(object)
1228
1227
 
1229
1228
  expected_options = {
1230
- :content_type => "image/png",
1229
+ content_type: "image/png",
1231
1230
  acl: :"public-read"
1232
1231
  }
1233
- expected_options.merge!(:storage_class => :reduced_redundancy) if style == :thumb
1232
+ expected_options.merge!(storage_class: :reduced_redundancy) if style == :thumb
1234
1233
 
1235
- object.expects(:upload_file)
1236
- .with(anything, expected_options)
1234
+ expect(object).to receive(:upload_file).
1235
+ with(anything, expected_options)
1237
1236
  end
1238
1237
  @dummy.save
1239
1238
  end
@@ -1247,22 +1246,22 @@ describe Paperclip::Storage::S3 do
1247
1246
 
1248
1247
  context "using global hash option" do
1249
1248
  before do
1250
- rebuild_model (aws2_add_region).merge :storage => :s3,
1251
- :bucket => "testing",
1252
- :path => ":attachment/:style/:basename.:extension",
1253
- :styles => {
1254
- :thumb => "80x80>"
1255
- },
1256
- :s3_credentials => {
1257
- 'access_key_id' => "12345",
1258
- 'secret_access_key' => "54321"
1259
- },
1260
- :s3_storage_class => :reduced_redundancy
1249
+ rebuild_model aws2_add_region.merge storage: :s3,
1250
+ bucket: "testing",
1251
+ path: ":attachment/:style/:basename.:extension",
1252
+ styles: {
1253
+ thumb: "80x80>"
1254
+ },
1255
+ s3_credentials: {
1256
+ "access_key_id" => "12345",
1257
+ "secret_access_key" => "54321"
1258
+ },
1259
+ s3_storage_class: :reduced_redundancy
1261
1260
  end
1262
1261
 
1263
1262
  context "when assigned" do
1264
1263
  before do
1265
- @file = File.new(fixture_file('5k.png'), 'rb')
1264
+ @file = File.new(fixture_file("5k.png"), "rb")
1266
1265
  @dummy = Dummy.new
1267
1266
  @dummy.avatar = @file
1268
1267
  end
@@ -1271,14 +1270,14 @@ describe Paperclip::Storage::S3 do
1271
1270
 
1272
1271
  context "and saved" do
1273
1272
  before do
1274
- object = stub
1273
+ object = double
1275
1274
  [:thumb, :original].each do |style|
1276
- @dummy.avatar.stubs(:s3_object).with(style).returns(object)
1275
+ allow(@dummy.avatar).to receive(:s3_object).with(style).and_return(object)
1277
1276
 
1278
- object.expects(:upload_file)
1279
- .with(anything, :content_type => "image/png",
1280
- acl: :"public-read",
1281
- :storage_class => :reduced_redundancy)
1277
+ expect(object).to receive(:upload_file).
1278
+ with(anything, content_type: "image/png",
1279
+ acl: :"public-read",
1280
+ storage_class: :reduced_redundancy)
1282
1281
  end
1283
1282
  @dummy.save
1284
1283
  end
@@ -1292,21 +1291,23 @@ describe Paperclip::Storage::S3 do
1292
1291
  end
1293
1292
 
1294
1293
  context "Can disable AES256 encryption multiple ways" do
1295
- [nil, false, ''].each do |tech|
1294
+ [nil, false, ""].each do |tech|
1296
1295
  before do
1297
1296
  rebuild_model(
1298
- (aws2_add_region).merge storage: :s3,
1299
- bucket: "testing",
1300
- path: ":attachment/:style/:basename:dotextension",
1301
- s3_credentials: {
1302
- 'access_key_id' => "12345",
1303
- 'secret_access_key' => "54321"},
1304
- s3_server_side_encryption: tech)
1297
+ aws2_add_region.merge(storage: :s3,
1298
+ bucket: "testing",
1299
+ path: ":attachment/:style/:basename:dotextension",
1300
+ s3_credentials: {
1301
+ "access_key_id" => "12345",
1302
+ "secret_access_key" => "54321"
1303
+ },
1304
+ s3_server_side_encryption: tech)
1305
+ )
1305
1306
  end
1306
1307
 
1307
1308
  context "when assigned" do
1308
1309
  before do
1309
- @file = File.new(fixture_file('5k.png'), 'rb')
1310
+ @file = File.new(fixture_file("5k.png"), "rb")
1310
1311
  @dummy = Dummy.new
1311
1312
  @dummy.avatar = @file
1312
1313
  end
@@ -1315,11 +1316,11 @@ describe Paperclip::Storage::S3 do
1315
1316
 
1316
1317
  context "and saved" do
1317
1318
  before do
1318
- object = stub
1319
- @dummy.avatar.stubs(:s3_object).returns(object)
1319
+ object = double
1320
+ allow(@dummy.avatar).to receive(:s3_object).and_return(object)
1320
1321
 
1321
- object.expects(:upload_file)
1322
- .with(anything, :content_type => "image/png", acl: :"public-read")
1322
+ expect(object).to receive(:upload_file).
1323
+ with(anything, content_type: "image/png", acl: :"public-read")
1323
1324
  @dummy.save
1324
1325
  end
1325
1326
 
@@ -1333,19 +1334,19 @@ describe Paperclip::Storage::S3 do
1333
1334
 
1334
1335
  context "An attachment with S3 storage and using AES256 encryption" do
1335
1336
  before do
1336
- rebuild_model (aws2_add_region).merge storage: :s3,
1337
- bucket: "testing",
1338
- path: ":attachment/:style/:basename:dotextension",
1339
- s3_credentials: {
1340
- 'access_key_id' => "12345",
1341
- 'secret_access_key' => "54321"
1342
- },
1343
- s3_server_side_encryption: "AES256"
1337
+ rebuild_model aws2_add_region.merge storage: :s3,
1338
+ bucket: "testing",
1339
+ path: ":attachment/:style/:basename:dotextension",
1340
+ s3_credentials: {
1341
+ "access_key_id" => "12345",
1342
+ "secret_access_key" => "54321"
1343
+ },
1344
+ s3_server_side_encryption: "AES256"
1344
1345
  end
1345
1346
 
1346
1347
  context "when assigned" do
1347
1348
  before do
1348
- @file = File.new(fixture_file('5k.png'), 'rb')
1349
+ @file = File.new(fixture_file("5k.png"), "rb")
1349
1350
  @dummy = Dummy.new
1350
1351
  @dummy.avatar = @file
1351
1352
  end
@@ -1354,13 +1355,13 @@ describe Paperclip::Storage::S3 do
1354
1355
 
1355
1356
  context "and saved" do
1356
1357
  before do
1357
- object = stub
1358
- @dummy.avatar.stubs(:s3_object).returns(object)
1358
+ object = double
1359
+ allow(@dummy.avatar).to receive(:s3_object).and_return(object)
1359
1360
 
1360
- object.expects(:upload_file)
1361
- .with(anything, content_type: "image/png",
1362
- acl: :"public-read",
1363
- server_side_encryption: "AES256")
1361
+ expect(object).to receive(:upload_file).
1362
+ with(anything, content_type: "image/png",
1363
+ acl: :"public-read",
1364
+ server_side_encryption: "AES256")
1364
1365
  @dummy.save
1365
1366
  end
1366
1367
 
@@ -1373,19 +1374,19 @@ describe Paperclip::Storage::S3 do
1373
1374
 
1374
1375
  context "An attachment with S3 storage and storage class set using the :storage_class option" do
1375
1376
  before do
1376
- rebuild_model (aws2_add_region).merge storage: :s3,
1377
- bucket: "testing",
1378
- path: ":attachment/:style/:basename:dotextension",
1379
- s3_credentials: {
1380
- 'access_key_id' => "12345",
1381
- 'secret_access_key' => "54321"
1382
- },
1383
- s3_storage_class: :reduced_redundancy
1377
+ rebuild_model aws2_add_region.merge storage: :s3,
1378
+ bucket: "testing",
1379
+ path: ":attachment/:style/:basename:dotextension",
1380
+ s3_credentials: {
1381
+ "access_key_id" => "12345",
1382
+ "secret_access_key" => "54321"
1383
+ },
1384
+ s3_storage_class: :reduced_redundancy
1384
1385
  end
1385
1386
 
1386
1387
  context "when assigned" do
1387
1388
  before do
1388
- @file = File.new(fixture_file('5k.png'), 'rb')
1389
+ @file = File.new(fixture_file("5k.png"), "rb")
1389
1390
  @dummy = Dummy.new
1390
1391
  @dummy.avatar = @file
1391
1392
  end
@@ -1394,14 +1395,14 @@ describe Paperclip::Storage::S3 do
1394
1395
 
1395
1396
  context "and saved" do
1396
1397
  before do
1397
- object = stub
1398
- @dummy.avatar.stubs(:s3_object).returns(object)
1399
-
1400
- object.expects(:upload_file)
1401
- .with(anything,
1402
- content_type: "image/png",
1403
- acl: :"public-read",
1404
- storage_class: :reduced_redundancy)
1398
+ object = double
1399
+ allow(@dummy.avatar).to receive(:s3_object).and_return(object)
1400
+
1401
+ expect(object).to receive(:upload_file).
1402
+ with(anything,
1403
+ content_type: "image/png",
1404
+ acl: :"public-read",
1405
+ storage_class: :reduced_redundancy)
1405
1406
  @dummy.save
1406
1407
  end
1407
1408
 
@@ -1414,13 +1415,13 @@ describe Paperclip::Storage::S3 do
1414
1415
 
1415
1416
  context "with S3 credentials supplied as Pathname" do
1416
1417
  before do
1417
- ENV['S3_KEY'] = 'pathname_key'
1418
- ENV['S3_BUCKET'] = 'pathname_bucket'
1419
- ENV['S3_SECRET'] = 'pathname_secret'
1418
+ ENV["S3_KEY"] = "pathname_key"
1419
+ ENV["S3_BUCKET"] = "pathname_bucket"
1420
+ ENV["S3_SECRET"] = "pathname_secret"
1420
1421
 
1421
- rails_env('test') do
1422
- rebuild_model (aws2_add_region).merge storage: :s3,
1423
- s3_credentials: Pathname.new(fixture_file('s3.yml'))
1422
+ rails_env("test") do
1423
+ rebuild_model aws2_add_region.merge storage: :s3,
1424
+ s3_credentials: Pathname.new(fixture_file("s3.yml"))
1424
1425
 
1425
1426
  Dummy.delete_all
1426
1427
  @dummy = Dummy.new
@@ -1428,25 +1429,25 @@ describe Paperclip::Storage::S3 do
1428
1429
  end
1429
1430
 
1430
1431
  it "parses the credentials" do
1431
- assert_equal 'pathname_bucket', @dummy.avatar.bucket_name
1432
+ assert_equal "pathname_bucket", @dummy.avatar.bucket_name
1432
1433
 
1433
- assert_equal 'pathname_key',
1434
- @dummy.avatar.s3_bucket.client.config.access_key_id
1434
+ assert_equal "pathname_key",
1435
+ @dummy.avatar.s3_bucket.client.config.access_key_id
1435
1436
 
1436
- assert_equal 'pathname_secret',
1437
- @dummy.avatar.s3_bucket.client.config.secret_access_key
1437
+ assert_equal "pathname_secret",
1438
+ @dummy.avatar.s3_bucket.client.config.secret_access_key
1438
1439
  end
1439
1440
  end
1440
1441
 
1441
1442
  context "with S3 credentials in a YAML file" do
1442
1443
  before do
1443
- ENV['S3_KEY'] = 'env_key'
1444
- ENV['S3_BUCKET'] = 'env_bucket'
1445
- ENV['S3_SECRET'] = 'env_secret'
1444
+ ENV["S3_KEY"] = "env_key"
1445
+ ENV["S3_BUCKET"] = "env_bucket"
1446
+ ENV["S3_SECRET"] = "env_secret"
1446
1447
 
1447
- rails_env('test') do
1448
- rebuild_model (aws2_add_region).merge storage: :s3,
1449
- s3_credentials: File.new(fixture_file('s3.yml'))
1448
+ rails_env("test") do
1449
+ rebuild_model aws2_add_region.merge storage: :s3,
1450
+ s3_credentials: File.new(fixture_file("s3.yml"))
1450
1451
 
1451
1452
  Dummy.delete_all
1452
1453
 
@@ -1455,31 +1456,31 @@ describe Paperclip::Storage::S3 do
1455
1456
  end
1456
1457
 
1457
1458
  it "runs the file through ERB" do
1458
- assert_equal 'env_bucket', @dummy.avatar.bucket_name
1459
+ assert_equal "env_bucket", @dummy.avatar.bucket_name
1459
1460
 
1460
- assert_equal 'env_key',
1461
- @dummy.avatar.s3_bucket.client.config.access_key_id
1461
+ assert_equal "env_key",
1462
+ @dummy.avatar.s3_bucket.client.config.access_key_id
1462
1463
 
1463
- assert_equal 'env_secret',
1464
- @dummy.avatar.s3_bucket.client.config.secret_access_key
1464
+ assert_equal "env_secret",
1465
+ @dummy.avatar.s3_bucket.client.config.secret_access_key
1465
1466
  end
1466
1467
  end
1467
1468
 
1468
1469
  context "S3 Permissions" do
1469
1470
  context "defaults to :public_read" do
1470
1471
  before do
1471
- rebuild_model (aws2_add_region).merge storage: :s3,
1472
- bucket: "testing",
1473
- path: ":attachment/:style/:basename:dotextension",
1474
- s3_credentials: {
1475
- 'access_key_id' => "12345",
1476
- 'secret_access_key' => "54321"
1477
- }
1472
+ rebuild_model aws2_add_region.merge storage: :s3,
1473
+ bucket: "testing",
1474
+ path: ":attachment/:style/:basename:dotextension",
1475
+ s3_credentials: {
1476
+ "access_key_id" => "12345",
1477
+ "secret_access_key" => "54321"
1478
+ }
1478
1479
  end
1479
1480
 
1480
1481
  context "when assigned" do
1481
1482
  before do
1482
- @file = File.new(fixture_file('5k.png'), 'rb')
1483
+ @file = File.new(fixture_file("5k.png"), "rb")
1483
1484
  @dummy = Dummy.new
1484
1485
  @dummy.avatar = @file
1485
1486
  end
@@ -1488,11 +1489,11 @@ describe Paperclip::Storage::S3 do
1488
1489
 
1489
1490
  context "and saved" do
1490
1491
  before do
1491
- object = stub
1492
- @dummy.avatar.stubs(:s3_object).returns(object)
1492
+ object = double
1493
+ allow(@dummy.avatar).to receive(:s3_object).and_return(object)
1493
1494
 
1494
- object.expects(:upload_file)
1495
- .with(anything, content_type: "image/png", acl: :"public-read")
1495
+ expect(object).to receive(:upload_file).
1496
+ with(anything, content_type: "image/png", acl: :"public-read")
1496
1497
  @dummy.save
1497
1498
  end
1498
1499
 
@@ -1505,19 +1506,19 @@ describe Paperclip::Storage::S3 do
1505
1506
 
1506
1507
  context "string permissions set" do
1507
1508
  before do
1508
- rebuild_model (aws2_add_region).merge storage: :s3,
1509
- bucket: "testing",
1510
- path: ":attachment/:style/:basename:dotextension",
1511
- s3_credentials: {
1512
- 'access_key_id' => "12345",
1513
- 'secret_access_key' => "54321"
1514
- },
1515
- s3_permissions: :private
1509
+ rebuild_model aws2_add_region.merge storage: :s3,
1510
+ bucket: "testing",
1511
+ path: ":attachment/:style/:basename:dotextension",
1512
+ s3_credentials: {
1513
+ "access_key_id" => "12345",
1514
+ "secret_access_key" => "54321"
1515
+ },
1516
+ s3_permissions: :private
1516
1517
  end
1517
1518
 
1518
1519
  context "when assigned" do
1519
1520
  before do
1520
- @file = File.new(fixture_file('5k.png'), 'rb')
1521
+ @file = File.new(fixture_file("5k.png"), "rb")
1521
1522
  @dummy = Dummy.new
1522
1523
  @dummy.avatar = @file
1523
1524
  end
@@ -1526,11 +1527,11 @@ describe Paperclip::Storage::S3 do
1526
1527
 
1527
1528
  context "and saved" do
1528
1529
  before do
1529
- object = stub
1530
- @dummy.avatar.stubs(:s3_object).returns(object)
1530
+ object = double
1531
+ allow(@dummy.avatar).to receive(:s3_object).and_return(object)
1531
1532
 
1532
- object.expects(:upload_file)
1533
- .with(anything, content_type: "image/png", acl: :private)
1533
+ expect(object).to receive(:upload_file).
1534
+ with(anything, content_type: "image/png", acl: :private)
1534
1535
  @dummy.save
1535
1536
  end
1536
1537
 
@@ -1543,25 +1544,25 @@ describe Paperclip::Storage::S3 do
1543
1544
 
1544
1545
  context "hash permissions set" do
1545
1546
  before do
1546
- rebuild_model (aws2_add_region).merge storage: :s3,
1547
- bucket: "testing",
1548
- path: ":attachment/:style/:basename:dotextension",
1549
- styles: {
1550
- thumb: "80x80>"
1551
- },
1552
- s3_credentials: {
1553
- 'access_key_id' => "12345",
1554
- 'secret_access_key' => "54321"
1555
- },
1556
- s3_permissions: {
1557
- original: :private,
1558
- thumb: :public_read
1559
- }
1547
+ rebuild_model aws2_add_region.merge storage: :s3,
1548
+ bucket: "testing",
1549
+ path: ":attachment/:style/:basename:dotextension",
1550
+ styles: {
1551
+ thumb: "80x80>"
1552
+ },
1553
+ s3_credentials: {
1554
+ "access_key_id" => "12345",
1555
+ "secret_access_key" => "54321"
1556
+ },
1557
+ s3_permissions: {
1558
+ original: :private,
1559
+ thumb: :public_read
1560
+ }
1560
1561
  end
1561
1562
 
1562
1563
  context "when assigned" do
1563
1564
  before do
1564
- @file = File.new(fixture_file('5k.png'), 'rb')
1565
+ @file = File.new(fixture_file("5k.png"), "rb")
1565
1566
  @dummy = Dummy.new
1566
1567
  @dummy.avatar = @file
1567
1568
  end
@@ -1571,13 +1572,13 @@ describe Paperclip::Storage::S3 do
1571
1572
  context "and saved" do
1572
1573
  before do
1573
1574
  [:thumb, :original].each do |style|
1574
- object = stub
1575
- @dummy.avatar.stubs(:s3_object).with(style).returns(object)
1575
+ object = double
1576
+ allow(@dummy.avatar).to receive(:s3_object).with(style).and_return(object)
1576
1577
 
1577
- object.expects(:upload_file)
1578
- .with(anything,
1579
- content_type: "image/png",
1580
- acl: style == :thumb ? :public_read : :private)
1578
+ expect(object).to receive(:upload_file).
1579
+ with(anything,
1580
+ content_type: "image/png",
1581
+ acl: style == :thumb ? :public_read : :private)
1581
1582
  end
1582
1583
  @dummy.save
1583
1584
  end
@@ -1592,19 +1593,19 @@ describe Paperclip::Storage::S3 do
1592
1593
  context "proc permission set" do
1593
1594
  before do
1594
1595
  rebuild_model(
1595
- (aws2_add_region).merge storage: :s3,
1596
- bucket: "testing",
1597
- path: ":attachment/:style/:basename:dotextension",
1598
- styles: {
1599
- thumb: "80x80>"
1600
- },
1601
- s3_credentials: {
1602
- 'access_key_id' => "12345",
1603
- 'secret_access_key' => "54321"
1604
- },
1605
- s3_permissions: lambda {|attachment, style|
1606
- attachment.instance.private_attachment? && style.to_sym != :thumb ? :private : :"public-read"
1607
- }
1596
+ aws2_add_region.merge(storage: :s3,
1597
+ bucket: "testing",
1598
+ path: ":attachment/:style/:basename:dotextension",
1599
+ styles: {
1600
+ thumb: "80x80>"
1601
+ },
1602
+ s3_credentials: {
1603
+ "access_key_id" => "12345",
1604
+ "secret_access_key" => "54321"
1605
+ },
1606
+ s3_permissions: lambda { |attachment, style|
1607
+ attachment.instance.private_attachment? && style.to_sym != :thumb ? :private : :"public-read"
1608
+ })
1608
1609
  )
1609
1610
  end
1610
1611
  end
@@ -1613,27 +1614,27 @@ describe Paperclip::Storage::S3 do
1613
1614
  context "An attachment with S3 storage and metadata set using a proc as headers" do
1614
1615
  before do
1615
1616
  rebuild_model(
1616
- (aws2_add_region).merge storage: :s3,
1617
- bucket: "testing",
1618
- path: ":attachment/:style/:basename:dotextension",
1619
- styles: {
1620
- thumb: "80x80>"
1621
- },
1622
- s3_credentials: {
1623
- 'access_key_id' => "12345",
1624
- 'secret_access_key' => "54321"
1625
- },
1626
- s3_headers: lambda {|attachment|
1627
- {'Content-Disposition' => "attachment; filename=\"#{attachment.name}\""}
1628
- }
1617
+ aws2_add_region.merge(storage: :s3,
1618
+ bucket: "testing",
1619
+ path: ":attachment/:style/:basename:dotextension",
1620
+ styles: {
1621
+ thumb: "80x80>"
1622
+ },
1623
+ s3_credentials: {
1624
+ "access_key_id" => "12345",
1625
+ "secret_access_key" => "54321"
1626
+ },
1627
+ s3_headers: lambda { |attachment|
1628
+ { "Content-Disposition" => "attachment; filename=\"#{attachment.name}\"" }
1629
+ })
1629
1630
  )
1630
1631
  end
1631
1632
 
1632
1633
  context "when assigned" do
1633
1634
  before do
1634
- @file = File.new(fixture_file('5k.png'), 'rb')
1635
+ @file = File.new(fixture_file("5k.png"), "rb")
1635
1636
  @dummy = Dummy.new
1636
- @dummy.stubs(name: 'Custom Avatar Name.png')
1637
+ allow(@dummy).to receive(:name).and_return("Custom Avatar Name.png")
1637
1638
  @dummy.avatar = @file
1638
1639
  end
1639
1640
 
@@ -1642,14 +1643,14 @@ describe Paperclip::Storage::S3 do
1642
1643
  context "and saved" do
1643
1644
  before do
1644
1645
  [:thumb, :original].each do |style|
1645
- object = stub
1646
- @dummy.avatar.stubs(:s3_object).with(style).returns(object)
1647
-
1648
- object.expects(:upload_file)
1649
- .with(anything,
1650
- content_type: "image/png",
1651
- acl: :"public-read",
1652
- content_disposition: 'attachment; filename="Custom Avatar Name.png"')
1646
+ object = double
1647
+ allow(@dummy.avatar).to receive(:s3_object).with(style).and_return(object)
1648
+
1649
+ expect(object).to receive(:upload_file).
1650
+ with(anything,
1651
+ content_type: "image/png",
1652
+ acl: :"public-read",
1653
+ content_disposition: 'attachment; filename="Custom Avatar Name.png"')
1653
1654
  end
1654
1655
  @dummy.save
1655
1656
  end
@@ -1663,27 +1664,28 @@ describe Paperclip::Storage::S3 do
1663
1664
 
1664
1665
  context "path is a proc" do
1665
1666
  before do
1666
- rebuild_model (aws2_add_region).merge storage: :s3,
1667
- path: ->(attachment) { attachment.instance.attachment_path }
1667
+ rebuild_model aws2_add_region.merge storage: :s3,
1668
+ path: ->(attachment) { attachment.instance.attachment_path }
1668
1669
 
1669
1670
  @dummy = Dummy.new
1670
1671
  @dummy.class_eval do
1671
1672
  def attachment_path
1672
- '/some/dynamic/path'
1673
+ "/some/dynamic/path"
1673
1674
  end
1674
1675
  end
1675
1676
  @dummy.avatar = stringy_file
1676
1677
  end
1677
1678
 
1678
1679
  it "returns a correct path" do
1679
- assert_match '/some/dynamic/path', @dummy.avatar.path
1680
+ assert_match "/some/dynamic/path", @dummy.avatar.path
1680
1681
  end
1681
1682
  end
1682
1683
 
1683
1684
  private
1684
1685
 
1685
1686
  def rails_env(env)
1686
- stored_env, Rails.env = Rails.env, env
1687
+ stored_env = Rails.env
1688
+ Rails.env = env
1687
1689
  begin
1688
1690
  yield
1689
1691
  ensure