paperclip 3.5.4 → 4.0.0

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 (54) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +1 -0
  3. data/LICENSE +1 -3
  4. data/NEWS +25 -21
  5. data/README.md +35 -1
  6. data/features/step_definitions/attachment_steps.rb +2 -0
  7. data/features/step_definitions/rails_steps.rb +1 -0
  8. data/lib/paperclip.rb +1 -0
  9. data/lib/paperclip/attachment.rb +25 -1
  10. data/lib/paperclip/callbacks.rb +1 -1
  11. data/lib/paperclip/content_type_detector.rb +1 -13
  12. data/lib/paperclip/errors.rb +5 -0
  13. data/lib/paperclip/has_attached_file.rb +5 -0
  14. data/lib/paperclip/io_adapters/abstract_adapter.rb +1 -1
  15. data/lib/paperclip/io_adapters/attachment_adapter.rb +4 -4
  16. data/lib/paperclip/io_adapters/data_uri_adapter.rb +4 -9
  17. data/lib/paperclip/io_adapters/stringio_adapter.rb +10 -8
  18. data/lib/paperclip/media_type_spoof_detector.rb +36 -0
  19. data/lib/paperclip/tempfile_factory.rb +5 -1
  20. data/lib/paperclip/validators.rb +6 -1
  21. data/lib/paperclip/validators/attachment_content_type_validator.rb +4 -0
  22. data/lib/paperclip/validators/attachment_file_name_validator.rb +80 -0
  23. data/lib/paperclip/validators/attachment_file_type_ignorance_validator.rb +29 -0
  24. data/lib/paperclip/validators/attachment_presence_validator.rb +4 -0
  25. data/lib/paperclip/validators/attachment_size_validator.rb +4 -0
  26. data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +27 -0
  27. data/lib/paperclip/version.rb +1 -1
  28. data/test/attachment_definitions_test.rb +1 -0
  29. data/test/attachment_test.rb +40 -43
  30. data/test/content_type_detector_test.rb +0 -10
  31. data/test/fixtures/empty.html +1 -0
  32. data/test/has_attached_file_test.rb +3 -1
  33. data/test/helper.rb +11 -4
  34. data/test/io_adapters/abstract_adapter_test.rb +1 -0
  35. data/test/io_adapters/attachment_adapter_test.rb +1 -1
  36. data/test/io_adapters/data_uri_adapter_test.rb +2 -2
  37. data/test/io_adapters/file_adapter_test.rb +0 -11
  38. data/test/io_adapters/http_url_proxy_adapter_test.rb +2 -3
  39. data/test/io_adapters/stringio_adapter_test.rb +1 -1
  40. data/test/matchers/have_attached_file_matcher_test.rb +3 -2
  41. data/test/matchers/validate_attachment_content_type_matcher_test.rb +13 -12
  42. data/test/matchers/validate_attachment_presence_matcher_test.rb +8 -7
  43. data/test/matchers/validate_attachment_size_matcher_test.rb +12 -11
  44. data/test/media_type_spoof_detector_test.rb +28 -0
  45. data/test/meta_class_test.rb +2 -2
  46. data/test/schema_test.rb +6 -0
  47. data/test/storage/fog_test.rb +4 -4
  48. data/test/storage/s3_test.rb +32 -31
  49. data/test/tempfile_factory_test.rb +13 -1
  50. data/test/validators/attachment_file_name_validator_test.rb +162 -0
  51. data/test/validators/attachment_presence_validator_test.rb +1 -1
  52. data/test/validators/media_type_spoof_detection_validator_test.rb +12 -0
  53. data/test/validators_test.rb +43 -3
  54. metadata +14 -2
@@ -3,15 +3,27 @@ require './test/helper'
3
3
  class Paperclip::TempfileFactoryTest < Test::Unit::TestCase
4
4
  should "be able to generate a tempfile with the right name" do
5
5
  file = subject.generate("omg.png")
6
+ assert File.extname(file.path), "png"
6
7
  end
8
+
7
9
  should "be able to generate a tempfile with the right name with a tilde at the beginning" do
8
10
  file = subject.generate("~omg.png")
11
+ assert File.extname(file.path), "png"
9
12
  end
13
+
10
14
  should "be able to generate a tempfile with the right name with a tilde at the end" do
11
15
  file = subject.generate("omg.png~")
16
+ assert File.extname(file.path), "png"
12
17
  end
18
+
13
19
  should "be able to generate a tempfile from a file with a really long name" do
14
- filename = "#{"longfilename" * 100}.txt"
20
+ filename = "#{"longfilename" * 100}.png"
15
21
  file = subject.generate(filename)
