kt-paperclip 5.4.0 → 7.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/FUNDING.yml +3 -0
- data/.github/ISSUE_TEMPLATE/bug_report.md +38 -0
- data/.github/ISSUE_TEMPLATE/custom.md +10 -0
- data/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
- data/.hound.yml +3 -1055
- data/.rubocop.yml +1061 -1
- data/.travis.yml +23 -4
- data/Appraisals +23 -0
- data/CONTRIBUTING.md +4 -5
- data/Gemfile +10 -7
- data/NEWS +52 -0
- data/README.md +58 -46
- 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 +23 -13
- 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/gemfiles/6.1.gemfile +21 -0
- data/gemfiles/7.0.gemfile +21 -0
- data/lib/generators/paperclip/paperclip_generator.rb +6 -8
- data/lib/paperclip/attachment.rb +103 -105
- data/lib/paperclip/attachment_registry.rb +2 -2
- data/lib/paperclip/content_type_detector.rb +10 -5
- 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 +1 -1
- 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 +21 -9
- 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 +16 -12
- data/lib/paperclip/storage/filesystem.rb +6 -8
- data/lib/paperclip/storage/fog.rb +36 -32
- data/lib/paperclip/storage/s3.rb +68 -76
- 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 -5
- data/lib/paperclip/validators/attachment_content_type_validator.rb +11 -4
- data/lib/paperclip/validators/attachment_file_name_validator.rb +14 -12
- 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 +28 -11
- data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +3 -1
- data/lib/paperclip/validators.rb +16 -17
- 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 +18 -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 +58 -37
- 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 +90 -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 +29 -6
- data/spec/paperclip/storage/fog_spec.rb +122 -82
- data/spec/paperclip/storage/s3_live_spec.rb +22 -22
- data/spec/paperclip/storage/s3_spec.rb +649 -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 -19
- data/spec/paperclip/validators/attachment_content_type_validator_spec.rb +115 -27
- data/spec/paperclip/validators/attachment_file_name_validator_spec.rb +105 -16
- data/spec/paperclip/validators/attachment_presence_validator_spec.rb +5 -5
- data/spec/paperclip/validators/attachment_size_validator_spec.rb +111 -21
- data/spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb +9 -13
- data/spec/paperclip/validators_spec.rb +61 -46
- 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/fixtures/aws_s3.yml +13 -0
- data/spec/support/fixtures/sample.xlsm +0 -0
- 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 +58 -106
- data/spec/support/conditional_filter_helper.rb +0 -5
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
describe Paperclip::Validators::AttachmentPresenceValidator do
|
4
4
|
before do
|
@@ -6,10 +6,10 @@ describe Paperclip::Validators::AttachmentPresenceValidator do
|
|
6
6
|
@dummy = Dummy.new
|
7
7
|
end
|
8
8
|
|
9
|
-
def build_validator(options={})
|
9
|
+
def build_validator(options = {})
|
10
10
|
@validator = Paperclip::Validators::AttachmentPresenceValidator.new(options.merge(
|
11
|
-
|
12
|
-
|
11
|
+
attributes: :avatar
|
12
|
+
))
|
13
13
|
end
|
14
14
|
|
15
15
|
context "nil attachment" do
|
@@ -79,7 +79,7 @@ describe Paperclip::Validators::AttachmentPresenceValidator do
|
|
79
79
|
end
|
80
80
|
|
81
81
|
it "adds the validator to the class" do
|
82
|
-
assert Dummy.validators_on(:avatar).any?{ |validator| validator.kind == :attachment_presence }
|
82
|
+
assert Dummy.validators_on(:avatar).any? { |validator| validator.kind == :attachment_presence }
|
83
83
|
end
|
84
84
|
end
|
85
85
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
describe Paperclip::Validators::AttachmentSizeValidator do
|
4
4
|
before do
|
@@ -8,22 +8,22 @@ describe Paperclip::Validators::AttachmentSizeValidator do
|
|
8
8
|
|
9
9
|
def build_validator(options)
|
10
10
|
@validator = Paperclip::Validators::AttachmentSizeValidator.new(options.merge(
|
11
|
-
|
12
|
-
|
11
|
+
attributes: :avatar
|
12
|
+
))
|
13
13
|
end
|
14
14
|
|
15
15
|
def self.should_allow_attachment_file_size(size)
|
16
16
|
context "when the attachment size is #{size}" do
|
17
17
|
it "adds error to dummy object" do
|
18
|
-
@dummy.
|
18
|
+
allow(@dummy).to receive(:avatar_content_type).and_return(size)
|
19
19
|
@validator.validate(@dummy)
|
20
20
|
assert @dummy.errors[:avatar_file_size].blank?,
|
21
|
-
|
21
|
+
"Expect an error message on :avatar_file_size, got none."
|
22
22
|
end
|
23
23
|
|
24
24
|
it "does not add error to the base dummy object" do
|
25
25
|
assert @dummy.errors[:avatar].blank?,
|
26
|
-
|
26
|
+
"Error added to base attribute"
|
27
27
|
end
|
28
28
|
end
|
29
29
|
end
|
@@ -31,18 +31,18 @@ describe Paperclip::Validators::AttachmentSizeValidator do
|
|
31
31
|
def self.should_not_allow_attachment_file_size(size, options = {})
|
32
32
|
context "when the attachment size is #{size}" do
|
33
33
|
before do
|
34
|
-
@dummy.
|
34
|
+
allow(@dummy).to receive(:avatar_file_size).and_return(size)
|
35
35
|
@validator.validate(@dummy)
|
36
36
|
end
|
37
37
|
|
38
38
|
it "adds error to dummy object" do
|
39
39
|
assert @dummy.errors[:avatar_file_size].present?,
|
40
|
-
|
40
|
+
"Unexpected error message on :avatar_file_size"
|
41
41
|
end
|
42
42
|
|
43
43
|
it "adds error to the base dummy object" do
|
44
44
|
assert @dummy.errors[:avatar].present?,
|
45
|
-
|
45
|
+
"Error not added to base attribute"
|
46
46
|
end
|
47
47
|
|
48
48
|
it "adds error to base object as a string" do
|
@@ -70,7 +70,7 @@ describe Paperclip::Validators::AttachmentSizeValidator do
|
|
70
70
|
|
71
71
|
context "as a proc" do
|
72
72
|
before do
|
73
|
-
build_validator in: lambda { |
|
73
|
+
build_validator in: lambda { |_avatar| (5.kilobytes..10.kilobytes) }
|
74
74
|
end
|
75
75
|
|
76
76
|
should_allow_attachment_file_size(7.kilobytes)
|
@@ -91,7 +91,7 @@ describe Paperclip::Validators::AttachmentSizeValidator do
|
|
91
91
|
|
92
92
|
context "as a proc" do
|
93
93
|
before do
|
94
|
-
build_validator greater_than: lambda { |
|
94
|
+
build_validator greater_than: lambda { |_avatar| 10.kilobytes }
|
95
95
|
end
|
96
96
|
|
97
97
|
should_allow_attachment_file_size 11.kilobytes
|
@@ -111,7 +111,7 @@ describe Paperclip::Validators::AttachmentSizeValidator do
|
|
111
111
|
|
112
112
|
context "as a proc" do
|
113
113
|
before do
|
114
|
-
build_validator less_than: lambda { |
|
114
|
+
build_validator less_than: lambda { |_avatar| 10.kilobytes }
|
115
115
|
end
|
116
116
|
|
117
117
|
should_allow_attachment_file_size 9.kilobytes
|
@@ -123,7 +123,7 @@ describe Paperclip::Validators::AttachmentSizeValidator do
|
|
123
123
|
context "as numbers" do
|
124
124
|
before do
|
125
125
|
build_validator greater_than: 5.kilobytes,
|
126
|
-
|
126
|
+
less_than: 10.kilobytes
|
127
127
|
end
|
128
128
|
|
129
129
|
should_allow_attachment_file_size 7.kilobytes
|
@@ -133,8 +133,8 @@ describe Paperclip::Validators::AttachmentSizeValidator do
|
|
133
133
|
|
134
134
|
context "as a proc" do
|
135
135
|
before do
|
136
|
-
build_validator greater_than: lambda { |
|
137
|
-
|
136
|
+
build_validator greater_than: lambda { |_avatar| 5.kilobytes },
|
137
|
+
less_than: lambda { |_avatar| 10.kilobytes }
|
138
138
|
end
|
139
139
|
|
140
140
|
should_allow_attachment_file_size 7.kilobytes
|
@@ -147,7 +147,7 @@ describe Paperclip::Validators::AttachmentSizeValidator do
|
|
147
147
|
context "given a range" do
|
148
148
|
before do
|
149
149
|
build_validator in: (5.kilobytes..10.kilobytes),
|
150
|
-
|
150
|
+
message: "is invalid. (Between %{min} and %{max} please.)"
|
151
151
|
end
|
152
152
|
|
153
153
|
should_not_allow_attachment_file_size(
|
@@ -159,8 +159,8 @@ describe Paperclip::Validators::AttachmentSizeValidator do
|
|
159
159
|
context "given :less_than and :greater_than" do
|
160
160
|
before do
|
161
161
|
build_validator less_than: 10.kilobytes,
|
162
|
-
|
163
|
-
|
162
|
+
greater_than: 5.kilobytes,
|
163
|
+
message: "is invalid. (Between %{min} and %{max} please.)"
|
164
164
|
end
|
165
165
|
|
166
166
|
should_not_allow_attachment_file_size(
|
@@ -174,7 +174,7 @@ describe Paperclip::Validators::AttachmentSizeValidator do
|
|
174
174
|
context "given :less_than and :greater_than" do
|
175
175
|
before do
|
176
176
|
build_validator greater_than: 5.kilobytes,
|
177
|
-
|
177
|
+
less_than: 10.kilobytes
|
178
178
|
end
|
179
179
|
|
180
180
|
should_not_allow_attachment_file_size(
|
@@ -205,13 +205,103 @@ describe Paperclip::Validators::AttachmentSizeValidator do
|
|
205
205
|
end
|
206
206
|
end
|
207
207
|
|
208
|
+
context "with add_validation_errors_to not set (implicitly :both)" do
|
209
|
+
it "adds error to both attribute and base" do
|
210
|
+
build_validator in: (5.kilobytes..10.kilobytes)
|
211
|
+
allow(@dummy).to receive(:avatar_file_size).and_return(11.kilobytes)
|
212
|
+
@validator.validate(@dummy)
|
213
|
+
|
214
|
+
assert @dummy.errors[:avatar_file_size].present?,
|
215
|
+
"Error not added to attribute"
|
216
|
+
|
217
|
+
assert @dummy.errors[:avatar].present?,
|
218
|
+
"Error not added to base attribute"
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
context "with add_validation_errors_to set to :attribute globally" do
|
223
|
+
before do
|
224
|
+
Paperclip.options[:add_validation_errors_to] = :attribute
|
225
|
+
end
|
226
|
+
|
227
|
+
after do
|
228
|
+
Paperclip.options[:add_validation_errors_to] = :both
|
229
|
+
end
|
230
|
+
|
231
|
+
it "only adds error to attribute not base" do
|
232
|
+
build_validator in: (5.kilobytes..10.kilobytes)
|
233
|
+
allow(@dummy).to receive(:avatar_file_size).and_return(11.kilobytes)
|
234
|
+
@validator.validate(@dummy)
|
235
|
+
|
236
|
+
assert @dummy.errors[:avatar_file_size].present?,
|
237
|
+
"Error not added to attribute"
|
238
|
+
|
239
|
+
assert @dummy.errors[:avatar].blank?,
|
240
|
+
"Error added to base attribute"
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
context "with add_validation_errors_to set to :base globally" do
|
245
|
+
before do
|
246
|
+
Paperclip.options[:add_validation_errors_to] = :base
|
247
|
+
end
|
248
|
+
|
249
|
+
after do
|
250
|
+
Paperclip.options[:add_validation_errors_to] = :both
|
251
|
+
end
|
252
|
+
|
253
|
+
it "only adds error to base not attribute" do
|
254
|
+
build_validator in: (5.kilobytes..10.kilobytes)
|
255
|
+
allow(@dummy).to receive(:avatar_file_size).and_return(11.kilobytes)
|
256
|
+
@validator.validate(@dummy)
|
257
|
+
|
258
|
+
assert @dummy.errors[:avatar].present?,
|
259
|
+
"Error not added to base attribute"
|
260
|
+
|
261
|
+
assert @dummy.errors[:avatar_file_size].blank?,
|
262
|
+
"Error added to attribute"
|
263
|
+
end
|
264
|
+
end
|
265
|
+
|
266
|
+
context "with add_validation_errors_to set to :attribute" do
|
267
|
+
it "only adds error to attribute not base" do
|
268
|
+
build_validator in: (5.kilobytes..10.kilobytes),
|
269
|
+
add_validation_errors_to: :attribute
|
270
|
+
|
271
|
+
allow(@dummy).to receive(:avatar_file_size).and_return(11.kilobytes)
|
272
|
+
@validator.validate(@dummy)
|
273
|
+
|
274
|
+
assert @dummy.errors[:avatar_file_size].present?,
|
275
|
+
"Error not added to attribute"
|
276
|
+
|
277
|
+
assert @dummy.errors[:avatar].blank?,
|
278
|
+
"Error added to base attribute"
|
279
|
+
end
|
280
|
+
end
|
281
|
+
|
282
|
+
context "with add_validation_errors_to set to :base" do
|
283
|
+
it "only adds error to base not attribute" do
|
284
|
+
build_validator in: (5.kilobytes..10.kilobytes),
|
285
|
+
add_validation_errors_to: :base
|
286
|
+
|
287
|
+
allow(@dummy).to receive(:avatar_file_size).and_return(11.kilobytes)
|
288
|
+
@validator.validate(@dummy)
|
289
|
+
|
290
|
+
assert @dummy.errors[:avatar].present?,
|
291
|
+
"Error not added to base attribute"
|
292
|
+
|
293
|
+
assert @dummy.errors[:avatar_file_size].blank?,
|
294
|
+
"Error added to attribute"
|
295
|
+
end
|
296
|
+
end
|
297
|
+
|
208
298
|
context "using the helper" do
|
209
299
|
before do
|
210
300
|
Dummy.validates_attachment_size :avatar, in: (5.kilobytes..10.kilobytes)
|
211
301
|
end
|
212
302
|
|
213
303
|
it "adds the validator to the class" do
|
214
|
-
assert Dummy.validators_on(:avatar).any?{ |validator| validator.kind == :attachment_size }
|
304
|
+
assert Dummy.validators_on(:avatar).any? { |validator| validator.kind == :attachment_size }
|
215
305
|
end
|
216
306
|
end
|
217
307
|
|
@@ -222,7 +312,7 @@ describe Paperclip::Validators::AttachmentSizeValidator do
|
|
222
312
|
end
|
223
313
|
end
|
224
314
|
|
225
|
-
|
315
|
+
Paperclip::Validators::AttachmentSizeValidator::AVAILABLE_CHECKS.each do |argument|
|
226
316
|
it "does not raise arguemnt error if #{argument} was given" do
|
227
317
|
build_validator argument => 5.kilobytes
|
228
318
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
describe Paperclip::Validators::MediaTypeSpoofDetectionValidator do
|
4
4
|
before do
|
@@ -8,17 +8,17 @@ describe Paperclip::Validators::MediaTypeSpoofDetectionValidator do
|
|
8
8
|
|
9
9
|
def build_validator(options = {})
|
10
10
|
@validator = Paperclip::Validators::MediaTypeSpoofDetectionValidator.new(options.merge(
|
11
|
-
|
12
|
-
|
11
|
+
attributes: :avatar
|
12
|
+
))
|
13
13
|
end
|
14
14
|
|
15
15
|
it "is on the attachment without being explicitly added" do
|
16
|
-
assert Dummy.validators_on(:avatar).any?{ |validator| validator.kind == :media_type_spoof_detection }
|
16
|
+
assert Dummy.validators_on(:avatar).any? { |validator| validator.kind == :media_type_spoof_detection }
|
17
17
|
end
|
18
18
|
|
19
19
|
it "is not on the attachment when explicitly rejected" do
|
20
20
|
rebuild_model validate_media_type: false
|
21
|
-
assert Dummy.validators_on(:avatar).none?{ |validator| validator.kind == :media_type_spoof_detection }
|
21
|
+
assert Dummy.validators_on(:avatar).none? { |validator| validator.kind == :media_type_spoof_detection }
|
22
22
|
end
|
23
23
|
|
24
24
|
it "returns default error message for spoofed media type" do
|
@@ -26,8 +26,8 @@ describe Paperclip::Validators::MediaTypeSpoofDetectionValidator do
|
|
26
26
|
file = File.new(fixture_file("5k.png"), "rb")
|
27
27
|
@dummy.avatar.assign(file)
|
28
28
|
|
29
|
-
detector =
|
30
|
-
Paperclip::MediaTypeSpoofDetector.
|
29
|
+
detector = double("detector", spoofed?: true)
|
30
|
+
allow(Paperclip::MediaTypeSpoofDetector).to receive(:using).and_return(detector)
|
31
31
|
@validator.validate(@dummy)
|
32
32
|
|
33
33
|
assert_equal I18n.t("errors.messages.spoofed_media_type"), @dummy.errors[:avatar].first
|
@@ -37,16 +37,12 @@ describe Paperclip::Validators::MediaTypeSpoofDetectionValidator do
|
|
37
37
|
build_validator
|
38
38
|
file = File.new(fixture_file("5k.png"), "rb")
|
39
39
|
@dummy.avatar.assign(file)
|
40
|
-
Paperclip::MediaTypeSpoofDetector.
|
41
|
-
|
40
|
+
expect(Paperclip::MediaTypeSpoofDetector).to receive(:using).once.and_return(double("detector", spoofed?: false))
|
42
41
|
@dummy.valid?
|
43
|
-
|
44
|
-
assert_received(Paperclip::MediaTypeSpoofDetector, :using){|e| e.once }
|
45
42
|
end
|
46
43
|
|
47
44
|
it "does not run when attachment is not dirty" do
|
48
|
-
Paperclip::MediaTypeSpoofDetector.
|
45
|
+
expect(Paperclip::MediaTypeSpoofDetector).to_not receive(:using)
|
49
46
|
@dummy.valid?
|
50
|
-
assert_received(Paperclip::MediaTypeSpoofDetector, :using){|e| e.never }
|
51
47
|
end
|
52
48
|
end
|
@@ -1,6 +1,11 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
describe Paperclip::Validators do
|
4
|
+
# required to support a range of rubies
|
5
|
+
def error_attribute_names(error)
|
6
|
+
error.try(:attribute_names) || error.keys
|
7
|
+
end
|
8
|
+
|
4
9
|
context "using the helper" do
|
5
10
|
before do
|
6
11
|
rebuild_class
|
@@ -8,65 +13,73 @@ describe Paperclip::Validators do
|
|
8
13
|
end
|
9
14
|
|
10
15
|
it "adds the attachment_presence validator to the class" do
|
11
|
-
assert Dummy.validators_on(:avatar).any?{ |validator| validator.kind == :attachment_presence }
|
16
|
+
assert Dummy.validators_on(:avatar).any? { |validator| validator.kind == :attachment_presence }
|
12
17
|
end
|
13
18
|
|
14
19
|
it "adds the attachment_content_type validator to the class" do
|
15
|
-
assert Dummy.validators_on(:avatar).any?{ |validator| validator.kind == :attachment_content_type }
|
20
|
+
assert Dummy.validators_on(:avatar).any? { |validator| validator.kind == :attachment_content_type }
|
16
21
|
end
|
17
22
|
|
18
23
|
it "adds the attachment_size validator to the class" do
|
19
|
-
assert Dummy.validators_on(:avatar).any?{ |validator| validator.kind == :attachment_size }
|
24
|
+
assert Dummy.validators_on(:avatar).any? { |validator| validator.kind == :attachment_size }
|
20
25
|
end
|
21
26
|
|
22
|
-
it
|
23
|
-
Dummy.class_eval{ validate(:name) { raise "DO NOT RUN THIS" } }
|
27
|
+
it "prevents you from attaching a file that violates that validation" do
|
28
|
+
Dummy.class_eval { validate(:name) { raise "DO NOT RUN THIS" } }
|
24
29
|
dummy = Dummy.new(avatar: File.new(fixture_file("12k.png")))
|
25
|
-
expect(dummy.errors
|
26
|
-
|
30
|
+
expect(error_attribute_names(dummy.errors)).to match_array(
|
31
|
+
%i[avatar_content_type avatar avatar_file_size]
|
32
|
+
)
|
33
|
+
assert_raises(RuntimeError) { dummy.valid? }
|
27
34
|
end
|
28
35
|
end
|
29
36
|
|
30
|
-
context
|
37
|
+
context "using the helper with array of validations" do
|
31
38
|
before do
|
32
39
|
rebuild_class
|
33
40
|
Dummy.validates_attachment :avatar, file_type_ignorance: true, file_name: [
|
34
|
-
|
35
|
-
|
41
|
+
{ matches: /\A.*\.jpe?g\z/i, message: :invalid_extension },
|
42
|
+
{ matches: /\A.{,8}\..+\z/i, message: [:too_long, count: 8] },
|
36
43
|
]
|
37
44
|
end
|
38
45
|
|
39
|
-
it
|
40
|
-
assert Dummy.validators_on(:avatar).any?{ |validator| validator.kind == :attachment_file_name }
|
46
|
+
it "adds the attachment_file_name validator to the class" do
|
47
|
+
assert Dummy.validators_on(:avatar).any? { |validator| validator.kind == :attachment_file_name }
|
41
48
|
end
|
42
49
|
|
43
|
-
it
|
44
|
-
assert_equal 2, Dummy.validators_on(:avatar).select{ |validator| validator.kind == :attachment_file_name }.size
|
50
|
+
it "adds the attachment_file_name validator with two validations" do
|
51
|
+
assert_equal 2, Dummy.validators_on(:avatar).select { |validator| validator.kind == :attachment_file_name }.size
|
45
52
|
end
|
46
53
|
|
47
|
-
it
|
48
|
-
Dummy.class_eval{ validate(:name) { raise
|
49
|
-
dummy = Dummy.new(avatar: File.new(fixture_file(
|
50
|
-
expect(dummy.errors
|
51
|
-
|
54
|
+
it "prevents you from attaching a file that violates all of these validations" do
|
55
|
+
Dummy.class_eval { validate(:name) { raise "DO NOT RUN THIS" } }
|
56
|
+
dummy = Dummy.new(avatar: File.new(fixture_file("spaced file.png")))
|
57
|
+
expect(error_attribute_names(dummy.errors)).to match_array(
|
58
|
+
%i[avatar avatar_file_name]
|
59
|
+
)
|
60
|
+
assert_raises(RuntimeError) { dummy.valid? }
|
52
61
|
end
|
53
62
|
|
54
|
-
it
|
55
|
-
Dummy.class_eval{ validate(:name) { raise
|
56
|
-
dummy = Dummy.new(avatar: File.new(fixture_file(
|
57
|
-
expect(dummy.errors
|
58
|
-
|
63
|
+
it "prevents you from attaching a file that violates only first of these validations" do
|
64
|
+
Dummy.class_eval { validate(:name) { raise "DO NOT RUN THIS" } }
|
65
|
+
dummy = Dummy.new(avatar: File.new(fixture_file("5k.png")))
|
66
|
+
expect(error_attribute_names(dummy.errors)).to match_array(
|
67
|
+
%i[avatar avatar_file_name]
|
68
|
+
)
|
69
|
+
assert_raises(RuntimeError) { dummy.valid? }
|
59
70
|
end
|
60
71
|
|
61
|
-
it
|
62
|
-
Dummy.class_eval{ validate(:name) { raise
|
63
|
-
dummy = Dummy.new(avatar: File.new(fixture_file(
|
64
|
-
expect(dummy.errors
|
65
|
-
|
72
|
+
it "prevents you from attaching a file that violates only second of these validations" do
|
73
|
+
Dummy.class_eval { validate(:name) { raise "DO NOT RUN THIS" } }
|
74
|
+
dummy = Dummy.new(avatar: File.new(fixture_file("spaced file.jpg")))
|
75
|
+
expect(error_attribute_names(dummy.errors)).to match_array(
|
76
|
+
%i[avatar avatar_file_name]
|
77
|
+
)
|
78
|
+
assert_raises(RuntimeError) { dummy.valid? }
|
66
79
|
end
|
67
80
|
|
68
|
-
it
|
69
|
-
dummy = Dummy.new(avatar: File.new(fixture_file(
|
81
|
+
it "allows you to attach a file that does not violate these validations" do
|
82
|
+
dummy = Dummy.new(avatar: File.new(fixture_file("rotated.jpg")))
|
70
83
|
expect(dummy.errors.full_messages).to be_empty
|
71
84
|
assert dummy.valid?
|
72
85
|
end
|
@@ -76,9 +89,9 @@ describe Paperclip::Validators do
|
|
76
89
|
before do
|
77
90
|
rebuild_class
|
78
91
|
Dummy.validates_attachment :avatar, presence: true,
|
79
|
-
|
80
|
-
|
81
|
-
|
92
|
+
content_type: { content_type: "image/jpeg" },
|
93
|
+
size: { in: 0..10240 },
|
94
|
+
if: :title_present?
|
82
95
|
end
|
83
96
|
|
84
97
|
it "validates the attachment if title is present" do
|
@@ -88,7 +101,9 @@ describe Paperclip::Validators do
|
|
88
101
|
end
|
89
102
|
end
|
90
103
|
dummy = Dummy.new(avatar: File.new(fixture_file("12k.png")))
|
91
|
-
expect(dummy.errors
|
104
|
+
expect(error_attribute_names(dummy.errors)).to match_array(
|
105
|
+
%i[avatar_content_type avatar avatar_file_size]
|
106
|
+
)
|
92
107
|
end
|
93
108
|
|
94
109
|
it "does not validate attachment if title is not present" do
|
@@ -98,24 +113,24 @@ describe Paperclip::Validators do
|
|
98
113
|
end
|
99
114
|
end
|
100
115
|
dummy = Dummy.new(avatar: File.new(fixture_file("12k.png")))
|
101
|
-
assert_equal [], dummy.errors
|
116
|
+
assert_equal [], error_attribute_names(dummy.errors)
|
102
117
|
end
|
103
118
|
end
|
104
119
|
|
105
|
-
context
|
120
|
+
context "with no other validations on the Dummy#avatar attachment" do
|
106
121
|
before do
|
107
122
|
reset_class("Dummy")
|
108
123
|
Dummy.has_attached_file :avatar
|
109
124
|
Paperclip.reset_duplicate_clash_check!
|
110
125
|
end
|
111
126
|
|
112
|
-
it
|
127
|
+
it "raises an error when no content_type validation exists" do
|
113
128
|
assert_raises(Paperclip::Errors::MissingRequiredValidatorError) do
|
114
129
|
Dummy.new(avatar: File.new(fixture_file("12k.png")))
|
115
130
|
end
|
116
131
|
end
|
117
132
|
|
118
|
-
it
|
133
|
+
it "does not raise an error when a content_type validation exists" do
|
119
134
|
Dummy.validates_attachment :avatar, content_type: { content_type: "image/jpeg" }
|
120
135
|
|
121
136
|
assert_nothing_raised do
|
@@ -123,18 +138,18 @@ describe Paperclip::Validators do
|
|
123
138
|
end
|
124
139
|
end
|
125
140
|
|
126
|
-
it
|
127
|
-
Dummy.validates_with Paperclip::Validators::AttachmentContentTypeValidator, attributes: :attachment, content_type:
|
141
|
+
it "does not raise an error when a content_type validation exists using validates_with" do
|
142
|
+
Dummy.validates_with Paperclip::Validators::AttachmentContentTypeValidator, attributes: :attachment, content_type: "images/jpeg"
|
128
143
|
|
129
144
|
assert_nothing_raised do
|
130
145
|
Dummy.new(avatar: File.new(fixture_file("12k.png")))
|
131
146
|
end
|
132
147
|
end
|
133
148
|
|
134
|
-
it
|
149
|
+
it "does not raise an error when an inherited validator is used" do
|
135
150
|
class MyValidator < Paperclip::Validators::AttachmentContentTypeValidator
|
136
151
|
def initialize(options)
|
137
|
-
options[:content_type] = "images/jpeg" unless options.
|
152
|
+
options[:content_type] = "images/jpeg" unless options.key?(:content_type)
|
138
153
|
super
|
139
154
|
end
|
140
155
|
end
|
@@ -145,7 +160,7 @@ describe Paperclip::Validators do
|
|
145
160
|
end
|
146
161
|
end
|
147
162
|
|
148
|
-
it
|
163
|
+
it "does not raise an error when a file_name validation exists" do
|
149
164
|
Dummy.validates_attachment :avatar, file_name: { matches: /png$/ }
|
150
165
|
|
151
166
|
assert_nothing_raised do
|
@@ -153,7 +168,7 @@ describe Paperclip::Validators do
|
|
153
168
|
end
|
154
169
|
end
|
155
170
|
|
156
|
-
it
|
171
|
+
it "does not raise an error when a the validation has been explicitly rejected" do
|
157
172
|
Dummy.validates_attachment :avatar, file_type_ignorance: true
|
158
173
|
|
159
174
|
assert_nothing_raised do
|
data/spec/spec_helper.rb
CHANGED
@@ -1,36 +1,35 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
7
|
-
require
|
8
|
-
require
|
9
|
-
require
|
10
|
-
require
|
11
|
-
|
12
|
-
|
13
|
-
ROOT = Pathname(File.expand_path(File.join(File.dirname(__FILE__), '..')))
|
1
|
+
require "rubygems"
|
2
|
+
require "rspec"
|
3
|
+
require "active_record"
|
4
|
+
require "active_record/version"
|
5
|
+
require "active_support"
|
6
|
+
require "active_support/core_ext"
|
7
|
+
require "ostruct"
|
8
|
+
require "pathname"
|
9
|
+
require "activerecord-import"
|
10
|
+
require "yaml"
|
11
|
+
|
12
|
+
ROOT = Pathname(File.expand_path(File.join(File.dirname(__FILE__), "..")))
|
14
13
|
|
15
14
|
puts "Testing against version #{ActiveRecord::VERSION::STRING}"
|
16
15
|
|
17
|
-
$LOAD_PATH << File.join(ROOT,
|
18
|
-
$LOAD_PATH << File.join(ROOT,
|
19
|
-
require File.join(ROOT,
|
16
|
+
$LOAD_PATH << File.join(ROOT, "lib")
|
17
|
+
$LOAD_PATH << File.join(ROOT, "lib", "paperclip")
|
18
|
+
require File.join(ROOT, "lib", "paperclip.rb")
|
20
19
|
|
21
20
|
FIXTURES_DIR = File.join(File.dirname(__FILE__), "fixtures")
|
22
|
-
config = YAML::
|
21
|
+
config = YAML::safe_load(IO.read(File.dirname(__FILE__) + "/database.yml"))
|
23
22
|
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
|
24
|
-
ActiveRecord::Base.establish_connection(config[
|
23
|
+
ActiveRecord::Base.establish_connection(config["test"])
|
25
24
|
if ActiveRecord::VERSION::STRING >= "4.2" &&
|
26
|
-
|
25
|
+
ActiveRecord::VERSION::STRING < "5.0"
|
27
26
|
ActiveRecord::Base.raise_in_transactional_callbacks = true
|
28
27
|
end
|
29
28
|
Paperclip.options[:logger] = ActiveRecord::Base.logger
|
30
29
|
|
31
|
-
Dir[File.join(ROOT,
|
30
|
+
Dir[File.join(ROOT, "spec", "support", "**", "*.rb")].each { |f| require f }
|
32
31
|
|
33
|
-
Rails = FakeRails.new(
|
32
|
+
Rails = FakeRails.new("test", Pathname.new(ROOT).join("tmp"))
|
34
33
|
ActiveSupport::Deprecation.silenced = true
|
35
34
|
|
36
35
|
RSpec.configure do |config|
|
@@ -39,8 +38,7 @@ RSpec.configure do |config|
|
|
39
38
|
config.include TestData
|
40
39
|
config.include Reporting
|
41
40
|
config.extend VersionHelper
|
42
|
-
|
43
|
-
config.mock_framework = :mocha
|
41
|
+
|
44
42
|
config.before(:all) do
|
45
43
|
rebuild_model
|
46
44
|
end
|
data/spec/support/assertions.rb
CHANGED
@@ -51,8 +51,8 @@ module Assertions
|
|
51
51
|
url = "http:#{url}" unless url =~ /http/
|
52
52
|
Net::HTTP.get_response(URI.parse(url)) do |response|
|
53
53
|
assert_equal "200",
|
54
|
-
|
55
|
-
|
54
|
+
response.code,
|
55
|
+
"Expected HTTP response code 200, got #{response.code}"
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
@@ -60,7 +60,7 @@ module Assertions
|
|
60
60
|
url = "http:#{url}" unless url =~ /http/
|
61
61
|
Net::HTTP.get_response(URI.parse(url)) do |response|
|
62
62
|
assert_equal "404", response.code,
|
63
|
-
|
63
|
+
"Expected HTTP response code 404, got #{response.code}"
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
@@ -68,14 +68,16 @@ module Assertions
|
|
68
68
|
url = "http:#{url}" unless url =~ /http/
|
69
69
|
Net::HTTP.get_response(URI.parse(url)) do |response|
|
70
70
|
assert_equal "403", response.code,
|
71
|
-
|
71
|
+
"Expected HTTP response code 403, got #{response.code}"
|
72
72
|
end
|
73
73
|
end
|
74
74
|
|
75
75
|
def assert_frame_dimensions(range, frames)
|
76
76
|
frames.each_with_index do |frame, frame_index|
|
77
|
-
frame.split(
|
78
|
-
assert range.include?(dimension.to_i),
|
77
|
+
frame.split("x").each_with_index do |dimension, dimension_index|
|
78
|
+
assert range.include?(dimension.to_i),
|
79
|
+
"Frame #{frame_index}[#{dimension_index}] should have been within #{range.inspect},
|
80
|
+
but was #{dimension}"
|
79
81
|
end
|
80
82
|
end
|
81
83
|
end
|
data/spec/support/fake_model.rb
CHANGED