paperclip 3.5.4 → 5.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (212) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +17 -0
  3. data/.gitignore +0 -6
  4. data/.hound.yml +1055 -0
  5. data/.rubocop.yml +1 -0
  6. data/.travis.yml +17 -20
  7. data/Appraisals +4 -16
  8. data/CONTRIBUTING.md +29 -13
  9. data/Gemfile +11 -3
  10. data/LICENSE +1 -3
  11. data/NEWS +241 -49
  12. data/README.md +471 -166
  13. data/RELEASING.md +17 -0
  14. data/Rakefile +6 -8
  15. data/UPGRADING +12 -9
  16. data/features/basic_integration.feature +27 -8
  17. data/features/migration.feature +0 -24
  18. data/features/step_definitions/attachment_steps.rb +36 -28
  19. data/features/step_definitions/html_steps.rb +2 -2
  20. data/features/step_definitions/rails_steps.rb +68 -37
  21. data/features/step_definitions/s3_steps.rb +2 -2
  22. data/features/step_definitions/web_steps.rb +1 -103
  23. data/features/support/env.rb +3 -2
  24. data/features/support/file_helpers.rb +2 -2
  25. data/features/support/fixtures/gemfile.txt +1 -1
  26. data/features/support/paths.rb +1 -1
  27. data/features/support/rails.rb +2 -25
  28. data/gemfiles/4.2.gemfile +17 -0
  29. data/gemfiles/5.0.gemfile +17 -0
  30. data/lib/generators/paperclip/paperclip_generator.rb +9 -3
  31. data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +2 -2
  32. data/lib/paperclip/attachment.rb +151 -46
  33. data/lib/paperclip/attachment_registry.rb +3 -2
  34. data/lib/paperclip/callbacks.rb +13 -1
  35. data/lib/paperclip/content_type_detector.rb +26 -22
  36. data/lib/paperclip/errors.rb +8 -1
  37. data/lib/paperclip/file_command_content_type_detector.rb +6 -8
  38. data/lib/paperclip/geometry_detector_factory.rb +5 -3
  39. data/lib/paperclip/geometry_parser_factory.rb +1 -1
  40. data/lib/paperclip/glue.rb +1 -1
  41. data/lib/paperclip/has_attached_file.rb +17 -1
  42. data/lib/paperclip/helpers.rb +15 -11
  43. data/lib/paperclip/interpolations/plural_cache.rb +6 -5
  44. data/lib/paperclip/interpolations.rb +26 -13
  45. data/lib/paperclip/io_adapters/abstract_adapter.rb +32 -4
  46. data/lib/paperclip/io_adapters/attachment_adapter.rb +13 -8
  47. data/lib/paperclip/io_adapters/data_uri_adapter.rb +11 -16
  48. data/lib/paperclip/io_adapters/empty_string_adapter.rb +5 -4
  49. data/lib/paperclip/io_adapters/file_adapter.rb +12 -6
  50. data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +7 -7
  51. data/lib/paperclip/io_adapters/identity_adapter.rb +12 -6
  52. data/lib/paperclip/io_adapters/nil_adapter.rb +8 -5
  53. data/lib/paperclip/io_adapters/registry.rb +6 -2
  54. data/lib/paperclip/io_adapters/stringio_adapter.rb +15 -16
  55. data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +10 -6
  56. data/lib/paperclip/io_adapters/uri_adapter.rb +41 -19
  57. data/lib/paperclip/locales/en.yml +1 -0
  58. data/lib/paperclip/matchers/have_attached_file_matcher.rb +2 -1
  59. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +4 -4
  60. data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +2 -1
  61. data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +2 -1
  62. data/lib/paperclip/media_type_spoof_detector.rb +90 -0
  63. data/lib/paperclip/processor.rb +5 -41
  64. data/lib/paperclip/processor_helpers.rb +50 -0
  65. data/lib/paperclip/rails_environment.rb +25 -0
  66. data/lib/paperclip/schema.rb +9 -7
  67. data/lib/paperclip/storage/filesystem.rb +14 -3
  68. data/lib/paperclip/storage/fog.rb +37 -19
  69. data/lib/paperclip/storage/s3.rb +129 -69
  70. data/lib/paperclip/style.rb +8 -2
  71. data/lib/paperclip/tempfile_factory.rb +5 -1
  72. data/lib/paperclip/thumbnail.rb +30 -18
  73. data/lib/paperclip/url_generator.rb +26 -14
  74. data/lib/paperclip/validators/attachment_content_type_validator.rb +4 -0
  75. data/lib/paperclip/validators/attachment_file_name_validator.rb +80 -0
  76. data/lib/paperclip/validators/attachment_file_type_ignorance_validator.rb +29 -0
  77. data/lib/paperclip/validators/attachment_presence_validator.rb +4 -0
  78. data/lib/paperclip/validators/attachment_size_validator.rb +5 -3
  79. data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +27 -0
  80. data/lib/paperclip/validators.rb +11 -4
  81. data/lib/paperclip/version.rb +3 -1
  82. data/lib/paperclip.rb +30 -11
  83. data/lib/tasks/paperclip.rake +34 -5
  84. data/paperclip.gemspec +21 -16
  85. data/shoulda_macros/paperclip.rb +0 -1
  86. data/spec/paperclip/attachment_definitions_spec.rb +13 -0
  87. data/{test/attachment_processing_test.rb → spec/paperclip/attachment_processing_spec.rb} +17 -20
  88. data/spec/paperclip/attachment_registry_spec.rb +158 -0
  89. data/{test/attachment_test.rb → spec/paperclip/attachment_spec.rb} +502 -407
  90. data/{test/content_type_detector_test.rb → spec/paperclip/content_type_detector_spec.rb} +17 -20
  91. data/spec/paperclip/file_command_content_type_detector_spec.rb +40 -0
  92. data/spec/paperclip/filename_cleaner_spec.rb +14 -0
  93. data/spec/paperclip/geometry_detector_spec.rb +39 -0
  94. data/{test/geometry_parser_test.rb → spec/paperclip/geometry_parser_spec.rb} +27 -27
  95. data/{test/geometry_test.rb → spec/paperclip/geometry_spec.rb} +50 -52
  96. data/spec/paperclip/glue_spec.rb +44 -0
  97. data/spec/paperclip/has_attached_file_spec.rb +158 -0
  98. data/{test/integration_test.rb → spec/paperclip/integration_spec.rb} +137 -128
  99. data/{test/interpolations_test.rb → spec/paperclip/interpolations_spec.rb} +70 -46
  100. data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +132 -0
  101. data/{test/io_adapters/attachment_adapter_test.rb → spec/paperclip/io_adapters/attachment_adapter_spec.rb} +33 -32
  102. data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +89 -0
  103. data/spec/paperclip/io_adapters/empty_string_adapter_spec.rb +17 -0
  104. data/{test/io_adapters/file_adapter_test.rb → spec/paperclip/io_adapters/file_adapter_spec.rb} +38 -42
  105. data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +121 -0
  106. data/spec/paperclip/io_adapters/identity_adapter_spec.rb +8 -0
  107. data/{test/io_adapters/nil_adapter_test.rb → spec/paperclip/io_adapters/nil_adapter_spec.rb} +7 -7
  108. data/{test/io_adapters/registry_test.rb → spec/paperclip/io_adapters/registry_spec.rb} +12 -9
  109. data/{test/io_adapters/stringio_adapter_test.rb → spec/paperclip/io_adapters/stringio_adapter_spec.rb} +21 -18
  110. data/{test/io_adapters/uploaded_file_adapter_test.rb → spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb} +46 -46
  111. data/spec/paperclip/io_adapters/uri_adapter_spec.rb +172 -0
  112. data/spec/paperclip/matchers/have_attached_file_matcher_spec.rb +19 -0
  113. data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +109 -0
  114. data/spec/paperclip/matchers/validate_attachment_presence_matcher_spec.rb +69 -0
  115. data/spec/paperclip/matchers/validate_attachment_size_matcher_spec.rb +88 -0
  116. data/spec/paperclip/media_type_spoof_detector_spec.rb +94 -0
  117. data/spec/paperclip/meta_class_spec.rb +30 -0
  118. data/spec/paperclip/paperclip_missing_attachment_styles_spec.rb +84 -0
  119. data/spec/paperclip/paperclip_spec.rb +192 -0
  120. data/spec/paperclip/plural_cache_spec.rb +37 -0
  121. data/spec/paperclip/processor_helpers_spec.rb +57 -0
  122. data/{test/processor_test.rb → spec/paperclip/processor_spec.rb} +7 -7
  123. data/spec/paperclip/rails_environment_spec.rb +33 -0
  124. data/{test/rake_test.rb → spec/paperclip/rake_spec.rb} +15 -15
  125. data/spec/paperclip/schema_spec.rb +248 -0
  126. data/{test/storage/filesystem_test.rb → spec/paperclip/storage/filesystem_spec.rb} +18 -18
  127. data/spec/paperclip/storage/fog_spec.rb +566 -0
  128. data/spec/paperclip/storage/s3_live_spec.rb +188 -0
  129. data/spec/paperclip/storage/s3_spec.rb +1693 -0
  130. data/spec/paperclip/style_spec.rb +255 -0
  131. data/spec/paperclip/tempfile_factory_spec.rb +33 -0
  132. data/spec/paperclip/tempfile_spec.rb +35 -0
  133. data/{test/thumbnail_test.rb → spec/paperclip/thumbnail_spec.rb} +153 -134
  134. data/spec/paperclip/url_generator_spec.rb +222 -0
  135. data/spec/paperclip/validators/attachment_content_type_validator_spec.rb +322 -0
  136. data/spec/paperclip/validators/attachment_file_name_validator_spec.rb +160 -0
  137. data/{test/validators/attachment_presence_validator_test.rb → spec/paperclip/validators/attachment_presence_validator_spec.rb} +20 -20
  138. data/{test/validators/attachment_size_validator_test.rb → spec/paperclip/validators/attachment_size_validator_spec.rb} +77 -64
  139. data/spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb +52 -0
  140. data/spec/paperclip/validators_spec.rb +164 -0
  141. data/spec/spec_helper.rb +47 -0
  142. data/spec/support/assertions.rb +82 -0
  143. data/spec/support/conditional_filter_helper.rb +5 -0
  144. data/spec/support/fake_model.rb +25 -0
  145. data/spec/support/fake_rails.rb +12 -0
  146. data/spec/support/fixtures/empty.html +1 -0
  147. data/spec/support/fixtures/empty.xlsx +0 -0
  148. data/spec/support/fixtures/spaced file.jpg +0 -0
  149. data/spec/support/matchers/accept.rb +5 -0
  150. data/spec/support/matchers/exist.rb +5 -0
  151. data/spec/support/matchers/have_column.rb +23 -0
  152. data/{test → spec}/support/mock_attachment.rb +2 -0
  153. data/{test → spec}/support/mock_url_generator_builder.rb +2 -2
  154. data/spec/support/model_reconstruction.rb +68 -0
  155. data/spec/support/reporting.rb +11 -0
  156. data/spec/support/test_data.rb +13 -0
  157. data/spec/support/version_helper.rb +9 -0
  158. metadata +348 -225
  159. data/RUNNING_TESTS.md +0 -4
  160. data/cucumber/paperclip_steps.rb +0 -6
  161. data/gemfiles/3.0.gemfile +0 -11
  162. data/gemfiles/3.1.gemfile +0 -11
  163. data/gemfiles/3.2.gemfile +0 -11
  164. data/gemfiles/4.0.gemfile +0 -11
  165. data/test/attachment_definitions_test.rb +0 -12
  166. data/test/attachment_registry_test.rb +0 -88
  167. data/test/file_command_content_type_detector_test.rb +0 -27
  168. data/test/filename_cleaner_test.rb +0 -14
  169. data/test/generator_test.rb +0 -84
  170. data/test/geometry_detector_test.rb +0 -24
  171. data/test/has_attached_file_test.rb +0 -125
  172. data/test/helper.rb +0 -232
  173. data/test/io_adapters/abstract_adapter_test.rb +0 -58
  174. data/test/io_adapters/data_uri_adapter_test.rb +0 -74
  175. data/test/io_adapters/empty_string_adapter_test.rb +0 -18
  176. data/test/io_adapters/http_url_proxy_adapter_test.rb +0 -102
  177. data/test/io_adapters/identity_adapter_test.rb +0 -8
  178. data/test/io_adapters/uri_adapter_test.rb +0 -102
  179. data/test/matchers/have_attached_file_matcher_test.rb +0 -24
  180. data/test/matchers/validate_attachment_content_type_matcher_test.rb +0 -110
  181. data/test/matchers/validate_attachment_presence_matcher_test.rb +0 -69
  182. data/test/matchers/validate_attachment_size_matcher_test.rb +0 -86
  183. data/test/meta_class_test.rb +0 -32
  184. data/test/paperclip_missing_attachment_styles_test.rb +0 -90
  185. data/test/paperclip_test.rb +0 -217
  186. data/test/plural_cache_test.rb +0 -36
  187. data/test/schema_test.rb +0 -200
  188. data/test/storage/fog_test.rb +0 -473
  189. data/test/storage/s3_live_test.rb +0 -179
  190. data/test/storage/s3_test.rb +0 -1356
  191. data/test/style_test.rb +0 -213
  192. data/test/support/mock_model.rb +0 -2
  193. data/test/tempfile_factory_test.rb +0 -17
  194. data/test/url_generator_test.rb +0 -187
  195. data/test/validators/attachment_content_type_validator_test.rb +0 -324
  196. data/test/validators_test.rb +0 -61
  197. /data/{test → spec}/database.yml +0 -0
  198. /data/{test → spec/support}/fixtures/12k.png +0 -0
  199. /data/{test → spec/support}/fixtures/50x50.png +0 -0
  200. /data/{test → spec/support}/fixtures/5k.png +0 -0
  201. /data/{test → spec/support}/fixtures/animated +0 -0
  202. /data/{test → spec/support}/fixtures/animated.gif +0 -0
  203. /data/{test → spec/support}/fixtures/animated.unknown +0 -0
  204. /data/{test → spec/support}/fixtures/bad.png +0 -0
  205. /data/{test → spec/support}/fixtures/fog.yml +0 -0
  206. /data/{test → spec/support}/fixtures/rotated.jpg +0 -0
  207. /data/{test → spec/support}/fixtures/s3.yml +0 -0
  208. /data/{test → spec/support}/fixtures/spaced file.png +0 -0
  209. /data/{test → spec/support}/fixtures/text.txt +0 -0
  210. /data/{test → spec/support}/fixtures/twopage.pdf +0 -0
  211. /data/{test → spec/support}/fixtures/uppercase.PNG +0 -0
  212. /data/{test → spec}/support/mock_interpolator.rb +0 -0