22
+ assert File.extname(file.path), "png"
23
+ end
24
+
25
+ should 'be able to take nothing as a parameter and not error' do
26
+ file = subject.generate
27
+ assert File.exists?(file.path)
16
28
  end
17
29
  end
@@ -0,0 +1,162 @@
1
+ require './test/helper'
2
+
3
+ class AttachmentFileNameValidatorTest < Test::Unit::TestCase
4
+ def setup
5
+ rebuild_model
6
+ @dummy = Dummy.new
7
+ super
8
+ end
9
+
10
+ def build_validator(options)
11
+ @validator = Paperclip::Validators::AttachmentFileNameValidator.new(options.merge(
12
+ :attributes => :avatar
13
+ ))
14
+ end
15
+
16
+ context "with a failing validation" do
17
+ setup do
18
+ build_validator :matches => /.*\.png$/, :allow_nil => false
19
+ @dummy.stubs(:avatar_file_name => "data.txt")
20
+ @validator.validate(@dummy)
21
+ end
22
+
23
+ should "add error to the base object" do
24
+ assert @dummy.errors[:avatar].present?,
25
+ "Error not added to base attribute"
26
+ end
27
+
28
+ should "add error to base object as a string" do
29
+ assert_kind_of String, @dummy.errors[:avatar].first,
30
+ "Error added to base attribute as something other than a String"
31
+ end
32
+ end
33
+
34
+ should "not add error to the base object with a successful validation" do
35
+ build_validator :matches => /.*\.png$/, :allow_nil => false
36
+ @dummy.stubs(:avatar_file_name => "image.png")
37
+ @validator.validate(@dummy)
38
+
39
+ assert @dummy.errors[:avatar].blank?, "Error was added to base attribute"
40
+ end
41
+
42
+ context "whitelist format" do
43
+ context "with an allowed type" do
44
+ context "as a single regexp" do
45
+ setup do
46
+ build_validator :matches => /.*\.jpg$/
47
+ @dummy.stubs(:avatar_file_name => "image.jpg")
48
+ @validator.validate(@dummy)
49
+ end
50
+
51
+ should "not set an error message" do
52
+ assert @dummy.errors[:avatar_file_name].blank?
53
+ end
54
+ end
55
+
56
+ context "as a list" do
57
+ setup do
58
+ build_validator :matches => [/.*\.png$/, /.*\.jpe?g$/]
59
+ @dummy.stubs(:avatar_file_name => "image.jpg")
60
+ @validator.validate(@dummy)
61
+ end
62
+
63
+ should "not set an error message" do
64
+ assert @dummy.errors[:avatar_file_name].blank?
65
+ end
66
+ end
67
+ end
68
+
69
+ context "with a disallowed type" do
70
+ should "set a correct default error message" do
71
+ build_validator :matches => /^text\/.*/
72
+ @dummy.stubs(:avatar_file_name => "image.jpg")
73
+ @validator.validate(@dummy)
74
+
75
+ assert @dummy.errors[:avatar_file_name].present?
76
+ assert_includes @dummy.errors[:avatar_file_name], "is invalid"
77
+ end
78
+
79
+ should "set a correct custom error message" do
80
+ build_validator :matches => /.*\.png$/, :message => "should be a PNG image"
81
+ @dummy.stubs(:avatar_file_name => "image.jpg")
82
+ @validator.validate(@dummy)
83
+
84
+ assert_includes @dummy.errors[:avatar_file_name], "should be a PNG image"
85
+ end
86
+ end
87
+ end
88
+
89
+ context "blacklist format" do
90
+ context "with an allowed type" do
91
+ context "as a single regexp" do
92
+ setup do
93
+ build_validator :not => /^text\/.*/
94
+ @dummy.stubs(:avatar_file_name => "image.jpg")
95
+ @validator.validate(@dummy)
96
+ end
97
+
98
+ should "not set an error message" do
99
+ assert @dummy.errors[:avatar_file_name].blank?
100
+ end
101
+ end
102
+
103
+ context "as a list" do
104
+ setup do
105
+ build_validator :not => [/.*\.png$/, /.*\.jpe?g$/]
106
+ @dummy.stubs(:avatar_file_name => "image.gif")
107
+ @validator.validate(@dummy)
108
+ end
109
+
110
+ should "not set an error message" do
111
+ assert @dummy.errors[:avatar_file_name].blank?
112
+ end
113
+ end
114
+ end
115
+
116
+ context "with a disallowed type" do
117
+ should "set a correct default error message" do
118
+ build_validator :not => /data.*/
119
+ @dummy.stubs(:avatar_file_name => "data.txt")
120
+ @validator.validate(@dummy)
121
+
122
+ assert @dummy.errors[:avatar_file_name].present?
123
+ assert_includes @dummy.errors[:avatar_file_name], "is invalid"
124
+ end
125
+
126
+ should "set a correct custom error message" do
127
+ build_validator :not => /.*\.png$/, :message => "should not be a PNG image"
128
+ @dummy.stubs(:avatar_file_name => "image.png")
129
+ @validator.validate(@dummy)
130
+
131
+ assert_includes @dummy.errors[:avatar_file_name], "should not be a PNG image"
132
+ end
133
+ end
134
+ end
135
+
136
+ context "using the helper" do
137
+ setup do
138
+ Dummy.validates_attachment_file_name :avatar, :matches => /.*\.jpg$/
139
+ end
140
+
141
+ should "add the validator to the class" do
142
+ assert Dummy.validators_on(:avatar).any?{ |validator| validator.kind == :attachment_file_name }
143
+ end
144
+ end
145
+
146
+ context "given options" do
147
+ should "raise argument error if no required argument was given" do
148
+ assert_raises(ArgumentError) do
149
+ build_validator :message => "Some message"
150
+ end
151
+ end
152
+
153
+ should "not raise argument error if :matches was given" do
154
+ build_validator :matches => /.*\.jpg$/
155
+ end
156
+
157
+ should "not raise argument error if :not was given" do
158
+ build_validator :not => /.*\.jpg$/
159
+ end
160
+ end
161
+ end
162
+
@@ -60,7 +60,7 @@ class AttachmentPresenceValidatorTest < Test::Unit::TestCase
60
60
  context "with attachment" do
