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.

Files changed (198) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -6
  3. data/.hound.yml +1066 -0
  4. data/.rubocop.yml +1 -0
  5. data/.travis.yml +11 -17
  6. data/Appraisals +6 -14
  7. data/CONTRIBUTING.md +13 -8
  8. data/Gemfile +16 -3
  9. data/LICENSE +1 -3
  10. data/NEWS +167 -49
  11. data/README.md +294 -75
  12. data/RELEASING.md +17 -0
  13. data/Rakefile +6 -8
  14. data/features/basic_integration.feature +24 -6
  15. data/features/step_definitions/attachment_steps.rb +30 -22
  16. data/features/step_definitions/html_steps.rb +2 -2
  17. data/features/step_definitions/rails_steps.rb +44 -14
  18. data/features/step_definitions/web_steps.rb +1 -103
  19. data/features/support/env.rb +2 -2
  20. data/features/support/file_helpers.rb +2 -2
  21. data/features/support/fixtures/gemfile.txt +1 -1
  22. data/features/support/rails.rb +2 -1
  23. data/gemfiles/3.2.gemfile +14 -6
  24. data/gemfiles/4.1.gemfile +19 -0
  25. data/gemfiles/4.2.gemfile +19 -0
  26. data/lib/generators/paperclip/paperclip_generator.rb +0 -2
  27. data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +1 -1
  28. data/lib/paperclip/attachment.rb +132 -38
  29. data/lib/paperclip/attachment_registry.rb +1 -1
  30. data/lib/paperclip/callbacks.rb +11 -1
  31. data/lib/paperclip/content_type_detector.rb +25 -22
  32. data/lib/paperclip/deprecations.rb +42 -0
  33. data/lib/paperclip/errors.rb +5 -0
  34. data/lib/paperclip/file_command_content_type_detector.rb +6 -8
  35. data/lib/paperclip/geometry_detector_factory.rb +3 -1
  36. data/lib/paperclip/geometry_parser_factory.rb +1 -1
  37. data/lib/paperclip/has_attached_file.rb +10 -0
  38. data/lib/paperclip/interpolations/plural_cache.rb +6 -5
  39. data/lib/paperclip/interpolations.rb +25 -12
  40. data/lib/paperclip/io_adapters/abstract_adapter.rb +3 -1
  41. data/lib/paperclip/io_adapters/attachment_adapter.rb +4 -4
  42. data/lib/paperclip/io_adapters/data_uri_adapter.rb +5 -10
  43. data/lib/paperclip/io_adapters/stringio_adapter.rb +6 -10
  44. data/lib/paperclip/io_adapters/uri_adapter.rb +30 -11
  45. data/lib/paperclip/locales/de.yml +18 -0
  46. data/lib/paperclip/locales/en.yml +1 -0
  47. data/lib/paperclip/locales/es.yml +18 -0
  48. data/lib/paperclip/locales/ja.yml +18 -0
  49. data/lib/paperclip/locales/pt-BR.yml +18 -0
  50. data/lib/paperclip/locales/zh-CN.yml +18 -0
  51. data/lib/paperclip/locales/zh-HK.yml +18 -0
  52. data/lib/paperclip/locales/zh-TW.yml +18 -0
  53. data/lib/paperclip/matchers/have_attached_file_matcher.rb +2 -1
  54. data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +2 -1
  55. data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +2 -1
  56. data/lib/paperclip/media_type_spoof_detector.rb +89 -0
  57. data/lib/paperclip/processor.rb +0 -37
  58. data/lib/paperclip/processor_helpers.rb +50 -0
  59. data/lib/paperclip/rails_environment.rb +25 -0
  60. data/lib/paperclip/schema.rb +10 -2
  61. data/lib/paperclip/storage/filesystem.rb +1 -1
  62. data/lib/paperclip/storage/fog.rb +18 -7
  63. data/lib/paperclip/storage/s3.rb +53 -22
  64. data/lib/paperclip/style.rb +8 -2
  65. data/lib/paperclip/tempfile_factory.rb +5 -1
  66. data/lib/paperclip/thumbnail.rb +12 -10
  67. data/lib/paperclip/url_generator.rb +11 -3
  68. data/lib/paperclip/validators/attachment_content_type_validator.rb +4 -0
  69. data/lib/paperclip/validators/attachment_file_name_validator.rb +80 -0
  70. data/lib/paperclip/validators/attachment_file_type_ignorance_validator.rb +29 -0
  71. data/lib/paperclip/validators/attachment_presence_validator.rb +4 -0
  72. data/lib/paperclip/validators/attachment_size_validator.rb +11 -3
  73. data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +27 -0
  74. data/lib/paperclip/validators.rb +10 -3
  75. data/lib/paperclip/version.rb +1 -1
  76. data/lib/paperclip.rb +26 -8
  77. data/lib/tasks/paperclip.rake +17 -2
  78. data/paperclip.gemspec +16 -14
  79. data/shoulda_macros/paperclip.rb +0 -1
  80. data/spec/paperclip/attachment_definitions_spec.rb +13 -0
  81. data/{test/attachment_processing_test.rb → spec/paperclip/attachment_processing_spec.rb} +20 -21
  82. data/spec/paperclip/attachment_registry_spec.rb +130 -0
  83. data/{test/attachment_test.rb → spec/paperclip/attachment_spec.rb} +438 -397
  84. data/{test/content_type_detector_test.rb → spec/paperclip/content_type_detector_spec.rb} +16 -19
  85. data/spec/paperclip/deprecations_spec.rb +65 -0
  86. data/{test/file_command_content_type_detector_test.rb → spec/paperclip/file_command_content_type_detector_spec.rb} +5 -6
  87. data/spec/paperclip/filename_cleaner_spec.rb +14 -0
  88. data/spec/paperclip/geometry_detector_spec.rb +39 -0
  89. data/{test/geometry_parser_test.rb → spec/paperclip/geometry_parser_spec.rb} +27 -27
  90. data/{test/geometry_test.rb → spec/paperclip/geometry_spec.rb} +50 -52
  91. data/spec/paperclip/glue_spec.rb +44 -0
  92. data/{test/has_attached_file_test.rb → spec/paperclip/has_attached_file_spec.rb} +45 -28
  93. data/{test/integration_test.rb → spec/paperclip/integration_spec.rb} +134 -126
  94. data/{test/interpolations_test.rb → spec/paperclip/interpolations_spec.rb} +70 -46
  95. data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +78 -0
  96. data/{test/io_adapters/attachment_adapter_test.rb → spec/paperclip/io_adapters/attachment_adapter_spec.rb} +27 -29
  97. data/{test/io_adapters/data_uri_adapter_test.rb → spec/paperclip/io_adapters/data_uri_adapter_spec.rb} +26 -17
  98. data/spec/paperclip/io_adapters/empty_string_adapter_spec.rb +17 -0
  99. data/{test/io_adapters/file_adapter_test.rb → spec/paperclip/io_adapters/file_adapter_spec.rb} +36 -40
  100. data/{test/io_adapters/http_url_proxy_adapter_test.rb → spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb} +31 -29
  101. data/spec/paperclip/io_adapters/identity_adapter_spec.rb +8 -0
  102. data/{test/io_adapters/nil_adapter_test.rb → spec/paperclip/io_adapters/nil_adapter_spec.rb} +7 -7
  103. data/{test/io_adapters/registry_test.rb → spec/paperclip/io_adapters/registry_spec.rb} +10 -7
  104. data/{test/io_adapters/stringio_adapter_test.rb → spec/paperclip/io_adapters/stringio_adapter_spec.rb} +20 -17
  105. data/{test/io_adapters/uploaded_file_adapter_test.rb → spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb} +41 -41
  106. data/{test/io_adapters/uri_adapter_test.rb → spec/paperclip/io_adapters/uri_adapter_spec.rb} +53 -28
  107. data/spec/paperclip/matchers/have_attached_file_matcher_spec.rb +19 -0
  108. data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +99 -0
  109. data/spec/paperclip/matchers/validate_attachment_presence_matcher_spec.rb +69 -0
  110. data/spec/paperclip/matchers/validate_attachment_size_matcher_spec.rb +88 -0
  111. data/spec/paperclip/media_type_spoof_detector_spec.rb +79 -0
  112. data/spec/paperclip/meta_class_spec.rb +30 -0
  113. data/spec/paperclip/paperclip_missing_attachment_styles_spec.rb +84 -0
  114. data/{test/paperclip_test.rb → spec/paperclip/paperclip_spec.rb} +53 -48
  115. data/spec/paperclip/plural_cache_spec.rb +37 -0
  116. data/spec/paperclip/processor_helpers_spec.rb +57 -0
  117. data/{test/processor_test.rb → spec/paperclip/processor_spec.rb} +5 -5
  118. data/spec/paperclip/rails_environment_spec.rb +33 -0
  119. data/{test/rake_test.rb → spec/paperclip/rake_spec.rb} +15 -15
  120. data/spec/paperclip/schema_spec.rb +248 -0
  121. data/{test/storage/filesystem_test.rb → spec/paperclip/storage/filesystem_spec.rb} +18 -18
  122. data/spec/paperclip/storage/fog_spec.rb +535 -0
  123. data/spec/paperclip/storage/s3_live_spec.rb +182 -0
  124. data/spec/paperclip/storage/s3_spec.rb +1526 -0
  125. data/spec/paperclip/style_spec.rb +255 -0
  126. data/spec/paperclip/tempfile_factory_spec.rb +33 -0
  127. data/{test/thumbnail_test.rb → spec/paperclip/thumbnail_spec.rb} +123 -107
  128. data/spec/paperclip/url_generator_spec.rb +211 -0
  129. data/spec/paperclip/validators/attachment_content_type_validator_spec.rb +322 -0
  130. data/spec/paperclip/validators/attachment_file_name_validator_spec.rb +160 -0
  131. data/{test/validators/attachment_presence_validator_test.rb → spec/paperclip/validators/attachment_presence_validator_spec.rb} +20 -20
  132. data/{test/validators/attachment_size_validator_test.rb → spec/paperclip/validators/attachment_size_validator_spec.rb} +65 -58
  133. data/spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb +52 -0
  134. data/spec/paperclip/validators_spec.rb +164 -0
  135. data/spec/spec_helper.rb +43 -0
  136. data/spec/support/assertions.rb +71 -0
  137. data/spec/support/deprecations.rb +9 -0
  138. data/spec/support/fake_model.rb +25 -0
  139. data/spec/support/fake_rails.rb +12 -0
  140. data/spec/support/fixtures/empty.html +1 -0
  141. data/spec/support/fixtures/empty.xlsx +0 -0
  142. data/spec/support/fixtures/spaced file.jpg +0 -0
  143. data/spec/support/matchers/accept.rb +5 -0
  144. data/spec/support/matchers/exist.rb +5 -0
  145. data/spec/support/matchers/have_column.rb +23 -0
  146. data/spec/support/model_reconstruction.rb +60 -0
  147. data/spec/support/rails_helpers.rb +7 -0
  148. data/spec/support/test_data.rb +13 -0
  149. data/spec/support/version_helper.rb +9 -0
  150. metadata +334 -219
  151. data/RUNNING_TESTS.md +0 -4
  152. data/gemfiles/3.0.gemfile +0 -11
  153. data/gemfiles/3.1.gemfile +0 -11
  154. data/gemfiles/4.0.gemfile +0 -11
  155. data/test/attachment_definitions_test.rb +0 -12
  156. data/test/attachment_registry_test.rb +0 -88
  157. data/test/filename_cleaner_test.rb +0 -14
  158. data/test/generator_test.rb +0 -84
  159. data/test/geometry_detector_test.rb +0 -24
  160. data/test/helper.rb +0 -232
  161. data/test/io_adapters/abstract_adapter_test.rb +0 -58
  162. data/test/io_adapters/empty_string_adapter_test.rb +0 -18
  163. data/test/io_adapters/identity_adapter_test.rb +0 -8
  164. data/test/matchers/have_attached_file_matcher_test.rb +0 -24
  165. data/test/matchers/validate_attachment_content_type_matcher_test.rb +0 -110
  166. data/test/matchers/validate_attachment_presence_matcher_test.rb +0 -69
  167. data/test/matchers/validate_attachment_size_matcher_test.rb +0 -86
  168. data/test/meta_class_test.rb +0 -32
  169. data/test/paperclip_missing_attachment_styles_test.rb +0 -90
  170. data/test/plural_cache_test.rb +0 -36
  171. data/test/schema_test.rb +0 -200
  172. data/test/storage/fog_test.rb +0 -473
  173. data/test/storage/s3_live_test.rb +0 -179
  174. data/test/storage/s3_test.rb +0 -1356
  175. data/test/style_test.rb +0 -213
  176. data/test/support/mock_model.rb +0 -2
  177. data/test/tempfile_factory_test.rb +0 -17
  178. data/test/url_generator_test.rb +0 -187
  179. data/test/validators/attachment_content_type_validator_test.rb +0 -324
  180. data/test/validators_test.rb +0 -61
  181. /data/{test → spec}/database.yml +0 -0
  182. /data/{test → spec/support}/fixtures/12k.png +0 -0
  183. /data/{test → spec/support}/fixtures/50x50.png +0 -0
  184. /data/{test → spec/support}/fixtures/5k.png +0 -0
  185. /data/{test → spec/support}/fixtures/animated +0 -0
  186. /data/{test → spec/support}/fixtures/animated.gif +0 -0
  187. /data/{test → spec/support}/fixtures/animated.unknown +0 -0
  188. /data/{test → spec/support}/fixtures/bad.png +0 -0
  189. /data/{test → spec/support}/fixtures/fog.yml +0 -0
  190. /data/{test → spec/support}/fixtures/rotated.jpg +0 -0
  191. /data/{test → spec/support}/fixtures/s3.yml +0 -0
  192. /data/{test → spec/support}/fixtures/spaced file.png +0 -0
  193. /data/{test → spec/support}/fixtures/text.txt +0 -0
  194. /data/{test → spec/support}/fixtures/twopage.pdf +0 -0
  195. /data/{test → spec/support}/fixtures/uppercase.PNG +0 -0
  196. /data/{test → spec}/support/mock_attachment.rb +0 -0
  197. /data/{test → spec}/support/mock_interpolator.rb +0 -0
  198. /data/{test → spec}/support/mock_url_generator_builder.rb +0 -0
