paperclip 4.3.7 → 6.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.codeclimate.yml +17 -0
- data/.github/issue_template.md +3 -0
- data/.hound.yml +5 -16
- data/.travis.yml +15 -12
- data/Appraisals +4 -8
- data/CONTRIBUTING.md +16 -5
- data/Gemfile +3 -8
- data/LICENSE +1 -1
- data/MIGRATING-ES.md +317 -0
- data/MIGRATING.md +375 -0
- data/NEWS +126 -31
- data/README.md +264 -156
- data/Rakefile +1 -1
- data/UPGRADING +12 -9
- data/features/basic_integration.feature +3 -2
- data/features/migration.feature +0 -24
- data/features/step_definitions/attachment_steps.rb +14 -14
- data/features/step_definitions/rails_steps.rb +29 -28
- data/features/step_definitions/s3_steps.rb +2 -2
- data/features/support/env.rb +1 -0
- data/features/support/paths.rb +1 -1
- data/features/support/rails.rb +0 -24
- data/gemfiles/4.2.gemfile +3 -5
- data/gemfiles/{3.2.gemfile → 5.0.gemfile} +4 -6
- data/lib/generators/paperclip/paperclip_generator.rb +9 -1
- data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +1 -1
- data/lib/paperclip.rb +14 -12
- data/lib/paperclip/attachment.rb +44 -20
- data/lib/paperclip/attachment_registry.rb +2 -1
- data/lib/paperclip/callbacks.rb +8 -6
- data/lib/paperclip/content_type_detector.rb +3 -2
- data/lib/paperclip/errors.rb +3 -1
- data/lib/paperclip/file_command_content_type_detector.rb +1 -1
- data/lib/paperclip/filename_cleaner.rb +0 -1
- data/lib/paperclip/geometry_detector_factory.rb +3 -3
- data/lib/paperclip/glue.rb +1 -1
- data/lib/paperclip/has_attached_file.rb +7 -1
- data/lib/paperclip/helpers.rb +15 -11
- data/lib/paperclip/interpolations.rb +7 -2
- data/lib/paperclip/io_adapters/abstract_adapter.rb +31 -4
- data/lib/paperclip/io_adapters/attachment_adapter.rb +17 -6
- data/lib/paperclip/io_adapters/data_uri_adapter.rb +8 -8
- data/lib/paperclip/io_adapters/empty_string_adapter.rb +5 -4
- data/lib/paperclip/io_adapters/file_adapter.rb +12 -6
- data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +8 -7
- data/lib/paperclip/io_adapters/identity_adapter.rb +12 -6
- data/lib/paperclip/io_adapters/nil_adapter.rb +8 -5
- data/lib/paperclip/io_adapters/registry.rb +6 -2
- data/lib/paperclip/io_adapters/stringio_adapter.rb +9 -6
- data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +10 -6
- data/lib/paperclip/io_adapters/uri_adapter.rb +22 -17
- data/lib/paperclip/logger.rb +1 -1
- data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +4 -4
- data/lib/paperclip/media_type_spoof_detector.rb +11 -7
- data/lib/paperclip/processor.rb +15 -6
- data/lib/paperclip/schema.rb +3 -9
- data/lib/paperclip/storage/filesystem.rb +13 -2
- data/lib/paperclip/storage/fog.rb +21 -14
- data/lib/paperclip/storage/s3.rb +81 -61
- data/lib/paperclip/style.rb +0 -1
- data/lib/paperclip/thumbnail.rb +22 -9
- data/lib/paperclip/url_generator.rb +17 -13
- data/lib/paperclip/validators.rb +1 -1
- data/lib/paperclip/validators/attachment_size_validator.rb +1 -7
- data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +4 -0
- data/lib/paperclip/version.rb +3 -1
- data/lib/tasks/paperclip.rake +18 -4
- data/paperclip.gemspec +13 -10
- data/spec/paperclip/attachment_processing_spec.rb +2 -5
- data/spec/paperclip/attachment_registry_spec.rb +28 -0
- data/spec/paperclip/attachment_spec.rb +89 -20
- data/spec/paperclip/content_type_detector_spec.rb +1 -1
- data/spec/paperclip/file_command_content_type_detector_spec.rb +15 -1
- data/spec/paperclip/filename_cleaner_spec.rb +0 -1
- data/spec/paperclip/has_attached_file_spec.rb +24 -8
- data/spec/paperclip/integration_spec.rb +42 -5
- data/spec/paperclip/interpolations_spec.rb +9 -0
- data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +104 -22
- data/spec/paperclip/io_adapters/attachment_adapter_spec.rb +6 -3
- data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +7 -1
- data/spec/paperclip/io_adapters/file_adapter_spec.rb +2 -2
- data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +47 -13
- data/spec/paperclip/io_adapters/identity_adapter_spec.rb +1 -1
- data/spec/paperclip/io_adapters/registry_spec.rb +2 -2
- data/spec/paperclip/io_adapters/stringio_adapter_spec.rb +1 -1
- data/spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb +5 -5
- data/spec/paperclip/io_adapters/uri_adapter_spec.rb +104 -11
- data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +10 -0
- data/spec/paperclip/matchers/validate_attachment_size_matcher_spec.rb +1 -1
- data/spec/paperclip/media_type_spoof_detector_spec.rb +41 -0
- data/spec/paperclip/paperclip_spec.rb +15 -45
- data/spec/paperclip/processor_spec.rb +4 -4
- data/spec/paperclip/schema_spec.rb +46 -46
- data/spec/paperclip/storage/fog_spec.rb +31 -0
- data/spec/paperclip/storage/s3_live_spec.rb +20 -14
- data/spec/paperclip/storage/s3_spec.rb +359 -192
- data/spec/paperclip/style_spec.rb +0 -1
- data/spec/paperclip/tempfile_spec.rb +35 -0
- data/spec/paperclip/thumbnail_spec.rb +43 -38
- data/spec/paperclip/url_generator_spec.rb +54 -44
- data/spec/paperclip/validators/attachment_size_validator_spec.rb +26 -20
- data/spec/paperclip/validators_spec.rb +5 -5
- data/spec/spec_helper.rb +5 -2
- data/spec/support/assertions.rb +12 -1
- data/spec/support/mock_attachment.rb +2 -0
- data/spec/support/mock_url_generator_builder.rb +2 -2
- data/spec/support/model_reconstruction.rb +11 -3
- data/spec/support/reporting.rb +11 -0
- metadata +64 -61
- data/cucumber/paperclip_steps.rb +0 -6
- data/gemfiles/4.1.gemfile +0 -19
- data/lib/paperclip/deprecations.rb +0 -42
- data/lib/paperclip/locales/de.yml +0 -18
- data/lib/paperclip/locales/es.yml +0 -18
- data/lib/paperclip/locales/ja.yml +0 -18
- data/lib/paperclip/locales/pt-BR.yml +0 -18
- data/lib/paperclip/locales/zh-CN.yml +0 -18
- data/lib/paperclip/locales/zh-HK.yml +0 -18
- data/lib/paperclip/locales/zh-TW.yml +0 -18
- data/spec/paperclip/deprecations_spec.rb +0 -65
- data/spec/support/deprecations.rb +0 -9
- data/spec/support/rails_helpers.rb +0 -7
@@ -15,7 +15,7 @@ describe Paperclip::FileAdapter do
|
|
15
15
|
|
16
16
|
context 'doing normal things' do
|
17
17
|
before do
|
18
|
-
@subject = Paperclip.io_adapters.for(@file)
|
18
|
+
@subject = Paperclip.io_adapters.for(@file, hash_digest: Digest::MD5)
|
19
19
|
end
|
20
20
|
|
21
21
|
it 'uses the original filename to generate the tempfile' do
|
@@ -61,7 +61,7 @@ describe Paperclip::FileAdapter do
|
|
61
61
|
context "file with multiple possible content type" do
|
62
62
|
before do
|
63
63
|
MIME::Types.stubs(:type_for).returns([MIME::Type.new('image/x-png'), MIME::Type.new('image/png')])
|
64
|
-
@subject = Paperclip.io_adapters.for(@file)
|
64
|
+
@subject = Paperclip.io_adapters.for(@file, hash_digest: Digest::MD5)
|
65
65
|
end
|
66
66
|
|
67
67
|
it "prefers officially registered mime type" do
|
@@ -3,16 +3,20 @@ require 'spec_helper'
|
|
3
3
|
describe Paperclip::HttpUrlProxyAdapter do
|
4
4
|
before do
|
5
5
|
@open_return = StringIO.new("xxx")
|
6
|
-
@open_return.stubs(:
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
@open_return.stubs(:meta).returns("content-type" => "image/png")
|
7
|
+
Paperclip::HttpUrlProxyAdapter.any_instance.stubs(:download_content).
|
8
|
+
returns(@open_return)
|
9
|
+
Paperclip::HttpUrlProxyAdapter.register
|
10
|
+
end
|
11
|
+
|
12
|
+
after do
|
13
|
+
Paperclip.io_adapters.unregister(described_class)
|
10
14
|
end
|
11
15
|
|
12
16
|
context "a new instance" do
|
13
17
|
before do
|
14
18
|
@url = "http://thoughtbot.com/images/thoughtbot-logo.png"
|
15
|
-
@subject = Paperclip.io_adapters.for(@url)
|
19
|
+
@subject = Paperclip.io_adapters.for(@url, hash_digest: Digest::MD5)
|
16
20
|
end
|
17
21
|
|
18
22
|
after do
|
@@ -64,17 +68,18 @@ describe Paperclip::HttpUrlProxyAdapter do
|
|
64
68
|
end
|
65
69
|
|
66
70
|
context "a url with query params" do
|
67
|
-
|
68
|
-
@url = "https://github.com/thoughtbot/paperclip?file=test"
|
69
|
-
@subject = Paperclip.io_adapters.for(@url)
|
70
|
-
end
|
71
|
+
subject { Paperclip.io_adapters.for(url) }
|
71
72
|
|
72
|
-
after
|
73
|
-
|
74
|
-
|
73
|
+
after { subject.close }
|
74
|
+
|
75
|
+
let(:url) { "https://github.com/thoughtbot/paperclip?file=test" }
|
75
76
|
|
76
77
|
it "returns a file name" do
|
77
|
-
assert_equal "paperclip",
|
78
|
+
assert_equal "paperclip", subject.original_filename
|
79
|
+
end
|
80
|
+
|
81
|
+
it "preserves params" do
|
82
|
+
assert_equal url, subject.instance_variable_get(:@target).to_s
|
78
83
|
end
|
79
84
|
end
|
80
85
|
|
@@ -101,4 +106,33 @@ describe Paperclip::HttpUrlProxyAdapter do
|
|
101
106
|
end
|
102
107
|
end
|
103
108
|
|
109
|
+
context "a url with special characters in the filename" do
|
110
|
+
before do
|
111
|
+
Paperclip::HttpUrlProxyAdapter.any_instance.stubs(:download_content).
|
112
|
+
returns(@open_return)
|
113
|
+
end
|
114
|
+
|
115
|
+
let(:filename) do
|
116
|
+
"paperclip-%C3%B6%C3%A4%C3%BC%E5%AD%97%C2%B4%C2%BD%E2%99%A5"\
|
117
|
+
"%C3%98%C2%B2%C3%88.png"
|
118
|
+
end
|
119
|
+
let(:url) { "https://github.com/thoughtbot/paperclip-öäü字´½♥زÈ.png" }
|
120
|
+
|
121
|
+
subject { Paperclip.io_adapters.for(url) }
|
122
|
+
|
123
|
+
it "returns a encoded filename" do
|
124
|
+
assert_equal filename, subject.original_filename
|
125
|
+
end
|
126
|
+
|
127
|
+
context "when already URI encoded" do
|
128
|
+
let(:url) do
|
129
|
+
"https://github.com/thoughtbot/paperclip-%C3%B6%C3%A4%C3%BC%E5%AD%97"\
|
130
|
+
"%C2%B4%C2%BD%E2%99%A5%C3%98%C2%B2%C3%88.png"
|
131
|
+
end
|
132
|
+
|
133
|
+
it "returns a encoded filename" do
|
134
|
+
assert_equal filename, subject.original_filename
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
104
138
|
end
|
@@ -4,7 +4,7 @@ describe Paperclip::AttachmentRegistry do
|
|
4
4
|
context "for" do
|
5
5
|
before do
|
6
6
|
class AdapterTest
|
7
|
-
def initialize(
|
7
|
+
def initialize(_target, _ = {}); end
|
8
8
|
end
|
9
9
|
@subject = Paperclip::AdapterRegistry.new
|
10
10
|
@subject.register(AdapterTest){|t| Symbol === t }
|
@@ -18,7 +18,7 @@ describe Paperclip::AttachmentRegistry do
|
|
18
18
|
context "registered?" do
|
19
19
|
before do
|
20
20
|
class AdapterTest
|
21
|
-
def initialize(
|
21
|
+
def initialize(_target, _ = {}); end
|
22
22
|
end
|
23
23
|
@subject = Paperclip::AdapterRegistry.new
|
24
24
|
@subject.register(AdapterTest){|t| Symbol === t }
|
@@ -5,7 +5,7 @@ describe Paperclip::StringioAdapter do
|
|
5
5
|
before do
|
6
6
|
@contents = "abc123"
|
7
7
|
@stringio = StringIO.new(@contents)
|
8
|
-
@subject = Paperclip.io_adapters.for(@stringio)
|
8
|
+
@subject = Paperclip.io_adapters.for(@stringio, hash_digest: Digest::MD5)
|
9
9
|
end
|
10
10
|
|
11
11
|
it "returns a file name" do
|
@@ -17,7 +17,7 @@ describe Paperclip::UploadedFileAdapter do
|
|
17
17
|
tempfile: tempfile,
|
18
18
|
path: tempfile.path
|
19
19
|
)
|
20
|
-
@subject = Paperclip.io_adapters.for(@file)
|
20
|
+
@subject = Paperclip.io_adapters.for(@file, hash_digest: Digest::MD5)
|
21
21
|
end
|
22
22
|
|
23
23
|
it "gets the right filename" do
|
@@ -29,7 +29,7 @@ describe Paperclip::UploadedFileAdapter do
|
|
29
29
|
end
|
30
30
|
|
31
31
|
it "gets the content type" do
|
32
|
-
assert_equal "image/
|
32
|
+
assert_equal "image/png", @subject.content_type
|
33
33
|
end
|
34
34
|
|
35
35
|
it "gets the file's size" do
|
@@ -63,7 +63,7 @@ describe Paperclip::UploadedFileAdapter do
|
|
63
63
|
head: "",
|
64
64
|
path: fixture_file("5k.png")
|
65
65
|
)
|
66
|
-
@subject = Paperclip.io_adapters.for(@file)
|
66
|
+
@subject = Paperclip.io_adapters.for(@file, hash_digest: Digest::MD5)
|
67
67
|
end
|
68
68
|
|
69
69
|
it "does not generate paths that include restricted characters" do
|
@@ -86,7 +86,7 @@ describe Paperclip::UploadedFileAdapter do
|
|
86
86
|
head: "",
|
87
87
|
path: fixture_file("5k.png")
|
88
88
|
)
|
89
|
-
@subject = Paperclip.io_adapters.for(@file)
|
89
|
+
@subject = Paperclip.io_adapters.for(@file, hash_digest: Digest::MD5)
|
90
90
|
end
|
91
91
|
|
92
92
|
it "gets the right filename" do
|
@@ -98,7 +98,7 @@ describe Paperclip::UploadedFileAdapter do
|
|
98
98
|
end
|
99
99
|
|
100
100
|
it "gets the content type" do
|
101
|
-
assert_equal "image/
|
101
|
+
assert_equal "image/png", @subject.content_type
|
102
102
|
end
|
103
103
|
|
104
104
|
it "gets the file's size" do
|
@@ -8,14 +8,22 @@ describe Paperclip::UriAdapter do
|
|
8
8
|
@open_return = StringIO.new("xxx")
|
9
9
|
@open_return.stubs(:content_type).returns(content_type)
|
10
10
|
@open_return.stubs(:meta).returns(meta)
|
11
|
-
Paperclip::UriAdapter.
|
12
|
-
|
11
|
+
Paperclip::UriAdapter.register
|
12
|
+
end
|
13
|
+
|
14
|
+
after do
|
15
|
+
Paperclip.io_adapters.unregister(described_class)
|
13
16
|
end
|
14
17
|
|
15
18
|
context "a new instance" do
|
19
|
+
let(:meta) { { "content-type" => "image/png" } }
|
20
|
+
|
16
21
|
before do
|
22
|
+
Paperclip::UriAdapter.any_instance.
|
23
|
+
stubs(:download_content).returns(@open_return)
|
24
|
+
|
17
25
|
@uri = URI.parse("http://thoughtbot.com/images/thoughtbot-logo.png")
|
18
|
-
@subject = Paperclip.io_adapters.for(@uri)
|
26
|
+
@subject = Paperclip.io_adapters.for(@uri, hash_digest: Digest::MD5)
|
19
27
|
end
|
20
28
|
|
21
29
|
it "returns a file name" do
|
@@ -56,7 +64,7 @@ describe Paperclip::UriAdapter do
|
|
56
64
|
assert_equal 'image/png', @subject.content_type
|
57
65
|
end
|
58
66
|
|
59
|
-
it
|
67
|
+
it "accepts an original_filename" do
|
60
68
|
@subject.original_filename = 'image.png'
|
61
69
|
assert_equal 'image.png', @subject.original_filename
|
62
70
|
end
|
@@ -65,8 +73,12 @@ describe Paperclip::UriAdapter do
|
|
65
73
|
|
66
74
|
context "a directory index url" do
|
67
75
|
let(:content_type) { "text/html" }
|
76
|
+
let(:meta) { { "content-type" => "text/html" } }
|
68
77
|
|
69
78
|
before do
|
79
|
+
Paperclip::UriAdapter.any_instance.
|
80
|
+
stubs(:download_content).returns(@open_return)
|
81
|
+
|
70
82
|
@uri = URI.parse("http://thoughtbot.com")
|
71
83
|
@subject = Paperclip.io_adapters.for(@uri)
|
72
84
|
end
|
@@ -82,6 +94,9 @@ describe Paperclip::UriAdapter do
|
|
82
94
|
|
83
95
|
context "a url with query params" do
|
84
96
|
before do
|
97
|
+
Paperclip::UriAdapter.any_instance.
|
98
|
+
stubs(:download_content).returns(@open_return)
|
99
|
+
|
85
100
|
@uri = URI.parse("https://github.com/thoughtbot/paperclip?file=test")
|
86
101
|
@subject = Paperclip.io_adapters.for(@uri)
|
87
102
|
end
|
@@ -93,24 +108,75 @@ describe Paperclip::UriAdapter do
|
|
93
108
|
|
94
109
|
context "a url with content disposition headers" do
|
95
110
|
let(:file_name) { "test_document.pdf" }
|
96
|
-
let(:
|
97
|
-
{
|
98
|
-
"content-disposition" => "attachment; filename=\"#{file_name}\";",
|
99
|
-
}
|
100
|
-
end
|
111
|
+
let(:filename_from_path) { "paperclip" }
|
101
112
|
|
102
113
|
before do
|
103
|
-
|
114
|
+
Paperclip::UriAdapter.any_instance.
|
115
|
+
stubs(:download_content).returns(@open_return)
|
116
|
+
|
117
|
+
@uri = URI.parse(
|
118
|
+
"https://github.com/thoughtbot/#{filename_from_path}?file=test")
|
119
|
+
end
|
120
|
+
|
121
|
+
it "returns file name from path" do
|
122
|
+
meta["content-disposition"] = "inline;"
|
123
|
+
|
104
124
|
@subject = Paperclip.io_adapters.for(@uri)
|
125
|
+
|
126
|
+
assert_equal filename_from_path, @subject.original_filename
|
105
127
|
end
|
106
128
|
|
107
|
-
it "returns a file name" do
|
129
|
+
it "returns a file name enclosed in double quotes" do
|
130
|
+
file_name = "john's test document.pdf"
|
131
|
+
meta["content-disposition"] = "attachment; filename=\"#{file_name}\";"
|
132
|
+
|
133
|
+
@subject = Paperclip.io_adapters.for(@uri)
|
134
|
+
|
108
135
|
assert_equal file_name, @subject.original_filename
|
109
136
|
end
|
137
|
+
|
138
|
+
it "returns a file name not enclosed in double quotes" do
|
139
|
+
meta["content-disposition"] = "ATTACHMENT; FILENAME=#{file_name};"
|
140
|
+
|
141
|
+
@subject = Paperclip.io_adapters.for(@uri)
|
142
|
+
|
143
|
+
assert_equal file_name, @subject.original_filename
|
144
|
+
end
|
145
|
+
|
146
|
+
it "does not crash when an empty filename is given" do
|
147
|
+
meta["content-disposition"] = "ATTACHMENT; FILENAME=\"\";"
|
148
|
+
|
149
|
+
@subject = Paperclip.io_adapters.for(@uri)
|
150
|
+
|
151
|
+
assert_equal "", @subject.original_filename
|
152
|
+
end
|
153
|
+
|
154
|
+
it "returns a file name ignoring RFC 5987 encoding" do
|
155
|
+
meta["content-disposition"] =
|
156
|
+
"attachment; filename=#{file_name}; filename* = utf-8''%e2%82%ac%20rates"
|
157
|
+
|
158
|
+
@subject = Paperclip.io_adapters.for(@uri)
|
159
|
+
|
160
|
+
assert_equal file_name, @subject.original_filename
|
161
|
+
end
|
162
|
+
|
163
|
+
context "when file name has consecutive periods" do
|
164
|
+
let(:file_name) { "test_document..pdf" }
|
165
|
+
|
166
|
+
it "returns a file name" do
|
167
|
+
@uri = URI.parse(
|
168
|
+
"https://github.com/thoughtbot/#{file_name}?file=test")
|
169
|
+
@subject = Paperclip.io_adapters.for(@uri)
|
170
|
+
assert_equal file_name, @subject.original_filename
|
171
|
+
end
|
172
|
+
end
|
110
173
|
end
|
111
174
|
|
112
175
|
context "a url with restricted characters in the filename" do
|
113
176
|
before do
|
177
|
+
Paperclip::UriAdapter.any_instance.
|
178
|
+
stubs(:download_content).returns(@open_return)
|
179
|
+
|
114
180
|
@uri = URI.parse("https://github.com/thoughtbot/paper:clip.jpg")
|
115
181
|
@subject = Paperclip.io_adapters.for(@uri)
|
116
182
|
end
|
@@ -124,4 +190,31 @@ describe Paperclip::UriAdapter do
|
|
124
190
|
end
|
125
191
|
end
|
126
192
|
|
193
|
+
describe "#download_content" do
|
194
|
+
before do
|
195
|
+
Paperclip::UriAdapter.any_instance.stubs(:open).returns(@open_return)
|
196
|
+
@uri = URI.parse("https://github.com/thoughtbot/paper:clip.jpg")
|
197
|
+
@subject = Paperclip.io_adapters.for(@uri)
|
198
|
+
end
|
199
|
+
|
200
|
+
after do
|
201
|
+
@subject.send(:download_content)
|
202
|
+
end
|
203
|
+
|
204
|
+
context "with default read_timeout" do
|
205
|
+
it "calls open without options" do
|
206
|
+
@subject.expects(:open).with(@uri, {}).at_least_once
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
context "with custom read_timeout" do
|
211
|
+
before do
|
212
|
+
Paperclip.options[:read_timeout] = 120
|
213
|
+
end
|
214
|
+
|
215
|
+
it "calls open with read_timeout option" do
|
216
|
+
@subject.expects(:open).with(@uri, read_timeout: 120).at_least_once
|
217
|
+
end
|
218
|
+
end
|
219
|
+
end
|
127
220
|
end
|
@@ -17,22 +17,26 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentContentTypeMatcher do
|
|
17
17
|
|
18
18
|
it "rejects a class with no validation" do
|
19
19
|
expect(matcher).to_not accept(Dummy)
|
20
|
+
expect { matcher.failure_message }.to_not raise_error
|
20
21
|
end
|
21
22
|
|
22
23
|
it 'rejects a class when the validation fails' do
|
23
24
|
Dummy.validates_attachment_content_type :avatar, content_type: %r{audio/.*}
|
24
25
|
expect(matcher).to_not accept(Dummy)
|
26
|
+
expect { matcher.failure_message }.to_not raise_error
|
25
27
|
end
|
26
28
|
|
27
29
|
it "accepts a class with a matching validation" do
|
28
30
|
Dummy.validates_attachment_content_type :avatar, content_type: %r{image/.*}
|
29
31
|
expect(matcher).to accept(Dummy)
|
32
|
+
expect { matcher.failure_message }.to_not raise_error
|
30
33
|
end
|
31
34
|
|
32
35
|
it "accepts a class with other validations but matching types" do
|
33
36
|
Dummy.validates_presence_of :title
|
34
37
|
Dummy.validates_attachment_content_type :avatar, content_type: %r{image/.*}
|
35
38
|
expect(matcher).to accept(Dummy)
|
39
|
+
expect { matcher.failure_message }.to_not raise_error
|
36
40
|
end
|
37
41
|
|
38
42
|
it "accepts a class that matches and a matcher that only specifies 'allowing'" do
|
@@ -40,6 +44,7 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentContentTypeMatcher do
|
|
40
44
|
matcher = plain_matcher.allowing(%w(image/png image/jpeg))
|
41
45
|
|
42
46
|
expect(matcher).to accept(Dummy)
|
47
|
+
expect { matcher.failure_message }.to_not raise_error
|
43
48
|
end
|
44
49
|
|
45
50
|
it "rejects a class that does not match and a matcher that only specifies 'allowing'" do
|
@@ -47,6 +52,7 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentContentTypeMatcher do
|
|
47
52
|
matcher = plain_matcher.allowing(%w(image/png image/jpeg))
|
48
53
|
|
49
54
|
expect(matcher).to_not accept(Dummy)
|
55
|
+
expect { matcher.failure_message }.to_not raise_error
|
50
56
|
end
|
51
57
|
|
52
58
|
it "accepts a class that matches and a matcher that only specifies 'rejecting'" do
|
@@ -54,6 +60,7 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentContentTypeMatcher do
|
|
54
60
|
matcher = plain_matcher.rejecting(%w(audio/mp3 application/octet-stream))
|
55
61
|
|
56
62
|
expect(matcher).to accept(Dummy)
|
63
|
+
expect { matcher.failure_message }.to_not raise_error
|
57
64
|
end
|
58
65
|
|
59
66
|
it "rejects a class that does not match and a matcher that only specifies 'rejecting'" do
|
@@ -61,6 +68,7 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentContentTypeMatcher do
|
|
61
68
|
matcher = plain_matcher.rejecting(%w(audio/mp3 application/octet-stream))
|
62
69
|
|
63
70
|
expect(matcher).to_not accept(Dummy)
|
71
|
+
expect { matcher.failure_message }.to_not raise_error
|
64
72
|
end
|
65
73
|
|
66
74
|
context "using an :if to control the validation" do
|
@@ -75,12 +83,14 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentContentTypeMatcher do
|
|
75
83
|
dummy = Dummy.new
|
76
84
|
dummy.go = true
|
77
85
|
expect(matcher).to accept(dummy)
|
86
|
+
expect { matcher.failure_message }.to_not raise_error
|
78
87
|
end
|
79
88
|
|
80
89
|
it "does not run the validation if the control is false" do
|
81
90
|
dummy = Dummy.new
|
82
91
|
dummy.go = false
|
83
92
|
expect(matcher).to_not accept(dummy)
|
93
|
+
expect { matcher.failure_message }.to_not raise_error
|
84
94
|
end
|
85
95
|
end
|
86
96
|
|
@@ -7,7 +7,7 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentSizeMatcher do
|
|
7
7
|
before do
|
8
8
|
reset_table("dummies") do |d|
|
9
9
|
d.string :avatar_file_name
|
10
|
-
d.
|
10
|
+
d.bigint :avatar_file_size
|
11
11
|
end
|
12
12
|
reset_class "Dummy"
|
13
13
|
Dummy.do_not_validate_attachment_file_type :avatar
|
@@ -58,6 +58,32 @@ describe Paperclip::MediaTypeSpoofDetector do
|
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
61
|
+
context "GIF file named without extension, but we're told GIF" do
|
62
|
+
let(:file) { File.open(fixture_file("animated")) }
|
63
|
+
let(:spoofed?) do
|
64
|
+
Paperclip::MediaTypeSpoofDetector.
|
65
|
+
using(file, "animated", "image/gif").
|
66
|
+
spoofed?
|
67
|
+
end
|
68
|
+
|
69
|
+
it "accepts the file" do
|
70
|
+
assert !spoofed?
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
context "GIF file named without extension, but we're told HTML" do
|
75
|
+
let(:file) { File.open(fixture_file("animated")) }
|
76
|
+
let(:spoofed?) do
|
77
|
+
Paperclip::MediaTypeSpoofDetector.
|
78
|
+
using(file, "animated", "text/html").
|
79
|
+
spoofed?
|
80
|
+
end
|
81
|
+
|
82
|
+
it "rejects the file" do
|
83
|
+
assert spoofed?
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
61
87
|
it "does not reject if content_type is empty but otherwise checks out" do
|
62
88
|
file = File.open(fixture_file("empty.html"))
|
63
89
|
assert ! Paperclip::MediaTypeSpoofDetector.using(file, "empty.html", "").spoofed?
|
@@ -76,4 +102,19 @@ describe Paperclip::MediaTypeSpoofDetector do
|
|
76
102
|
Paperclip.options[:content_type_mappings] = {}
|
77
103
|
end
|
78
104
|
end
|
105
|
+
|
106
|
+
context "#type_from_file_command" do
|
107
|
+
let(:file) { File.new(fixture_file("empty.html")) }
|
108
|
+
let(:detector) { Paperclip::MediaTypeSpoofDetector.new(file, "html", "") }
|
109
|
+
|
110
|
+
it "does work with the output of old versions of file" do
|
111
|
+
Paperclip.stubs(:run).returns("text/html charset=us-ascii")
|
112
|
+
expect(detector.send(:type_from_file_command)).to eq("text/html")
|
113
|
+
end
|
114
|
+
|
115
|
+
it "does work with the output of new versions of file" do
|
116
|
+
Paperclip.stubs(:run).returns("text/html; charset=us-ascii")
|
117
|
+
expect(detector.send(:type_from_file_command)).to eq("text/html")
|
118
|
+
end
|
119
|
+
end
|
79
120
|
end
|