paperclip 3.5.2 → 5.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (211) hide show
  1. checksums.yaml +7 -0
  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 +19 -13
  7. data/Appraisals +4 -16
  8. data/CONTRIBUTING.md +29 -13
  9. data/Gemfile +10 -7
  10. data/LICENSE +1 -3
  11. data/NEWS +226 -23
  12. data/README.md +494 -152
  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 +0 -2
  31. data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +1 -1
  32. data/lib/paperclip/attachment.rb +160 -47
  33. data/lib/paperclip/attachment_registry.rb +4 -1
  34. data/lib/paperclip/callbacks.rb +13 -1
  35. data/lib/paperclip/content_type_detector.rb +26 -24
  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 +10 -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 +14 -10
  43. data/lib/paperclip/interpolations/plural_cache.rb +6 -5
  44. data/lib/paperclip/interpolations.rb +27 -14
  45. data/lib/paperclip/io_adapters/abstract_adapter.rb +28 -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 +8 -8
  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 +89 -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 +47 -22
  69. data/lib/paperclip/storage/s3.rb +144 -73
  70. data/lib/paperclip/style.rb +8 -2
  71. data/lib/paperclip/tempfile_factory.rb +6 -4
  72. data/lib/paperclip/thumbnail.rb +26 -14
  73. data/lib/paperclip/url_generator.rb +25 -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 +12 -3
  81. data/lib/paperclip/version.rb +3 -1
  82. data/lib/paperclip.rb +31 -11
  83. data/lib/tasks/paperclip.rake +34 -5
  84. data/paperclip.gemspec +18 -17
  85. data/shoulda_macros/paperclip.rb +13 -3
  86. data/{test → spec}/database.yml +0 -0
  87. data/spec/paperclip/attachment_definitions_spec.rb +13 -0
  88. data/{test/attachment_processing_test.rb → spec/paperclip/attachment_processing_spec.rb} +17 -20
  89. data/spec/paperclip/attachment_registry_spec.rb +158 -0
  90. data/{test/attachment_test.rb → spec/paperclip/attachment_spec.rb} +524 -400
  91. data/{test/content_type_detector_test.rb → spec/paperclip/content_type_detector_spec.rb} +17 -19
  92. data/{test/file_command_content_type_detector_test.rb → spec/paperclip/file_command_content_type_detector_spec.rb} +7 -6
  93. data/spec/paperclip/filename_cleaner_spec.rb +14 -0
  94. data/spec/paperclip/geometry_detector_spec.rb +39 -0
  95. data/{test/geometry_parser_test.rb → spec/paperclip/geometry_parser_spec.rb} +27 -27
  96. data/{test/geometry_test.rb → spec/paperclip/geometry_spec.rb} +50 -52
  97. data/spec/paperclip/glue_spec.rb +44 -0
  98. data/spec/paperclip/has_attached_file_spec.rb +158 -0
  99. data/{test/integration_test.rb → spec/paperclip/integration_spec.rb} +141 -133
  100. data/{test/interpolations_test.rb → spec/paperclip/interpolations_spec.rb} +70 -46
  101. data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +101 -0
  102. data/{test/io_adapters/attachment_adapter_test.rb → spec/paperclip/io_adapters/attachment_adapter_spec.rb} +38 -34
  103. data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +89 -0
  104. data/spec/paperclip/io_adapters/empty_string_adapter_spec.rb +17 -0
  105. data/spec/paperclip/io_adapters/file_adapter_spec.rb +131 -0
  106. data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +121 -0
  107. data/spec/paperclip/io_adapters/identity_adapter_spec.rb +8 -0
  108. data/{test/io_adapters/nil_adapter_test.rb → spec/paperclip/io_adapters/nil_adapter_spec.rb} +7 -7
  109. data/{test/io_adapters/registry_test.rb → spec/paperclip/io_adapters/registry_spec.rb} +12 -9
  110. data/{test/io_adapters/stringio_adapter_test.rb → spec/paperclip/io_adapters/stringio_adapter_spec.rb} +21 -18
  111. data/{test/io_adapters/uploaded_file_adapter_test.rb → spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb} +46 -46
  112. data/spec/paperclip/io_adapters/uri_adapter_spec.rb +172 -0
  113. data/spec/paperclip/matchers/have_attached_file_matcher_spec.rb +19 -0
  114. data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +109 -0
  115. data/spec/paperclip/matchers/validate_attachment_presence_matcher_spec.rb +69 -0
  116. data/spec/paperclip/matchers/validate_attachment_size_matcher_spec.rb +88 -0
  117. data/spec/paperclip/media_type_spoof_detector_spec.rb +79 -0
  118. data/spec/paperclip/meta_class_spec.rb +30 -0
  119. data/spec/paperclip/paperclip_missing_attachment_styles_spec.rb +84 -0
  120. data/{test/paperclip_test.rb → spec/paperclip/paperclip_spec.rb} +46 -71
  121. data/spec/paperclip/plural_cache_spec.rb +37 -0
  122. data/spec/paperclip/processor_helpers_spec.rb +57 -0
  123. data/{test/processor_test.rb → spec/paperclip/processor_spec.rb} +5 -5
  124. data/spec/paperclip/rails_environment_spec.rb +33 -0
  125. data/{test/rake_test.rb → spec/paperclip/rake_spec.rb} +15 -15
  126. data/spec/paperclip/schema_spec.rb +248 -0
  127. data/{test/storage/filesystem_test.rb → spec/paperclip/storage/filesystem_spec.rb} +18 -18
  128. data/spec/paperclip/storage/fog_spec.rb +561 -0
  129. data/spec/paperclip/storage/s3_live_spec.rb +188 -0
  130. data/spec/paperclip/storage/s3_spec.rb +1693 -0
  131. data/spec/paperclip/style_spec.rb +255 -0
  132. data/spec/paperclip/tempfile_factory_spec.rb +33 -0
  133. data/spec/paperclip/tempfile_spec.rb +35 -0
  134. data/{test/thumbnail_test.rb → spec/paperclip/thumbnail_spec.rb} +150 -131
  135. data/spec/paperclip/url_generator_spec.rb +222 -0
  136. data/spec/paperclip/validators/attachment_content_type_validator_spec.rb +322 -0
  137. data/spec/paperclip/validators/attachment_file_name_validator_spec.rb +160 -0
  138. data/{test/validators/attachment_presence_validator_test.rb → spec/paperclip/validators/attachment_presence_validator_spec.rb} +20 -20
  139. data/{test/validators/attachment_size_validator_test.rb → spec/paperclip/validators/attachment_size_validator_spec.rb} +77 -64
  140. data/spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb +52 -0
  141. data/spec/paperclip/validators_spec.rb +164 -0
  142. data/spec/spec_helper.rb +47 -0
  143. data/spec/support/assertions.rb +82 -0
  144. data/spec/support/conditional_filter_helper.rb +5 -0
  145. data/spec/support/fake_model.rb +25 -0
  146. data/spec/support/fake_rails.rb +12 -0
  147. data/{test → spec/support}/fixtures/12k.png +0 -0
  148. data/{test → spec/support}/fixtures/50x50.png +0 -0
  149. data/{test → spec/support}/fixtures/5k.png +0 -0
  150. data/{test → spec/support}/fixtures/animated +0 -0
  151. data/{test → spec/support}/fixtures/animated.gif +0 -0
  152. data/{test → spec/support}/fixtures/animated.unknown +0 -0
  153. data/{test → spec/support}/fixtures/bad.png +0 -0
  154. data/spec/support/fixtures/empty.html +1 -0
  155. data/spec/support/fixtures/empty.xlsx +0 -0
  156. data/{test → spec/support}/fixtures/fog.yml +0 -0
  157. data/{test → spec/support}/fixtures/rotated.jpg +0 -0
  158. data/{test → spec/support}/fixtures/s3.yml +0 -0
  159. data/spec/support/fixtures/spaced file.jpg +0 -0
  160. data/{test → spec/support}/fixtures/spaced file.png +0 -0
  161. data/{test → spec/support}/fixtures/text.txt +0 -0
  162. data/{test → spec/support}/fixtures/twopage.pdf +0 -0
  163. data/{test → spec/support}/fixtures/uppercase.PNG +0 -0
  164. data/spec/support/matchers/accept.rb +5 -0
  165. data/spec/support/matchers/exist.rb +5 -0
  166. data/spec/support/matchers/have_column.rb +23 -0
  167. data/{test → spec}/support/mock_attachment.rb +2 -0
  168. data/{test → spec}/support/mock_interpolator.rb +0 -0
  169. data/{test → spec}/support/mock_url_generator_builder.rb +2 -2
  170. data/spec/support/model_reconstruction.rb +68 -0
  171. data/spec/support/reporting.rb +11 -0
  172. data/spec/support/test_data.rb +13 -0
  173. data/spec/support/version_helper.rb +9 -0
  174. metadata +262 -297
  175. data/RUNNING_TESTS.md +0 -4
  176. data/cucumber/paperclip_steps.rb +0 -6
  177. data/gemfiles/3.0.gemfile +0 -11
  178. data/gemfiles/3.1.gemfile +0 -11
  179. data/gemfiles/3.2.gemfile +0 -11
  180. data/gemfiles/4.0.gemfile +0 -11
  181. data/test/attachment_definitions_test.rb +0 -12
  182. data/test/attachment_registry_test.rb +0 -77
  183. data/test/filename_cleaner_test.rb +0 -14
  184. data/test/generator_test.rb +0 -80
  185. data/test/geometry_detector_test.rb +0 -24
  186. data/test/has_attached_file_test.rb +0 -125
  187. data/test/helper.rb +0 -215
  188. data/test/io_adapters/abstract_adapter_test.rb +0 -58
  189. data/test/io_adapters/data_uri_adapter_test.rb +0 -67
  190. data/test/io_adapters/empty_string_adapter_test.rb +0 -17
  191. data/test/io_adapters/file_adapter_test.rb +0 -119
  192. data/test/io_adapters/http_url_proxy_adapter_test.rb +0 -102
  193. data/test/io_adapters/identity_adapter_test.rb +0 -8
  194. data/test/io_adapters/uri_adapter_test.rb +0 -102
  195. data/test/matchers/have_attached_file_matcher_test.rb +0 -24
  196. data/test/matchers/validate_attachment_content_type_matcher_test.rb +0 -110
  197. data/test/matchers/validate_attachment_presence_matcher_test.rb +0 -69
  198. data/test/matchers/validate_attachment_size_matcher_test.rb +0 -86
  199. data/test/meta_class_test.rb +0 -32
  200. data/test/paperclip_missing_attachment_styles_test.rb +0 -90
  201. data/test/plural_cache_test.rb +0 -36
  202. data/test/schema_test.rb +0 -200
  203. data/test/storage/fog_test.rb +0 -453
  204. data/test/storage/s3_live_test.rb +0 -179
  205. data/test/storage/s3_test.rb +0 -1348
  206. data/test/style_test.rb +0 -213
  207. data/test/support/mock_model.rb +0 -2
  208. data/test/tempfile_factory_test.rb +0 -13
  209. data/test/url_generator_test.rb +0 -187
  210. data/test/validators/attachment_content_type_validator_test.rb +0 -323
  211. data/test/validators_test.rb +0 -32