@@ -1,56 +1,56 @@
1
- require './test/helper'
1
+ require 'spec_helper'
2
2
 
3
- class AttachmentPresenceValidatorTest < Test::Unit::TestCase
4
- def setup
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
- :attributes => :avatar
11
+ attributes: :avatar
12
12
  ))
13
13
  end
14
14
 
15
15
  context "nil attachment" do
16
- setup do
16
+ before do
17
17
  @dummy.avatar = nil
18
18
  end
19
19
 
20
20
  context "with default options" do
21
- setup do
21
+ before do
22
22
  build_validator
23
23
  @validator.validate(@dummy)
24
24
  end
25
25
 
26
- should "add error on the attachment" do
26
+ it "adds error on the attachment" do
27
27
  assert @dummy.errors[:avatar].present?
28
28
  end
29
29
 
30
- should "not add an error on the file_name attribute" do
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
- setup do
38
- build_validator :if => true
37
+ before do
38
+ build_validator if: true
39
39
  @validator.validate(@dummy)
40
40
  end
41
41
 
42
- should "perform a validation" do
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
- setup do
49
- build_validator :if => false
48
+ before do
49
+ build_validator if: false
50
50
  @validator.validate(@dummy)
51
51
  end
52
52
 
53
- should "perform a validation" do
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
- setup do
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
- should "not add error on the attachment" do
67
+ it "does not add error on the attachment" do
68
68
  assert @dummy.errors[:avatar].blank?
