paperclip 3.5.4 → 6.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.codeclimate.yml +17 -0
- data/.github/issue_template.md +3 -0
- data/.gitignore +0 -6
- data/.hound.yml +1055 -0
- data/.rubocop.yml +1 -0
- data/.travis.yml +17 -20
- data/Appraisals +4 -16
- data/CONTRIBUTING.md +29 -13
- data/Gemfile +11 -3
- data/LICENSE +1 -3
- data/MIGRATING-ES.md +317 -0
- data/MIGRATING.md +375 -0
- data/NEWS +262 -49
- data/README.md +496 -169
- data/RELEASING.md +17 -0
- data/Rakefile +6 -8
- data/UPGRADING +12 -9
- data/features/basic_integration.feature +27 -8
- data/features/migration.feature +0 -24
- data/features/step_definitions/attachment_steps.rb +44 -36
- data/features/step_definitions/html_steps.rb +2 -2
- data/features/step_definitions/rails_steps.rb +68 -37
- data/features/step_definitions/s3_steps.rb +2 -2
- data/features/step_definitions/web_steps.rb +1 -103
- data/features/support/env.rb +3 -2
- data/features/support/file_helpers.rb +2 -2
- data/features/support/fixtures/gemfile.txt +1 -1
- data/features/support/paths.rb +1 -1
- data/features/support/rails.rb +2 -25
- data/gemfiles/4.2.gemfile +17 -0
- data/gemfiles/5.0.gemfile +17 -0
- data/lib/generators/paperclip/paperclip_generator.rb +9 -3
- data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +2 -2
- data/lib/paperclip/attachment.rb +170 -52
- data/lib/paperclip/attachment_registry.rb +3 -2
- data/lib/paperclip/callbacks.rb +13 -1
- data/lib/paperclip/content_type_detector.rb +26 -22
- data/lib/paperclip/errors.rb +8 -1
- data/lib/paperclip/file_command_content_type_detector.rb +6 -8
- data/lib/paperclip/filename_cleaner.rb +0 -1
- data/lib/paperclip/geometry_detector_factory.rb +6 -4
- data/lib/paperclip/geometry_parser_factory.rb +1 -1
- data/lib/paperclip/glue.rb +1 -1
- data/lib/paperclip/has_attached_file.rb +17 -1
- data/lib/paperclip/helpers.rb +15 -11
- data/lib/paperclip/interpolations/plural_cache.rb +6 -5
- data/lib/paperclip/interpolations.rb +31 -13
- data/lib/paperclip/io_adapters/abstract_adapter.rb +34 -5
- data/lib/paperclip/io_adapters/attachment_adapter.rb +19 -8
- data/lib/paperclip/io_adapters/data_uri_adapter.rb +11 -16
- data/lib/paperclip/io_adapters/empty_string_adapter.rb +5 -4
- data/lib/paperclip/io_adapters/file_adapter.rb +12 -6
- data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +8 -7
- data/lib/paperclip/io_adapters/identity_adapter.rb +12 -6
- data/lib/paperclip/io_adapters/nil_adapter.rb +8 -5
- data/lib/paperclip/io_adapters/registry.rb +6 -2
- data/lib/paperclip/io_adapters/stringio_adapter.rb +15 -16
- data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +10 -6
- data/lib/paperclip/io_adapters/uri_adapter.rb +43 -19
- data/lib/paperclip/locales/en.yml +1 -0
- data/lib/paperclip/logger.rb +1 -1
- data/lib/paperclip/matchers/have_attached_file_matcher.rb +2 -1
- data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +4 -4
- data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +2 -1
- data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +2 -1
- data/lib/paperclip/media_type_spoof_detector.rb +93 -0
- data/lib/paperclip/processor.rb +15 -43
- data/lib/paperclip/processor_helpers.rb +50 -0
- data/lib/paperclip/rails_environment.rb +25 -0
- data/lib/paperclip/schema.rb +10 -8
- data/lib/paperclip/storage/filesystem.rb +14 -3
- data/lib/paperclip/storage/fog.rb +38 -20
- data/lib/paperclip/storage/s3.rb +124 -73
- data/lib/paperclip/style.rb +8 -3
- data/lib/paperclip/tempfile_factory.rb +5 -1
- data/lib/paperclip/thumbnail.rb +34 -19
- data/lib/paperclip/url_generator.rb +26 -14
- data/lib/paperclip/validators/attachment_content_type_validator.rb +4 -0
- data/lib/paperclip/validators/attachment_file_name_validator.rb +80 -0
- data/lib/paperclip/validators/attachment_file_type_ignorance_validator.rb +29 -0
- data/lib/paperclip/validators/attachment_presence_validator.rb +4 -0
- data/lib/paperclip/validators/attachment_size_validator.rb +5 -3
- data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +31 -0
- data/lib/paperclip/validators.rb +11 -4
- data/lib/paperclip/version.rb +3 -1
- data/lib/paperclip.rb +31 -11
- data/lib/tasks/paperclip.rake +34 -5
- data/paperclip.gemspec +21 -16
- data/shoulda_macros/paperclip.rb +0 -1
- data/spec/paperclip/attachment_definitions_spec.rb +13 -0
- data/{test/attachment_processing_test.rb → spec/paperclip/attachment_processing_spec.rb} +17 -21
- data/spec/paperclip/attachment_registry_spec.rb +158 -0
- data/{test/attachment_test.rb → spec/paperclip/attachment_spec.rb} +519 -409
- data/{test/content_type_detector_test.rb → spec/paperclip/content_type_detector_spec.rb} +17 -20
- data/spec/paperclip/file_command_content_type_detector_spec.rb +40 -0
- data/spec/paperclip/filename_cleaner_spec.rb +13 -0
- data/spec/paperclip/geometry_detector_spec.rb +39 -0
- data/{test/geometry_parser_test.rb → spec/paperclip/geometry_parser_spec.rb} +27 -27
- data/{test/geometry_test.rb → spec/paperclip/geometry_spec.rb} +50 -52
- data/spec/paperclip/glue_spec.rb +44 -0
- data/spec/paperclip/has_attached_file_spec.rb +158 -0
- data/{test/integration_test.rb → spec/paperclip/integration_spec.rb} +174 -129
- data/{test/interpolations_test.rb → spec/paperclip/interpolations_spec.rb} +79 -46
- data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +160 -0
- data/{test/io_adapters/attachment_adapter_test.rb → spec/paperclip/io_adapters/attachment_adapter_spec.rb} +33 -32
- data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +89 -0
- data/spec/paperclip/io_adapters/empty_string_adapter_spec.rb +17 -0
- data/{test/io_adapters/file_adapter_test.rb → spec/paperclip/io_adapters/file_adapter_spec.rb} +38 -42
- data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +138 -0
- data/spec/paperclip/io_adapters/identity_adapter_spec.rb +8 -0
- data/{test/io_adapters/nil_adapter_test.rb → spec/paperclip/io_adapters/nil_adapter_spec.rb} +7 -7
- data/{test/io_adapters/registry_test.rb → spec/paperclip/io_adapters/registry_spec.rb} +12 -9
- data/{test/io_adapters/stringio_adapter_test.rb → spec/paperclip/io_adapters/stringio_adapter_spec.rb} +21 -18
- data/{test/io_adapters/uploaded_file_adapter_test.rb → spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb} +46 -46
- data/spec/paperclip/io_adapters/uri_adapter_spec.rb +220 -0
- data/spec/paperclip/matchers/have_attached_file_matcher_spec.rb +19 -0
- data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +109 -0
- data/spec/paperclip/matchers/validate_attachment_presence_matcher_spec.rb +69 -0
- data/spec/paperclip/matchers/validate_attachment_size_matcher_spec.rb +88 -0
- data/spec/paperclip/media_type_spoof_detector_spec.rb +120 -0
- data/spec/paperclip/meta_class_spec.rb +30 -0
- data/spec/paperclip/paperclip_missing_attachment_styles_spec.rb +84 -0
- data/spec/paperclip/paperclip_spec.rb +192 -0
- data/spec/paperclip/plural_cache_spec.rb +37 -0
- data/spec/paperclip/processor_helpers_spec.rb +57 -0
- data/{test/processor_test.rb → spec/paperclip/processor_spec.rb} +7 -7
- data/spec/paperclip/rails_environment_spec.rb +33 -0
- data/{test/rake_test.rb → spec/paperclip/rake_spec.rb} +15 -15
- data/spec/paperclip/schema_spec.rb +248 -0
- data/{test/storage/filesystem_test.rb → spec/paperclip/storage/filesystem_spec.rb} +18 -18
- data/spec/paperclip/storage/fog_spec.rb +566 -0
- data/spec/paperclip/storage/s3_live_spec.rb +188 -0
- data/spec/paperclip/storage/s3_spec.rb +1693 -0
- data/spec/paperclip/style_spec.rb +254 -0
- data/spec/paperclip/tempfile_factory_spec.rb +33 -0
- data/spec/paperclip/tempfile_spec.rb +35 -0
- data/{test/thumbnail_test.rb → spec/paperclip/thumbnail_spec.rb} +158 -137
- data/spec/paperclip/url_generator_spec.rb +221 -0
- data/spec/paperclip/validators/attachment_content_type_validator_spec.rb +322 -0
- data/spec/paperclip/validators/attachment_file_name_validator_spec.rb +160 -0
- data/{test/validators/attachment_presence_validator_test.rb → spec/paperclip/validators/attachment_presence_validator_spec.rb} +20 -20
- data/{test/validators/attachment_size_validator_test.rb → spec/paperclip/validators/attachment_size_validator_spec.rb} +77 -64
- data/spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb +52 -0
- data/spec/paperclip/validators_spec.rb +164 -0
- data/spec/spec_helper.rb +46 -0
- data/spec/support/assertions.rb +82 -0
- data/spec/support/fake_model.rb +25 -0
- data/spec/support/fake_rails.rb +12 -0
- data/spec/support/fixtures/empty.html +1 -0
- data/spec/support/fixtures/empty.xlsx +0 -0
- data/spec/support/fixtures/spaced file.jpg +0 -0
- data/spec/support/matchers/accept.rb +5 -0
- data/spec/support/matchers/exist.rb +5 -0
- data/spec/support/matchers/have_column.rb +23 -0
- data/{test → spec}/support/mock_attachment.rb +2 -0
- data/{test → spec}/support/mock_url_generator_builder.rb +2 -2
- data/spec/support/model_reconstruction.rb +68 -0
- data/spec/support/reporting.rb +11 -0
- data/spec/support/test_data.rb +13 -0
- data/spec/support/version_helper.rb +9 -0
- metadata +344 -226
- data/RUNNING_TESTS.md +0 -4
- data/cucumber/paperclip_steps.rb +0 -6
- data/gemfiles/3.0.gemfile +0 -11
- data/gemfiles/3.1.gemfile +0 -11
- data/gemfiles/3.2.gemfile +0 -11
- data/gemfiles/4.0.gemfile +0 -11
- data/test/attachment_definitions_test.rb +0 -12
- data/test/attachment_registry_test.rb +0 -88
- data/test/file_command_content_type_detector_test.rb +0 -27
- data/test/filename_cleaner_test.rb +0 -14
- data/test/generator_test.rb +0 -84
- data/test/geometry_detector_test.rb +0 -24
- data/test/has_attached_file_test.rb +0 -125
- data/test/helper.rb +0 -232
- data/test/io_adapters/abstract_adapter_test.rb +0 -58
- data/test/io_adapters/data_uri_adapter_test.rb +0 -74
- data/test/io_adapters/empty_string_adapter_test.rb +0 -18
- data/test/io_adapters/http_url_proxy_adapter_test.rb +0 -102
- data/test/io_adapters/identity_adapter_test.rb +0 -8
- data/test/io_adapters/uri_adapter_test.rb +0 -102
- data/test/matchers/have_attached_file_matcher_test.rb +0 -24
- data/test/matchers/validate_attachment_content_type_matcher_test.rb +0 -110
- data/test/matchers/validate_attachment_presence_matcher_test.rb +0 -69
- data/test/matchers/validate_attachment_size_matcher_test.rb +0 -86
- data/test/meta_class_test.rb +0 -32
- data/test/paperclip_missing_attachment_styles_test.rb +0 -90
- data/test/paperclip_test.rb +0 -217
- data/test/plural_cache_test.rb +0 -36
- data/test/schema_test.rb +0 -200
- data/test/storage/fog_test.rb +0 -473
- data/test/storage/s3_live_test.rb +0 -179
- data/test/storage/s3_test.rb +0 -1356
- data/test/style_test.rb +0 -213
- data/test/support/mock_model.rb +0 -2
- data/test/tempfile_factory_test.rb +0 -17
- data/test/url_generator_test.rb +0 -187
- data/test/validators/attachment_content_type_validator_test.rb +0 -324
- data/test/validators_test.rb +0 -61
- /data/{test → spec}/database.yml +0 -0
- /data/{test → spec/support}/fixtures/12k.png +0 -0
- /data/{test → spec/support}/fixtures/50x50.png +0 -0
- /data/{test → spec/support}/fixtures/5k.png +0 -0
- /data/{test → spec/support}/fixtures/animated +0 -0
- /data/{test → spec/support}/fixtures/animated.gif +0 -0
- /data/{test → spec/support}/fixtures/animated.unknown +0 -0
- /data/{test → spec/support}/fixtures/bad.png +0 -0
- /data/{test → spec/support}/fixtures/fog.yml +0 -0
- /data/{test → spec/support}/fixtures/rotated.jpg +0 -0
- /data/{test → spec/support}/fixtures/s3.yml +0 -0
- /data/{test → spec/support}/fixtures/spaced file.png +0 -0
- /data/{test → spec/support}/fixtures/text.txt +0 -0
- /data/{test → spec/support}/fixtures/twopage.pdf +0 -0
- /data/{test → spec/support}/fixtures/uppercase.PNG +0 -0
- /data/{test → spec}/support/mock_interpolator.rb +0 -0
@@ -1,56 +1,56 @@
|
|
1
|
-
require '
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
describe Paperclip::Validators::AttachmentPresenceValidator do
|
4
|
+
before do
|
5
5
|
rebuild_model
|
6
6
|
@dummy = Dummy.new
|
7
7
|
end
|
8
8
|
|
9
9
|
def build_validator(options={})
|
10
10
|
@validator = Paperclip::Validators::AttachmentPresenceValidator.new(options.merge(
|
11
|
-
:
|
11
|
+
attributes: :avatar
|
12
12
|
))
|
13
13
|
end
|
14
14
|
|
15
15
|
context "nil attachment" do
|
16
|
-
|
16
|
+
before do
|
17
17
|
@dummy.avatar = nil
|
18
18
|
end
|
19
19
|
|
20
20
|
context "with default options" do
|
21
|
-
|
21
|
+
before do
|
22
22
|
build_validator
|
23
23
|
@validator.validate(@dummy)
|
24
24
|
end
|
25
25
|
|
26
|
-
|
26
|
+
it "adds error on the attachment" do
|
27
27
|
assert @dummy.errors[:avatar].present?
|
28
28
|
end
|
29
29
|
|
30
|
-
|
30
|
+
it "does not add an error on the file_name attribute" do
|
31
31
|
assert @dummy.errors[:avatar_file_name].blank?
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
35
35
|
context "with :if option" do
|
36
36
|
context "returning true" do
|
37
|
-
|
38
|
-
build_validator :
|
37
|
+
before do
|
38
|
+
build_validator if: true
|
39
39
|
@validator.validate(@dummy)
|
40
40
|
end
|
41
41
|
|
42
|
-
|
42
|
+
it "performs a validation" do
|
43
43
|
assert @dummy.errors[:avatar].present?
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
47
|
context "returning false" do
|
48
|
-
|
49
|
-
build_validator :
|
48
|
+
before do
|
49
|
+
build_validator if: false
|
50
50
|
@validator.validate(@dummy)
|
51
51
|
end
|
52
52
|
|
53
|
-
|
53
|
+
it "performs a validation" do
|
54
54
|
assert @dummy.errors[:avatar].present?
|
55
55
|
end
|
56
56
|
end
|
@@ -58,27 +58,27 @@ class AttachmentPresenceValidatorTest < Test::Unit::TestCase
|
|
58
58
|
end
|
59
59
|
|
60
60
|
context "with attachment" do
|
61
|
-
|
61
|
+
before do
|
62
62
|
build_validator
|
63
|
-
@dummy.avatar = StringIO.new('
|
63
|
+
@dummy.avatar = StringIO.new('.\n')
|
64
64
|
@validator.validate(@dummy)
|
65
65
|
end
|
66
66
|
|
67
|
-
|
67
|
+
it "does not add error on the attachment" do
|
68
68
|
assert @dummy.errors[:avatar].blank?
|
69
69
|
end
|
70
70
|
|
71
|
-
|
71
|
+
it "does not add an error on the file_name attribute" do
|
72
72
|
assert @dummy.errors[:avatar_file_name].blank?
|
73
73
|
end
|
74
74
|
end
|
75
75
|
|
76
76
|
context "using the helper" do
|
77
|
-
|
77
|
+
before do
|
78
78
|
Dummy.validates_attachment_presence :avatar
|
79
79
|
end
|
80
80
|
|
81
|
-
|
81
|
+
it "adds the validator to the class" do
|
82
82
|
assert Dummy.validators_on(:avatar).any?{ |validator| validator.kind == :attachment_presence }
|
83
83
|
end
|
84
84
|
end
|
@@ -1,27 +1,27 @@
|
|
1
|
-
require '
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
describe Paperclip::Validators::AttachmentSizeValidator do
|
4
|
+
before do
|
5
5
|
rebuild_model
|
6
6
|
@dummy = Dummy.new
|
7
7
|
end
|
8
8
|
|
9
9
|
def build_validator(options)
|
10
10
|
@validator = Paperclip::Validators::AttachmentSizeValidator.new(options.merge(
|
11
|
-
:
|
11
|
+
attributes: :avatar
|
12
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
18
|
@dummy.stubs(:avatar_file_size).returns(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
|
@@ -30,29 +30,28 @@ class AttachmentSizeValidatorTest < Test::Unit::TestCase
|
|
30
30
|
|
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
34
|
@dummy.stubs(:avatar_file_size).returns(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
|
-
|
49
|
-
|
50
|
-
"Error added to base attribute as something other than a String"
|
48
|
+
it "adds error to base object as a string" do
|
49
|
+
expect(@dummy.errors[:avatar].first).to be_a String
|
51
50
|
end
|
52
51
|
|
53
52
|
if options[:message]
|
54
|
-
|
55
|
-
|
53
|
+
it "returns a correct error message" do
|
54
|
+
expect(@dummy.errors[:avatar_file_size]).to include options[:message]
|
56
55
|
end
|
57
56
|
end
|
58
57
|
end
|
@@ -60,8 +59,8 @@ class AttachmentSizeValidatorTest < Test::Unit::TestCase
|
|
60
59
|
|
61
60
|
context "with :in option" do
|
62
61
|
context "as a range" do
|
63
|
-
|
64
|
-
build_validator :
|
62
|
+
before do
|
63
|
+
build_validator in: (5.kilobytes..10.kilobytes)
|
65
64
|
end
|
66
65
|
|
67
66
|
should_allow_attachment_file_size(7.kilobytes)
|
@@ -70,8 +69,8 @@ class AttachmentSizeValidatorTest < Test::Unit::TestCase
|
|
70
69
|
end
|
71
70
|
|
72
71
|
context "as a proc" do
|
73
|
-
|
74
|
-
build_validator :
|
72
|
+
before do
|
73
|
+
build_validator in: lambda { |avatar| (5.kilobytes..10.kilobytes) }
|
75
74
|
end
|
76
75
|
|
77
76
|
should_allow_attachment_file_size(7.kilobytes)
|
@@ -82,8 +81,8 @@ class AttachmentSizeValidatorTest < Test::Unit::TestCase
|
|
82
81
|
|
83
82
|
context "with :greater_than option" do
|
84
83
|
context "as number" do
|
85
|
-
|
86
|
-
build_validator :
|
84
|
+
before do
|
85
|
+
build_validator greater_than: 10.kilobytes
|
87
86
|
end
|
88
87
|
|
89
88
|
should_allow_attachment_file_size 11.kilobytes
|
@@ -91,8 +90,8 @@ class AttachmentSizeValidatorTest < Test::Unit::TestCase
|
|
91
90
|
end
|
92
91
|
|
93
92
|
context "as a proc" do
|
94
|
-
|
95
|
-
build_validator :
|
93
|
+
before do
|
94
|
+
build_validator greater_than: lambda { |avatar| 10.kilobytes }
|
96
95
|
end
|
97
96
|
|
98
97
|
should_allow_attachment_file_size 11.kilobytes
|
@@ -102,8 +101,8 @@ class AttachmentSizeValidatorTest < Test::Unit::TestCase
|
|
102
101
|
|
103
102
|
context "with :less_than option" do
|
104
103
|
context "as number" do
|
105
|
-
|
106
|
-
build_validator :
|
104
|
+
before do
|
105
|
+
build_validator less_than: 10.kilobytes
|
107
106
|
end
|
108
107
|
|
109
108
|
should_allow_attachment_file_size 9.kilobytes
|
@@ -111,8 +110,8 @@ class AttachmentSizeValidatorTest < Test::Unit::TestCase
|
|
111
110
|
end
|
112
111
|
|
113
112
|
context "as a proc" do
|
114
|
-
|
115
|
-
build_validator :
|
113
|
+
before do
|
114
|
+
build_validator less_than: lambda { |avatar| 10.kilobytes }
|
116
115
|
end
|
117
116
|
|
118
117
|
should_allow_attachment_file_size 9.kilobytes
|
@@ -122,9 +121,9 @@ class AttachmentSizeValidatorTest < Test::Unit::TestCase
|
|
122
121
|
|
123
122
|
context "with :greater_than and :less_than option" do
|
124
123
|
context "as numbers" do
|
125
|
-
|
126
|
-
build_validator :
|
127
|
-
:
|
124
|
+
before do
|
125
|
+
build_validator greater_than: 5.kilobytes,
|
126
|
+
less_than: 10.kilobytes
|
128
127
|
end
|
129
128
|
|
130
129
|
should_allow_attachment_file_size 7.kilobytes
|
@@ -133,9 +132,9 @@ class AttachmentSizeValidatorTest < Test::Unit::TestCase
|
|
133
132
|
end
|
134
133
|
|
135
134
|
context "as a proc" do
|
136
|
-
|
137
|
-
build_validator :
|
138
|
-
:
|
135
|
+
before do
|
136
|
+
build_validator greater_than: lambda { |avatar| 5.kilobytes },
|
137
|
+
less_than: lambda { |avatar| 10.kilobytes }
|
139
138
|
end
|
140
139
|
|
141
140
|
should_allow_attachment_file_size 7.kilobytes
|
@@ -146,77 +145,91 @@ class AttachmentSizeValidatorTest < Test::Unit::TestCase
|
|
146
145
|
|
147
146
|
context "with :message option" do
|
148
147
|
context "given a range" do
|
149
|
-
|
150
|
-
build_validator :
|
151
|
-
:
|
148
|
+
before do
|
149
|
+
build_validator in: (5.kilobytes..10.kilobytes),
|
150
|
+
message: "is invalid. (Between %{min} and %{max} please.)"
|
152
151
|
end
|
153
152
|
|
154
|
-
should_not_allow_attachment_file_size
|
155
|
-
|
153
|
+
should_not_allow_attachment_file_size(
|
154
|
+
11.kilobytes,
|
155
|
+
message: "is invalid. (Between 5 KB and 10 KB please.)"
|
156
|
+
)
|
156
157
|
end
|
157
158
|
|
158
159
|
context "given :less_than and :greater_than" do
|
159
|
-
|
160
|
-
build_validator :
|
161
|
-
:
|
162
|
-
:
|
160
|
+
before do
|
161
|
+
build_validator less_than: 10.kilobytes,
|
162
|
+
greater_than: 5.kilobytes,
|
163
|
+
message: "is invalid. (Between %{min} and %{max} please.)"
|
163
164
|
end
|
164
165
|
|
165
|
-
should_not_allow_attachment_file_size
|
166
|
-
|
166
|
+
should_not_allow_attachment_file_size(
|
167
|
+
11.kilobytes,
|
168
|
+
message: "is invalid. (Between 5 KB and 10 KB please.)"
|
169
|
+
)
|
167
170
|
end
|
168
171
|
end
|
169
172
|
|
170
173
|
context "default error messages" do
|
171
174
|
context "given :less_than and :greater_than" do
|
172
|
-
|
173
|
-
build_validator :
|
174
|
-
:
|
175
|
+
before do
|
176
|
+
build_validator greater_than: 5.kilobytes,
|
177
|
+
less_than: 10.kilobytes
|
175
178
|
end
|
176
179
|
|
177
|
-
should_not_allow_attachment_file_size
|
178
|
-
|
179
|
-
|
180
|
-
|
180
|
+
should_not_allow_attachment_file_size(
|
181
|
+
11.kilobytes,
|
182
|
+
message: "must be less than 10 KB"
|
183
|
+
)
|
184
|
+
|
185
|
+
should_not_allow_attachment_file_size(
|
186
|
+
4.kilobytes,
|
187
|
+
message: "must be greater than 5 KB"
|
188
|
+
)
|
181
189
|
end
|
182
190
|
|
183
191
|
context "given a size range" do
|
184
|
-
|
185
|
-
build_validator :
|
192
|
+
before do
|
193
|
+
build_validator in: (5.kilobytes..10.kilobytes)
|
186
194
|
end
|
187
195
|
|
188
|
-
should_not_allow_attachment_file_size
|
189
|
-
|
190
|
-
|
191
|
-
|
196
|
+
should_not_allow_attachment_file_size(
|
197
|
+
11.kilobytes,
|
198
|
+
message: "must be in between 5 KB and 10 KB"
|
199
|
+
)
|
200
|
+
|
201
|
+
should_not_allow_attachment_file_size(
|
202
|
+
4.kilobytes,
|
203
|
+
message: "must be in between 5 KB and 10 KB"
|
204
|
+
)
|
192
205
|
end
|
193
206
|
end
|
194
207
|
|
195
208
|
context "using the helper" do
|
196
|
-
|
197
|
-
Dummy.validates_attachment_size :avatar, :
|
209
|
+
before do
|
210
|
+
Dummy.validates_attachment_size :avatar, in: (5.kilobytes..10.kilobytes)
|
198
211
|
end
|
199
212
|
|
200
|
-
|
213
|
+
it "adds the validator to the class" do
|
201
214
|
assert Dummy.validators_on(:avatar).any?{ |validator| validator.kind == :attachment_size }
|
202
215
|
end
|
203
216
|
end
|
204
217
|
|
205
218
|
context "given options" do
|
206
|
-
|
219
|
+
it "raises argument error if no required argument was given" do
|
207
220
|
assert_raises(ArgumentError) do
|
208
|
-
build_validator :
|
221
|
+
build_validator message: "Some message"
|
209
222
|
end
|
210
223
|
end
|
211
224
|
|
212
225
|
(Paperclip::Validators::AttachmentSizeValidator::AVAILABLE_CHECKS).each do |argument|
|
213
|
-
|
226
|
+
it "does not raise arguemnt error if #{argument} was given" do
|
214
227
|
build_validator argument => 5.kilobytes
|
215
228
|
end
|
216
229
|
end
|
217
230
|
|
218
|
-
|
219
|
-
build_validator :
|
231
|
+
it "does not raise argument error if :in was given" do
|
232
|
+
build_validator in: (5.kilobytes..10.kilobytes)
|
220
233
|
end
|
221
234
|
end
|
222
235
|
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Paperclip::Validators::MediaTypeSpoofDetectionValidator do
|
4
|
+
before do
|
5
|
+
rebuild_model
|
6
|
+
@dummy = Dummy.new
|
7
|
+
end
|
8
|
+
|
9
|
+
def build_validator(options = {})
|
10
|
+
@validator = Paperclip::Validators::MediaTypeSpoofDetectionValidator.new(options.merge(
|
11
|
+
attributes: :avatar
|
12
|
+
))
|
13
|
+
end
|
14
|
+
|
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 }
|
17
|
+
end
|
18
|
+
|
19
|
+
it "is not on the attachment when explicitly rejected" do
|
20
|
+
rebuild_model validate_media_type: false
|
21
|
+
assert Dummy.validators_on(:avatar).none?{ |validator| validator.kind == :media_type_spoof_detection }
|
22
|
+
end
|
23
|
+
|
24
|
+
it "returns default error message for spoofed media type" do
|
25
|
+
build_validator
|
26
|
+
file = File.new(fixture_file("5k.png"), "rb")
|
27
|
+
@dummy.avatar.assign(file)
|
28
|
+
|
29
|
+
detector = mock("detector", :spoofed? => true)
|
30
|
+
Paperclip::MediaTypeSpoofDetector.stubs(:using).returns(detector)
|
31
|
+
@validator.validate(@dummy)
|
32
|
+
|
33
|
+
assert_equal I18n.t("errors.messages.spoofed_media_type"), @dummy.errors[:avatar].first
|
34
|
+
end
|
35
|
+
|
36
|
+
it "runs when attachment is dirty" do
|
37
|
+
build_validator
|
38
|
+
file = File.new(fixture_file("5k.png"), "rb")
|
39
|
+
@dummy.avatar.assign(file)
|
40
|
+
Paperclip::MediaTypeSpoofDetector.stubs(:using).returns(stub(:spoofed? => false))
|
41
|
+
|
42
|
+
@dummy.valid?
|
43
|
+
|
44
|
+
assert_received(Paperclip::MediaTypeSpoofDetector, :using){|e| e.once }
|
45
|
+
end
|
46
|
+
|
47
|
+
it "does not run when attachment is not dirty" do
|
48
|
+
Paperclip::MediaTypeSpoofDetector.stubs(:using).never
|
49
|
+
@dummy.valid?
|
50
|
+
assert_received(Paperclip::MediaTypeSpoofDetector, :using){|e| e.never }
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,164 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Paperclip::Validators do
|
4
|
+
context "using the helper" do
|
5
|
+
before do
|
6
|
+
rebuild_class
|
7
|
+
Dummy.validates_attachment :avatar, presence: true, content_type: { content_type: "image/jpeg" }, size: { in: 0..10240 }
|
8
|
+
end
|
9
|
+
|
10
|
+
it "adds the attachment_presence validator to the class" do
|
11
|
+
assert Dummy.validators_on(:avatar).any?{ |validator| validator.kind == :attachment_presence }
|
12
|
+
end
|
13
|
+
|
14
|
+
it "adds the attachment_content_type validator to the class" do
|
15
|
+
assert Dummy.validators_on(:avatar).any?{ |validator| validator.kind == :attachment_content_type }
|
16
|
+
end
|
17
|
+
|
18
|
+
it "adds the attachment_size validator to the class" do
|
19
|
+
assert Dummy.validators_on(:avatar).any?{ |validator| validator.kind == :attachment_size }
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'prevents you from attaching a file that violates that validation' do
|
23
|
+
Dummy.class_eval{ validate(:name) { raise "DO NOT RUN THIS" } }
|
24
|
+
dummy = Dummy.new(avatar: File.new(fixture_file("12k.png")))
|
25
|
+
expect(dummy.errors.keys).to match_array [:avatar_content_type, :avatar, :avatar_file_size]
|
26
|
+
assert_raises(RuntimeError){ dummy.valid? }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'using the helper with array of validations' do
|
31
|
+
before do
|
32
|
+
rebuild_class
|
33
|
+
Dummy.validates_attachment :avatar, file_type_ignorance: true, file_name: [
|
34
|
+
{ matches: /\A.*\.jpe?g\z/i, message: :invalid_extension },
|
35
|
+
{ matches: /\A.{,8}\..+\z/i, message: [:too_long, count: 8] },
|
36
|
+
]
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'adds the attachment_file_name validator to the class' do
|
40
|
+
assert Dummy.validators_on(:avatar).any?{ |validator| validator.kind == :attachment_file_name }
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'adds the attachment_file_name validator with two validations' do
|
44
|
+
assert_equal 2, Dummy.validators_on(:avatar).select{ |validator| validator.kind == :attachment_file_name }.size
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'prevents you from attaching a file that violates all of these validations' do
|
48
|
+
Dummy.class_eval{ validate(:name) { raise 'DO NOT RUN THIS' } }
|
49
|
+
dummy = Dummy.new(avatar: File.new(fixture_file('spaced file.png')))
|
50
|
+
expect(dummy.errors.keys).to match_array [:avatar, :avatar_file_name]
|
51
|
+
assert_raises(RuntimeError){ dummy.valid? }
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'prevents you from attaching a file that violates only first of these validations' do
|
55
|
+
Dummy.class_eval{ validate(:name) { raise 'DO NOT RUN THIS' } }
|
56
|
+
dummy = Dummy.new(avatar: File.new(fixture_file('5k.png')))
|
57
|
+
expect(dummy.errors.keys).to match_array [:avatar, :avatar_file_name]
|
58
|
+
assert_raises(RuntimeError){ dummy.valid? }
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'prevents you from attaching a file that violates only second of these validations' do
|
62
|
+
Dummy.class_eval{ validate(:name) { raise 'DO NOT RUN THIS' } }
|
63
|
+
dummy = Dummy.new(avatar: File.new(fixture_file('spaced file.jpg')))
|
64
|
+
expect(dummy.errors.keys).to match_array [:avatar, :avatar_file_name]
|
65
|
+
assert_raises(RuntimeError){ dummy.valid? }
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'allows you to attach a file that does not violate these validations' do
|
69
|
+
dummy = Dummy.new(avatar: File.new(fixture_file('rotated.jpg')))
|
70
|
+
expect(dummy.errors.full_messages).to be_empty
|
71
|
+
assert dummy.valid?
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
context "using the helper with a conditional" do
|
76
|
+
before do
|
77
|
+
rebuild_class
|
78
|
+
Dummy.validates_attachment :avatar, presence: true,
|
79
|
+
content_type: { content_type: "image/jpeg" },
|
80
|
+
size: { in: 0..10240 },
|
81
|
+
if: :title_present?
|
82
|
+
end
|
83
|
+
|
84
|
+
it "validates the attachment if title is present" do
|
85
|
+
Dummy.class_eval do
|
86
|
+
def title_present?
|
87
|
+
true
|
88
|
+
end
|
89
|
+
end
|
90
|
+
dummy = Dummy.new(avatar: File.new(fixture_file("12k.png")))
|
91
|
+
expect(dummy.errors.keys).to match_array [:avatar_content_type, :avatar, :avatar_file_size]
|
92
|
+
end
|
93
|
+
|
94
|
+
it "does not validate attachment if title is not present" do
|
95
|
+
Dummy.class_eval do
|
96
|
+
def title_present?
|
97
|
+
false
|
98
|
+
end
|
99
|
+
end
|
100
|
+
dummy = Dummy.new(avatar: File.new(fixture_file("12k.png")))
|
101
|
+
assert_equal [], dummy.errors.keys
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
context 'with no other validations on the Dummy#avatar attachment' do
|
106
|
+
before do
|
107
|
+
reset_class("Dummy")
|
108
|
+
Dummy.has_attached_file :avatar
|
109
|
+
Paperclip.reset_duplicate_clash_check!
|
110
|
+
end
|
111
|
+
|
112
|
+
it 'raises an error when no content_type validation exists' do
|
113
|
+
assert_raises(Paperclip::Errors::MissingRequiredValidatorError) do
|
114
|
+
Dummy.new(avatar: File.new(fixture_file("12k.png")))
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'does not raise an error when a content_type validation exists' do
|
119
|
+
Dummy.validates_attachment :avatar, content_type: { content_type: "image/jpeg" }
|
120
|
+
|
121
|
+
assert_nothing_raised do
|
122
|
+
Dummy.new(avatar: File.new(fixture_file("12k.png")))
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
it 'does not raise an error when a content_type validation exists using validates_with' do
|
127
|
+
Dummy.validates_with Paperclip::Validators::AttachmentContentTypeValidator, attributes: :attachment, content_type: 'images/jpeg'
|
128
|
+
|
129
|
+
assert_nothing_raised do
|
130
|
+
Dummy.new(avatar: File.new(fixture_file("12k.png")))
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
it 'does not raise an error when an inherited validator is used' do
|
135
|
+
class MyValidator < Paperclip::Validators::AttachmentContentTypeValidator
|
136
|
+
def initialize(options)
|
137
|
+
options[:content_type] = "images/jpeg" unless options.has_key?(:content_type)
|
138
|
+
super
|
139
|
+
end
|
140
|
+
end
|
141
|
+
Dummy.validates_with MyValidator, attributes: :attachment
|
142
|
+
|
143
|
+
assert_nothing_raised do
|
144
|
+
Dummy.new(avatar: File.new(fixture_file("12k.png")))
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
it 'does not raise an error when a file_name validation exists' do
|
149
|
+
Dummy.validates_attachment :avatar, file_name: { matches: /png$/ }
|
150
|
+
|
151
|
+
assert_nothing_raised do
|
152
|
+
Dummy.new(avatar: File.new(fixture_file("12k.png")))
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
it 'does not raise an error when a the validation has been explicitly rejected' do
|
157
|
+
Dummy.validates_attachment :avatar, file_type_ignorance: true
|
158
|
+
|
159
|
+
assert_nothing_raised do
|
160
|
+
Dummy.new(avatar: File.new(fixture_file("12k.png")))
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,46 @@
|
|
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 'mocha/api'
|
8
|
+
require 'bourne'
|
9
|
+
require 'ostruct'
|
10
|
+
require 'pathname'
|
11
|
+
require 'activerecord-import'
|
12
|
+
|
13
|
+
ROOT = Pathname(File.expand_path(File.join(File.dirname(__FILE__), '..')))
|
14
|
+
|
15
|
+
puts "Testing against version #{ActiveRecord::VERSION::STRING}"
|
16
|
+
|
17
|
+
$LOAD_PATH << File.join(ROOT, 'lib')
|
18
|
+
$LOAD_PATH << File.join(ROOT, 'lib', 'paperclip')
|
19
|
+
require File.join(ROOT, 'lib', 'paperclip.rb')
|
20
|
+
|
21
|
+
FIXTURES_DIR = File.join(File.dirname(__FILE__), "fixtures")
|
22
|
+
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
|
23
|
+
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
|
24
|
+
ActiveRecord::Base.establish_connection(config['test'])
|
25
|
+
if ActiveRecord::VERSION::STRING >= "4.2" &&
|
26
|
+
ActiveRecord::VERSION::STRING < "5.0"
|
27
|
+
ActiveRecord::Base.raise_in_transactional_callbacks = true
|
28
|
+
end
|
29
|
+
Paperclip.options[:logger] = ActiveRecord::Base.logger
|
30
|
+
|
31
|
+
Dir[File.join(ROOT, 'spec', 'support', '**', '*.rb')].each{|f| require f }
|
32
|
+
|
33
|
+
Rails = FakeRails.new('test', Pathname.new(ROOT).join('tmp'))
|
34
|
+
ActiveSupport::Deprecation.silenced = true
|
35
|
+
|
36
|
+
RSpec.configure do |config|
|
37
|
+
config.include Assertions
|
38
|
+
config.include ModelReconstruction
|
39
|
+
config.include TestData
|
40
|
+
config.include Reporting
|
41
|
+
config.extend VersionHelper
|
42
|
+
config.mock_framework = :mocha
|
43
|
+
config.before(:all) do
|
44
|
+
rebuild_model
|
45
|
+
end
|
46
|
+
end
|