paperclip 3.4.0 → 6.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (220) hide show
  1. checksums.yaml +7 -0
  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 +19 -12
  8. data/Appraisals +4 -11
  9. data/CONTRIBUTING.md +29 -13
  10. data/Gemfile +13 -4
  11. data/LICENSE +1 -3
  12. data/MIGRATING-ES.md +317 -0
  13. data/MIGRATING.md +375 -0
  14. data/NEWS +390 -71
  15. data/README.md +607 -152
  16. data/RELEASING.md +17 -0
  17. data/Rakefile +6 -8
  18. data/UPGRADING +12 -9
  19. data/features/basic_integration.feature +34 -21
  20. data/features/migration.feature +0 -24
  21. data/features/rake_tasks.feature +2 -3
  22. data/features/step_definitions/attachment_steps.rb +44 -36
  23. data/features/step_definitions/html_steps.rb +2 -2
  24. data/features/step_definitions/rails_steps.rb +125 -26
  25. data/features/step_definitions/s3_steps.rb +3 -3
  26. data/features/step_definitions/web_steps.rb +1 -103
  27. data/features/support/env.rb +3 -2
  28. data/features/support/fakeweb.rb +4 -1
  29. data/features/support/file_helpers.rb +12 -2
  30. data/features/support/fixtures/gemfile.txt +1 -1
  31. data/features/support/paths.rb +1 -1
  32. data/features/support/rails.rb +4 -11
  33. data/gemfiles/4.2.gemfile +17 -0
  34. data/gemfiles/5.0.gemfile +17 -0
  35. data/lib/generators/paperclip/paperclip_generator.rb +9 -3
  36. data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +2 -2
  37. data/lib/paperclip/attachment.rb +215 -82
  38. data/lib/paperclip/attachment_registry.rb +60 -0
  39. data/lib/paperclip/callbacks.rb +13 -1
  40. data/lib/paperclip/content_type_detector.rb +48 -24
  41. data/lib/paperclip/errors.rb +8 -1
  42. data/lib/paperclip/file_command_content_type_detector.rb +6 -8
  43. data/lib/paperclip/filename_cleaner.rb +15 -0
  44. data/lib/paperclip/geometry_detector_factory.rb +12 -5
  45. data/lib/paperclip/geometry_parser_factory.rb +1 -1
  46. data/lib/paperclip/glue.rb +1 -2
  47. data/lib/paperclip/has_attached_file.rb +115 -0
  48. data/lib/paperclip/helpers.rb +15 -20
  49. data/lib/paperclip/interpolations/plural_cache.rb +18 -0
  50. data/lib/paperclip/interpolations.rb +36 -14
  51. data/lib/paperclip/io_adapters/abstract_adapter.rb +42 -5
  52. data/lib/paperclip/io_adapters/attachment_adapter.rb +20 -9
  53. data/lib/paperclip/io_adapters/data_uri_adapter.rb +22 -0
  54. data/lib/paperclip/io_adapters/empty_string_adapter.rb +19 -0
  55. data/lib/paperclip/io_adapters/file_adapter.rb +13 -7
  56. data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +16 -0
  57. data/lib/paperclip/io_adapters/identity_adapter.rb +12 -6
  58. data/lib/paperclip/io_adapters/nil_adapter.rb +8 -5
  59. data/lib/paperclip/io_adapters/registry.rb +6 -2
  60. data/lib/paperclip/io_adapters/stringio_adapter.rb +15 -16
  61. data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +11 -7
  62. data/lib/paperclip/io_adapters/uri_adapter.rb +43 -19
  63. data/lib/paperclip/locales/en.yml +1 -0
  64. data/lib/paperclip/logger.rb +1 -1
  65. data/lib/paperclip/matchers/have_attached_file_matcher.rb +3 -6
  66. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +4 -4
  67. data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +7 -2
  68. data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +2 -1
  69. data/lib/paperclip/matchers.rb +1 -1
  70. data/lib/paperclip/media_type_spoof_detector.rb +93 -0
  71. data/lib/paperclip/missing_attachment_styles.rb +11 -16
  72. data/lib/paperclip/processor.rb +15 -43
  73. data/lib/paperclip/processor_helpers.rb +50 -0
  74. data/lib/paperclip/rails_environment.rb +25 -0
  75. data/lib/paperclip/schema.rb +10 -8
  76. data/lib/paperclip/storage/filesystem.rb +20 -5
  77. data/lib/paperclip/storage/fog.rb +49 -23
  78. data/lib/paperclip/storage/s3.rb +153 -82
  79. data/lib/paperclip/style.rb +8 -3
  80. data/lib/paperclip/tempfile_factory.rb +6 -4
  81. data/lib/paperclip/thumbnail.rb +35 -19
  82. data/lib/paperclip/url_generator.rb +26 -14
  83. data/lib/paperclip/validators/attachment_content_type_validator.rb +15 -2
  84. data/lib/paperclip/validators/attachment_file_name_validator.rb +80 -0
  85. data/lib/paperclip/validators/attachment_file_type_ignorance_validator.rb +29 -0
  86. data/lib/paperclip/validators/attachment_presence_validator.rb +12 -8
  87. data/lib/paperclip/validators/attachment_size_validator.rb +17 -10
  88. data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +31 -0
  89. data/lib/paperclip/validators.rb +31 -3
  90. data/lib/paperclip/version.rb +3 -1
  91. data/lib/paperclip.rb +41 -55
  92. data/lib/tasks/paperclip.rake +56 -9
  93. data/paperclip.gemspec +18 -17
  94. data/shoulda_macros/paperclip.rb +13 -3
  95. data/spec/paperclip/attachment_definitions_spec.rb +13 -0
  96. data/spec/paperclip/attachment_processing_spec.rb +79 -0
  97. data/spec/paperclip/attachment_registry_spec.rb +158 -0
  98. data/{test/attachment_test.rb → spec/paperclip/attachment_spec.rb} +597 -389
  99. data/spec/paperclip/content_type_detector_spec.rb +48 -0
  100. data/spec/paperclip/file_command_content_type_detector_spec.rb +40 -0
  101. data/spec/paperclip/filename_cleaner_spec.rb +13 -0
  102. data/spec/paperclip/geometry_detector_spec.rb +39 -0
  103. data/{test/geometry_parser_test.rb → spec/paperclip/geometry_parser_spec.rb} +27 -27
  104. data/{test/geometry_test.rb → spec/paperclip/geometry_spec.rb} +50 -52
  105. data/spec/paperclip/glue_spec.rb +44 -0
  106. data/spec/paperclip/has_attached_file_spec.rb +158 -0
  107. data/{test/integration_test.rb → spec/paperclip/integration_spec.rb} +179 -199
  108. data/{test/interpolations_test.rb → spec/paperclip/interpolations_spec.rb} +79 -46
  109. data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +160 -0
  110. data/{test/io_adapters/attachment_adapter_test.rb → spec/paperclip/io_adapters/attachment_adapter_spec.rb} +54 -25
  111. data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +89 -0
  112. data/spec/paperclip/io_adapters/empty_string_adapter_spec.rb +17 -0
  113. data/spec/paperclip/io_adapters/file_adapter_spec.rb +131 -0
  114. data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +138 -0
  115. data/spec/paperclip/io_adapters/identity_adapter_spec.rb +8 -0
  116. data/{test/io_adapters/nil_adapter_test.rb → spec/paperclip/io_adapters/nil_adapter_spec.rb} +7 -7
  117. data/{test/io_adapters/registry_test.rb → spec/paperclip/io_adapters/registry_spec.rb} +12 -9
  118. data/spec/paperclip/io_adapters/stringio_adapter_spec.rb +64 -0
  119. data/spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb +146 -0
  120. data/spec/paperclip/io_adapters/uri_adapter_spec.rb +220 -0
  121. data/spec/paperclip/matchers/have_attached_file_matcher_spec.rb +19 -0
  122. data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +109 -0
  123. data/spec/paperclip/matchers/validate_attachment_presence_matcher_spec.rb +69 -0
  124. data/spec/paperclip/matchers/validate_attachment_size_matcher_spec.rb +88 -0
  125. data/spec/paperclip/media_type_spoof_detector_spec.rb +120 -0
  126. data/spec/paperclip/meta_class_spec.rb +30 -0
  127. data/spec/paperclip/paperclip_missing_attachment_styles_spec.rb +84 -0
  128. data/spec/paperclip/paperclip_spec.rb +192 -0
  129. data/spec/paperclip/plural_cache_spec.rb +37 -0
  130. data/spec/paperclip/processor_helpers_spec.rb +57 -0
  131. data/{test/processor_test.rb → spec/paperclip/processor_spec.rb} +7 -7
  132. data/spec/paperclip/rails_environment_spec.rb +33 -0
  133. data/spec/paperclip/rake_spec.rb +103 -0
  134. data/spec/paperclip/schema_spec.rb +248 -0
  135. data/{test/storage/filesystem_test.rb → spec/paperclip/storage/filesystem_spec.rb} +18 -18
  136. data/spec/paperclip/storage/fog_spec.rb +566 -0
  137. data/spec/paperclip/storage/s3_live_spec.rb +188 -0
  138. data/spec/paperclip/storage/s3_spec.rb +1693 -0
  139. data/spec/paperclip/style_spec.rb +254 -0
  140. data/spec/paperclip/tempfile_factory_spec.rb +33 -0
  141. data/spec/paperclip/tempfile_spec.rb +35 -0
  142. data/{test/thumbnail_test.rb → spec/paperclip/thumbnail_spec.rb} +186 -141
  143. data/spec/paperclip/url_generator_spec.rb +221 -0
  144. data/spec/paperclip/validators/attachment_content_type_validator_spec.rb +322 -0
  145. data/spec/paperclip/validators/attachment_file_name_validator_spec.rb +160 -0
  146. data/{test/validators/attachment_presence_validator_test.rb → spec/paperclip/validators/attachment_presence_validator_spec.rb} +20 -20
  147. data/{test/validators/attachment_size_validator_test.rb → spec/paperclip/validators/attachment_size_validator_spec.rb} +87 -59
  148. data/spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb +52 -0
  149. data/spec/paperclip/validators_spec.rb +164 -0
  150. data/spec/spec_helper.rb +46 -0
  151. data/spec/support/assertions.rb +82 -0
  152. data/spec/support/fake_model.rb +25 -0
  153. data/spec/support/fake_rails.rb +12 -0
  154. data/spec/support/fixtures/empty.html +1 -0
  155. data/spec/support/fixtures/empty.xlsx +0 -0
  156. data/spec/support/fixtures/spaced file.jpg +0 -0
  157. data/spec/support/matchers/accept.rb +5 -0
  158. data/spec/support/matchers/exist.rb +5 -0
  159. data/spec/support/matchers/have_column.rb +23 -0
  160. data/{test → spec}/support/mock_attachment.rb +2 -0
  161. data/{test → spec}/support/mock_url_generator_builder.rb +2 -2
  162. data/spec/support/model_reconstruction.rb +68 -0
  163. data/spec/support/reporting.rb +11 -0
  164. data/spec/support/test_data.rb +13 -0
  165. data/spec/support/version_helper.rb +9 -0
  166. metadata +395 -346
  167. data/Gemfile.lock +0 -200
  168. data/RUNNING_TESTS.md +0 -4
  169. data/cucumber/paperclip_steps.rb +0 -6
  170. data/gemfiles/3.0.gemfile +0 -11
  171. data/gemfiles/3.1.gemfile +0 -11
  172. data/gemfiles/3.2.gemfile +0 -11
  173. data/lib/paperclip/attachment_options.rb +0 -9
  174. data/lib/paperclip/instance_methods.rb +0 -35
  175. data/test/attachment_options_test.rb +0 -27
  176. data/test/attachment_processing_test.rb +0 -29
  177. data/test/content_type_detector_test.rb +0 -40
  178. data/test/file_command_content_type_detector_test.rb +0 -25
  179. data/test/generator_test.rb +0 -80
  180. data/test/geometry_detector_test.rb +0 -24
  181. data/test/helper.rb +0 -199
  182. data/test/io_adapters/abstract_adapter_test.rb +0 -50
  183. data/test/io_adapters/file_adapter_test.rb +0 -100
  184. data/test/io_adapters/identity_adapter_test.rb +0 -8
  185. data/test/io_adapters/stringio_adapter_test.rb +0 -51
  186. data/test/io_adapters/uploaded_file_adapter_test.rb +0 -123
  187. data/test/io_adapters/uri_adapter_test.rb +0 -86
  188. data/test/matchers/have_attached_file_matcher_test.rb +0 -24
  189. data/test/matchers/validate_attachment_content_type_matcher_test.rb +0 -110
  190. data/test/matchers/validate_attachment_presence_matcher_test.rb +0 -47
  191. data/test/matchers/validate_attachment_size_matcher_test.rb +0 -86
  192. data/test/meta_class_test.rb +0 -32
  193. data/test/paperclip_missing_attachment_styles_test.rb +0 -94
  194. data/test/paperclip_test.rb +0 -259
  195. data/test/schema_test.rb +0 -200
  196. data/test/storage/fog_test.rb +0 -453
  197. data/test/storage/s3_live_test.rb +0 -179
  198. data/test/storage/s3_test.rb +0 -1236
  199. data/test/style_test.rb +0 -213
  200. data/test/support/mock_model.rb +0 -2
  201. data/test/tempfile_factory_test.rb +0 -13
  202. data/test/url_generator_test.rb +0 -187
  203. data/test/validators/attachment_content_type_validator_test.rb +0 -292
  204. data/test/validators_test.rb +0 -25
  205. /data/{test → spec}/database.yml +0 -0
  206. /data/{test → spec/support}/fixtures/12k.png +0 -0
  207. /data/{test → spec/support}/fixtures/50x50.png +0 -0
  208. /data/{test → spec/support}/fixtures/5k.png +0 -0
  209. /data/{test → spec/support}/fixtures/animated +0 -0
  210. /data/{test → spec/support}/fixtures/animated.gif +0 -0
  211. /data/{test → spec/support}/fixtures/animated.unknown +0 -0
  212. /data/{test → spec/support}/fixtures/bad.png +0 -0
  213. /data/{test → spec/support}/fixtures/fog.yml +0 -0
  214. /data/{test → spec/support}/fixtures/rotated.jpg +0 -0
  215. /data/{test → spec/support}/fixtures/s3.yml +0 -0
  216. /data/{test → spec/support}/fixtures/spaced file.png +0 -0
  217. /data/{test → spec/support}/fixtures/text.txt +0 -0
  218. /data/{test → spec/support}/fixtures/twopage.pdf +0 -0
  219. /data/{test → spec/support}/fixtures/uppercase.PNG +0 -0
  220. /data/{test → spec}/support/mock_interpolator.rb +0 -0