69
69
  end
70
70
 
71
- should "not add an error on the file_name attribute" do
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
- setup do
77
+ before do
78
78
  Dummy.validates_attachment_presence :avatar
79
79
  end
80
80
 
81
- should "add the validator to the class" do
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 './test/helper'
1
+ require 'spec_helper'
2
2
 
3
- class AttachmentSizeValidatorTest < Test::Unit::TestCase
4
- def setup
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
- :attributes => :avatar
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
- should "add error to dummy object" do
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
- should "not add error to the base dummy object" do
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
- setup do
41
+ before do
34
42
  @dummy.stubs(:avatar_file_size).returns(size)
35
43
  @validator.validate(@dummy)
36
44
  end
37
45
 
38
- should "add error to dummy object" do
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
- should "add error to the base dummy object" do
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
- should "add error to base object as a string" do
49
- assert_kind_of String, @dummy.errors[:avatar].first,
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
- should "return a correct error message" do
55
- assert_includes @dummy.errors[:avatar_file_size], options[:message]
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
- setup do
64
- build_validator :in => (5.kilobytes..10.kilobytes)
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
- setup do
74
- build_validator :in => lambda { |avatar| (5.kilobytes..10.kilobytes) }
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
- setup do
86
- build_validator :greater_than => 10.kilobytes
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
- setup do
95
- build_validator :greater_than => lambda { |avatar| 10.kilobytes }
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
- setup do
106
- build_validator :less_than => 10.kilobytes
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
- setup do
115
- build_validator :less_than => lambda { |avatar| 10.kilobytes }
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
- setup do
126
- build_validator :greater_than => 5.kilobytes,
127
- :less_than => 10.kilobytes
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
- setup do
137
- build_validator :greater_than => lambda { |avatar| 5.kilobytes },
138
- :less_than => lambda { |avatar| 10.kilobytes }
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
- setup do
150
- build_validator :in => (5.kilobytes..10.kilobytes),
151
- :message => "is invalid. (Between %{min} and %{max} please.)"
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
- :message => "is invalid. (Between 5120 Bytes and 10240 Bytes please.)"
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
- setup do
160
- build_validator :less_than => 10.kilobytes,
161
- :greater_than => 5.kilobytes,
162
- :message => "is invalid. (Between %{min} and %{max} please.)"
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
- :message => "is invalid. (Between 5120 Bytes and 10240 Bytes please.)"
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
- setup do
173
- build_validator :greater_than => 5.kilobytes,
174
- :less_than => 10.kilobytes
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
- :message => "must be less than 10240 Bytes"
185
+ message: "must be less than #{storage_units[10240]}"
179
186
  should_not_allow_attachment_file_size 4.kilobytes,