61
61
  setup 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
 
@@ -0,0 +1,12 @@
1
+ require './test/helper'
2
+
3
+ class MediaTypeSpoofDetectionValidatorTest < Test::Unit::TestCase
4
+ def setup
5
+ rebuild_model
6
+ @dummy = Dummy.new
7
+ end
8
+
9
+ should "be on the attachment without being explicitly added" do
10
+ assert Dummy.validators_on(:avatar).any?{ |validator| validator.kind == :media_type_spoof_detection }
11
+ end
12
+ end
@@ -1,6 +1,8 @@
1
1
  require './test/helper'
2
2
 
3
3
  class ValidatorsTest < Test::Unit::TestCase
4
+ include ActiveSupport::Testing::Deprecation
5
+
4
6
  def setup
5
7
  rebuild_model
6
8
  end
@@ -33,9 +35,9 @@ class ValidatorsTest < Test::Unit::TestCase
33
35
  context "using the helper with a conditional" do
34
36
  setup do
35
37
  Dummy.validates_attachment :avatar, :presence => true,
36
- :content_type => { :content_type => "image/jpeg" },
37
- :size => { :in => 0..10.kilobytes },
38
- :if => :title_present?
38
+ :content_type => { :content_type => "image/jpeg" },
39
+ :size => { :in => 0..10.kilobytes },
40
+ :if => :title_present?
39
41
  end
40
42
 
41
43
  should "validate the attachment if title is present" do
@@ -58,4 +60,42 @@ class ValidatorsTest < Test::Unit::TestCase
58
60
  assert_equal [], dummy.errors.keys
59
61
  end
60
62
  end
63
+
64
+ context 'with no other validations on the Dummy#avatar attachment' do
65
+ setup do
66
+ reset_class("Dummy")
67
+ Dummy.has_attached_file :avatar
68
+ Paperclip.reset_duplicate_clash_check!
69
+ end
70
+
71
+ should 'raise an error when no content_type validation exists' do
72
+ assert_raises(Paperclip::Errors::MissingRequiredValidatorError) do
73
+ Dummy.new(:avatar => File.new(fixture_file("12k.png")))
74
+ end
75
+ end
76
+
77
+ should 'not raise an error when a content_type validation exists' do
78
+ Dummy.validates_attachment :avatar, :content_type => { :content_type => "image/jpeg" }
79
+
80
+ assert_nothing_raised(Paperclip::Errors::MissingRequiredValidatorError) do
81
+ Dummy.new(:avatar => File.new(fixture_file("12k.png")))
82
+ end
83
+ end
84
+
85
+ should 'not raise an error when a file_name validation exists' do
86
+ Dummy.validates_attachment :avatar, :file_name => { :matches => /png$/ }
87
+
88
+ assert_nothing_raised(Paperclip::Errors::MissingRequiredValidatorError) do
89
+ Dummy.new(:avatar => File.new(fixture_file("12k.png")))
90
+ end
91
+ end
92
+
93
+ should 'not raise an error when a the validation has been explicitly rejected' do
94
+ Dummy.validates_attachment :avatar, :file_type_ignorance => true
95
+
96
+ assert_nothing_raised(Paperclip::Errors::MissingRequiredValidatorError) do
97
+ Dummy.new(:avatar => File.new(fixture_file("12k.png")))
98
+ end
99
+ end
100
+ end
61
101
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paperclip
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.5.4
4
+ version: 4.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Yurek
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-07 00:00:00.000000000 Z
11
+ date: 2014-01-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -396,6 +396,7 @@ files:
396
396
  - lib/paperclip/matchers/validate_attachment_content_type_matcher.rb
