kt-paperclip 4.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (198) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/.hound.yml +1066 -0
  4. data/.rubocop.yml +1 -0
  5. data/.travis.yml +23 -0
  6. data/Appraisals +11 -0
  7. data/CONTRIBUTING.md +75 -0
  8. data/Gemfile +21 -0
  9. data/LICENSE +24 -0
  10. data/NEWS +420 -0
  11. data/README.md +977 -0
  12. data/RELEASING.md +17 -0
  13. data/Rakefile +44 -0
  14. data/UPGRADING +14 -0
  15. data/cucumber/paperclip_steps.rb +6 -0
  16. data/features/basic_integration.feature +80 -0
  17. data/features/migration.feature +94 -0
  18. data/features/rake_tasks.feature +62 -0
  19. data/features/step_definitions/attachment_steps.rb +110 -0
  20. data/features/step_definitions/html_steps.rb +15 -0
  21. data/features/step_definitions/rails_steps.rb +236 -0
  22. data/features/step_definitions/s3_steps.rb +14 -0
  23. data/features/step_definitions/web_steps.rb +107 -0
  24. data/features/support/env.rb +11 -0
  25. data/features/support/fakeweb.rb +13 -0
  26. data/features/support/file_helpers.rb +34 -0
  27. data/features/support/fixtures/boot_config.txt +15 -0
  28. data/features/support/fixtures/gemfile.txt +5 -0
  29. data/features/support/fixtures/preinitializer.txt +20 -0
  30. data/features/support/paths.rb +28 -0
  31. data/features/support/rails.rb +63 -0
  32. data/features/support/selectors.rb +19 -0
  33. data/gemfiles/3.2.gemfile +19 -0
  34. data/gemfiles/4.1.gemfile +19 -0
  35. data/gemfiles/4.2.gemfile +19 -0
  36. data/lib/generators/paperclip/USAGE +8 -0
  37. data/lib/generators/paperclip/paperclip_generator.rb +30 -0
  38. data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +15 -0
  39. data/lib/kt-paperclip.rb +1 -0
  40. data/lib/paperclip/attachment.rb +608 -0
  41. data/lib/paperclip/attachment_registry.rb +59 -0
  42. data/lib/paperclip/callbacks.rb +40 -0
  43. data/lib/paperclip/content_type_detector.rb +79 -0
  44. data/lib/paperclip/deprecations.rb +42 -0
  45. data/lib/paperclip/errors.rb +32 -0
  46. data/lib/paperclip/file_command_content_type_detector.rb +30 -0
  47. data/lib/paperclip/filename_cleaner.rb +16 -0
  48. data/lib/paperclip/geometry.rb +158 -0
  49. data/lib/paperclip/geometry_detector_factory.rb +48 -0
  50. data/lib/paperclip/geometry_parser_factory.rb +31 -0
  51. data/lib/paperclip/glue.rb +17 -0
  52. data/lib/paperclip/has_attached_file.rb +109 -0
  53. data/lib/paperclip/helpers.rb +56 -0
  54. data/lib/paperclip/interpolations/plural_cache.rb +18 -0
  55. data/lib/paperclip/interpolations.rb +197 -0
  56. data/lib/paperclip/io_adapters/abstract_adapter.rb +47 -0
  57. data/lib/paperclip/io_adapters/attachment_adapter.rb +36 -0
  58. data/lib/paperclip/io_adapters/data_uri_adapter.rb +22 -0
  59. data/lib/paperclip/io_adapters/empty_string_adapter.rb +18 -0
  60. data/lib/paperclip/io_adapters/file_adapter.rb +22 -0
  61. data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +16 -0
  62. data/lib/paperclip/io_adapters/identity_adapter.rb +12 -0
  63. data/lib/paperclip/io_adapters/nil_adapter.rb +34 -0
  64. data/lib/paperclip/io_adapters/registry.rb +32 -0
  65. data/lib/paperclip/io_adapters/stringio_adapter.rb +33 -0
  66. data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +42 -0
  67. data/lib/paperclip/io_adapters/uri_adapter.rb +63 -0
  68. data/lib/paperclip/locales/de.yml +18 -0
  69. data/lib/paperclip/locales/en.yml +18 -0
  70. data/lib/paperclip/locales/es.yml +18 -0
  71. data/lib/paperclip/locales/ja.yml +18 -0
  72. data/lib/paperclip/locales/pt-BR.yml +18 -0
  73. data/lib/paperclip/locales/zh-CN.yml +18 -0
  74. data/lib/paperclip/locales/zh-HK.yml +18 -0
  75. data/lib/paperclip/locales/zh-TW.yml +18 -0
  76. data/lib/paperclip/logger.rb +21 -0
  77. data/lib/paperclip/matchers/have_attached_file_matcher.rb +54 -0
  78. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +100 -0
  79. data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +59 -0
  80. data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +96 -0
  81. data/lib/paperclip/matchers.rb +64 -0
  82. data/lib/paperclip/media_type_spoof_detector.rb +89 -0
  83. data/lib/paperclip/missing_attachment_styles.rb +79 -0
  84. data/lib/paperclip/processor.rb +48 -0
  85. data/lib/paperclip/processor_helpers.rb +50 -0
  86. data/lib/paperclip/rails_environment.rb +25 -0
  87. data/lib/paperclip/railtie.rb +31 -0
  88. data/lib/paperclip/schema.rb +83 -0
  89. data/lib/paperclip/storage/filesystem.rb +90 -0
  90. data/lib/paperclip/storage/fog.rb +242 -0
  91. data/lib/paperclip/storage/s3.rb +440 -0
  92. data/lib/paperclip/storage.rb +3 -0
  93. data/lib/paperclip/style.rb +109 -0
  94. data/lib/paperclip/tempfile.rb +43 -0
  95. data/lib/paperclip/tempfile_factory.rb +23 -0
  96. data/lib/paperclip/thumbnail.rb +121 -0
  97. data/lib/paperclip/url_generator.rb +79 -0
  98. data/lib/paperclip/validators/attachment_content_type_validator.rb +88 -0
  99. data/lib/paperclip/validators/attachment_file_name_validator.rb +80 -0
  100. data/lib/paperclip/validators/attachment_file_type_ignorance_validator.rb +29 -0
  101. data/lib/paperclip/validators/attachment_presence_validator.rb +30 -0
  102. data/lib/paperclip/validators/attachment_size_validator.rb +115 -0
  103. data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +27 -0
  104. data/lib/paperclip/validators.rb +74 -0
  105. data/lib/paperclip/version.rb +3 -0
  106. data/lib/paperclip.rb +213 -0
  107. data/lib/tasks/paperclip.rake +127 -0
  108. data/paperclip.gemspec +52 -0
  109. data/shoulda_macros/paperclip.rb +134 -0
  110. data/spec/database.yml +4 -0
  111. data/spec/paperclip/attachment_definitions_spec.rb +13 -0
  112. data/spec/paperclip/attachment_processing_spec.rb +82 -0
  113. data/spec/paperclip/attachment_registry_spec.rb +130 -0
  114. data/spec/paperclip/attachment_spec.rb +1494 -0
  115. data/spec/paperclip/content_type_detector_spec.rb +48 -0
  116. data/spec/paperclip/deprecations_spec.rb +65 -0
  117. data/spec/paperclip/file_command_content_type_detector_spec.rb +26 -0
  118. data/spec/paperclip/filename_cleaner_spec.rb +14 -0
  119. data/spec/paperclip/geometry_detector_spec.rb +39 -0
  120. data/spec/paperclip/geometry_parser_spec.rb +73 -0
  121. data/spec/paperclip/geometry_spec.rb +255 -0
  122. data/spec/paperclip/glue_spec.rb +44 -0
  123. data/spec/paperclip/has_attached_file_spec.rb +142 -0
  124. data/spec/paperclip/integration_spec.rb +667 -0
  125. data/spec/paperclip/interpolations_spec.rb +262 -0
  126. data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +78 -0
  127. data/spec/paperclip/io_adapters/attachment_adapter_spec.rb +139 -0
  128. data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +83 -0
  129. data/spec/paperclip/io_adapters/empty_string_adapter_spec.rb +17 -0
  130. data/spec/paperclip/io_adapters/file_adapter_spec.rb +131 -0
  131. data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +104 -0
  132. data/spec/paperclip/io_adapters/identity_adapter_spec.rb +8 -0
  133. data/spec/paperclip/io_adapters/nil_adapter_spec.rb +25 -0
  134. data/spec/paperclip/io_adapters/registry_spec.rb +35 -0
  135. data/spec/paperclip/io_adapters/stringio_adapter_spec.rb +64 -0
  136. data/spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb +146 -0
  137. data/spec/paperclip/io_adapters/uri_adapter_spec.rb +127 -0
  138. data/spec/paperclip/matchers/have_attached_file_matcher_spec.rb +19 -0
  139. data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +99 -0
  140. data/spec/paperclip/matchers/validate_attachment_presence_matcher_spec.rb +69 -0
  141. data/spec/paperclip/matchers/validate_attachment_size_matcher_spec.rb +88 -0
  142. data/spec/paperclip/media_type_spoof_detector_spec.rb +79 -0
  143. data/spec/paperclip/meta_class_spec.rb +30 -0
  144. data/spec/paperclip/paperclip_missing_attachment_styles_spec.rb +84 -0
  145. data/spec/paperclip/paperclip_spec.rb +222 -0
  146. data/spec/paperclip/plural_cache_spec.rb +37 -0
  147. data/spec/paperclip/processor_helpers_spec.rb +57 -0
  148. data/spec/paperclip/processor_spec.rb +26 -0
  149. data/spec/paperclip/rails_environment_spec.rb +33 -0
  150. data/spec/paperclip/rake_spec.rb +103 -0
  151. data/spec/paperclip/schema_spec.rb +248 -0
  152. data/spec/paperclip/storage/filesystem_spec.rb +79 -0
  153. data/spec/paperclip/storage/fog_spec.rb +540 -0
  154. data/spec/paperclip/storage/s3_live_spec.rb +182 -0
  155. data/spec/paperclip/storage/s3_spec.rb +1526 -0
  156. data/spec/paperclip/style_spec.rb +255 -0
  157. data/spec/paperclip/tempfile_factory_spec.rb +33 -0
  158. data/spec/paperclip/thumbnail_spec.rb +500 -0
  159. data/spec/paperclip/url_generator_spec.rb +221 -0
  160. data/spec/paperclip/validators/attachment_content_type_validator_spec.rb +322 -0
  161. data/spec/paperclip/validators/attachment_file_name_validator_spec.rb +160 -0
  162. data/spec/paperclip/validators/attachment_presence_validator_spec.rb +85 -0
  163. data/spec/paperclip/validators/attachment_size_validator_spec.rb +229 -0
  164. data/spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb +52 -0
  165. data/spec/paperclip/validators_spec.rb +164 -0
  166. data/spec/spec_helper.rb +43 -0
  167. data/spec/support/assertions.rb +71 -0
  168. data/spec/support/deprecations.rb +9 -0
  169. data/spec/support/fake_model.rb +25 -0
  170. data/spec/support/fake_rails.rb +12 -0
  171. data/spec/support/fixtures/12k.png +0 -0
  172. data/spec/support/fixtures/50x50.png +0 -0
  173. data/spec/support/fixtures/5k.png +0 -0
  174. data/spec/support/fixtures/animated +0 -0
  175. data/spec/support/fixtures/animated.gif +0 -0
  176. data/spec/support/fixtures/animated.unknown +0 -0
  177. data/spec/support/fixtures/bad.png +1 -0
  178. data/spec/support/fixtures/empty.html +1 -0
  179. data/spec/support/fixtures/empty.xlsx +0 -0
  180. data/spec/support/fixtures/fog.yml +8 -0
  181. data/spec/support/fixtures/rotated.jpg +0 -0
  182. data/spec/support/fixtures/s3.yml +8 -0
  183. data/spec/support/fixtures/spaced file.jpg +0 -0
  184. data/spec/support/fixtures/spaced file.png +0 -0
  185. data/spec/support/fixtures/text.txt +1 -0
  186. data/spec/support/fixtures/twopage.pdf +0 -0
  187. data/spec/support/fixtures/uppercase.PNG +0 -0
  188. data/spec/support/matchers/accept.rb +5 -0
  189. data/spec/support/matchers/exist.rb +5 -0
  190. data/spec/support/matchers/have_column.rb +23 -0
  191. data/spec/support/mock_attachment.rb +22 -0
  192. data/spec/support/mock_interpolator.rb +24 -0
  193. data/spec/support/mock_url_generator_builder.rb +27 -0
  194. data/spec/support/model_reconstruction.rb +60 -0
  195. data/spec/support/rails_helpers.rb +7 -0
  196. data/spec/support/test_data.rb +13 -0
  197. data/spec/support/version_helper.rb +9 -0
  198. metadata +648 -0