data/test/helper.rb DELETED
@@ -1,199 +0,0 @@
1
- require 'rubygems'
2
- require 'tempfile'
3
- require 'pathname'
4
- require 'test/unit'
5
-
6
- require 'shoulda'
7
- require 'mocha'
8
- require 'bourne'
9
-
10
- require 'active_record'
11
- require 'active_record/version'
12
- require 'active_support'
13
- require 'active_support/core_ext'
14
- require 'mime/types'
15
- require 'pathname'
16
- require 'ostruct'
17
- require 'pry'
18
-
19
- puts "Testing against version #{ActiveRecord::VERSION::STRING}"
20
-
21
- `ruby -e 'exit 0'` # Prime $? with a value.
22
-
23
- begin
24
- require 'ruby-debug'
25
- rescue LoadError => e
26
- puts "debugger disabled"
27
- end
28
-
29
- ROOT = Pathname(File.expand_path(File.join(File.dirname(__FILE__), '..')))
30
-
31
- class Test::Unit::TestCase
32
- def setup
33
- silence_warnings do
34
- Object.const_set(:Rails, stub('Rails'))
35
- Rails.stubs(:root).returns(Pathname.new(ROOT).join('tmp'))
36
- Rails.stubs(:env).returns('test')
37
- Rails.stubs(:const_defined?).with(:Railtie).returns(false)
38
- end
39
- end
40
- end
41
-
42
- $LOAD_PATH << File.join(ROOT, 'lib')
43
- $LOAD_PATH << File.join(ROOT, 'lib', 'paperclip')
44
-
45
- require File.join(ROOT, 'lib', 'paperclip.rb')
46
-
47
- require './shoulda_macros/paperclip'
48
-
49
- FIXTURES_DIR = File.join(File.dirname(__FILE__), "fixtures")
50
- config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
51
- ActiveRecord::Base.logger = ActiveSupport::BufferedLogger.new(File.dirname(__FILE__) + "/debug.log")
52
- ActiveRecord::Base.establish_connection(config['test'])
53
- Paperclip.options[:logger] = ActiveRecord::Base.logger
54
-
55
- def require_everything_in_directory(directory_name)
56
- Dir[File.join(File.dirname(__FILE__), directory_name, '*')].each do |f|
57
- require f
58
- end
59
- end
60
-
61
- require_everything_in_directory('support')
62
-
63
- def reset_class class_name
64
- ActiveRecord::Base.send(:include, Paperclip::Glue)
65
- Object.send(:remove_const, class_name) rescue nil
66
- klass = Object.const_set(class_name, Class.new(ActiveRecord::Base))
67
-
68
- klass.class_eval do
69
- include Paperclip::Glue
70
- end
71
-
72
- klass.reset_column_information
73
- klass.connection_pool.clear_table_cache!(klass.table_name) if klass.connection_pool.respond_to?(:clear_table_cache!)
74
- klass.connection.schema_cache.clear_table_cache!(klass.table_name) if klass.connection.respond_to?(:schema_cache)
75
- klass
76
- end
77
-
78
- def reset_table table_name, &block
79
- block ||= lambda { |table| true }
80
- ActiveRecord::Base.connection.create_table :dummies, {:force => true}, &block
81
- end
82
-
83
- def modify_table table_name, &block
84
- ActiveRecord::Base.connection.change_table :dummies, &block
85
- end
86
-
87
- def rebuild_model options = {}
88
- ActiveRecord::Base.connection.create_table :dummies, :force => true do |table|
89
- table.column :title, :string
90
- table.column :other, :string
91
- table.column :avatar_file_name, :string
92
- table.column :avatar_content_type, :string
93
- table.column :avatar_file_size, :integer
94
- table.column :avatar_updated_at, :datetime
95
- table.column :avatar_fingerprint, :string
96
- end
97
- rebuild_class options
98
- end
99
-
100
- def rebuild_class options = {}
101
- reset_class("Dummy").tap do |klass|
102
- klass.has_attached_file :avatar, options
103
- Paperclip.reset_duplicate_clash_check!
104
- end
105
- end
106
-
107
- def rebuild_meta_class_of obj, options = {}
108
- (class << obj; self; end).tap do |metaklass|
109
- metaklass.has_attached_file :avatar, options
110
- Paperclip.reset_duplicate_clash_check!
111
- end
112
- end
113
-
114
- class FakeModel
115
- attr_accessor :avatar_file_name,
116
- :avatar_file_size,
117
- :avatar_updated_at,
118
- :avatar_content_type,
119
- :avatar_fingerprint,
120
- :id
121
-
122
- def errors
123
- @errors ||= []
124
- end
125
-
126
- def run_paperclip_callbacks name, *args
127
- end
128
-
129
- def valid?
130
- errors.empty?
131
- end
132
- end
133
-
134
- def attachment(options={})
135
- Paperclip::Attachment.new(:avatar, FakeModel.new, options)
136
- end
137
-
138
- def silence_warnings
139
- old_verbose, $VERBOSE = $VERBOSE, nil
140
- yield
141
- ensure
142
- $VERBOSE = old_verbose
143
- end
144
-
145
- def should_accept_dummy_class
146
- should "accept the class" do
147
- assert_accepts @matcher, @dummy_class
148
- end
149
-
150
- should "accept an instance of that class" do
151
- assert_accepts @matcher, @dummy_class.new
152
- end
153
- end
154
-
155
- def should_reject_dummy_class
156
- should "reject the class" do
157
- assert_rejects @matcher, @dummy_class
158
- end
159
-
160
- should "reject an instance of that class" do
161
- assert_rejects @matcher, @dummy_class.new
162
- end
163
- end
164
-
165
- def with_exitstatus_returning(code)
166
- saved_exitstatus = $?.nil? ? 0 : $?.exitstatus
167
- begin
168
- `ruby -e 'exit #{code.to_i}'`
169
- yield
170
- ensure
171
- `ruby -e 'exit #{saved_exitstatus.to_i}'`
172
- end
173
- end
174
-
175
- def fixture_file(filename)
176
- File.join(File.dirname(__FILE__), 'fixtures', filename)
177
- end
178
-
179
- def assert_success_response(url)
180
- Net::HTTP.get_response(URI.parse(url)) do |response|
181
- assert_equal "200", response.code,
182
- "Expected HTTP response code 200, got #{response.code}"
183
- end
184
- end
185
-
186
- def assert_not_found_response(url)
187
- Net::HTTP.get_response(URI.parse(url)) do |response|
188
- assert_equal "404", response.code,
189
- "Expected HTTP response code 404, got #{response.code}"
190
- end
191
- end
192
-
193
- def assert_file_exists(path)
194
- assert File.exists?(path), %(Expect "#{path}" to be exists.)
195
- end
196
-
197
- def assert_file_not_exists(path)
198
- assert !File.exists?(path), %(Expect "#{path}" to not exists.)
199
- end
@@ -1,50 +0,0 @@
1
- require './test/helper'
2
-
3
- class AbstractAdapterTest < Test::Unit::TestCase
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
- context "content type from file command" do
13
- setup do
14
- @adapter = TestAdapter.new
15
- @adapter.stubs(:path).returns("image.png")
16
- end
17
-
18
- should "return the content type without newline" do
19
- assert_equal "image/png", @adapter.content_type
20
- end
21
- end
22
-
23
- context "nil?" do
24
- should "return false" do
25
- assert !TestAdapter.new.nil?
26
- end
27
- end
28
-
29
- context "delegation" do
30
- setup do
31
- @adapter = TestAdapter.new
32
- @adapter.tempfile = stub("Tempfile")
33
- end
34
-
35
- [:close, :closed?, :eof?, :path, :rewind, :unlink].each do |method|
36
- should "delegate #{method} to @tempfile" do
37
- @adapter.tempfile.stubs(method)
38
- @adapter.public_send(method)
39
- assert_received @adapter.tempfile, method
40
- end
41
- end
42
- end
43
-
44
- should 'get rid of slashes and colons in filenames' do
45
- @adapter = TestAdapter.new
46
- @adapter.original_filename = "awesome/file:name.png"
47
-
48
- assert_equal "awesome_file_name.png", @adapter.original_filename
49
- end
50
- end
@@ -1,100 +0,0 @@
1
- require './test/helper'
2
-
3
- class FileAdapterTest < Test::Unit::TestCase
4
- context "a new instance" do
5
- context "with normal file" do
6
- setup do
7
- @file = File.new(fixture_file("5k.png"))
8
- @file.binmode
9
- @subject = Paperclip.io_adapters.for(@file)
10
- end
11
-
12
- teardown { @file.close }
13
-
14
- should "get the right filename" do
15
- assert_equal "5k.png", @subject.original_filename
16
- end
17
-
18
- should "force binmode on tempfile" do
19
- assert @subject.instance_variable_get("@tempfile").binmode?
20
- end
21
-
22
- should "get the content type" do
23
- assert_equal "image/png", @subject.content_type
24
- end
25
-
26
- should "return content type as a string" do
27
- assert_kind_of String, @subject.content_type
28
- end
29
-
30
- should "get the file's size" do
31
- assert_equal 4456, @subject.size
32
- end
33
-
34
- should "return false for a call to nil?" do
35
- assert ! @subject.nil?
36
- end
37
-
38
- should "generate a MD5 hash of the contents" do
39
- expected = Digest::MD5.file(@file.path).to_s
40
- assert_equal expected, @subject.fingerprint
41
- end
42
-
43
- should "read the contents of the file" do
44
- expected = @file.read
45
- assert expected.length > 0
46
- assert_equal expected, @subject.read
47
- end
48
-
49
- context "file with multiple possible content type" do
50
- setup do
51
- MIME::Types.stubs(:type_for).returns([MIME::Type.new('image/x-png'), MIME::Type.new('image/png')])
52
- end
53
-
54
- should "prefer officially registered mime type" do
55
- assert_equal "image/png", @subject.content_type
56
- end
57
-
58
- should "return content type as a string" do
59
- assert_kind_of String, @subject.content_type
60
- end
61
- end
62
-
63
- context "file with multiple possible x-types but no official type" do
64
- setup do
65
- MIME::Types.stubs(:type_for).returns([MIME::Type.new('image/x-mp4'), MIME::Type.new('image/x-video')])
66
- @subject = Paperclip.io_adapters.for(@file)
67
- end
68
-
69
- should "return the first" do
70
- assert_equal "image/x-mp4", @subject.content_type
71
- end
72
- end
73
-
74
- context "file with content type derived from file command on *nix" do
75
- setup do
76
- MIME::Types.stubs(:type_for).returns([])
77
- Paperclip.stubs(:run).returns("application/vnd.ms-office\n")
78
- @subject = Paperclip.io_adapters.for(@file)
79
- end
80
-
81
- should "return content type without newline character" do
82
- assert_equal "application/vnd.ms-office", @subject.content_type
83
- end
84
- end
85
- end
86
-
87
- context "empty file" do
88
- setup do
89
- @file = Tempfile.new("file_adapter_test")
90
- @subject = Paperclip.io_adapters.for(@file)
91
- end
92
-
93
- teardown { @file.close }
94
-
95
- should "provide correct mime-type" do
96
- assert_match %r{.*/x-empty}, @subject.content_type
97
- end
98
- end
99
- end
100
- end
@@ -1,8 +0,0 @@
1
- require './test/helper'
2
-
3
- class IdentityAdapterTest < Test::Unit::TestCase
4
- should "respond to #new by returning the argument" do
5
- adapter = Paperclip::IdentityAdapter.new
6
- assert_equal :target, adapter.new(:target)
7
- end
8
- end
@@ -1,51 +0,0 @@
1
- require './test/helper'
2
-
3
- class StringioFileProxyTest < Test::Unit::TestCase
4
- context "a new instance" do
5
- setup do
6
- @contents = "abc123"
7
- @stringio = StringIO.new(@contents)
8
- @subject = Paperclip.io_adapters.for(@stringio)
9
- end
10
-
11
- should "return a file name" do
12
- assert_equal "stringio.txt", @subject.original_filename
13
- end
14
-
15
- should "return a content type" do
16
- assert_equal "text/plain", @subject.content_type
17
- end
18
-
19
- should "return the size of the data" do
20
- assert_equal 6, @subject.size
21
- end
22
-
23
- should "generate an MD5 hash of the contents" do
24
- assert_equal Digest::MD5.hexdigest(@contents), @subject.fingerprint
25
- end
26
-
27
- should "generate correct fingerprint after read" do
28
- fingerprint = Digest::MD5.hexdigest(@subject.read)
29
- assert_equal fingerprint, @subject.fingerprint
30
- end
31
-
32
- should "generate same fingerprint" do
33
- assert_equal @subject.fingerprint, @subject.fingerprint
34
- end
35
-
36
- should "return the data contained in the StringIO" do
37
- assert_equal "abc123", @subject.read
38
- end
39
-
40
- should 'accept a content_type' do
41
- @subject.content_type = 'image/png'
42
- assert_equal 'image/png', @subject.content_type
43
- end
44
-
45
- should 'accept an orgiginal_filename' do
46
- @subject.original_filename = 'image.png'
47
- assert_equal 'image.png', @subject.original_filename
48
- end
49
-
50
- end
51
- end
@@ -1,123 +0,0 @@
1
- require './test/helper'
2
-
3
- class UploadedFileAdapterTest < Test::Unit::TestCase
4
- context "a new instance" do
5
- context "with UploadedFile responding to #tempfile" do
6
- setup do
7
- Paperclip::UploadedFileAdapter.content_type_detector = nil
8
-
9
- class UploadedFile < OpenStruct; end
10
- tempfile = File.new(fixture_file("5k.png"))
11
- tempfile.binmode
12
-
13
- @file = UploadedFile.new(
14
- :original_filename => "5k.png",
15
- :content_type => "image/x-png-by-browser\r",
16
- :head => "",
17
- :tempfile => tempfile,
18
- :path => tempfile.path
19
- )
20
- @subject = Paperclip.io_adapters.for(@file)
21
- end
22
-
23
- should "get the right filename" do
24
- assert_equal "5k.png", @subject.original_filename
25
- end
26
-
27
- should "force binmode on tempfile" do
28
- assert @subject.instance_variable_get("@tempfile").binmode?
29
- end
30
-
31
- should "get the content type" do
32
- assert_equal "image/x-png-by-browser", @subject.content_type
33
- end
34
-
35
- should "get the file's size" do
36
- assert_equal 4456, @subject.size
37
- end
38
-
39
- should "return false for a call to nil?" do
40
- assert ! @subject.nil?
41
- end
42
-
43
- should "generate a MD5 hash of the contents" do
44
- expected = Digest::MD5.file(@file.tempfile.path).to_s
45
- assert_equal expected, @subject.fingerprint
46
- end
47
-
48
- should "read the contents of the file" do
49
- expected = @file.tempfile.read
50
- assert expected.length > 0
51
- assert_equal expected, @subject.read
52
- end
53
- end
54
-
55
- context "with UploadFile responding to #path" do
56
- setup do
57
- Paperclip::UploadedFileAdapter.content_type_detector = nil
58
-
59
- class UploadedFile < OpenStruct; end
60
- @file = UploadedFile.new(
61
- :original_filename => "5k.png",
62
- :content_type => "image/x-png-by-browser",
63
- :head => "",
64
- :path => fixture_file("5k.png")
65
- )
66
- @subject = Paperclip.io_adapters.for(@file)
67
- end
68
-
69
- should "get the right filename" do
70
- assert_equal "5k.png", @subject.original_filename
71
- end
72
-
73
- should "force binmode on tempfile" do
74
- assert @subject.instance_variable_get("@tempfile").binmode?
75
- end
76
-
77
- should "get the content type" do
78
- assert_equal "image/x-png-by-browser", @subject.content_type
79
- end
80
-
81
- should "get the file's size" do
82
- assert_equal 4456, @subject.size
83
- end
84
-
85
- should "return false for a call to nil?" do
86
- assert ! @subject.nil?
87
- end
88
-
89
- should "generate a MD5 hash of the contents" do
90
- expected = Digest::MD5.file(@file.path).to_s
91
- assert_equal expected, @subject.fingerprint
92
- end
93
-
94
- should "read the contents of the file" do
95
- expected_file = File.new(@file.path)
96
- expected_file.binmode
97
- expected = expected_file.read
98
- assert expected.length > 0
99
- assert_equal expected, @subject.read
100
- end
101
-
102
- context "don't trust client-given MIME type" do
103
- setup do
104
- Paperclip::UploadedFileAdapter.content_type_detector =
105
- Paperclip::FileCommandContentTypeDetector
106
-
107
- class UploadedFile < OpenStruct; end
108
- @file = UploadedFile.new(
109
- :original_filename => "5k.png",
110
- :content_type => "image/x-png-by-browser",
111
- :head => "",
112
- :path => fixture_file("5k.png")
113
- )
114
- @subject = Paperclip.io_adapters.for(@file)
115
- end
116
-
117
- should "get the content type" do
118
- assert_equal "image/png", @subject.content_type
119
- end
120
- end
121
- end
122
- end
123
- end
@@ -1,86 +0,0 @@
1
- require './test/helper'
2
-
3
- class UriProxyTest < Test::Unit::TestCase
4
- context "a new instance" do
5
- setup do
6
- @open_return = StringIO.new("xxx")
7
- @open_return.stubs(:content_type).returns("image/png")
8
- Paperclip::UriAdapter.any_instance.stubs(:download_content).returns(@open_return)
9
- @uri = URI.parse("http://thoughtbot.com/images/thoughtbot-logo.png")
10
- @subject = Paperclip.io_adapters.for(@uri)
11
- end
12
-
13
- should "return a file name" do
14
- assert_equal "thoughtbot-logo.png", @subject.original_filename
15
- end
16
-
17
- should 'close open handle after reading' do
18
- assert_equal true, @open_return.closed?
19
- end
20
-
21
- should "return a content type" do
22
- assert_equal "image/png", @subject.content_type
23
- end
24
-
25
- should "return the size of the data" do
26
- assert_equal @open_return.size, @subject.size
27
- end
28
-
29
- should "generate an MD5 hash of the contents" do
30
- assert_equal Digest::MD5.hexdigest("xxx"), @subject.fingerprint
31
- end
32
-
33
- should "generate correct fingerprint after read" do
34
- fingerprint = Digest::MD5.hexdigest(@subject.read)
35
- assert_equal fingerprint, @subject.fingerprint
36
- end
37
-
38
- should "generate same fingerprint" do
39
- assert_equal @subject.fingerprint, @subject.fingerprint
40
- end
41
-
42
- should "return the data contained in the StringIO" do
43
- assert_equal "xxx", @subject.read
44
- end
45
-
46
- should 'accept a content_type' do
47
- @subject.content_type = 'image/png'
48
- assert_equal 'image/png', @subject.content_type
49
- end
50
-
51
- should 'accept an orgiginal_filename' do
52
- @subject.original_filename = 'image.png'
53
- assert_equal 'image.png', @subject.original_filename
54
- end
55
-
56
- end
57
-
58
- context "a directory index url" do
59
- setup do
60
- Paperclip::UriAdapter.any_instance.stubs(:download_content).returns(StringIO.new("xxx"))
61
- @uri = URI.parse("http://thoughtbot.com")
62
- @subject = Paperclip.io_adapters.for(@uri)
63
- end
64
-
65
- should "return a file name" do
66
- assert_equal "index.html", @subject.original_filename
67
- end
68
-
69
- should "return a content type" do
70
- assert_equal "text/html", @subject.content_type
71
- end
72
- end
73
-
74
- context "a url with query params" do
75
- setup do
76
- Paperclip::UriAdapter.any_instance.stubs(:download_content).returns(StringIO.new("xxx"))
77
- @uri = URI.parse("https://github.com/thoughtbot/paperclip?file=test")
78
- @subject = Paperclip.io_adapters.for(@uri)
79
- end
80
-
81
- should "return a file name" do
82
- assert_equal "paperclip", @subject.original_filename
83
- end
84
- end
85
-
86
- end
@@ -1,24 +0,0 @@
1
- require './test/helper'
2
-
3
- class HaveAttachedFileMatcherTest < Test::Unit::TestCase
4
- context "have_attached_file" do
5
- setup do
6
- @dummy_class = reset_class "Dummy"
7
- reset_table "dummies"
8
- @matcher = self.class.have_attached_file(:avatar)
9
- end
10
-
11
- context "given a class with no attachment" do
12
- should_reject_dummy_class
13
- end
14
-
15
- context "given a class with an attachment" do
16
- setup do
17
- modify_table("dummies"){|d| d.string :avatar_file_name }
18
- @dummy_class.has_attached_file :avatar
19
- end
20
-
21
- should_accept_dummy_class
22
- end
23
- end
24
- end