paperclip 3.5.4 → 4.3.7
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of paperclip might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/.gitignore +0 -6
- data/.hound.yml +1066 -0
- data/.rubocop.yml +1 -0
- data/.travis.yml +11 -17
- data/Appraisals +6 -14
- data/CONTRIBUTING.md +13 -8
- data/Gemfile +16 -3
- data/LICENSE +1 -3
- data/NEWS +167 -49
- data/README.md +294 -75
- data/RELEASING.md +17 -0
- data/Rakefile +6 -8
- data/features/basic_integration.feature +24 -6
- data/features/step_definitions/attachment_steps.rb +30 -22
- data/features/step_definitions/html_steps.rb +2 -2
- data/features/step_definitions/rails_steps.rb +44 -14
- data/features/step_definitions/web_steps.rb +1 -103
- data/features/support/env.rb +2 -2
- data/features/support/file_helpers.rb +2 -2
- data/features/support/fixtures/gemfile.txt +1 -1
- data/features/support/rails.rb +2 -1
- data/gemfiles/3.2.gemfile +14 -6
- data/gemfiles/4.1.gemfile +19 -0
- data/gemfiles/4.2.gemfile +19 -0
- data/lib/generators/paperclip/paperclip_generator.rb +0 -2
- data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +1 -1
- data/lib/paperclip/attachment.rb +132 -38
- data/lib/paperclip/attachment_registry.rb +1 -1
- data/lib/paperclip/callbacks.rb +11 -1
- data/lib/paperclip/content_type_detector.rb +25 -22
- data/lib/paperclip/deprecations.rb +42 -0
- data/lib/paperclip/errors.rb +5 -0
- data/lib/paperclip/file_command_content_type_detector.rb +6 -8
- data/lib/paperclip/geometry_detector_factory.rb +3 -1
- data/lib/paperclip/geometry_parser_factory.rb +1 -1
- data/lib/paperclip/has_attached_file.rb +10 -0
- data/lib/paperclip/interpolations/plural_cache.rb +6 -5
- data/lib/paperclip/interpolations.rb +25 -12
- data/lib/paperclip/io_adapters/abstract_adapter.rb +3 -1
- data/lib/paperclip/io_adapters/attachment_adapter.rb +4 -4
- data/lib/paperclip/io_adapters/data_uri_adapter.rb +5 -10
- data/lib/paperclip/io_adapters/stringio_adapter.rb +6 -10
- data/lib/paperclip/io_adapters/uri_adapter.rb +30 -11
- data/lib/paperclip/locales/de.yml +18 -0
- data/lib/paperclip/locales/en.yml +1 -0
- data/lib/paperclip/locales/es.yml +18 -0
- data/lib/paperclip/locales/ja.yml +18 -0
- data/lib/paperclip/locales/pt-BR.yml +18 -0
- data/lib/paperclip/locales/zh-CN.yml +18 -0
- data/lib/paperclip/locales/zh-HK.yml +18 -0
- data/lib/paperclip/locales/zh-TW.yml +18 -0
- data/lib/paperclip/matchers/have_attached_file_matcher.rb +2 -1
- 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 +89 -0
- data/lib/paperclip/processor.rb +0 -37
- data/lib/paperclip/processor_helpers.rb +50 -0
- data/lib/paperclip/rails_environment.rb +25 -0
- data/lib/paperclip/schema.rb +10 -2
- data/lib/paperclip/storage/filesystem.rb +1 -1
- data/lib/paperclip/storage/fog.rb +18 -7
- data/lib/paperclip/storage/s3.rb +53 -22
- data/lib/paperclip/style.rb +8 -2
- data/lib/paperclip/tempfile_factory.rb +5 -1
- data/lib/paperclip/thumbnail.rb +12 -10
- data/lib/paperclip/url_generator.rb +11 -3
- 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 +11 -3
- data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +27 -0
- data/lib/paperclip/validators.rb +10 -3
- data/lib/paperclip/version.rb +1 -1
- data/lib/paperclip.rb +26 -8
- data/lib/tasks/paperclip.rake +17 -2
- data/paperclip.gemspec +16 -14
- 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} +20 -21
- data/spec/paperclip/attachment_registry_spec.rb +130 -0
- data/{test/attachment_test.rb → spec/paperclip/attachment_spec.rb} +438 -397
- data/{test/content_type_detector_test.rb → spec/paperclip/content_type_detector_spec.rb} +16 -19
- data/spec/paperclip/deprecations_spec.rb +65 -0
- data/{test/file_command_content_type_detector_test.rb → spec/paperclip/file_command_content_type_detector_spec.rb} +5 -6
- data/spec/paperclip/filename_cleaner_spec.rb +14 -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/{test/has_attached_file_test.rb → spec/paperclip/has_attached_file_spec.rb} +45 -28
- data/{test/integration_test.rb → spec/paperclip/integration_spec.rb} +134 -126
- data/{test/interpolations_test.rb → spec/paperclip/interpolations_spec.rb} +70 -46
- data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +78 -0
- data/{test/io_adapters/attachment_adapter_test.rb → spec/paperclip/io_adapters/attachment_adapter_spec.rb} +27 -29
- data/{test/io_adapters/data_uri_adapter_test.rb → spec/paperclip/io_adapters/data_uri_adapter_spec.rb} +26 -17
- 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} +36 -40
- data/{test/io_adapters/http_url_proxy_adapter_test.rb → spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb} +31 -29
- 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} +10 -7
- data/{test/io_adapters/stringio_adapter_test.rb → spec/paperclip/io_adapters/stringio_adapter_spec.rb} +20 -17
- data/{test/io_adapters/uploaded_file_adapter_test.rb → spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb} +41 -41
- data/{test/io_adapters/uri_adapter_test.rb → spec/paperclip/io_adapters/uri_adapter_spec.rb} +53 -28
- data/spec/paperclip/matchers/have_attached_file_matcher_spec.rb +19 -0
- data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +99 -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 +79 -0
- data/spec/paperclip/meta_class_spec.rb +30 -0
- data/spec/paperclip/paperclip_missing_attachment_styles_spec.rb +84 -0
- data/{test/paperclip_test.rb → spec/paperclip/paperclip_spec.rb} +53 -48
- 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} +5 -5
- 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 +535 -0
- data/spec/paperclip/storage/s3_live_spec.rb +182 -0
- data/spec/paperclip/storage/s3_spec.rb +1526 -0
- data/spec/paperclip/style_spec.rb +255 -0
- data/spec/paperclip/tempfile_factory_spec.rb +33 -0
- data/{test/thumbnail_test.rb → spec/paperclip/thumbnail_spec.rb} +123 -107
- data/spec/paperclip/url_generator_spec.rb +211 -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} +65 -58
- 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 +43 -0
- data/spec/support/assertions.rb +71 -0
- data/spec/support/deprecations.rb +9 -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/spec/support/model_reconstruction.rb +60 -0
- data/spec/support/rails_helpers.rb +7 -0
- data/spec/support/test_data.rb +13 -0
- data/spec/support/version_helper.rb +9 -0
- metadata +334 -219
- data/RUNNING_TESTS.md +0 -4
- data/gemfiles/3.0.gemfile +0 -11
- data/gemfiles/3.1.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/filename_cleaner_test.rb +0 -14
- data/test/generator_test.rb +0 -84
- data/test/geometry_detector_test.rb +0 -24
- data/test/helper.rb +0 -232
- data/test/io_adapters/abstract_adapter_test.rb +0 -58
- data/test/io_adapters/empty_string_adapter_test.rb +0 -18
- data/test/io_adapters/identity_adapter_test.rb +0 -8
- 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/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_attachment.rb +0 -0
- /data/{test → spec}/support/mock_interpolator.rb +0 -0
- /data/{test → spec}/support/mock_url_generator_builder.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,35 @@
|
|
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
|
+
def self.storage_units
|
16
|
+
if defined?(ActiveSupport::NumberHelper) # Rails 4.0+
|
17
|
+
{ 5120 => '5 KB', 10240 => '10 KB' }
|
18
|
+
else
|
19
|
+
{ 5120 => '5120 Bytes', 10240 => '10240 Bytes' }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
15
23
|
def self.should_allow_attachment_file_size(size)
|
16
24
|
context "when the attachment size is #{size}" do
|
17
|
-
|
25
|
+
it "adds error to dummy object" do
|
18
26
|
@dummy.stubs(:avatar_file_size).returns(size)
|
19
27
|
@validator.validate(@dummy)
|
20
28
|
assert @dummy.errors[:avatar_file_size].blank?,
|
21
29
|
"Expect an error message on :avatar_file_size, got none."
|
22
30
|
end
|
23
31
|
|
24
|
-
|
32
|
+
it "does not add error to the base dummy object" do
|
25
33
|
assert @dummy.errors[:avatar].blank?,
|
26
34
|
"Error added to base attribute"
|
27
35
|
end
|
@@ -30,29 +38,28 @@ class AttachmentSizeValidatorTest < Test::Unit::TestCase
|
|
30
38
|
|
31
39
|
def self.should_not_allow_attachment_file_size(size, options = {})
|
32
40
|
context "when the attachment size is #{size}" do
|
33
|
-
|
41
|
+
before do
|
34
42
|
@dummy.stubs(:avatar_file_size).returns(size)
|
35
43
|
@validator.validate(@dummy)
|
36
44
|
end
|
37
45
|
|
38
|
-
|
46
|
+
it "adds error to dummy object" do
|
39
47
|
assert @dummy.errors[:avatar_file_size].present?,
|
40
48
|
"Unexpected error message on :avatar_file_size"
|
41
49
|
end
|
42
50
|
|
43
|
-
|
51
|
+
it "adds error to the base dummy object" do
|
44
52
|
assert @dummy.errors[:avatar].present?,
|
45
53
|
"Error not added to base attribute"
|
46
54
|
end
|
47
55
|
|
48
|
-
|
49
|
-
|
50
|
-
"Error added to base attribute as something other than a String"
|
56
|
+
it "adds error to base object as a string" do
|
57
|
+
expect(@dummy.errors[:avatar].first).to be_a String
|
51
58
|
end
|
52
59
|
|
53
60
|
if options[:message]
|
54
|
-
|
55
|
-
|
61
|
+
it "returns a correct error message" do
|
62
|
+
expect(@dummy.errors[:avatar_file_size]).to include options[:message]
|
56
63
|
end
|
57
64
|
end
|
58
65
|
end
|
@@ -60,8 +67,8 @@ class AttachmentSizeValidatorTest < Test::Unit::TestCase
|
|
60
67
|
|
61
68
|
context "with :in option" do
|
62
69
|
context "as a range" do
|
63
|
-
|
64
|
-
build_validator :
|
70
|
+
before do
|
71
|
+
build_validator in: (5.kilobytes..10.kilobytes)
|
65
72
|
end
|
66
73
|
|
67
74
|
should_allow_attachment_file_size(7.kilobytes)
|
@@ -70,8 +77,8 @@ class AttachmentSizeValidatorTest < Test::Unit::TestCase
|
|
70
77
|
end
|
71
78
|
|
72
79
|
context "as a proc" do
|
73
|
-
|
74
|
-
build_validator :
|
80
|
+
before do
|
81
|
+
build_validator in: lambda { |avatar| (5.kilobytes..10.kilobytes) }
|
75
82
|
end
|
76
83
|
|
77
84
|
should_allow_attachment_file_size(7.kilobytes)
|
@@ -82,8 +89,8 @@ class AttachmentSizeValidatorTest < Test::Unit::TestCase
|
|
82
89
|
|
83
90
|
context "with :greater_than option" do
|
84
91
|
context "as number" do
|
85
|
-
|
86
|
-
build_validator :
|
92
|
+
before do
|
93
|
+
build_validator greater_than: 10.kilobytes
|
87
94
|
end
|
88
95
|
|
89
96
|
should_allow_attachment_file_size 11.kilobytes
|
@@ -91,8 +98,8 @@ class AttachmentSizeValidatorTest < Test::Unit::TestCase
|
|
91
98
|
end
|
92
99
|
|
93
100
|
context "as a proc" do
|
94
|
-
|
95
|
-
build_validator :
|
101
|
+
before do
|
102
|
+
build_validator greater_than: lambda { |avatar| 10.kilobytes }
|
96
103
|
end
|
97
104
|
|
98
105
|
should_allow_attachment_file_size 11.kilobytes
|
@@ -102,8 +109,8 @@ class AttachmentSizeValidatorTest < Test::Unit::TestCase
|
|
102
109
|
|
103
110
|
context "with :less_than option" do
|
104
111
|
context "as number" do
|
105
|
-
|
106
|
-
build_validator :
|
112
|
+
before do
|
113
|
+
build_validator less_than: 10.kilobytes
|
107
114
|
end
|
108
115
|
|
109
116
|
should_allow_attachment_file_size 9.kilobytes
|
@@ -111,8 +118,8 @@ class AttachmentSizeValidatorTest < Test::Unit::TestCase
|
|
111
118
|
end
|
112
119
|
|
113
120
|
context "as a proc" do
|
114
|
-
|
115
|
-
build_validator :
|
121
|
+
before do
|
122
|
+
build_validator less_than: lambda { |avatar| 10.kilobytes }
|
116
123
|
end
|
117
124
|
|
118
125
|
should_allow_attachment_file_size 9.kilobytes
|
@@ -122,9 +129,9 @@ class AttachmentSizeValidatorTest < Test::Unit::TestCase
|
|
122
129
|
|
123
130
|
context "with :greater_than and :less_than option" do
|
124
131
|
context "as numbers" do
|
125
|
-
|
126
|
-
build_validator :
|
127
|
-
:
|
132
|
+
before do
|
133
|
+
build_validator greater_than: 5.kilobytes,
|
134
|
+
less_than: 10.kilobytes
|
128
135
|
end
|
129
136
|
|
130
137
|
should_allow_attachment_file_size 7.kilobytes
|
@@ -133,9 +140,9 @@ class AttachmentSizeValidatorTest < Test::Unit::TestCase
|
|
133
140
|
end
|
134
141
|
|
135
142
|
context "as a proc" do
|
136
|
-
|
137
|
-
build_validator :
|
138
|
-
:
|
143
|
+
before do
|
144
|
+
build_validator greater_than: lambda { |avatar| 5.kilobytes },
|
145
|
+
less_than: lambda { |avatar| 10.kilobytes }
|
139
146
|
end
|
140
147
|
|
141
148
|
should_allow_attachment_file_size 7.kilobytes
|
@@ -146,77 +153,77 @@ class AttachmentSizeValidatorTest < Test::Unit::TestCase
|
|
146
153
|
|
147
154
|
context "with :message option" do
|
148
155
|
context "given a range" do
|
149
|
-
|
150
|
-
build_validator :
|
151
|
-
:
|
156
|
+
before do
|
157
|
+
build_validator in: (5.kilobytes..10.kilobytes),
|
158
|
+
message: "is invalid. (Between %{min} and %{max} please.)"
|
152
159
|
end
|
153
160
|
|
154
161
|
should_not_allow_attachment_file_size 11.kilobytes,
|
155
|
-
:
|
162
|
+
message: "is invalid. (Between #{storage_units[5120]} and #{storage_units[10240]} please.)"
|
156
163
|
end
|
157
164
|
|
158
165
|
context "given :less_than and :greater_than" do
|
159
|
-
|
160
|
-
build_validator :
|
161
|
-
:
|
162
|
-
:
|
166
|
+
before do
|
167
|
+
build_validator less_than: 10.kilobytes,
|
168
|
+
greater_than: 5.kilobytes,
|
169
|
+
message: "is invalid. (Between %{min} and %{max} please.)"
|
163
170
|
end
|
164
171
|
|
165
172
|
should_not_allow_attachment_file_size 11.kilobytes,
|
166
|
-
:
|
173
|
+
message: "is invalid. (Between #{storage_units[5120]} and #{storage_units[10240]} please.)"
|
167
174
|
end
|
168
175
|
end
|
169
176
|
|
170
177
|
context "default error messages" do
|
171
178
|
context "given :less_than and :greater_than" do
|
172
|
-
|
173
|
-
build_validator :
|
174
|
-
:
|
179
|
+
before do
|
180
|
+
build_validator greater_than: 5.kilobytes,
|
181
|
+
less_than: 10.kilobytes
|
175
182
|
end
|
176
183
|
|
177
184
|
should_not_allow_attachment_file_size 11.kilobytes,
|
178
|
-
:
|
185
|
+
message: "must be less than #{storage_units[10240]}"
|
179
186
|
should_not_allow_attachment_file_size 4.kilobytes,
|
180
|
-
:
|
187
|
+
message: "must be greater than #{storage_units[5120]}"
|
181
188
|
end
|
182
189
|
|
183
190
|
context "given a size range" do
|
184
|
-
|
185
|
-
build_validator :
|
191
|
+
before do
|
192
|
+
build_validator in: (5.kilobytes..10.kilobytes)
|
186
193
|
end
|
187
194
|
|
188
195
|
should_not_allow_attachment_file_size 11.kilobytes,
|
189
|
-
:
|
196
|
+
message: "must be in between #{storage_units[5120]} and #{storage_units[10240]}"
|
190
197
|
should_not_allow_attachment_file_size 4.kilobytes,
|
191
|
-
:
|
198
|
+
message: "must be in between #{storage_units[5120]} and #{storage_units[10240]}"
|
192
199
|
end
|
193
200
|
end
|
194
201
|
|
195
202
|
context "using the helper" do
|
196
|
-
|
197
|
-
Dummy.validates_attachment_size :avatar, :
|
203
|
+
before do
|
204
|
+
Dummy.validates_attachment_size :avatar, in: (5.kilobytes..10.kilobytes)
|
198
205
|
end
|
199
206
|
|
200
|
-
|
207
|
+
it "adds the validator to the class" do
|
201
208
|
assert Dummy.validators_on(:avatar).any?{ |validator| validator.kind == :attachment_size }
|
202
209
|
end
|
203
210
|
end
|
204
211
|
|
205
212
|
context "given options" do
|
206
|
-
|
213
|
+
it "raises argument error if no required argument was given" do
|
207
214
|
assert_raises(ArgumentError) do
|
208
|
-
build_validator :
|
215
|
+
build_validator message: "Some message"
|
209
216
|
end
|
210
217
|
end
|
211
218
|
|
212
219
|
(Paperclip::Validators::AttachmentSizeValidator::AVAILABLE_CHECKS).each do |argument|
|
213
|
-
|
220
|
+
it "does not raise arguemnt error if #{argument} was given" do
|
214
221
|
build_validator argument => 5.kilobytes
|
215
222
|
end
|
216
223
|
end
|
217
224
|
|
218
|
-
|
219
|
-
build_validator :
|
225
|
+
it "does not raise argument error if :in was given" do
|
226
|
+
build_validator in: (5.kilobytes..10.kilobytes)
|
220
227
|
end
|
221
228
|
end
|
222
229
|
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
|
+
Dummy.validates_attachment :avatar, presence: true, content_type: { content_type: "image/jpeg" }, size: { in: 0..10240 }
|
7
|
+
end
|
8
|
+
|
9
|
+
it "adds the attachment_presence validator to the class" do
|
10
|
+
assert Dummy.validators_on(:avatar).any?{ |validator| validator.kind == :attachment_presence }
|
11
|
+
end
|
12
|
+
|
13
|
+
it "adds the attachment_content_type validator to the class" do
|
14
|
+
assert Dummy.validators_on(:avatar).any?{ |validator| validator.kind == :attachment_content_type }
|
15
|
+
end
|
16
|
+
|
17
|
+
it "adds the attachment_size validator to the class" do
|
18
|
+
assert Dummy.validators_on(:avatar).any?{ |validator| validator.kind == :attachment_size }
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'prevents you from attaching a file that violates that validation' do
|
22
|
+
Dummy.class_eval{ validate(:name) { raise "DO NOT RUN THIS" } }
|
23
|
+
dummy = Dummy.new(avatar: File.new(fixture_file("12k.png")))
|
24
|
+
expect(dummy.errors.keys).to match_array [:avatar_content_type, :avatar, :avatar_file_size]
|
25
|
+
assert_raises(RuntimeError){ dummy.valid? }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'using the helper with array of validations' do
|
30
|
+
before do
|
31
|
+
rebuild_class
|
32
|
+
Dummy.validates_attachment :avatar, file_type_ignorance: true, file_name: [
|
33
|
+
{ matches: /\A.*\.jpe?g\Z/i, message: :invalid_extension },
|
34
|
+
{ matches: /\A.{,8}\..+\Z/i, message: [:too_long, count: 8] },
|
35
|
+
]
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'adds the attachment_file_name validator to the class' do
|
39
|
+
assert Dummy.validators_on(:avatar).any?{ |validator| validator.kind == :attachment_file_name }
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'adds the attachment_file_name validator with two validations' do
|
43
|
+
assert_equal 2, Dummy.validators_on(:avatar).select{ |validator| validator.kind == :attachment_file_name }.size
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'prevents you from attaching a file that violates all of these validations' do
|
47
|
+
Dummy.class_eval{ validate(:name) { raise 'DO NOT RUN THIS' } }
|
48
|
+
dummy = Dummy.new(avatar: File.new(fixture_file('spaced file.png')))
|
49
|
+
expect(dummy.errors.keys).to match_array [:avatar, :avatar_file_name]
|
50
|
+
assert_raises(RuntimeError){ dummy.valid? }
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'prevents you from attaching a file that violates only first of these validations' do
|
54
|
+
Dummy.class_eval{ validate(:name) { raise 'DO NOT RUN THIS' } }
|
55
|
+
dummy = Dummy.new(avatar: File.new(fixture_file('5k.png')))
|
56
|
+
expect(dummy.errors.keys).to match_array [:avatar, :avatar_file_name]
|
57
|
+
assert_raises(RuntimeError){ dummy.valid? }
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'prevents you from attaching a file that violates only second of these validations' do
|
61
|
+
Dummy.class_eval{ validate(:name) { raise 'DO NOT RUN THIS' } }
|
62
|
+
dummy = Dummy.new(avatar: File.new(fixture_file('spaced file.jpg')))
|
63
|
+
expect(dummy.errors.keys).to match_array [:avatar, :avatar_file_name]
|
64
|
+
assert_raises(RuntimeError){ dummy.valid? }
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'allows you to attach a file that does not violates these validations' do
|
68
|
+
dummy = Dummy.new(avatar: File.new(fixture_file('rotated.jpg')))
|
69
|
+
expect(dummy.errors.keys).to match_array []
|
70
|
+
assert dummy.valid?
|
71
|
+
end
|
72
|
+
|
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,43 @@
|
|
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
|
+
|
11
|
+
ROOT = Pathname(File.expand_path(File.join(File.dirname(__FILE__), '..')))
|
12
|
+
|
13
|
+
puts "Testing against version #{ActiveRecord::VERSION::STRING}"
|
14
|
+
|
15
|
+
$LOAD_PATH << File.join(ROOT, 'lib')
|
16
|
+
$LOAD_PATH << File.join(ROOT, 'lib', 'paperclip')
|
17
|
+
require File.join(ROOT, 'lib', 'paperclip.rb')
|
18
|
+
|
19
|
+
FIXTURES_DIR = File.join(File.dirname(__FILE__), "fixtures")
|
20
|
+
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
|
21
|
+
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
|
22
|
+
ActiveRecord::Base.establish_connection(config['test'])
|
23
|
+
unless ActiveRecord::VERSION::STRING < "4.2"
|
24
|
+
ActiveRecord::Base.raise_in_transactional_callbacks = true
|
25
|
+
end
|
26
|
+
Paperclip.options[:logger] = ActiveRecord::Base.logger
|
27
|
+
|
28
|
+
Dir[File.join(ROOT, 'spec', 'support', '**', '*.rb')].each{|f| require f }
|
29
|
+
|
30
|
+
Rails = FakeRails.new('test', Pathname.new(ROOT).join('tmp'))
|
31
|
+
ActiveSupport::Deprecation.silenced = true
|
32
|
+
|
33
|
+
RSpec.configure do |config|
|
34
|
+
config.include Assertions
|
35
|
+
config.include ModelReconstruction
|
36
|
+
config.include TestData
|
37
|
+
config.extend VersionHelper
|
38
|
+
config.extend RailsHelpers::ClassMethods
|
39
|
+
config.mock_framework = :mocha
|
40
|
+
config.before(:all) do
|
41
|
+
rebuild_model
|
42
|
+
end
|
43
|
+
end
|