@@ -0,0 +1,540 @@
1
+ require 'spec_helper'
2
+ require 'fog/aws'
3
+ require 'fog/local'
4
+ require 'timecop'
5
+
6
+ describe Paperclip::Storage::Fog do
7
+ context "" do
8
+ before { Fog.mock! }
9
+
10
+ context "with credentials provided in a path string" do
11
+ before do
12
+ rebuild_model styles: { medium: "300x300>", thumb: "100x100>" },
13
+ storage: :fog,
14
+ url: '/:attachment/:filename',
15
+ fog_directory: "paperclip",
16
+ fog_credentials: fixture_file('fog.yml')
17
+ @file = File.new(fixture_file('5k.png'), 'rb')
18
+ @dummy = Dummy.new
19
+ @dummy.avatar = @file
20
+ end
21
+
22
+ after { @file.close }
23
+
24
+ it "has the proper information loading credentials from a file" do
25
+ assert_equal @dummy.avatar.fog_credentials[:provider], 'AWS'
26
+ end
27
+ end
28
+
29
+ context "with credentials provided in a File object" do
30
+ before do
31
+ rebuild_model styles: { medium: "300x300>", thumb: "100x100>" },
32
+ storage: :fog,
33
+ url: '/:attachment/:filename',
34
+ fog_directory: "paperclip",
35
+ fog_credentials: File.open(fixture_file('fog.yml'))
36
+ @file = File.new(fixture_file('5k.png'), 'rb')
37
+ @dummy = Dummy.new
38
+ @dummy.avatar = @file
39
+ end
40
+
41
+ after { @file.close }
42
+
43
+ it "has the proper information loading credentials from a file" do
44
+ assert_equal @dummy.avatar.fog_credentials[:provider], 'AWS'
45
+ end
46
+ end
47
+
48
+ context "with default values for path and url" do
49
+ before do
50
+ rebuild_model styles: { medium: "300x300>", thumb: "100x100>" },
51
+ storage: :fog,
52
+ url: '/:attachment/:filename',
53
+ fog_directory: "paperclip",
54
+ fog_credentials: {
55
+ provider: 'AWS',
56
+ aws_access_key_id: 'AWS_ID',
57
+ aws_secret_access_key: 'AWS_SECRET'
58
+ }
59
+ @file = File.new(fixture_file('5k.png'), 'rb')
60
+ @dummy = Dummy.new
61
+ @dummy.avatar = @file
62
+ end
63
+
64
+ after { @file.close }
65
+
66
+ it "is able to interpolate the path without blowing up" do
67
+ assert_equal File.expand_path(File.join(File.dirname(__FILE__), "../../../tmp/public/avatars/5k.png")),
68
+ @dummy.avatar.path
69
+ end
70
+ end
71
+
72
+ context "with no path or url given and using defaults" do
73
+ before do
74
+ rebuild_model styles: { medium: "300x300>", thumb: "100x100>" },
75
+ storage: :fog,
76
+ fog_directory: "paperclip",
77
+ fog_credentials: {
78
+ provider: 'AWS',
79
+ aws_access_key_id: 'AWS_ID',
80
+ aws_secret_access_key: 'AWS_SECRET'
81
+ }
82
+ @file = File.new(fixture_file('5k.png'), 'rb')
83
+ @dummy = Dummy.new
84
+ @dummy.id = 1
85
+ @dummy.avatar = @file
86
+ end
87
+
88
+ after { @file.close }
89
+
90
+ it "has correct path and url from interpolated defaults" do
91
+ assert_equal "dummies/avatars/000/000/001/original/5k.png", @dummy.avatar.path
92
+ end
93
+ end
94
+
95
+ context "with file params provided as lambda" do
96
+ before do
97
+ fog_file = lambda{ |a| { custom_header: a.instance.custom_method }}
98
+ klass = rebuild_model storage: :fog,
99
+ fog_file: fog_file
100
+
101
+ klass.class_eval do
102
+ def custom_method
103
+ 'foobar'
104
+ end
105
+ end
106
+
107
+
108
+ @dummy = Dummy.new
109
+ end
110
+
111
+ it "is able to evaluate correct values for file headers" do
112
+ assert_equal @dummy.avatar.send(:fog_file), { custom_header: 'foobar' }
113
+ end
114
+ end
115
+
116
+ before do
117
+ @fog_directory = 'papercliptests'
118
+
119
+ @credentials = {
120
+ provider: 'AWS',
121
+ aws_access_key_id: 'ID',
122
+ aws_secret_access_key: 'SECRET'
123
+ }
124
+
125
+ @connection = Fog::Storage.new(@credentials)
126
+ @connection.directories.create(
127
+ key: @fog_directory
128
+ )
129
+
130
+ @options = {
131
+ fog_directory: @fog_directory,
132
+ fog_credentials: @credentials,
133
+ fog_host: nil,
134
+ fog_file: {cache_control: 1234},
135
+ path: ":attachment/:basename:dotextension",
136
+ storage: :fog
137
+ }
138
+
139
+ rebuild_model(@options)
140
+ end
141
+
142
+ it "is extended by the Fog module" do
143
+ assert Dummy.new.avatar.is_a?(Paperclip::Storage::Fog)
144
+ end
145
+
146
+ context "when assigned" do
147
+ before do
148
+ @file = File.new(fixture_file('5k.png'), 'rb')
149
+ @dummy = Dummy.new
150
+ @dummy.avatar = @file
151
+ end
152
+
153
+ after do
154
+ @file.close
155
+ directory = @connection.directories.new(key: @fog_directory)
156
+ directory.files.each {|file| file.destroy}
157
+ directory.destroy
158
+ end
159
+
160
+ it "is rewound after flush_writes" do
161
+ @dummy.avatar.instance_eval "def after_flush_writes; end"
162
+
163
+ files = @dummy.avatar.queued_for_write.values
164
+ @dummy.save
165
+ assert files.none?(&:eof?), "Expect all the files to be rewinded."
166
+ end
167
+
168
+ it "is removed after after_flush_writes" do
169
+ paths = @dummy.avatar.queued_for_write.values.map(&:path)
170
+ @dummy.save
171
+ assert paths.none?{ |path| File.exist?(path) },
172
+ "Expect all the files to be deleted."
173
+ end
174
+
175
+ it 'is able to be copied to a local file' do
176
+ @dummy.save
177
+ tempfile = Tempfile.new("known_location")
178
+ tempfile.binmode
179
+ @dummy.avatar.copy_to_local_file(:original, tempfile.path)
180
+ tempfile.rewind
181
+ assert_equal @connection.directories.get(@fog_directory).files.get(@dummy.avatar.path).body,
182
+ tempfile.read
183
+ tempfile.close
184
+ end
185
+
186
+ it "passes the content type to the Fog::Storage::AWS::Files instance" do
187
+ Fog::Storage::AWS::Files.any_instance.expects(:create).with do |hash|
188
+ hash[:content_type]
189
+ end
190
+ @dummy.save
191
+ end
192
+
193
+ context "without a bucket" do
194
+ before do
195
+ @connection.directories.get(@fog_directory).destroy
196
+ end
197
+
198
+ it "creates the bucket" do
199
+ assert @dummy.save
200
+ assert @connection.directories.get(@fog_directory)
201
+ end
202
+
203
+ it "sucessfully rewinds the file during bucket creation" do
204
+ assert @dummy.save
205
+ expect(Paperclip.io_adapters.for(@dummy.avatar).read.length).to be > 0
206
+ end
207
+ end
208
+
209
+ context "with a bucket" do
210
+ it "succeeds" do
211
+ assert @dummy.save
212
+ end
213
+ end
214
+
215
+ context "without a fog_host" do
216
+ before do
217
+ rebuild_model(@options.merge(fog_host: nil))
218
+ @dummy = Dummy.new
219
+ @dummy.avatar = StringIO.new('.')
220
+ @dummy.save
221
+ end
222
+
223
+ it "provides a public url" do
224
+ assert !@dummy.avatar.url.nil?
225
+ end
226
+ end
227
+
228
+ context "with a fog_host" do
229
+ before do
230
+ rebuild_model(@options.merge(fog_host: 'http://example.com'))
231
+ @dummy = Dummy.new
232
+ @dummy.avatar = StringIO.new(".\n")
233
+ @dummy.save
234
+ end
235
+
236
+ it "provides a public url" do
237
+ expect(@dummy.avatar.url).to match(/^http:\/\/example\.com\/avatars\/data\?\d*$/)
238
+ end
239
+ end
240
+
241
+ context "with a fog_host that includes a wildcard placeholder" do
242
+ before do
243
+ rebuild_model(
244
+ fog_directory: @fog_directory,
245
+ fog_credentials: @credentials,
246
+ fog_host: 'http://img%d.example.com',
247
+ path: ":attachment/:basename:dotextension",
248
+ storage: :fog
249
+ )
250
+ @dummy = Dummy.new
251
+ @dummy.avatar = StringIO.new(".\n")
252
+ @dummy.save
253
+ end
254
+
255
+ it "provides a public url" do
256
+ expect(@dummy.avatar.url).to match(/^http:\/\/img[0123]\.example\.com\/avatars\/data\?\d*$/)
257
+ end
258
+ end
259
+
260
+ context "with fog_public set to false" do
261
+ before do
262
+ rebuild_model(@options.merge(fog_public: false))
263
+ @dummy = Dummy.new
264
+ @dummy.avatar = StringIO.new('.')
265
+ @dummy.save
266
+ end
267
+
268
+ it 'sets the @fog_public instance variable to false' do
269
+ assert_equal false, @dummy.avatar.instance_variable_get('@options')[:fog_public]
270
+ assert_equal false, @dummy.avatar.fog_public
271
+ end
272
+ end
273
+
274
+ context "with styles set and fog_public set to false" do
275
+ before do
276
+ rebuild_model(@options.merge(fog_public: false, styles: { medium: "300x300>", thumb: "100x100>" }))
277
+ @file = File.new(fixture_file('5k.png'), 'rb')
278
+ @dummy = Dummy.new
279
+ @dummy.avatar = @file
280
+ @dummy.save
281
+ end
282
+
283
+ it 'sets the @fog_public for a particular style to false' do
284
+ assert_equal false, @dummy.avatar.instance_variable_get('@options')[:fog_public]
285
+ assert_equal false, @dummy.avatar.fog_public(:thumb)
286
+ end
287
+ end
288
+
289
+ context "with styles set and fog_public set per-style" do
290
+ before do
291
+ rebuild_model(@options.merge(fog_public: { medium: false, thumb: true}, styles: { medium: "300x300>", thumb: "100x100>" }))
292
+ @file = File.new(fixture_file('5k.png'), 'rb')
293
+ @dummy = Dummy.new
294
+ @dummy.avatar = @file
295
+ @dummy.save
296
+ end
297
+
298
+ it 'sets the fog_public for a particular style to correct value' do
299
+ assert_equal false, @dummy.avatar.fog_public(:medium)
300
+ assert_equal true, @dummy.avatar.fog_public(:thumb)
301
+ end
302
+ end
303
+
304
+ context "with fog_public not set" do
305
+ before do
306
+ rebuild_model(@options)
307
+ @dummy = Dummy.new
308
+ @dummy.avatar = StringIO.new('.')
309
+ @dummy.save
310
+ end
311
+
312
+ it "defaults fog_public to true" do
313
+ assert_equal true, @dummy.avatar.fog_public
314
+ end
315
+ end
316
+
317
+ context "with scheme set" do
318
+ before do
319
+ rebuild_model(@options.merge(:fog_credentials => @credentials.merge(:scheme => 'http')))
320
+ @file = File.new(fixture_file('5k.png'), 'rb')
321
+ @dummy = Dummy.new
322
+ @dummy.avatar = @file
323
+ @dummy.save
324
+ end
325
+
326
+ it "honors the scheme in public url" do
327
+ assert_match(/^http:\/\//, @dummy.avatar.url)
328
+ end
329
+ it "honors the scheme in expiring url" do
330
+ assert_match(/^http:\/\//, @dummy.avatar.expiring_url)
331
+ end
332
+ end
333
+
334
+ context "with scheme not set" do
335
+ before do
336
+ rebuild_model(@options)
337
+ @file = File.new(fixture_file('5k.png'), 'rb')
338
+ @dummy = Dummy.new
339
+ @dummy.avatar = @file
340
+ @dummy.save
341
+ end
342
+
343
+ it "provides HTTPS public url" do
344
+ assert_match(/^https:\/\//, @dummy.avatar.url)
345
+ end
346
+ it "provides HTTPS expiring url" do
347
+ assert_match(/^https:\/\//, @dummy.avatar.expiring_url)
348
+ end
349
+ end
350
+
351
+ context "with a valid bucket name for a subdomain" do
352
+ before { @dummy.stubs(:new_record?).returns(false) }
353
+
354
+ it "provides an url in subdomain style" do
355
+ assert_match(/^https:\/\/papercliptests.s3.amazonaws.com\/avatars\/5k.png/, @dummy.avatar.url)
356
+ end
357
+
358
+ it "provides an url that expires in subdomain style" do
359
+ assert_match(/^https:\/\/papercliptests.s3.amazonaws.com\/avatars\/5k.png.+Expires=.+$/, @dummy.avatar.expiring_url)
360
+ end
361
+ end
362
+
363
+ context "generating an expiring url" do
364
+ it "generates the same url when using Times and Integer offsets" do
365
+ Timecop.freeze do
366
+ offset = 1234
367
+ rebuild_model(@options)
368
+ dummy = Dummy.new
369
+ dummy.avatar = StringIO.new('.')
370
+
371
+ assert_equal dummy.avatar.expiring_url(offset),
372
+ dummy.avatar.expiring_url(Time.now + offset )
373
+ end
374
+ end
375
+
376
+ it 'matches the default url if there is no assignment' do
377
+ dummy = Dummy.new
378
+ assert_equal dummy.avatar.url, dummy.avatar.expiring_url
379
+ end
380
+
381
+ it 'matches the default url when given a style if there is no assignment' do
382
+ dummy = Dummy.new
383
+ assert_equal dummy.avatar.url(:thumb), dummy.avatar.expiring_url(3600, :thumb)
384
+ end
385
+ end
386
+
387
+ context "with an invalid bucket name for a subdomain" do
388
+ before do
389
+ rebuild_model(@options.merge(fog_directory: "this_is_invalid"))
390
+ @dummy = Dummy.new
391
+ @dummy.avatar = @file
392
+ @dummy.save
393
+ end
394
+
395
+ it "does not match the bucket-subdomain restrictions" do
396
+ invalid_subdomains = %w(this_is_invalid in iamareallylongbucketnameiamareallylongbucketnameiamareallylongbu invalid- inval..id inval-.id inval.-id -invalid 192.168.10.2)
397
+ invalid_subdomains.each do |name|
398
+ assert_no_match Paperclip::Storage::Fog::AWS_BUCKET_SUBDOMAIN_RESTRICTON_REGEX, name
399
+ end
400
+ end
401
+
402
+ it "provides an url in folder style" do
403
+ assert_match(/^https:\/\/s3.amazonaws.com\/this_is_invalid\/avatars\/5k.png\?\d*$/, @dummy.avatar.url)
404
+ end
405
+
406
+ it "provides a url that expires in folder style" do
407
+ assert_match(/^https:\/\/s3.amazonaws.com\/this_is_invalid\/avatars\/5k.png.+Expires=.+$/, @dummy.avatar.expiring_url)
408
+ end
409
+
410
+ end
411
+
412
+ context "with a proc for a bucket name evaluating a model method" do
413
+ before do
414
+ @dynamic_fog_directory = 'dynamicpaperclip'
415
+ rebuild_model(@options.merge(fog_directory: lambda { |attachment| attachment.instance.bucket_name }))
416
+ @dummy = Dummy.new
417
+ @dummy.stubs(:bucket_name).returns(@dynamic_fog_directory)
418
+ @dummy.avatar = @file
419
+ @dummy.save
420
+ end
421
+
422
+ it "has created the bucket" do
423
+ assert @connection.directories.get(@dynamic_fog_directory).inspect
424
+ end
425
+
426
+ end
427
+
428
+ context "with a proc for the fog_host evaluating a model method" do
429
+ before do
430
+ rebuild_model(@options.merge(fog_host: lambda { |attachment| attachment.instance.fog_host }))
431
+ @dummy = Dummy.new
432
+ @dummy.stubs(:fog_host).returns('http://dynamicfoghost.com')
433
+ @dummy.avatar = @file
434
+ @dummy.save
435
+ end
436
+
437
+ it "provides a public url" do
438
+ assert_match(/http:\/\/dynamicfoghost\.com/, @dummy.avatar.url)
439
+ end
440
+
441
+ end
442
+
443
+ context "with a custom fog_host" do
444
+ before do
445
+ rebuild_model(@options.merge(fog_host: "http://dynamicfoghost.com"))
446
+ @dummy = Dummy.new
447
+ @dummy.avatar = @file
448
+ @dummy.save
449
+ end
450
+
451
+ it "provides a public url" do
452
+ assert_match(/http:\/\/dynamicfoghost\.com/, @dummy.avatar.url)
453
+ end
454
+
455
+ it "provides an expiring url" do
456
+ assert_match(/http:\/\/dynamicfoghost\.com/, @dummy.avatar.expiring_url)
457
+ end
458
+
459
+ context "with an invalid bucket name for a subdomain" do
460
+ before do
461
+ rebuild_model(@options.merge({fog_directory: "this_is_invalid", fog_host: "http://dynamicfoghost.com"}))
462
+ @dummy = Dummy.new
463
+ @dummy.avatar = @file
464
+ @dummy.save
465
+ end
466
+
467
+ it "provides an expiring url" do
468
+ assert_match(/http:\/\/dynamicfoghost\.com/, @dummy.avatar.expiring_url)
469
+ end
470
+ end
471
+
472
+ end
473
+
474
+ context "with a proc for the fog_credentials evaluating a model method" do
475
+ before do
476
+ @dynamic_fog_credentials = {
477
+ provider: 'AWS',
478
+ aws_access_key_id: 'DYNAMIC_ID',
479
+ aws_secret_access_key: 'DYNAMIC_SECRET'
480
+ }
481
+ rebuild_model(@options.merge(fog_credentials: lambda { |attachment| attachment.instance.fog_credentials }))
482
+ @dummy = Dummy.new
483
+ @dummy.stubs(:fog_credentials).returns(@dynamic_fog_credentials)
484
+ @dummy.avatar = @file
485
+ @dummy.save
486
+ end
487
+
488
+ it "provides a public url" do
489
+ assert_equal @dummy.avatar.fog_credentials, @dynamic_fog_credentials
490
+ end
491
+ end
492
+
493
+ context "with custom fog_options" do
494
+ before do
495
+ rebuild_model(
496
+ @options.merge(fog_options: { multipart_chunk_size: 104857600 }),
497
+ )
498
+ @dummy = Dummy.new
499
+ @dummy.avatar = @file
500
+ end
501
+
502
+ it "applies the options to the fog #create call" do
503
+ files = stub
504
+ @dummy.avatar.stubs(:directory).returns stub(files: files)
505
+ files.expects(:create).with(
506
+ has_entries(multipart_chunk_size: 104857600),
507
+ )
508
+ @dummy.save
509
+ end
510
+ end
511
+ end
512
+
513
+ end
514
+
515
+ context "when using local storage" do
516
+ before do
517
+ Fog.unmock!
518
+ rebuild_model styles: { medium: "300x300>", thumb: "100x100>" },
519
+ storage: :fog,
520
+ url: '/:attachment/:filename',
521
+ fog_directory: "paperclip",
522
+ fog_credentials: { provider: :local, local_root: "." },
523
+ fog_host: 'localhost'
524
+
525
+ @file = File.new(fixture_file('5k.png'), 'rb')
526
+ @dummy = Dummy.new
527
+ @dummy.avatar = @file
528
+ @dummy.stubs(:new_record?).returns(false)
529
+ end
530
+
531
+ after do
532
+ @file.close
533
+ Fog.mock!
534
+ end
535
+
536
+ it "returns the public url in place of the expiring url" do
537
+ assert_match @dummy.avatar.public_url, @dummy.avatar.expiring_url
538
+ end
539
+ end
540
+ end