397
397
  - lib/paperclip/matchers/validate_attachment_presence_matcher.rb
398
398
  - lib/paperclip/matchers/validate_attachment_size_matcher.rb
399
+ - lib/paperclip/media_type_spoof_detector.rb
399
400
  - lib/paperclip/missing_attachment_styles.rb
400
401
  - lib/paperclip/processor.rb
401
402
  - lib/paperclip/railtie.rb
@@ -411,8 +412,11 @@ files:
411
412
  - lib/paperclip/url_generator.rb
412
413
  - lib/paperclip/validators.rb
413
414
  - lib/paperclip/validators/attachment_content_type_validator.rb
415
+ - lib/paperclip/validators/attachment_file_name_validator.rb
416
+ - lib/paperclip/validators/attachment_file_type_ignorance_validator.rb
414
417
  - lib/paperclip/validators/attachment_presence_validator.rb
415
418
  - lib/paperclip/validators/attachment_size_validator.rb
419
+ - lib/paperclip/validators/media_type_spoof_detection_validator.rb
416
420
  - lib/paperclip/version.rb
417
421
  - lib/tasks/paperclip.rake
418
422
  - paperclip.gemspec
@@ -432,6 +436,7 @@ files:
432
436
  - test/fixtures/animated.gif
433
437
  - test/fixtures/animated.unknown
434
438
  - test/fixtures/bad.png
439
+ - test/fixtures/empty.html
435
440
  - test/fixtures/fog.yml
436
441
  - test/fixtures/rotated.jpg
437
442
  - test/fixtures/s3.yml
@@ -463,6 +468,7 @@ files:
463
468
  - test/matchers/validate_attachment_content_type_matcher_test.rb
464
469
  - test/matchers/validate_attachment_presence_matcher_test.rb
465
470
  - test/matchers/validate_attachment_size_matcher_test.rb
471
+ - test/media_type_spoof_detector_test.rb
466
472
  - test/meta_class_test.rb
467
473
  - test/paperclip_missing_attachment_styles_test.rb
468
474
  - test/paperclip_test.rb
@@ -483,8 +489,10 @@ files:
483
489
  - test/thumbnail_test.rb
484
490
  - test/url_generator_test.rb
485
491
  - test/validators/attachment_content_type_validator_test.rb
492
+ - test/validators/attachment_file_name_validator_test.rb
486
493
  - test/validators/attachment_presence_validator_test.rb
487
494
  - test/validators/attachment_size_validator_test.rb
495
+ - test/validators/media_type_spoof_detection_validator_test.rb
488
496
  - test/validators_test.rb
489
497
  homepage: https://github.com/thoughtbot/paperclip
490
498
  licenses:
@@ -544,6 +552,7 @@ test_files:
544
552
  - test/fixtures/animated.gif
545
553
  - test/fixtures/animated.unknown
546
554
  - test/fixtures/bad.png
555
+ - test/fixtures/empty.html
547
556
  - test/fixtures/fog.yml
548
557
  - test/fixtures/rotated.jpg
549
558
  - test/fixtures/s3.yml
@@ -575,6 +584,7 @@ test_files:
575
584
  - test/matchers/validate_attachment_content_type_matcher_test.rb
576
585
  - test/matchers/validate_attachment_presence_matcher_test.rb
577
586
  - test/matchers/validate_attachment_size_matcher_test.rb
587
+ - test/media_type_spoof_detector_test.rb
578
588
  - test/meta_class_test.rb
579
589
  - test/paperclip_missing_attachment_styles_test.rb
580
590
  - test/paperclip_test.rb
@@ -595,6 +605,8 @@ test_files:
595
605
  - test/thumbnail_test.rb
596
606
  - test/url_generator_test.rb
597
607
  - test/validators/attachment_content_type_validator_test.rb
608
+ - test/validators/attachment_file_name_validator_test.rb
598
609
  - test/validators/attachment_presence_validator_test.rb
599
610
  - test/validators/attachment_size_validator_test.rb
611
+ - test/validators/media_type_spoof_detection_validator_test.rb
600
612
  - test/validators_test.rb