@@ -1,453 +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 "with an invalid bucket name for a subdomain" do
321
- setup do
322
- rebuild_model(@options.merge(:fog_directory => "this_is_invalid"))
323
- @dummy = Dummy.new
324
- @dummy.avatar = @file
325
- @dummy.save
326
- end
327
-
328
- should "not match the bucket-subdomain restrictions" do
329
- invalid_subdomains = %w(this_is_invalid in iamareallylongbucketnameiamareallylongbucketnameiamareallylongbu invalid- inval..id inval-.id inval.-id -invalid 192.168.10.2)
330
- invalid_subdomains.each do |name|
331
- assert_no_match Paperclip::Storage::Fog::AWS_BUCKET_SUBDOMAIN_RESTRICTON_REGEX, name
332
- end
333
- end
334
-
335
- should "provide an url in folder style" do
336
- assert_match /^https:\/\/s3.amazonaws.com\/this_is_invalid\/avatars\/5k.png\?\d*$/, @dummy.avatar.url
337
- end
338
-
339
- should "provide a url that expires in folder style" do
340
- assert_match /^http:\/\/s3.amazonaws.com\/this_is_invalid\/avatars\/5k.png\?AWSAccessKeyId=.+$/, @dummy.avatar.expiring_url
341
- end
342
-
343
- end
344
-
345
- context "with a proc for a bucket name evaluating a model method" do
346
- setup do
347
- @dynamic_fog_directory = 'dynamicpaperclip'
348
- rebuild_model(@options.merge(:fog_directory => lambda { |attachment| attachment.instance.bucket_name }))
349
- @dummy = Dummy.new
350
- @dummy.stubs(:bucket_name).returns(@dynamic_fog_directory)
351
- @dummy.avatar = @file
352
- @dummy.save
353
- end
354
-
355
- should "have created the bucket" do
356
- assert @connection.directories.get(@dynamic_fog_directory).inspect
357
- end
358
-
359
- end
360
-
361
- context "with a proc for the fog_host evaluating a model method" do
362
- setup do
363
- rebuild_model(@options.merge(:fog_host => lambda { |attachment| attachment.instance.fog_host }))
364
- @dummy = Dummy.new
365
- @dummy.stubs(:fog_host).returns('http://dynamicfoghost.com')
366
- @dummy.avatar = @file
367
- @dummy.save
368
- end
369
-
370
- should "provide a public url" do
371
- assert_match /http:\/\/dynamicfoghost\.com/, @dummy.avatar.url
372
- end
373
-
374
- end
375
-
376
- context "with a custom fog_host" do
377
- setup do
378
- rebuild_model(@options.merge(:fog_host => "http://dynamicfoghost.com"))
379
- @dummy = Dummy.new
380
- @dummy.avatar = @file
381
- @dummy.save
382
- end
383
-
384
- should "provide a public url" do
385
- assert_match /http:\/\/dynamicfoghost\.com/, @dummy.avatar.url
386
- end
387
-
388
- should "provide an expiring url" do
389
- assert_match /http:\/\/dynamicfoghost\.com/, @dummy.avatar.expiring_url
390
- end
391
-
392
- context "with an invalid bucket name for a subdomain" do
393
- setup do
394
- rebuild_model(@options.merge({:fog_directory => "this_is_invalid", :fog_host => "http://dynamicfoghost.com"}))
395
- @dummy = Dummy.new
396
- @dummy.avatar = @file
397
- @dummy.save
398
- end
399
-
400
- should "provide an expiring url" do
401
- assert_match /http:\/\/dynamicfoghost\.com/, @dummy.avatar.expiring_url
402
- end
403
- end
404
-
405
- end
406
-
407
- context "with a proc for the fog_credentials evaluating a model method" do
408
- setup do
409
- @dynamic_fog_credentials = {
410
- :provider => 'AWS',
411
- :aws_access_key_id => 'DYNAMIC_ID',
412
- :aws_secret_access_key => 'DYNAMIC_SECRET'
413
- }
414
- rebuild_model(@options.merge(:fog_credentials => lambda { |attachment| attachment.instance.fog_credentials }))
415
- @dummy = Dummy.new
416
- @dummy.stubs(:fog_credentials).returns(@dynamic_fog_credentials)
417
- @dummy.avatar = @file
418
- @dummy.save
419
- end
420
-
421
- should "provide a public url" do
422
- assert_equal @dummy.avatar.fog_credentials, @dynamic_fog_credentials
423
- end
424
- end
425
- end
426
-
427
- end
428
-
429
- context "when using local storage" do
430
- setup do
431
- Fog.unmock!
432
- rebuild_model :styles => { :medium => "300x300>", :thumb => "100x100>" },
433
- :storage => :fog,
434
- :url => '/:attachment/:filename',
435
- :fog_directory => "paperclip",
436
- :fog_credentials => { :provider => :local, :local_root => "." },
437
- :fog_host => 'localhost'
438
-
439
- @file = File.new(fixture_file('5k.png'), 'rb')
440
- @dummy = Dummy.new
441
- @dummy.avatar = @file
442
- end
443
-
444
- teardown do
445
- @file.close
446
- Fog.mock!
447
- end
448
-
449
- should "return the public url in place of the expiring url" do
450
- assert_match @dummy.avatar.public_url, @dummy.avatar.expiring_url
451
- end
452
- end
453
- 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