180
- :message => "must be greater than 5120 Bytes"
187
+ message: "must be greater than #{storage_units[5120]}"
181
188
  end
182
189
 
183
190
  context "given a size range" do
184
- setup do
185
- build_validator :in => (5.kilobytes..10.kilobytes)
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
- :message => "must be in between 5120 Bytes and 10240 Bytes"
196
+ message: "must be in between #{storage_units[5120]} and #{storage_units[10240]}"
190
197
  should_not_allow_attachment_file_size 4.kilobytes,
191
- :message => "must be in between 5120 Bytes and 10240 Bytes"
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
- setup do
197
- Dummy.validates_attachment_size :avatar, :in => (5.kilobytes..10.kilobytes)
203
+ before do
204
+ Dummy.validates_attachment_size :avatar, in: (5.kilobytes..10.kilobytes)
198
205
  end
199
206
 
200
- should "add the validator to the class" do
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
- should "raise argument error if no required argument was given" do
213
+ it "raises argument error if no required argument was given" do
207
214
  assert_raises(ArgumentError) do
208
- build_validator :message => "Some message"
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
- should "not raise arguemnt error if #{argument} was given" do
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
- should "not raise argument error if :in was given" do
219
- build_validator :in => (5.kilobytes..10.kilobytes)
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
@@ -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