paperclip 3.5.4 → 6.1.0

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