kt-paperclip 5.4.0 → 6.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/issue_template.md +3 -0
- data/.hound.yml +27 -32
- data/.travis.yml +23 -2
- data/Appraisals +17 -0
- data/Gemfile +9 -7
- data/NEWS +21 -0
- data/README.md +27 -37
- data/Rakefile +29 -21
- data/UPGRADING +3 -3
- data/features/basic_integration.feature +4 -0
- data/features/migration.feature +10 -51
- data/features/step_definitions/attachment_steps.rb +12 -12
- data/features/step_definitions/html_steps.rb +5 -5
- data/features/step_definitions/rails_steps.rb +29 -9
- data/features/step_definitions/s3_steps.rb +3 -3
- data/features/step_definitions/web_steps.rb +5 -6
- data/features/support/env.rb +4 -4
- data/features/support/fakeweb.rb +3 -5
- data/features/support/file_helpers.rb +2 -2
- data/features/support/paths.rb +4 -4
- data/features/support/rails.rb +7 -7
- data/features/support/selectors.rb +1 -1
- data/gemfiles/4.2.gemfile +7 -4
- data/gemfiles/5.0.gemfile +7 -4
- data/gemfiles/5.1.gemfile +20 -0
- data/gemfiles/5.2.gemfile +20 -0
- data/gemfiles/6.0.gemfile +20 -0
- data/lib/generators/paperclip/paperclip_generator.rb +6 -8
- data/lib/paperclip/attachment.rb +102 -104
- data/lib/paperclip/attachment_registry.rb +2 -2
- data/lib/paperclip/file_command_content_type_detector.rb +1 -3
- data/lib/paperclip/filename_cleaner.rb +0 -1
- data/lib/paperclip/geometry.rb +18 -19
- data/lib/paperclip/geometry_detector_factory.rb +13 -16
- data/lib/paperclip/geometry_parser_factory.rb +5 -5
- data/lib/paperclip/glue.rb +3 -3
- data/lib/paperclip/has_attached_file.rb +5 -4
- data/lib/paperclip/helpers.rb +3 -3
- data/lib/paperclip/interpolations.rb +42 -38
- data/lib/paperclip/io_adapters/abstract_adapter.rb +16 -14
- data/lib/paperclip/io_adapters/attachment_adapter.rb +12 -6
- data/lib/paperclip/io_adapters/data_uri_adapter.rb +1 -1
- data/lib/paperclip/io_adapters/file_adapter.rb +1 -3
- data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +3 -3
- data/lib/paperclip/io_adapters/identity_adapter.rb +1 -2
- data/lib/paperclip/io_adapters/registry.rb +1 -1
- data/lib/paperclip/io_adapters/stringio_adapter.rb +1 -1
- data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +6 -8
- data/lib/paperclip/io_adapters/uri_adapter.rb +9 -7
- data/lib/paperclip/logger.rb +1 -1
- data/lib/paperclip/matchers/have_attached_file_matcher.rb +4 -4
- data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +19 -18
- data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +4 -4
- data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +11 -10
- data/lib/paperclip/matchers.rb +4 -4
- data/lib/paperclip/media_type_spoof_detector.rb +13 -13
- data/lib/paperclip/missing_attachment_styles.rb +11 -6
- data/lib/paperclip/processor.rb +13 -6
- data/lib/paperclip/processor_helpers.rb +3 -1
- data/lib/paperclip/rails_environment.rb +1 -5
- data/lib/paperclip/railtie.rb +5 -5
- data/lib/paperclip/schema.rb +18 -14
- data/lib/paperclip/storage/filesystem.rb +5 -7
- data/lib/paperclip/storage/fog.rb +36 -32
- data/lib/paperclip/storage/s3.rb +67 -75
- data/lib/paperclip/style.rb +3 -6
- data/lib/paperclip/tempfile.rb +4 -5
- data/lib/paperclip/tempfile_factory.rb +0 -1
- data/lib/paperclip/thumbnail.rb +11 -11
- data/lib/paperclip/url_generator.rb +5 -12
- data/lib/paperclip/validators/attachment_content_type_validator.rb +3 -3
- data/lib/paperclip/validators/attachment_file_name_validator.rb +5 -10
- data/lib/paperclip/validators/attachment_file_type_ignorance_validator.rb +1 -2
- data/lib/paperclip/validators/attachment_presence_validator.rb +3 -5
- data/lib/paperclip/validators/attachment_size_validator.rb +8 -8
- data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +3 -1
- data/lib/paperclip/validators.rb +12 -13
- data/lib/paperclip/version.rb +1 -3
- data/lib/paperclip.rb +49 -48
- data/lib/tasks/paperclip.rake +23 -24
- data/paperclip.gemspec +29 -33
- data/shoulda_macros/paperclip.rb +16 -16
- data/spec/paperclip/attachment_definitions_spec.rb +5 -5
- data/spec/paperclip/attachment_processing_spec.rb +22 -23
- data/spec/paperclip/attachment_registry_spec.rb +15 -15
- data/spec/paperclip/attachment_spec.rb +238 -196
- data/spec/paperclip/content_type_detector_spec.rb +11 -12
- data/spec/paperclip/file_command_content_type_detector_spec.rb +10 -10
- data/spec/paperclip/filename_cleaner_spec.rb +3 -4
- data/spec/paperclip/geometry_detector_spec.rb +7 -8
- data/spec/paperclip/geometry_parser_spec.rb +31 -31
- data/spec/paperclip/geometry_spec.rb +24 -24
- data/spec/paperclip/glue_spec.rb +3 -5
- data/spec/paperclip/has_attached_file_spec.rb +46 -126
- data/spec/paperclip/integration_spec.rb +111 -77
- data/spec/paperclip/interpolations_spec.rb +101 -93
- data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +41 -13
- data/spec/paperclip/io_adapters/attachment_adapter_spec.rb +8 -10
- data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +13 -14
- data/spec/paperclip/io_adapters/empty_string_adapter_spec.rb +4 -4
- data/spec/paperclip/io_adapters/file_adapter_spec.rb +12 -12
- data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +42 -26
- data/spec/paperclip/io_adapters/identity_adapter_spec.rb +1 -1
- data/spec/paperclip/io_adapters/nil_adapter_spec.rb +2 -2
- data/spec/paperclip/io_adapters/registry_spec.rb +4 -4
- data/spec/paperclip/io_adapters/stringio_adapter_spec.rb +10 -10
- data/spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb +6 -6
- data/spec/paperclip/io_adapters/uri_adapter_spec.rb +80 -31
- data/spec/paperclip/matchers/have_attached_file_matcher_spec.rb +3 -3
- data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +4 -5
- data/spec/paperclip/matchers/validate_attachment_presence_matcher_spec.rb +4 -4
- data/spec/paperclip/matchers/validate_attachment_size_matcher_spec.rb +4 -4
- data/spec/paperclip/media_type_spoof_detector_spec.rb +50 -24
- data/spec/paperclip/meta_class_spec.rb +3 -3
- data/spec/paperclip/paperclip_missing_attachment_styles_spec.rb +28 -24
- data/spec/paperclip/paperclip_spec.rb +15 -11
- data/spec/paperclip/plural_cache_spec.rb +8 -8
- data/spec/paperclip/processor_helpers_spec.rb +35 -35
- data/spec/paperclip/processor_spec.rb +8 -8
- data/spec/paperclip/rails_environment_spec.rb +7 -10
- data/spec/paperclip/rake_spec.rb +39 -39
- data/spec/paperclip/schema_spec.rb +57 -53
- data/spec/paperclip/storage/filesystem_spec.rb +6 -6
- data/spec/paperclip/storage/fog_spec.rb +76 -82
- data/spec/paperclip/storage/s3_live_spec.rb +22 -22
- data/spec/paperclip/storage/s3_spec.rb +585 -583
- data/spec/paperclip/style_spec.rb +67 -71
- data/spec/paperclip/tempfile_factory_spec.rb +5 -5
- data/spec/paperclip/thumbnail_spec.rb +68 -67
- data/spec/paperclip/url_generator_spec.rb +18 -29
- data/spec/paperclip/validators/attachment_content_type_validator_spec.rb +27 -27
- data/spec/paperclip/validators/attachment_file_name_validator_spec.rb +15 -16
- data/spec/paperclip/validators/attachment_presence_validator_spec.rb +5 -5
- data/spec/paperclip/validators/attachment_size_validator_spec.rb +21 -21
- data/spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb +9 -13
- data/spec/paperclip/validators_spec.rb +40 -40
- data/spec/spec_helper.rb +21 -23
- data/spec/support/assertions.rb +8 -6
- data/spec/support/fake_model.rb +1 -2
- data/spec/support/fake_rails.rb +1 -1
- data/spec/support/matchers/exist.rb +1 -1
- data/spec/support/matchers/have_column.rb +1 -1
- data/spec/support/mock_url_generator_builder.rb +2 -3
- data/spec/support/model_reconstruction.rb +16 -12
- data/spec/support/reporting.rb +1 -1
- data/spec/support/test_data.rb +2 -2
- metadata +49 -105
- data/lib/kt-paperclip.rb +0 -1
- data/spec/support/conditional_filter_helper.rb +0 -5
@@ -1,5 +1,5 @@
|
|
1
|
-
require
|
2
|
-
require
|
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:
|
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
|
17
|
-
|
18
|
-
|
19
|
-
|
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(
|
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(
|
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
|
62
|
-
|
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,
|
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
|
76
|
-
|
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,
|
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
|
90
|
-
|
91
|
-
|
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
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
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.
|
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
|
134
|
-
|
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
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
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.
|
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
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
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.
|
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
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
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.
|
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
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
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.
|
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
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
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
|
-
|
227
|
-
|
228
|
-
|
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
|
-
#
|
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
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
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.
|
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
|
-
|
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
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
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.
|
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"
|
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.
|
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
|
-
|
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"
|
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.
|
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
|
-
|
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
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
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
|
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
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
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
|
-
|
380
|
+
@file = File.new(fixture_file("5k.png"), "rb")
|
387
381
|
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
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
|
-
|
397
|
-
|
390
|
+
@dummy = Dummy.new
|
391
|
+
@dummy.avatar = @file
|
398
392
|
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
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
|
-
|
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 =
|
442
|
-
@dummy.avatar.
|
443
|
-
@dummy.avatar.
|
444
|
-
@object.
|
445
|
-
@object.
|
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.
|
445
|
+
expect(@object).to receive(:upload_file).with(
|
450
446
|
anything,
|
451
447
|
content_type: "image/png",
|
452
|
-
acl: :"public-read"
|
453
|
-
|
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/
|
456
|
-
acl: :"public-read"
|
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.
|
460
|
+
expect(@object).to receive(:upload_file).with(
|
462
461
|
anything,
|
463
|
-
content_type: "image/
|
464
|
-
acl: :"public-read"
|
465
|
-
|
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
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
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.
|
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
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
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.
|
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
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
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.
|
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
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
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.
|
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.
|
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
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
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.
|
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
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
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.
|
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
|
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 =
|
672
|
-
@dummy.avatar.
|
669
|
+
object = double
|
670
|
+
allow(@dummy.avatar).to receive(:s3_object).and_return(object)
|
673
671
|
|
674
|
-
object.
|
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 =
|
687
|
-
@dummy.avatar.
|
688
|
-
object.
|
689
|
-
|
690
|
-
|
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.
|
703
|
-
Paperclip.
|
704
|
-
@file.
|
705
|
-
@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 =
|
710
|
-
@dummy.avatar.
|
711
|
-
object.
|
712
|
-
|
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
|
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
|
741
|
-
|
742
|
-
|
743
|
-
|
744
|
-
|
745
|
-
|
746
|
-
|
747
|
-
|
748
|
-
|
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 =
|
758
|
-
@dummy.avatar.
|
759
|
-
object.
|
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 =
|
765
|
-
@dummy.avatar.
|
766
|
-
object.
|
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
|
774
|
-
|
775
|
-
|
776
|
-
|
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
|
-
#
|
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
|
-
|
801
|
-
|
802
|
-
|
803
|
-
|
804
|
-
|
805
|
-
|
806
|
-
|
807
|
-
|
808
|
-
|
809
|
-
|
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
|
-
|
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
|
-
|
826
|
+
@dummy.avatar.s3_host_name
|
826
827
|
assert_match %r{^s3.ap-northeast-1.amazonaws.com},
|
827
|
-
|
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
|
844
|
-
|
845
|
-
|
846
|
-
|
847
|
-
|
848
|
-
|
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 !
|
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(
|
863
|
+
@file = File.new(fixture_file("5k.png"), "rb")
|
863
864
|
@dummy = Dummy.new
|
864
865
|
@dummy.avatar = @file
|
865
|
-
@dummy.
|
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.
|
872
|
-
@dummy.avatar.
|
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.
|
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.
|
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
|
-
|
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.
|
894
|
-
Aws::S3::Object.
|
895
|
-
|
896
|
-
|
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 =
|
908
|
-
@dummy.avatar.
|
909
|
-
object.
|
910
|
-
|
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::
|
922
|
-
|
923
|
-
|
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.
|
937
|
-
Aws::S3::Object.
|
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
|
945
|
+
context "that the file were missing" do
|
947
946
|
before do
|
948
|
-
Aws::S3::Object.
|
949
|
-
|
950
|
-
|
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
|
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
|
963
|
-
|
964
|
-
|
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:
|
969
|
-
assert "bucket_a", Dummy.new(other:
|
970
|
-
assert "bucket_b", Dummy.new(other:
|
971
|
-
assert "bucket_b", Dummy.new(other:
|
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
|
978
|
-
|
979
|
-
|
980
|
-
|
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:
|
986
|
-
assert "secret1234", Dummy.new(other:
|
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
|
995
|
-
|
996
|
-
|
997
|
-
|
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
|
1010
|
-
|
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
|
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
|
1035
|
-
|
1036
|
-
|
1037
|
-
|
1038
|
-
|
1039
|
-
|
1040
|
-
|
1041
|
-
|
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(
|
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 =
|
1056
|
-
@dummy.avatar.
|
1057
|
-
|
1058
|
-
object.
|
1059
|
-
|
1060
|
-
|
1061
|
-
|
1062
|
-
|
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
|
1076
|
-
|
1077
|
-
|
1078
|
-
|
1079
|
-
|
1080
|
-
|
1081
|
-
|
1082
|
-
|
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(
|
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 =
|
1097
|
-
@dummy.avatar.
|
1098
|
-
|
1099
|
-
object.
|
1100
|
-
|
1101
|
-
|
1102
|
-
|
1103
|
-
|
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
|
1117
|
-
|
1118
|
-
|
1119
|
-
|
1120
|
-
|
1121
|
-
|
1122
|
-
|
1123
|
-
|
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(
|
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 =
|
1138
|
-
@dummy.avatar.
|
1139
|
-
|
1140
|
-
object.
|
1141
|
-
|
1142
|
-
|
1143
|
-
|
1144
|
-
|
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
|
1159
|
-
|
1160
|
-
|
1161
|
-
|
1162
|
-
|
1163
|
-
|
1164
|
-
|
1165
|
-
|
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(
|
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 =
|
1180
|
-
@dummy.avatar.
|
1181
|
-
|
1182
|
-
object.
|
1183
|
-
|
1184
|
-
|
1185
|
-
|
1186
|
-
|
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
|
1200
|
-
|
1201
|
-
|
1202
|
-
|
1203
|
-
|
1204
|
-
|
1205
|
-
|
1206
|
-
|
1207
|
-
|
1208
|
-
|
1209
|
-
|
1210
|
-
|
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(
|
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 =
|
1224
|
+
object = double
|
1226
1225
|
[:thumb, :original].each do |style|
|
1227
|
-
@dummy.avatar.
|
1226
|
+
allow(@dummy.avatar).to receive(:s3_object).with(style).and_return(object)
|
1228
1227
|
|
1229
1228
|
expected_options = {
|
1230
|
-
:
|
1229
|
+
content_type: "image/png",
|
1231
1230
|
acl: :"public-read"
|
1232
1231
|
}
|
1233
|
-
expected_options.merge!(:
|
1232
|
+
expected_options.merge!(storage_class: :reduced_redundancy) if style == :thumb
|
1234
1233
|
|
1235
|
-
object.
|
1236
|
-
|
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
|
1251
|
-
|
1252
|
-
|
1253
|
-
|
1254
|
-
|
1255
|
-
|
1256
|
-
|
1257
|
-
|
1258
|
-
|
1259
|
-
|
1260
|
-
|
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(
|
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 =
|
1273
|
+
object = double
|
1275
1274
|
[:thumb, :original].each do |style|
|
1276
|
-
@dummy.avatar.
|
1275
|
+
allow(@dummy.avatar).to receive(:s3_object).with(style).and_return(object)
|
1277
1276
|
|
1278
|
-
object.
|
1279
|
-
|
1280
|
-
|
1281
|
-
|
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,
|
1294
|
+
[nil, false, ""].each do |tech|
|
1296
1295
|
before do
|
1297
1296
|
rebuild_model(
|
1298
|
-
|
1299
|
-
|
1300
|
-
|
1301
|
-
|
1302
|
-
|
1303
|
-
|
1304
|
-
|
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(
|
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 =
|
1319
|
-
@dummy.avatar.
|
1319
|
+
object = double
|
1320
|
+
allow(@dummy.avatar).to receive(:s3_object).and_return(object)
|
1320
1321
|
|
1321
|
-
object.
|
1322
|
-
|
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
|
1337
|
-
|
1338
|
-
|
1339
|
-
|
1340
|
-
|
1341
|
-
|
1342
|
-
|
1343
|
-
|
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(
|
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 =
|
1358
|
-
@dummy.avatar.
|
1358
|
+
object = double
|
1359
|
+
allow(@dummy.avatar).to receive(:s3_object).and_return(object)
|
1359
1360
|
|
1360
|
-
object.
|
1361
|
-
|
1362
|
-
|
1363
|
-
|
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
|
1377
|
-
|
1378
|
-
|
1379
|
-
|
1380
|
-
|
1381
|
-
|
1382
|
-
|
1383
|
-
|
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(
|
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 =
|
1398
|
-
@dummy.avatar.
|
1399
|
-
|
1400
|
-
object.
|
1401
|
-
|
1402
|
-
|
1403
|
-
|
1404
|
-
|
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[
|
1418
|
-
ENV[
|
1419
|
-
ENV[
|
1418
|
+
ENV["S3_KEY"] = "pathname_key"
|
1419
|
+
ENV["S3_BUCKET"] = "pathname_bucket"
|
1420
|
+
ENV["S3_SECRET"] = "pathname_secret"
|
1420
1421
|
|
1421
|
-
rails_env(
|
1422
|
-
rebuild_model
|
1423
|
-
|
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
|
1432
|
+
assert_equal "pathname_bucket", @dummy.avatar.bucket_name
|
1432
1433
|
|
1433
|
-
assert_equal
|
1434
|
-
|
1434
|
+
assert_equal "pathname_key",
|
1435
|
+
@dummy.avatar.s3_bucket.client.config.access_key_id
|
1435
1436
|
|
1436
|
-
assert_equal
|
1437
|
-
|
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[
|
1444
|
-
ENV[
|
1445
|
-
ENV[
|
1444
|
+
ENV["S3_KEY"] = "env_key"
|
1445
|
+
ENV["S3_BUCKET"] = "env_bucket"
|
1446
|
+
ENV["S3_SECRET"] = "env_secret"
|
1446
1447
|
|
1447
|
-
rails_env(
|
1448
|
-
rebuild_model
|
1449
|
-
|
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
|
1459
|
+
assert_equal "env_bucket", @dummy.avatar.bucket_name
|
1459
1460
|
|
1460
|
-
assert_equal
|
1461
|
-
|
1461
|
+
assert_equal "env_key",
|
1462
|
+
@dummy.avatar.s3_bucket.client.config.access_key_id
|
1462
1463
|
|
1463
|
-
assert_equal
|
1464
|
-
|
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
|
1472
|
-
|
1473
|
-
|
1474
|
-
|
1475
|
-
|
1476
|
-
|
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(
|
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 =
|
1492
|
-
@dummy.avatar.
|
1492
|
+
object = double
|
1493
|
+
allow(@dummy.avatar).to receive(:s3_object).and_return(object)
|
1493
1494
|
|
1494
|
-
object.
|
1495
|
-
|
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
|
1509
|
-
|
1510
|
-
|
1511
|
-
|
1512
|
-
|
1513
|
-
|
1514
|
-
|
1515
|
-
|
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(
|
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 =
|
1530
|
-
@dummy.avatar.
|
1530
|
+
object = double
|
1531
|
+
allow(@dummy.avatar).to receive(:s3_object).and_return(object)
|
1531
1532
|
|
1532
|
-
object.
|
1533
|
-
|
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
|
1547
|
-
|
1548
|
-
|
1549
|
-
|
1550
|
-
|
1551
|
-
|
1552
|
-
|
1553
|
-
|
1554
|
-
|
1555
|
-
|
1556
|
-
|
1557
|
-
|
1558
|
-
|
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(
|
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 =
|
1575
|
-
@dummy.avatar.
|
1575
|
+
object = double
|
1576
|
+
allow(@dummy.avatar).to receive(:s3_object).with(style).and_return(object)
|
1576
1577
|
|
1577
|
-
object.
|
1578
|
-
|
1579
|
-
|
1580
|
-
|
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
|
-
|
1596
|
-
|
1597
|
-
|
1598
|
-
|
1599
|
-
|
1600
|
-
|
1601
|
-
|
1602
|
-
|
1603
|
-
|
1604
|
-
|
1605
|
-
|
1606
|
-
|
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
|
-
|
1617
|
-
|
1618
|
-
|
1619
|
-
|
1620
|
-
|
1621
|
-
|
1622
|
-
|
1623
|
-
|
1624
|
-
|
1625
|
-
|
1626
|
-
|
1627
|
-
|
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(
|
1635
|
+
@file = File.new(fixture_file("5k.png"), "rb")
|
1635
1636
|
@dummy = Dummy.new
|
1636
|
-
@dummy.
|
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 =
|
1646
|
-
@dummy.avatar.
|
1647
|
-
|
1648
|
-
object.
|
1649
|
-
|
1650
|
-
|
1651
|
-
|
1652
|
-
|
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
|
1667
|
-
|
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
|
-
|
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
|
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
|
1687
|
+
stored_env = Rails.env
|
1688
|
+
Rails.env = env
|
1687
1689
|
begin
|
1688
1690
|
yield
|
1689
1691
|
ensure
|