@@ -1,473 +0,0 @@
1
- require './test/helper'
2
- require 'fog'
3
-
4
- class FogTest < Test::Unit::TestCase
5
- context "" do
6
- setup { Fog.mock! }
7
-
8
- context "with credentials provided in a path string" do
9
- setup do
10
- rebuild_model :styles => { :medium => "300x300>", :thumb => "100x100>" },
11
- :storage => :fog,
12
- :url => '/:attachment/:filename',
13
- :fog_directory => "paperclip",
14
- :fog_credentials => fixture_file('fog.yml')
15
- @file = File.new(fixture_file('5k.png'), 'rb')
16
- @dummy = Dummy.new
17
- @dummy.avatar = @file
18
- end
19
-
20
- teardown { @file.close }
21
-
22
- should "have the proper information loading credentials from a file" do
23
- assert_equal @dummy.avatar.fog_credentials[:provider], 'AWS'
24
- end
25
- end
26
-
27
- context "with credentials provided in a File object" do
28
- setup do
29
- rebuild_model :styles => { :medium => "300x300>", :thumb => "100x100>" },
30
- :storage => :fog,
31
- :url => '/:attachment/:filename',
32
- :fog_directory => "paperclip",
33
- :fog_credentials => File.open(fixture_file('fog.yml'))
34
- @file = File.new(fixture_file('5k.png'), 'rb')
35
- @dummy = Dummy.new
36
- @dummy.avatar = @file
37
- end
38
-
39
- teardown { @file.close }
40
-
41
- should "have the proper information loading credentials from a file" do
42
- assert_equal @dummy.avatar.fog_credentials[:provider], 'AWS'
43
- end
44
- end
45
-
46
- context "with default values for path and url" do
47
- setup do
48
- rebuild_model :styles => { :medium => "300x300>", :thumb => "100x100>" },
49
- :storage => :fog,
50
- :url => '/:attachment/:filename',
51
- :fog_directory => "paperclip",
52
- :fog_credentials => {
53
- :provider => 'AWS',
54
- :aws_access_key_id => 'AWS_ID',
55
- :aws_secret_access_key => 'AWS_SECRET'
56
- }
57
- @file = File.new(fixture_file('5k.png'), 'rb')
58
- @dummy = Dummy.new
59
- @dummy.avatar = @file
60
- end
61
-
62
- teardown { @file.close }
63
-
64
- should "be able to interpolate the path without blowing up" do
65
- assert_equal File.expand_path(File.join(File.dirname(__FILE__), "../../tmp/public/avatars/5k.png")),
66
- @dummy.avatar.path
67
- end
68
- end
69
-
70
- context "with no path or url given and using defaults" do
71
- setup do
72
- rebuild_model :styles => { :medium => "300x300>", :thumb => "100x100>" },
73
- :storage => :fog,
74
- :fog_directory => "paperclip",
75
- :fog_credentials => {
76
- :provider => 'AWS',
77
- :aws_access_key_id => 'AWS_ID',
78
- :aws_secret_access_key => 'AWS_SECRET'
79
- }
80
- @file = File.new(fixture_file('5k.png'), 'rb')
81
- @dummy = Dummy.new
82
- @dummy.id = 1
83
- @dummy.avatar = @file
84
- end
85
-
86
- teardown { @file.close }
87
-
88
- should "have correct path and url from interpolated defaults" do
89
- assert_equal "dummies/avatars/000/000/001/original/5k.png", @dummy.avatar.path
90
- end
91
- end
92
-
93
- context "with file params provided as lambda" do
94
- setup do
95
- fog_file = lambda{ |a| { :custom_header => a.instance.custom_method }}
96
- klass = rebuild_model :storage => :fog,
97
- :fog_file => fog_file
98
-
99
- klass.class_eval do
100
- def custom_method
101
- 'foobar'
102
- end
103
- end
104
-
105
-
106
- @dummy = Dummy.new
107
- end
108
-
109
- should "be able to evaluate correct values for file headers" do
110
- assert_equal @dummy.avatar.send(:fog_file), { :custom_header => 'foobar' }
111
- end
112
- end
113
-
114
- setup do
115
- @fog_directory = 'papercliptests'
116
-
117
- @credentials = {
118
- :provider => 'AWS',
119
- :aws_access_key_id => 'ID',
120
- :aws_secret_access_key => 'SECRET'
121
- }
122
-
123
- @connection = Fog::Storage.new(@credentials)
124
- @connection.directories.create(
125
- :key => @fog_directory
126
- )
127
-
128
- @options = {
129
- :fog_directory => @fog_directory,
130
- :fog_credentials => @credentials,
131
- :fog_host => nil,
132
- :fog_file => {:cache_control => 1234},
133
- :path => ":attachment/:basename.:extension",
134
- :storage => :fog
135
- }
136
-
137
- rebuild_model(@options)
138
- end
139
-
140
- should "be extended by the Fog module" do
141
- assert Dummy.new.avatar.is_a?(Paperclip::Storage::Fog)
142
- end
143
-
144
- context "when assigned" do
145
- setup do
146
- @file = File.new(fixture_file('5k.png'), 'rb')
147
- @dummy = Dummy.new
148
- @dummy.avatar = @file
149
- end
150
-
151
- teardown do
152
- @file.close
153
- directory = @connection.directories.new(:key => @fog_directory)
154
- directory.files.each {|file| file.destroy}
155
- directory.destroy
156
- end
157
-
158
- should "be rewinded after flush_writes" do
159
- @dummy.avatar.instance_eval "def after_flush_writes; end"
160
-
161
- files = @dummy.avatar.queued_for_write.values
162
- @dummy.save
163
- assert files.none?(&:eof?), "Expect all the files to be rewinded."
164
- end
165
-
166
- should "be removed after after_flush_writes" do
167
- paths = @dummy.avatar.queued_for_write.values.map(&:path)
168
- @dummy.save
169
- assert paths.none?{ |path| File.exists?(path) },
170
- "Expect all the files to be deleted."
171
- end
172
-
173
- should 'be able to be copied to a local file' do
174
- @dummy.save
175
- tempfile = Tempfile.new("known_location")
176
- tempfile.binmode
177
- @dummy.avatar.copy_to_local_file(:original, tempfile.path)
178
- tempfile.rewind
179
- assert_equal @connection.directories.get(@fog_directory).files.get(@dummy.avatar.path).body,
180
- tempfile.read
181
- tempfile.close
182
- end
183
-
184
- should "pass the content type to the Fog::Storage::AWS::Files instance" do
185
- Fog::Storage::AWS::Files.any_instance.expects(:create).with do |hash|
186
- hash[:content_type]
187
- end
188
- @dummy.save
189
- end
190
-
191
- context "without a bucket" do
192
- setup do
193
- @connection.directories.get(@fog_directory).destroy
194
- end
195
-
196
- should "create the bucket" do
197
- assert @dummy.save
198
- assert @connection.directories.get(@fog_directory)
199
- end
200
- end
201
-
202
- context "with a bucket" do
203
- should "succeed" do
204
- assert @dummy.save
205
- end
206
- end
207
-
208
- context "without a fog_host" do
209
- setup do
210
- rebuild_model(@options.merge(:fog_host => nil))
211
- @dummy = Dummy.new
212
- @dummy.avatar = StringIO.new('.')
213
- @dummy.save
214
- end
215
-
216
- should "provide a public url" do
217
- assert !@dummy.avatar.url.nil?
218
- end
219
- end
220
-
221
- context "with a fog_host" do
222
- setup do
223
- rebuild_model(@options.merge(:fog_host => 'http://example.com'))
224
- @dummy = Dummy.new
225
- @dummy.avatar = StringIO.new('.')
226
- @dummy.save
227
- end
228
-
229
- should "provide a public url" do
230
- assert @dummy.avatar.url =~ /^http:\/\/example\.com\/avatars\/stringio\.txt\?\d*$/
231
- end
232
- end
233
-
234
- context "with a fog_host that includes a wildcard placeholder" do
235
- setup do
236
- rebuild_model(
237
- :fog_directory => @fog_directory,
238
- :fog_credentials => @credentials,
239
- :fog_host => 'http://img%d.example.com',
240
- :path => ":attachment/:basename.:extension",
241
- :storage => :fog
242
- )
243
- @dummy = Dummy.new
244
- @dummy.avatar = StringIO.new('.')
245
- @dummy.save
246
- end
247
-
248
- should "provide a public url" do
249
- assert @dummy.avatar.url =~ /^http:\/\/img[0123]\.example\.com\/avatars\/stringio\.txt\?\d*$/
250
- end
251
- end
252
-
253
- context "with fog_public set to false" do
254
- setup do
255
- rebuild_model(@options.merge(:fog_public => false))
256
- @dummy = Dummy.new
257
- @dummy.avatar = StringIO.new('.')
258
- @dummy.save
259
- end
260
-
261
- should 'set the @fog_public instance variable to false' do
262
- assert_equal false, @dummy.avatar.instance_variable_get('@options')[:fog_public]
263
- assert_equal false, @dummy.avatar.fog_public
264
- end
265
- end
266
-
267
- context "with styles set and fog_public set to false" do
268
- setup do
269
- rebuild_model(@options.merge(:fog_public => false, :styles => { :medium => "300x300>", :thumb => "100x100>" }))
270
- @file = File.new(fixture_file('5k.png'), 'rb')
271
- @dummy = Dummy.new
272
- @dummy.avatar = @file
273
- @dummy.save
274
- end
275
-
276
- should 'set the @fog_public for a particular style to false' do
277
- assert_equal false, @dummy.avatar.instance_variable_get('@options')[:fog_public]
278
- assert_equal false, @dummy.avatar.fog_public(:thumb)
279
- end
280
- end
281
-
282
- context "with styles set and fog_public set per-style" do
283
- setup do
284
- rebuild_model(@options.merge(:fog_public => { :medium => false, :thumb => true}, :styles => { :medium => "300x300>", :thumb => "100x100>" }))
285
- @file = File.new(fixture_file('5k.png'), 'rb')
286
- @dummy = Dummy.new
287
- @dummy.avatar = @file
288
- @dummy.save
289
- end
290
-
291
- should 'set the fog_public for a particular style to correct value' do
292
- assert_equal false, @dummy.avatar.fog_public(:medium)
293
- assert_equal true, @dummy.avatar.fog_public(:thumb)
294
- end
295
- end
296
-
297
- context "with fog_public not set" do
298
- setup do
299
- rebuild_model(@options)
300
- @dummy = Dummy.new
301
- @dummy.avatar = StringIO.new('.')
302
- @dummy.save
303
- end
304
-
305
- should "default fog_public to true" do
306
- assert_equal true, @dummy.avatar.fog_public
307
- end
308
- end
309
-
310
- context "with a valid bucket name for a subdomain" do
311
- should "provide an url in subdomain style" do
312
- assert_match(/^https:\/\/papercliptests.s3.amazonaws.com\/avatars\/5k.png/, @dummy.avatar.url)
313
- end
314
-
315
- should "provide an url that expires in subdomain style" do
316
- assert_match(/^http:\/\/papercliptests.s3.amazonaws.com\/avatars\/5k.png\?AWSAccessKeyId=.+$/, @dummy.avatar.expiring_url)
317
- end
318
- end
319
-
320
- context "generating an expiring url" do
321
- should "generate the same url when using Times and Integer offsets" do
322
- rebuild_model(@options)
323
- dummy = Dummy.new
324
- dummy.avatar = StringIO.new('.')
325
-
326
- assert_equal dummy.avatar.expiring_url(1234), dummy.avatar.expiring_url(Time.now + 1234)
327
- end
328
-
329
- should 'match the default url if there is no assignment' do
330
- dummy = Dummy.new
331
- assert_equal dummy.avatar.url, dummy.avatar.expiring_url
332
- end
333
-
334
- should 'match the default url when given a style if there is no assignment' do
335
- dummy = Dummy.new
336
- assert_equal dummy.avatar.url(:thumb), dummy.avatar.expiring_url(3600, :thumb)
337
- end
338
- end
339
-
340
- context "with an invalid bucket name for a subdomain" do
341
- setup do
342
- rebuild_model(@options.merge(:fog_directory => "this_is_invalid"))
343
- @dummy = Dummy.new
344
- @dummy.avatar = @file
345
- @dummy.save
346
- end
347
-
348
- should "not match the bucket-subdomain restrictions" do
349
- invalid_subdomains = %w(this_is_invalid in iamareallylongbucketnameiamareallylongbucketnameiamareallylongbu invalid- inval..id inval-.id inval.-id -invalid 192.168.10.2)
350
- invalid_subdomains.each do |name|
351
- assert_no_match Paperclip::Storage::Fog::AWS_BUCKET_SUBDOMAIN_RESTRICTON_REGEX, name
352
- end
353
- end
354
-
355
- should "provide an url in folder style" do
356
- assert_match(/^https:\/\/s3.amazonaws.com\/this_is_invalid\/avatars\/5k.png\?\d*$/, @dummy.avatar.url)
357
- end
358
-
359
- should "provide a url that expires in folder style" do
360
- assert_match(/^http:\/\/s3.amazonaws.com\/this_is_invalid\/avatars\/5k.png\?AWSAccessKeyId=.+$/, @dummy.avatar.expiring_url)
361
- end
362
-
363
- end
364
-
365
- context "with a proc for a bucket name evaluating a model method" do
366
- setup do
367
- @dynamic_fog_directory = 'dynamicpaperclip'
368
- rebuild_model(@options.merge(:fog_directory => lambda { |attachment| attachment.instance.bucket_name }))
369
- @dummy = Dummy.new
370
- @dummy.stubs(:bucket_name).returns(@dynamic_fog_directory)
371
- @dummy.avatar = @file
372
- @dummy.save
373
- end
374
-
375
- should "have created the bucket" do
376
- assert @connection.directories.get(@dynamic_fog_directory).inspect
377
- end
378
-
379
- end
380
-
381
- context "with a proc for the fog_host evaluating a model method" do
382
- setup do
383
- rebuild_model(@options.merge(:fog_host => lambda { |attachment| attachment.instance.fog_host }))
384
- @dummy = Dummy.new
385
- @dummy.stubs(:fog_host).returns('http://dynamicfoghost.com')
386
- @dummy.avatar = @file
387
- @dummy.save
388
- end
389
-
390
- should "provide a public url" do
391
- assert_match(/http:\/\/dynamicfoghost\.com/, @dummy.avatar.url)
392
- end
393
-
394
- end
395
-
396
- context "with a custom fog_host" do
397
- setup do
398
- rebuild_model(@options.merge(:fog_host => "http://dynamicfoghost.com"))
399
- @dummy = Dummy.new
400
- @dummy.avatar = @file
401
- @dummy.save
402
- end
403
-
404
- should "provide a public url" do
405
- assert_match(/http:\/\/dynamicfoghost\.com/, @dummy.avatar.url)
406
- end
407
-
408
- should "provide an expiring url" do
409
- assert_match(/http:\/\/dynamicfoghost\.com/, @dummy.avatar.expiring_url)
410
- end
411
-
412
- context "with an invalid bucket name for a subdomain" do
413
- setup do
414
- rebuild_model(@options.merge({:fog_directory => "this_is_invalid", :fog_host => "http://dynamicfoghost.com"}))
415
- @dummy = Dummy.new
416
- @dummy.avatar = @file
417
- @dummy.save
418
- end
419
-
420
- should "provide an expiring url" do
421
- assert_match(/http:\/\/dynamicfoghost\.com/, @dummy.avatar.expiring_url)
422
- end
423
- end
424
-
425
- end
426
-
427
- context "with a proc for the fog_credentials evaluating a model method" do
428
- setup do
429
- @dynamic_fog_credentials = {
430
- :provider => 'AWS',
431
- :aws_access_key_id => 'DYNAMIC_ID',
432
- :aws_secret_access_key => 'DYNAMIC_SECRET'
433
- }
434
- rebuild_model(@options.merge(:fog_credentials => lambda { |attachment| attachment.instance.fog_credentials }))
435
- @dummy = Dummy.new
436
- @dummy.stubs(:fog_credentials).returns(@dynamic_fog_credentials)
437
- @dummy.avatar = @file
438
- @dummy.save
439
- end
440
-
441
- should "provide a public url" do
442
- assert_equal @dummy.avatar.fog_credentials, @dynamic_fog_credentials
443
- end
444
- end
445
- end
446
-
447
- end
448
-
449
- context "when using local storage" do
450
- setup do
451
- Fog.unmock!
452
- rebuild_model :styles => { :medium => "300x300>", :thumb => "100x100>" },
453
- :storage => :fog,
454
- :url => '/:attachment/:filename',
455
- :fog_directory => "paperclip",
456
- :fog_credentials => { :provider => :local, :local_root => "." },
457
- :fog_host => 'localhost'
458
-
459
- @file = File.new(fixture_file('5k.png'), 'rb')
460
- @dummy = Dummy.new
461
- @dummy.avatar = @file
462
- end
463
-
464
- teardown do
465
- @file.close
466
- Fog.mock!
467
- end
468
-
469
- should "return the public url in place of the expiring url" do
470
- assert_match @dummy.avatar.public_url, @dummy.avatar.expiring_url
471
- end
472
- end
473
- end
@@ -1,179 +0,0 @@
1
- require './test/helper'
2
- require 'aws'
3
-
4
- unless ENV["S3_BUCKET"].blank?
5
- class S3LiveTest < Test::Unit::TestCase
6
- context "when assigning an S3 attachment directly to another model" do
7
- setup do
8
- @s3_credentials = File.new(fixture_file("s3.yml"))
9
- rebuild_model :styles => { :thumb => "100x100", :square => "32x32#" },
10
- :storage => :s3,
11
- :bucket => ENV["S3_BUCKET"],
12
- :path => ":class/:attachment/:id/:style.:extension",
13
- :s3_credentials => @s3_credentials
14
-
15
- @file = File.new(fixture_file("5k.png"))
16
- end
17
-
18
- should "not raise any error" do
19
- @attachment = Dummy.new.avatar
20
- @attachment.assign(@file)
21
- @attachment.save
22
-
23
- @attachment2 = Dummy.new.avatar
24
- @attachment2.assign(@file)
25
- @attachment2.save
26
- end
27
-
28
- should "allow assignment from another S3 object" do
29
- @attachment = Dummy.new.avatar
30
- @attachment.assign(@file)
31
- @attachment.save
32
-
33
- @attachment2 = Dummy.new.avatar
34
- @attachment2.assign(@attachment)
35
- @attachment2.save
36
- end
37
-
38
- teardown { [@s3_credentials, @file].each(&:close) }
39
- end
40
-
41
- context "Generating an expiring url on a nonexistant attachment" do
42
- setup do
43
- @s3_credentials = File.new(fixture_file("s3.yml"))
44
- rebuild_model :styles => { :thumb => "100x100", :square => "32x32#" },
45
- :storage => :s3,
46
- :bucket => ENV["S3_BUCKET"],
47
- :path => ":class/:attachment/:id/:style.:extension",
48
- :s3_credentials => @s3_credentials
49
-
50
- @dummy = Dummy.new
51
- end
52
-
53
- should "return nil" do
54
- assert_nil @dummy.avatar.expiring_url
55
- end
56
- end
57
-
58
- context "Using S3 for real, an attachment with S3 storage" do
59
- setup do
60
- @s3_credentials = File.new(fixture_file("s3.yml"))
61
- rebuild_model :styles => { :thumb => "100x100", :square => "32x32#" },
62
- :storage => :s3,
63
- :bucket => ENV["S3_BUCKET"],
64
- :path => ":class/:attachment/:id/:style.:extension",
65
- :s3_credentials => @s3_credentials
66
-
67
- Dummy.delete_all
68
- @dummy = Dummy.new
69
- end
70
-
71
- teardown { @s3_credentials.close }
72
-
73
- should "be extended by the S3 module" do
74
- assert Dummy.new.avatar.is_a?(Paperclip::Storage::S3)
75
- end
76
-
77
- context "when assigned" do
78
- setup do
79
- @file = File.new(fixture_file('5k.png'), 'rb')
80
- @dummy.avatar = @file
81
- end
82
-
83
- teardown do
84
- @file.close
85
- @dummy.destroy
86
- end
87
-
88
- context "and saved" do
89
- setup do
90
- @dummy.save
91
- end
92
-
93
- should "be on S3" do
94
- assert true
95
- end
96
- end
97
- end
98
- end
99
-
100
- context "An attachment that uses S3 for storage and has spaces in file name" do
101
- setup do
102
- @s3_credentials = File.new(fixture_file("s3.yml"))
103
- rebuild_model :styles => { :thumb => "100x100", :square => "32x32#" },
104
- :storage => :s3,
105
- :bucket => ENV["S3_BUCKET"],
106
- :s3_credentials => @s3_credentials
107
-
108
- Dummy.delete_all
109
- @file = File.new(fixture_file('spaced file.png'), 'rb')
110
- @dummy = Dummy.new
111
- @dummy.avatar = @file
112
- @dummy.save
113
- end
114
-
115
- teardown { @s3_credentials.close }
116
-
117
- should "return a replaced version for path" do
118
- assert_match /.+\/spaced_file\.png/, @dummy.avatar.path
119
- end
120
-
121
- should "return a replaced version for url" do
122
- assert_match /.+\/spaced_file\.png/, @dummy.avatar.url
123
- end
124
-
125
- should "be accessible" do
126
- assert_success_response @dummy.avatar.url
127
- end
128
-
129
- should "be reprocessable" do
130
- assert @dummy.avatar.reprocess!
131
- end
132
-
133
- should "be destroyable" do
134
- url = @dummy.avatar.url
135
- @dummy.destroy
136
- assert_not_found_response url
137
- end
138
- end
139
-
140
- context "An attachment that uses S3 for storage and uses AES256 encryption" do
141
- setup do
142
- @s3_credentials = File.new(fixture_file("s3.yml"))
143
- rebuild_model :styles => { :thumb => "100x100", :square => "32x32#" },
144
- :storage => :s3,
145
- :bucket => ENV["S3_BUCKET"],
146
- :path => ":class/:attachment/:id/:style.:extension",
147
- :s3_credentials => @s3_credentials,
148
- :s3_server_side_encryption => :aes256
149
-
150
- Dummy.delete_all
151
- @dummy = Dummy.new
152
- end
153
-
154
- teardown { @s3_credentials.close }
155
-
156
- context "when assigned" do
157
- setup do
158
- @file = File.new(fixture_file('5k.png'), 'rb')
159
- @dummy.avatar = @file
160
- end
161
-
162
- teardown do
163
- @file.close
164
- @dummy.destroy
165
- end
166
-
167
- context "and saved" do
168
- setup do
169
- @dummy.save
170
- end
171
-
172
- should "be encrypted on S3" do
173
- assert @dummy.avatar.s3_object.server_side_encryption == :aes256
174
- end
175
- end
176
- end
177
- end
178
- end
179
- end