paperclip 3.4.0 → 6.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.codeclimate.yml +17 -0
- data/.github/issue_template.md +3 -0
- data/.gitignore +0 -6
- data/.hound.yml +1055 -0
- data/.rubocop.yml +1 -0
- data/.travis.yml +19 -12
- data/Appraisals +4 -11
- data/CONTRIBUTING.md +29 -13
- data/Gemfile +13 -4
- data/LICENSE +1 -3
- data/MIGRATING-ES.md +317 -0
- data/MIGRATING.md +375 -0
- data/NEWS +390 -71
- data/README.md +607 -152
- data/RELEASING.md +17 -0
- data/Rakefile +6 -8
- data/UPGRADING +12 -9
- data/features/basic_integration.feature +34 -21
- data/features/migration.feature +0 -24
- data/features/rake_tasks.feature +2 -3
- data/features/step_definitions/attachment_steps.rb +44 -36
- data/features/step_definitions/html_steps.rb +2 -2
- data/features/step_definitions/rails_steps.rb +125 -26
- data/features/step_definitions/s3_steps.rb +3 -3
- data/features/step_definitions/web_steps.rb +1 -103
- data/features/support/env.rb +3 -2
- data/features/support/fakeweb.rb +4 -1
- data/features/support/file_helpers.rb +12 -2
- data/features/support/fixtures/gemfile.txt +1 -1
- data/features/support/paths.rb +1 -1
- data/features/support/rails.rb +4 -11
- data/gemfiles/4.2.gemfile +17 -0
- data/gemfiles/5.0.gemfile +17 -0
- data/lib/generators/paperclip/paperclip_generator.rb +9 -3
- data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +2 -2
- data/lib/paperclip/attachment.rb +215 -82
- data/lib/paperclip/attachment_registry.rb +60 -0
- data/lib/paperclip/callbacks.rb +13 -1
- data/lib/paperclip/content_type_detector.rb +48 -24
- data/lib/paperclip/errors.rb +8 -1
- data/lib/paperclip/file_command_content_type_detector.rb +6 -8
- data/lib/paperclip/filename_cleaner.rb +15 -0
- data/lib/paperclip/geometry_detector_factory.rb +12 -5
- data/lib/paperclip/geometry_parser_factory.rb +1 -1
- data/lib/paperclip/glue.rb +1 -2
- data/lib/paperclip/has_attached_file.rb +115 -0
- data/lib/paperclip/helpers.rb +15 -20
- data/lib/paperclip/interpolations/plural_cache.rb +18 -0
- data/lib/paperclip/interpolations.rb +36 -14
- data/lib/paperclip/io_adapters/abstract_adapter.rb +42 -5
- data/lib/paperclip/io_adapters/attachment_adapter.rb +20 -9
- data/lib/paperclip/io_adapters/data_uri_adapter.rb +22 -0
- data/lib/paperclip/io_adapters/empty_string_adapter.rb +19 -0
- data/lib/paperclip/io_adapters/file_adapter.rb +13 -7
- data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +16 -0
- 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 +15 -16
- data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +11 -7
- data/lib/paperclip/io_adapters/uri_adapter.rb +43 -19
- data/lib/paperclip/locales/en.yml +1 -0
- data/lib/paperclip/logger.rb +1 -1
- data/lib/paperclip/matchers/have_attached_file_matcher.rb +3 -6
- data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +4 -4
- data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +7 -2
- data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +2 -1
- data/lib/paperclip/matchers.rb +1 -1
- data/lib/paperclip/media_type_spoof_detector.rb +93 -0
- data/lib/paperclip/missing_attachment_styles.rb +11 -16
- data/lib/paperclip/processor.rb +15 -43
- data/lib/paperclip/processor_helpers.rb +50 -0
- data/lib/paperclip/rails_environment.rb +25 -0
- data/lib/paperclip/schema.rb +10 -8
- data/lib/paperclip/storage/filesystem.rb +20 -5
- data/lib/paperclip/storage/fog.rb +49 -23
- data/lib/paperclip/storage/s3.rb +153 -82
- data/lib/paperclip/style.rb +8 -3
- data/lib/paperclip/tempfile_factory.rb +6 -4
- data/lib/paperclip/thumbnail.rb +35 -19
- data/lib/paperclip/url_generator.rb +26 -14
- data/lib/paperclip/validators/attachment_content_type_validator.rb +15 -2
- data/lib/paperclip/validators/attachment_file_name_validator.rb +80 -0
- data/lib/paperclip/validators/attachment_file_type_ignorance_validator.rb +29 -0
- data/lib/paperclip/validators/attachment_presence_validator.rb +12 -8
- data/lib/paperclip/validators/attachment_size_validator.rb +17 -10
- data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +31 -0
- data/lib/paperclip/validators.rb +31 -3
- data/lib/paperclip/version.rb +3 -1
- data/lib/paperclip.rb +41 -55
- data/lib/tasks/paperclip.rake +56 -9
- data/paperclip.gemspec +18 -17
- data/shoulda_macros/paperclip.rb +13 -3
- data/spec/paperclip/attachment_definitions_spec.rb +13 -0
- data/spec/paperclip/attachment_processing_spec.rb +79 -0
- data/spec/paperclip/attachment_registry_spec.rb +158 -0
- data/{test/attachment_test.rb → spec/paperclip/attachment_spec.rb} +597 -389
- data/spec/paperclip/content_type_detector_spec.rb +48 -0
- data/spec/paperclip/file_command_content_type_detector_spec.rb +40 -0
- data/spec/paperclip/filename_cleaner_spec.rb +13 -0
- data/spec/paperclip/geometry_detector_spec.rb +39 -0
- data/{test/geometry_parser_test.rb → spec/paperclip/geometry_parser_spec.rb} +27 -27
- data/{test/geometry_test.rb → spec/paperclip/geometry_spec.rb} +50 -52
- data/spec/paperclip/glue_spec.rb +44 -0
- data/spec/paperclip/has_attached_file_spec.rb +158 -0
- data/{test/integration_test.rb → spec/paperclip/integration_spec.rb} +179 -199
- data/{test/interpolations_test.rb → spec/paperclip/interpolations_spec.rb} +79 -46
- data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +160 -0
- data/{test/io_adapters/attachment_adapter_test.rb → spec/paperclip/io_adapters/attachment_adapter_spec.rb} +54 -25
- data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +89 -0
- data/spec/paperclip/io_adapters/empty_string_adapter_spec.rb +17 -0
- data/spec/paperclip/io_adapters/file_adapter_spec.rb +131 -0
- data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +138 -0
- data/spec/paperclip/io_adapters/identity_adapter_spec.rb +8 -0
- data/{test/io_adapters/nil_adapter_test.rb → spec/paperclip/io_adapters/nil_adapter_spec.rb} +7 -7
- data/{test/io_adapters/registry_test.rb → spec/paperclip/io_adapters/registry_spec.rb} +12 -9
- data/spec/paperclip/io_adapters/stringio_adapter_spec.rb +64 -0
- data/spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb +146 -0
- data/spec/paperclip/io_adapters/uri_adapter_spec.rb +220 -0
- data/spec/paperclip/matchers/have_attached_file_matcher_spec.rb +19 -0
- data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +109 -0
- data/spec/paperclip/matchers/validate_attachment_presence_matcher_spec.rb +69 -0
- data/spec/paperclip/matchers/validate_attachment_size_matcher_spec.rb +88 -0
- data/spec/paperclip/media_type_spoof_detector_spec.rb +120 -0
- data/spec/paperclip/meta_class_spec.rb +30 -0
- data/spec/paperclip/paperclip_missing_attachment_styles_spec.rb +84 -0
- data/spec/paperclip/paperclip_spec.rb +192 -0
- data/spec/paperclip/plural_cache_spec.rb +37 -0
- data/spec/paperclip/processor_helpers_spec.rb +57 -0
- data/{test/processor_test.rb → spec/paperclip/processor_spec.rb} +7 -7
- data/spec/paperclip/rails_environment_spec.rb +33 -0
- data/spec/paperclip/rake_spec.rb +103 -0
- data/spec/paperclip/schema_spec.rb +248 -0
- data/{test/storage/filesystem_test.rb → spec/paperclip/storage/filesystem_spec.rb} +18 -18
- data/spec/paperclip/storage/fog_spec.rb +566 -0
- data/spec/paperclip/storage/s3_live_spec.rb +188 -0
- data/spec/paperclip/storage/s3_spec.rb +1693 -0
- data/spec/paperclip/style_spec.rb +254 -0
- data/spec/paperclip/tempfile_factory_spec.rb +33 -0
- data/spec/paperclip/tempfile_spec.rb +35 -0
- data/{test/thumbnail_test.rb → spec/paperclip/thumbnail_spec.rb} +186 -141
- data/spec/paperclip/url_generator_spec.rb +221 -0
- data/spec/paperclip/validators/attachment_content_type_validator_spec.rb +322 -0
- data/spec/paperclip/validators/attachment_file_name_validator_spec.rb +160 -0
- data/{test/validators/attachment_presence_validator_test.rb → spec/paperclip/validators/attachment_presence_validator_spec.rb} +20 -20
- data/{test/validators/attachment_size_validator_test.rb → spec/paperclip/validators/attachment_size_validator_spec.rb} +87 -59
- data/spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb +52 -0
- data/spec/paperclip/validators_spec.rb +164 -0
- data/spec/spec_helper.rb +46 -0
- data/spec/support/assertions.rb +82 -0
- data/spec/support/fake_model.rb +25 -0
- data/spec/support/fake_rails.rb +12 -0
- data/spec/support/fixtures/empty.html +1 -0
- data/spec/support/fixtures/empty.xlsx +0 -0
- data/spec/support/fixtures/spaced file.jpg +0 -0
- data/spec/support/matchers/accept.rb +5 -0
- data/spec/support/matchers/exist.rb +5 -0
- data/spec/support/matchers/have_column.rb +23 -0
- data/{test → spec}/support/mock_attachment.rb +2 -0
- data/{test → spec}/support/mock_url_generator_builder.rb +2 -2
- data/spec/support/model_reconstruction.rb +68 -0
- data/spec/support/reporting.rb +11 -0
- data/spec/support/test_data.rb +13 -0
- data/spec/support/version_helper.rb +9 -0
- metadata +395 -346
- data/Gemfile.lock +0 -200
- data/RUNNING_TESTS.md +0 -4
- data/cucumber/paperclip_steps.rb +0 -6
- data/gemfiles/3.0.gemfile +0 -11
- data/gemfiles/3.1.gemfile +0 -11
- data/gemfiles/3.2.gemfile +0 -11
- data/lib/paperclip/attachment_options.rb +0 -9
- data/lib/paperclip/instance_methods.rb +0 -35
- data/test/attachment_options_test.rb +0 -27
- data/test/attachment_processing_test.rb +0 -29
- data/test/content_type_detector_test.rb +0 -40
- data/test/file_command_content_type_detector_test.rb +0 -25
- data/test/generator_test.rb +0 -80
- data/test/geometry_detector_test.rb +0 -24
- data/test/helper.rb +0 -199
- data/test/io_adapters/abstract_adapter_test.rb +0 -50
- data/test/io_adapters/file_adapter_test.rb +0 -100
- data/test/io_adapters/identity_adapter_test.rb +0 -8
- data/test/io_adapters/stringio_adapter_test.rb +0 -51
- data/test/io_adapters/uploaded_file_adapter_test.rb +0 -123
- data/test/io_adapters/uri_adapter_test.rb +0 -86
- data/test/matchers/have_attached_file_matcher_test.rb +0 -24
- data/test/matchers/validate_attachment_content_type_matcher_test.rb +0 -110
- data/test/matchers/validate_attachment_presence_matcher_test.rb +0 -47
- data/test/matchers/validate_attachment_size_matcher_test.rb +0 -86
- data/test/meta_class_test.rb +0 -32
- data/test/paperclip_missing_attachment_styles_test.rb +0 -94
- data/test/paperclip_test.rb +0 -259
- data/test/schema_test.rb +0 -200
- data/test/storage/fog_test.rb +0 -453
- data/test/storage/s3_live_test.rb +0 -179
- data/test/storage/s3_test.rb +0 -1236
- data/test/style_test.rb +0 -213
- data/test/support/mock_model.rb +0 -2
- data/test/tempfile_factory_test.rb +0 -13
- data/test/url_generator_test.rb +0 -187
- data/test/validators/attachment_content_type_validator_test.rb +0 -292
- data/test/validators_test.rb +0 -25
- /data/{test → spec}/database.yml +0 -0
- /data/{test → spec/support}/fixtures/12k.png +0 -0
- /data/{test → spec/support}/fixtures/50x50.png +0 -0
- /data/{test → spec/support}/fixtures/5k.png +0 -0
- /data/{test → spec/support}/fixtures/animated +0 -0
- /data/{test → spec/support}/fixtures/animated.gif +0 -0
- /data/{test → spec/support}/fixtures/animated.unknown +0 -0
- /data/{test → spec/support}/fixtures/bad.png +0 -0
- /data/{test → spec/support}/fixtures/fog.yml +0 -0
- /data/{test → spec/support}/fixtures/rotated.jpg +0 -0
- /data/{test → spec/support}/fixtures/s3.yml +0 -0
- /data/{test → spec/support}/fixtures/spaced file.png +0 -0
- /data/{test → spec/support}/fixtures/text.txt +0 -0
- /data/{test → spec/support}/fixtures/twopage.pdf +0 -0
- /data/{test → spec/support}/fixtures/uppercase.PNG +0 -0
- /data/{test → spec}/support/mock_interpolator.rb +0 -0
@@ -1,7 +1,7 @@
|
|
1
|
-
require '
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
describe Paperclip::Interpolations do
|
4
|
+
it "returns all methods but the infrastructure when sent #all" do
|
5
5
|
methods = Paperclip::Interpolations.all
|
6
6
|
assert ! methods.include?(:[])
|
7
7
|
assert ! methods.include?(:[]=)
|
@@ -11,58 +11,59 @@ class InterpolationsTest < Test::Unit::TestCase
|
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
-
|
14
|
+
it "returns the Rails.root" do
|
15
15
|
assert_equal Rails.root, Paperclip::Interpolations.rails_root(:attachment, :style)
|
16
16
|
end
|
17
17
|
|
18
|
-
|
18
|
+
it "returns the Rails.env" do
|
19
19
|
assert_equal Rails.env, Paperclip::Interpolations.rails_env(:attachment, :style)
|
20
20
|
end
|
21
21
|
|
22
|
-
|
22
|
+
it "returns the class of the Interpolations module when called with no params" do
|
23
23
|
assert_equal Module, Paperclip::Interpolations.class
|
24
24
|
end
|
25
25
|
|
26
|
-
|
26
|
+
it "returns the class of the instance" do
|
27
|
+
class Thing ; end
|
27
28
|
attachment = mock
|
28
29
|
attachment.expects(:instance).returns(attachment)
|
29
|
-
attachment.expects(:class).returns(
|
30
|
+
attachment.expects(:class).returns(Thing)
|
30
31
|
assert_equal "things", Paperclip::Interpolations.class(attachment, :style)
|
31
32
|
end
|
32
33
|
|
33
|
-
|
34
|
+
it "returns the basename of the file" do
|
34
35
|
attachment = mock
|
35
|
-
attachment.expects(:original_filename).returns("one.jpg").times(
|
36
|
+
attachment.expects(:original_filename).returns("one.jpg").times(1)
|
36
37
|
assert_equal "one", Paperclip::Interpolations.basename(attachment, :style)
|
37
38
|
end
|
38
39
|
|
39
|
-
|
40
|
+
it "returns the extension of the file" do
|
40
41
|
attachment = mock
|
41
42
|
attachment.expects(:original_filename).returns("one.jpg")
|
42
43
|
attachment.expects(:styles).returns({})
|
43
44
|
assert_equal "jpg", Paperclip::Interpolations.extension(attachment, :style)
|
44
45
|
end
|
45
46
|
|
46
|
-
|
47
|
+
it "returns the extension of the file as the format if defined in the style" do
|
47
48
|
attachment = mock
|
48
49
|
attachment.expects(:original_filename).never
|
49
|
-
attachment.expects(:styles).twice.returns({:
|
50
|
+
attachment.expects(:styles).twice.returns({style: {format: "png"}})
|
50
51
|
|
51
52
|
[:style, 'style'].each do |style|
|
52
53
|
assert_equal "png", Paperclip::Interpolations.extension(attachment, style)
|
53
54
|
end
|
54
55
|
end
|
55
56
|
|
56
|
-
|
57
|
+
it "returns the extension of the file based on the content type" do
|
57
58
|
attachment = mock
|
58
|
-
attachment.expects(:content_type).returns('image/
|
59
|
+
attachment.expects(:content_type).returns('image/png')
|
59
60
|
attachment.expects(:styles).returns({})
|
60
61
|
interpolations = Paperclip::Interpolations
|
61
62
|
interpolations.expects(:extension).returns('random')
|
62
|
-
assert_equal "
|
63
|
+
assert_equal "png", interpolations.content_type_extension(attachment, :style)
|
63
64
|
end
|
64
65
|
|
65
|
-
|
66
|
+
it "returns the original extension of the file if it matches a content type extension" do
|
66
67
|
attachment = mock
|
67
68
|
attachment.expects(:content_type).returns('image/jpeg')
|
68
69
|
attachment.expects(:styles).returns({})
|
@@ -71,7 +72,21 @@ class InterpolationsTest < Test::Unit::TestCase
|
|
71
72
|
assert_equal "jpe", interpolations.content_type_extension(attachment, :style)
|
72
73
|
end
|
73
74
|
|
74
|
-
|
75
|
+
it "returns the extension of the file with a dot" do
|
76
|
+
attachment = mock
|
77
|
+
attachment.expects(:original_filename).returns("one.jpg")
|
78
|
+
attachment.expects(:styles).returns({})
|
79
|
+
assert_equal ".jpg", Paperclip::Interpolations.dotextension(attachment, :style)
|
80
|
+
end
|
81
|
+
|
82
|
+
it "returns the extension of the file without a dot if the extension is empty" do
|
83
|
+
attachment = mock
|
84
|
+
attachment.expects(:original_filename).returns("one")
|
85
|
+
attachment.expects(:styles).returns({})
|
86
|
+
assert_equal "", Paperclip::Interpolations.dotextension(attachment, :style)
|
87
|
+
end
|
88
|
+
|
89
|
+
it "returns the latter half of the content type of the extension if no match found" do
|
75
90
|
attachment = mock
|
76
91
|
attachment.expects(:content_type).at_least_once().returns('not/found')
|
77
92
|
attachment.expects(:styles).returns({})
|
@@ -80,87 +95,96 @@ class InterpolationsTest < Test::Unit::TestCase
|
|
80
95
|
assert_equal "found", interpolations.content_type_extension(attachment, :style)
|
81
96
|
end
|
82
97
|
|
83
|
-
|
98
|
+
it "returns the format if defined in the style, ignoring the content type" do
|
84
99
|
attachment = mock
|
85
100
|
attachment.expects(:content_type).returns('image/jpeg')
|
86
|
-
attachment.expects(:styles).returns({:
|
101
|
+
attachment.expects(:styles).returns({style: {format: "png"}})
|
87
102
|
interpolations = Paperclip::Interpolations
|
88
103
|
interpolations.expects(:extension).returns('random')
|
89
104
|
assert_equal "png", interpolations.content_type_extension(attachment, :style)
|
90
105
|
end
|
91
106
|
|
92
|
-
|
107
|
+
it "is able to handle numeric style names" do
|
93
108
|
attachment = mock(
|
94
|
-
:
|
109
|
+
styles: {:"4" => {format: :expected_extension}}
|
95
110
|
)
|
96
111
|
assert_equal :expected_extension, Paperclip::Interpolations.extension(attachment, 4)
|
97
112
|
end
|
98
113
|
|
99
|
-
|
114
|
+
it "returns the #to_param of the attachment" do
|
100
115
|
attachment = mock
|
101
116
|
attachment.expects(:to_param).returns("23-awesome")
|
102
117
|
attachment.expects(:instance).returns(attachment)
|
103
118
|
assert_equal "23-awesome", Paperclip::Interpolations.param(attachment, :style)
|
104
119
|
end
|
105
120
|
|
106
|
-
|
121
|
+
it "returns the id of the attachment" do
|
107
122
|
attachment = mock
|
108
123
|
attachment.expects(:id).returns(23)
|
109
124
|
attachment.expects(:instance).returns(attachment)
|
110
125
|
assert_equal 23, Paperclip::Interpolations.id(attachment, :style)
|
111
126
|
end
|
112
127
|
|
113
|
-
|
128
|
+
it "returns nil for attachments to new records" do
|
114
129
|
attachment = mock
|
115
130
|
attachment.expects(:id).returns(nil)
|
116
131
|
attachment.expects(:instance).returns(attachment)
|
117
132
|
assert_nil Paperclip::Interpolations.id(attachment, :style)
|
118
133
|
end
|
119
134
|
|
120
|
-
|
135
|
+
it "returns the partitioned id of the attachment when the id is an integer" do
|
121
136
|
attachment = mock
|
122
137
|
attachment.expects(:id).returns(23)
|
123
138
|
attachment.expects(:instance).returns(attachment)
|
124
139
|
assert_equal "000/000/023", Paperclip::Interpolations.id_partition(attachment, :style)
|
125
140
|
end
|
126
141
|
|
127
|
-
|
142
|
+
it "returns the partitioned id when the id is above 999_999_999" do
|
143
|
+
attachment = mock
|
144
|
+
attachment.expects(:id).
|
145
|
+
returns(Paperclip::Interpolations::ID_PARTITION_LIMIT)
|
146
|
+
attachment.expects(:instance).returns(attachment)
|
147
|
+
assert_equal "001/000/000/000",
|
148
|
+
Paperclip::Interpolations.id_partition(attachment, :style)
|
149
|
+
end
|
150
|
+
|
151
|
+
it "returns the partitioned id of the attachment when the id is a string" do
|
128
152
|
attachment = mock
|
129
153
|
attachment.expects(:id).returns("32fnj23oio2f")
|
130
154
|
attachment.expects(:instance).returns(attachment)
|
131
155
|
assert_equal "32f/nj2/3oi", Paperclip::Interpolations.id_partition(attachment, :style)
|
132
156
|
end
|
133
157
|
|
134
|
-
|
158
|
+
it "returns nil for the partitioned id of an attachment to a new record (when the id is nil)" do
|
135
159
|
attachment = mock
|
136
160
|
attachment.expects(:id).returns(nil)
|
137
161
|
attachment.expects(:instance).returns(attachment)
|
138
162
|
assert_nil Paperclip::Interpolations.id_partition(attachment, :style)
|
139
163
|
end
|
140
164
|
|
141
|
-
|
165
|
+
it "returns the name of the attachment" do
|
142
166
|
attachment = mock
|
143
167
|
attachment.expects(:name).returns("file")
|
144
168
|
assert_equal "files", Paperclip::Interpolations.attachment(attachment, :style)
|
145
169
|
end
|
146
170
|
|
147
|
-
|
171
|
+
it "returns the style" do
|
148
172
|
assert_equal :style, Paperclip::Interpolations.style(:attachment, :style)
|
149
173
|
end
|
150
174
|
|
151
|
-
|
175
|
+
it "returns the default style" do
|
152
176
|
attachment = mock
|
153
177
|
attachment.expects(:default_style).returns(:default_style)
|
154
178
|
assert_equal :default_style, Paperclip::Interpolations.style(attachment, nil)
|
155
179
|
end
|
156
180
|
|
157
|
-
|
181
|
+
it "reinterpolates :url" do
|
158
182
|
attachment = mock
|
159
|
-
attachment.expects(:url).with(:style, :
|
183
|
+
attachment.expects(:url).with(:style, timestamp: false, escape: false).returns("1234")
|
160
184
|
assert_equal "1234", Paperclip::Interpolations.url(attachment, :style)
|
161
185
|
end
|
162
186
|
|
163
|
-
|
187
|
+
it "raises if infinite loop detcted reinterpolating :url" do
|
164
188
|
attachment = Object.new
|
165
189
|
class << attachment
|
166
190
|
def url(*args)
|
@@ -170,35 +194,35 @@ class InterpolationsTest < Test::Unit::TestCase
|
|
170
194
|
assert_raises(Paperclip::Errors::InfiniteInterpolationError){ Paperclip::Interpolations.url(attachment, :style) }
|
171
195
|
end
|
172
196
|
|
173
|
-
|
197
|
+
it "returns the filename as basename.extension" do
|
174
198
|
attachment = mock
|
175
199
|
attachment.expects(:styles).returns({})
|
176
|
-
attachment.expects(:original_filename).returns("one.jpg").times(
|
200
|
+
attachment.expects(:original_filename).returns("one.jpg").times(2)
|
177
201
|
assert_equal "one.jpg", Paperclip::Interpolations.filename(attachment, :style)
|
178
202
|
end
|
179
203
|
|
180
|
-
|
204
|
+
it "returns the filename as basename.extension when format supplied" do
|
181
205
|
attachment = mock
|
182
|
-
attachment.expects(:styles).returns({:
|
183
|
-
attachment.expects(:original_filename).returns("one.jpg").times(
|
206
|
+
attachment.expects(:styles).returns({style: {format: :png}})
|
207
|
+
attachment.expects(:original_filename).returns("one.jpg").times(1)
|
184
208
|
assert_equal "one.png", Paperclip::Interpolations.filename(attachment, :style)
|
185
209
|
end
|
186
210
|
|
187
|
-
|
211
|
+
it "returns the filename as basename when extension is blank" do
|
188
212
|
attachment = mock
|
189
213
|
attachment.stubs(:styles).returns({})
|
190
214
|
attachment.stubs(:original_filename).returns("one")
|
191
215
|
assert_equal "one", Paperclip::Interpolations.filename(attachment, :style)
|
192
216
|
end
|
193
217
|
|
194
|
-
|
218
|
+
it "returns the basename when the extension contains regexp special characters" do
|
195
219
|
attachment = mock
|
196
220
|
attachment.stubs(:styles).returns({})
|
197
221
|
attachment.stubs(:original_filename).returns("one.ab)")
|
198
222
|
assert_equal "one", Paperclip::Interpolations.basename(attachment, :style)
|
199
223
|
end
|
200
224
|
|
201
|
-
|
225
|
+
it "returns the timestamp" do
|
202
226
|
now = Time.now
|
203
227
|
zone = 'UTC'
|
204
228
|
attachment = mock
|
@@ -207,32 +231,41 @@ class InterpolationsTest < Test::Unit::TestCase
|
|
207
231
|
assert_equal now.in_time_zone(zone).to_s, Paperclip::Interpolations.timestamp(attachment, :style)
|
208
232
|
end
|
209
233
|
|
210
|
-
|
234
|
+
it "returns updated_at" do
|
211
235
|
attachment = mock
|
212
236
|
seconds_since_epoch = 1234567890
|
213
237
|
attachment.expects(:updated_at).returns(seconds_since_epoch)
|
214
238
|
assert_equal seconds_since_epoch, Paperclip::Interpolations.updated_at(attachment, :style)
|
215
239
|
end
|
216
240
|
|
217
|
-
|
241
|
+
it "returns attachment's hash when passing both arguments" do
|
218
242
|
attachment = mock
|
219
243
|
fake_hash = "a_wicked_secure_hash"
|
220
244
|
attachment.expects(:hash_key).returns(fake_hash)
|
221
245
|
assert_equal fake_hash, Paperclip::Interpolations.hash(attachment, :style)
|
222
246
|
end
|
223
247
|
|
224
|
-
|
248
|
+
it "returns Object#hash when passing no argument" do
|
225
249
|
attachment = mock
|
226
250
|
fake_hash = "a_wicked_secure_hash"
|
227
251
|
attachment.expects(:hash_key).never.returns(fake_hash)
|
228
252
|
assert_not_equal fake_hash, Paperclip::Interpolations.hash
|
229
253
|
end
|
230
254
|
|
231
|
-
|
255
|
+
it "calls all expected interpolations with the given arguments" do
|
232
256
|
Paperclip::Interpolations.expects(:id).with(:attachment, :style).returns(1234)
|
233
257
|
Paperclip::Interpolations.expects(:attachment).with(:attachment, :style).returns("attachments")
|
234
258
|
Paperclip::Interpolations.expects(:notreal).never
|
235
259
|
value = Paperclip::Interpolations.interpolate(":notreal/:id/:attachment", :attachment, :style)
|
236
260
|
assert_equal ":notreal/1234/attachments", value
|
237
261
|
end
|
262
|
+
|
263
|
+
it "handles question marks" do
|
264
|
+
Paperclip.interpolates :foo? do
|
265
|
+
"bar"
|
266
|
+
end
|
267
|
+
Paperclip::Interpolations.expects(:fool).never
|
268
|
+
value = Paperclip::Interpolations.interpolate(":fo/:foo?")
|
269
|
+
assert_equal ":fo/bar", value
|
270
|
+
end
|
238
271
|
end
|
@@ -0,0 +1,160 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Paperclip::AbstractAdapter do
|
4
|
+
class TestAdapter < Paperclip::AbstractAdapter
|
5
|
+
attr_accessor :tempfile
|
6
|
+
|
7
|
+
def content_type
|
8
|
+
Paperclip::ContentTypeDetector.new(path).detect
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
subject { TestAdapter.new(nil) }
|
13
|
+
|
14
|
+
context "content type from file contents" do
|
15
|
+
before do
|
16
|
+
subject.stubs(:path).returns("image.png")
|
17
|
+
Paperclip.stubs(:run).returns("image/png\n")
|
18
|
+
Paperclip::ContentTypeDetector.any_instance.stubs(:type_from_mime_magic).returns("image/png")
|
19
|
+
end
|
20
|
+
|
21
|
+
it "returns the content type without newline" do
|
22
|
+
assert_equal "image/png", subject.content_type
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "nil?" do
|
27
|
+
it "returns false" do
|
28
|
+
assert !subject.nil?
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context "delegation" do
|
33
|
+
before do
|
34
|
+
subject.tempfile = stub("Tempfile")
|
35
|
+
end
|
36
|
+
|
37
|
+
[:binmode, :binmode?, :close, :close!, :closed?, :eof?, :path, :readbyte, :rewind, :unlink].each do |method|
|
38
|
+
it "delegates #{method} to @tempfile" do
|
39
|
+
subject.tempfile.stubs(method)
|
40
|
+
subject.public_send(method)
|
41
|
+
assert_received subject.tempfile, method
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'gets rid of slashes and colons in filenames' do
|
47
|
+
subject.original_filename = "awesome/file:name.png"
|
48
|
+
|
49
|
+
assert_equal "awesome_file_name.png", subject.original_filename
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'is an assignment' do
|
53
|
+
assert subject.assignment?
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'is not nil' do
|
57
|
+
assert !subject.nil?
|
58
|
+
end
|
59
|
+
|
60
|
+
it "generates a destination filename with no original filename" do
|
61
|
+
expect(subject.send(:destination).path).to_not be_nil
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'uses the original filename to generate the tempfile' do
|
65
|
+
subject.original_filename = "file.png"
|
66
|
+
expect(subject.send(:destination).path).to end_with(".png")
|
67
|
+
end
|
68
|
+
|
69
|
+
context "generates a fingerprint" do
|
70
|
+
subject { TestAdapter.new(nil, options) }
|
71
|
+
|
72
|
+
before do
|
73
|
+
subject.stubs(:path).returns(fixture_file("50x50.png"))
|
74
|
+
end
|
75
|
+
|
76
|
+
context "MD5" do
|
77
|
+
let(:options) { { hash_digest: Digest::MD5 } }
|
78
|
+
|
79
|
+
it "returns a fingerprint" do
|
80
|
+
expect(subject.fingerprint).to be_a String
|
81
|
+
expect(subject.fingerprint).to eq "a790b00c9b5d58a8fd17a1ec5a187129"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
context "SHA256" do
|
86
|
+
let(:options) { { hash_digest: Digest::SHA256 } }
|
87
|
+
|
88
|
+
it "returns a fingerprint" do
|
89
|
+
expect(subject.fingerprint).to be_a String
|
90
|
+
expect(subject.fingerprint).
|
91
|
+
to eq "243d7ce1099719df25f600f1c369c629fb979f88d5a01dbe7d0d48c8e6715bb1"
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
context "#copy_to_tempfile" do
|
97
|
+
around do |example|
|
98
|
+
FileUtils.module_eval do
|
99
|
+
class << self
|
100
|
+
alias paperclip_ln ln
|
101
|
+
|
102
|
+
def ln(*)
|
103
|
+
raise Errno::EXDEV
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
example.run
|
109
|
+
|
110
|
+
FileUtils.module_eval do
|
111
|
+
class << self
|
112
|
+
alias ln paperclip_ln
|
113
|
+
undef paperclip_ln
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
it "should return a readable file even when linking fails" do
|
119
|
+
src = open(fixture_file("5k.png"), "rb")
|
120
|
+
expect(subject.send(:copy_to_tempfile, src).read).to eq src.read
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
context "#original_filename=" do
|
125
|
+
it "should not fail with a nil original filename" do
|
126
|
+
expect { subject.original_filename = nil }.not_to raise_error
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
context "#link_or_copy_file" do
|
131
|
+
class TestLinkOrCopyAdapter < Paperclip::AbstractAdapter
|
132
|
+
public :copy_to_tempfile, :destination
|
133
|
+
end
|
134
|
+
|
135
|
+
subject { TestLinkOrCopyAdapter.new(nil) }
|
136
|
+
let(:body) { "body" }
|
137
|
+
|
138
|
+
let(:file) do
|
139
|
+
t = Tempfile.new("destination")
|
140
|
+
t.print(body)
|
141
|
+
t.rewind
|
142
|
+
t
|
143
|
+
end
|
144
|
+
|
145
|
+
after do
|
146
|
+
file.close
|
147
|
+
file.unlink
|
148
|
+
end
|
149
|
+
|
150
|
+
it "should be able to read the file" do
|
151
|
+
expect(subject.copy_to_tempfile(file).read).to eq(body)
|
152
|
+
end
|
153
|
+
|
154
|
+
it "should be able to reopen the file after symlink has failed" do
|
155
|
+
FileUtils.expects(:ln).raises(Errno::EXDEV)
|
156
|
+
|
157
|
+
expect(subject.copy_to_tempfile(file).read).to eq(body)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
@@ -1,52 +1,53 @@
|
|
1
|
-
require '
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
rebuild_model :path => "tmp/:class/:attachment/:style/:filename", :styles => {:thumb => '50x50'}
|
3
|
+
describe Paperclip::AttachmentAdapter do
|
4
|
+
before do
|
5
|
+
rebuild_model path: "tmp/:class/:attachment/:style/:filename", styles: {thumb: '50x50'}
|
7
6
|
@attachment = Dummy.new.avatar
|
8
7
|
end
|
9
8
|
|
10
9
|
context "for an attachment" do
|
11
|
-
|
10
|
+
before do
|
12
11
|
@file = File.new(fixture_file("5k.png"))
|
13
12
|
@file.binmode
|
14
13
|
|
15
14
|
@attachment.assign(@file)
|
16
15
|
@attachment.save
|
17
|
-
@subject = Paperclip.io_adapters.for(@attachment
|
16
|
+
@subject = Paperclip.io_adapters.for(@attachment,
|
17
|
+
hash_digest: Digest::MD5)
|
18
18
|
end
|
19
19
|
|
20
|
-
|
20
|
+
after do
|
21
21
|
@file.close
|
22
|
+
@subject.close
|
22
23
|
end
|
23
24
|
|
24
|
-
|
25
|
+
it "gets the right filename" do
|
25
26
|
assert_equal "5k.png", @subject.original_filename
|
26
27
|
end
|
27
28
|
|
28
|
-
|
29
|
+
it "forces binmode on tempfile" do
|
29
30
|
assert @subject.instance_variable_get("@tempfile").binmode?
|
30
31
|
end
|
31
32
|
|
32
|
-
|
33
|
+
it "gets the content type" do
|
33
34
|
assert_equal "image/png", @subject.content_type
|
34
35
|
end
|
35
36
|
|
36
|
-
|
37
|
+
it "gets the file's size" do
|
37
38
|
assert_equal 4456, @subject.size
|
38
39
|
end
|
39
40
|
|
40
|
-
|
41
|
+
it "returns false for a call to nil?" do
|
41
42
|
assert ! @subject.nil?
|
42
43
|
end
|
43
44
|
|
44
|
-
|
45
|
+
it "generates a MD5 hash of the contents" do
|
45
46
|
expected = Digest::MD5.file(@file.path).to_s
|
46
47
|
assert_equal expected, @subject.fingerprint
|
47
48
|
end
|
48
49
|
|
49
|
-
|
50
|
+
it "reads the contents of the file" do
|
50
51
|
expected = @file.read
|
51
52
|
actual = @subject.read
|
52
53
|
assert expected.length > 0
|
@@ -56,8 +57,34 @@ class AttachmentAdapterTest < Test::Unit::TestCase
|
|
56
57
|
|
57
58
|
end
|
58
59
|
|
60
|
+
context "for a file with restricted characters in the name" do
|
61
|
+
before do
|
62
|
+
file_contents = IO.read(fixture_file("animated.gif"))
|
63
|
+
@file = StringIO.new(file_contents)
|
64
|
+
@file.stubs(:original_filename).returns('image:restricted.gif')
|
65
|
+
@file.binmode
|
66
|
+
|
67
|
+
@attachment.assign(@file)
|
68
|
+
@attachment.save
|
69
|
+
@subject = Paperclip.io_adapters.for(@attachment,
|
70
|
+
hash_digest: Digest::MD5)
|
71
|
+
end
|
72
|
+
|
73
|
+
after do
|
74
|
+
@subject.close
|
75
|
+
end
|
76
|
+
|
77
|
+
it "does not generate paths that include restricted characters" do
|
78
|
+
expect(@subject.path).to_not match(/:/)
|
79
|
+
end
|
80
|
+
|
81
|
+
it "does not generate filenames that include restricted characters" do
|
82
|
+
assert_equal 'image_restricted.gif', @subject.original_filename
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
59
86
|
context "for a style" do
|
60
|
-
|
87
|
+
before do
|
61
88
|
@file = File.new(fixture_file("5k.png"))
|
62
89
|
@file.binmode
|
63
90
|
|
@@ -67,40 +94,42 @@ class AttachmentAdapterTest < Test::Unit::TestCase
|
|
67
94
|
FileUtils.cp @attachment.queued_for_write[:thumb].path, @thumb.path
|
68
95
|
|
69
96
|
@attachment.save
|
70
|
-
@subject = Paperclip.io_adapters.for(@attachment.styles[:thumb]
|
97
|
+
@subject = Paperclip.io_adapters.for(@attachment.styles[:thumb],
|
98
|
+
hash_digest: Digest::MD5)
|
71
99
|
end
|
72
100
|
|
73
|
-
|
101
|
+
after do
|
74
102
|
@file.close
|
75
103
|
@thumb.close
|
104
|
+
@subject.close
|
76
105
|
end
|
77
106
|
|
78
|
-
|
107
|
+
it "gets the original filename" do
|
79
108
|
assert_equal "5k.png", @subject.original_filename
|
80
109
|
end
|
81
110
|
|
82
|
-
|
111
|
+
it "forces binmode on tempfile" do
|
83
112
|
assert @subject.instance_variable_get("@tempfile").binmode?
|
84
113
|
end
|
85
114
|
|
86
|
-
|
115
|
+
it "gets the content type" do
|
87
116
|
assert_equal "image/png", @subject.content_type
|
88
117
|
end
|
89
118
|
|
90
|
-
|
119
|
+
it "gets the thumbnail's file size" do
|
91
120
|
assert_equal @thumb.size, @subject.size
|
92
121
|
end
|
93
122
|
|
94
|
-
|
123
|
+
it "returns false for a call to nil?" do
|
95
124
|
assert ! @subject.nil?
|
96
125
|
end
|
97
126
|
|
98
|
-
|
127
|
+
it "generates a MD5 hash of the contents" do
|
99
128
|
expected = Digest::MD5.file(@thumb.path).to_s
|
100
129
|
assert_equal expected, @subject.fingerprint
|
101
130
|
end
|
102
131
|
|
103
|
-
|
132
|
+
it "reads the contents of the thumbnail" do
|
104
133
|
@thumb.rewind
|
105
134
|
expected = @thumb.read
|
106
135
|
actual = @subject.read
|