mender_paperclip 2.4.3
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +26 -0
- data/README.md +402 -0
- data/Rakefile +86 -0
- data/generators/paperclip/USAGE +5 -0
- data/generators/paperclip/paperclip_generator.rb +27 -0
- data/generators/paperclip/templates/paperclip_migration.rb.erb +19 -0
- data/init.rb +4 -0
- data/lib/generators/paperclip/USAGE +8 -0
- data/lib/generators/paperclip/paperclip_generator.rb +33 -0
- data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +19 -0
- data/lib/paperclip/attachment.rb +454 -0
- data/lib/paperclip/callback_compatibility.rb +61 -0
- data/lib/paperclip/geometry.rb +120 -0
- data/lib/paperclip/interpolations.rb +181 -0
- data/lib/paperclip/iostream.rb +45 -0
- data/lib/paperclip/matchers/have_attached_file_matcher.rb +57 -0
- data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +81 -0
- data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +54 -0
- data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +95 -0
- data/lib/paperclip/matchers.rb +33 -0
- data/lib/paperclip/missing_attachment_styles.rb +87 -0
- data/lib/paperclip/options.rb +79 -0
- data/lib/paperclip/processor.rb +58 -0
- data/lib/paperclip/railtie.rb +26 -0
- data/lib/paperclip/storage/filesystem.rb +81 -0
- data/lib/paperclip/storage/fog.rb +162 -0
- data/lib/paperclip/storage/s3.rb +262 -0
- data/lib/paperclip/storage.rb +3 -0
- data/lib/paperclip/style.rb +95 -0
- data/lib/paperclip/thumbnail.rb +105 -0
- data/lib/paperclip/upfile.rb +62 -0
- data/lib/paperclip/version.rb +3 -0
- data/lib/paperclip.rb +478 -0
- data/lib/tasks/paperclip.rake +97 -0
- data/rails/init.rb +2 -0
- data/shoulda_macros/paperclip.rb +124 -0
- data/test/attachment_test.rb +1120 -0
- data/test/database.yml +4 -0
- data/test/fixtures/12k.png +0 -0
- data/test/fixtures/50x50.png +0 -0
- data/test/fixtures/5k.png +0 -0
- data/test/fixtures/animated.gif +0 -0
- data/test/fixtures/bad.png +1 -0
- data/test/fixtures/fog.yml +8 -0
- data/test/fixtures/s3.yml +8 -0
- data/test/fixtures/spaced file.png +0 -0
- data/test/fixtures/text.txt +1 -0
- data/test/fixtures/twopage.pdf +0 -0
- data/test/fixtures/uppercase.PNG +0 -0
- data/test/fog_test.rb +191 -0
- data/test/geometry_test.rb +206 -0
- data/test/helper.rb +152 -0
- data/test/integration_test.rb +654 -0
- data/test/interpolations_test.rb +195 -0
- data/test/iostream_test.rb +71 -0
- data/test/matchers/have_attached_file_matcher_test.rb +24 -0
- data/test/matchers/validate_attachment_content_type_matcher_test.rb +87 -0
- data/test/matchers/validate_attachment_presence_matcher_test.rb +26 -0
- data/test/matchers/validate_attachment_size_matcher_test.rb +51 -0
- data/test/options_test.rb +68 -0
- data/test/paperclip_missing_attachment_styles_test.rb +80 -0
- data/test/paperclip_test.rb +329 -0
- data/test/processor_test.rb +10 -0
- data/test/storage/filesystem_test.rb +52 -0
- data/test/storage/s3_live_test.rb +51 -0
- data/test/storage/s3_test.rb +633 -0
- data/test/style_test.rb +180 -0
- data/test/thumbnail_test.rb +383 -0
- data/test/upfile_test.rb +53 -0
- metadata +243 -0
@@ -0,0 +1,633 @@
|
|
1
|
+
require './test/helper'
|
2
|
+
require 'aws/s3'
|
3
|
+
|
4
|
+
class S3Test < Test::Unit::TestCase
|
5
|
+
def rails_env(env)
|
6
|
+
silence_warnings do
|
7
|
+
Object.const_set(:Rails, stub('Rails', :env => env))
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
context "Parsing S3 credentials" do
|
12
|
+
setup do
|
13
|
+
@proxy_settings = {:host => "127.0.0.1", :port => 8888, :user => "foo", :password => "bar"}
|
14
|
+
AWS::S3::Base.stubs(:establish_connection!)
|
15
|
+
rebuild_model :storage => :s3,
|
16
|
+
:bucket => "testing",
|
17
|
+
:http_proxy => @proxy_settings,
|
18
|
+
:s3_credentials => {:not => :important}
|
19
|
+
|
20
|
+
@dummy = Dummy.new
|
21
|
+
@avatar = @dummy.avatar
|
22
|
+
end
|
23
|
+
|
24
|
+
should "get the correct credentials when RAILS_ENV is production" do
|
25
|
+
rails_env("production")
|
26
|
+
assert_equal({:key => "12345"},
|
27
|
+
@avatar.parse_credentials('production' => {:key => '12345'},
|
28
|
+
:development => {:key => "54321"}))
|
29
|
+
end
|
30
|
+
|
31
|
+
should "get the correct credentials when RAILS_ENV is development" do
|
32
|
+
rails_env("development")
|
33
|
+
assert_equal({:key => "54321"},
|
34
|
+
@avatar.parse_credentials('production' => {:key => '12345'},
|
35
|
+
:development => {:key => "54321"}))
|
36
|
+
end
|
37
|
+
|
38
|
+
should "return the argument if the key does not exist" do
|
39
|
+
rails_env("not really an env")
|
40
|
+
assert_equal({:test => "12345"}, @avatar.parse_credentials(:test => "12345"))
|
41
|
+
end
|
42
|
+
|
43
|
+
should "support HTTP proxy settings" do
|
44
|
+
rails_env("development")
|
45
|
+
assert_equal(true, @avatar.using_http_proxy?)
|
46
|
+
assert_equal(@proxy_settings[:host], @avatar.http_proxy_host)
|
47
|
+
assert_equal(@proxy_settings[:port], @avatar.http_proxy_port)
|
48
|
+
assert_equal(@proxy_settings[:user], @avatar.http_proxy_user)
|
49
|
+
assert_equal(@proxy_settings[:password], @avatar.http_proxy_password)
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
context "" do
|
55
|
+
setup do
|
56
|
+
AWS::S3::Base.stubs(:establish_connection!)
|
57
|
+
rebuild_model :storage => :s3,
|
58
|
+
:s3_credentials => {},
|
59
|
+
:bucket => "bucket",
|
60
|
+
:path => ":attachment/:basename.:extension",
|
61
|
+
:url => ":s3_path_url"
|
62
|
+
@dummy = Dummy.new
|
63
|
+
@dummy.avatar = StringIO.new(".")
|
64
|
+
end
|
65
|
+
|
66
|
+
should "return a url based on an S3 path" do
|
67
|
+
assert_match %r{^http://s3.amazonaws.com/bucket/avatars/stringio.txt}, @dummy.avatar.url
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
context "s3_host_name" do
|
72
|
+
setup do
|
73
|
+
AWS::S3::Base.stubs(:establish_connection!)
|
74
|
+
rebuild_model :storage => :s3,
|
75
|
+
:s3_credentials => {},
|
76
|
+
:bucket => "bucket",
|
77
|
+
:path => ":attachment/:basename.:extension",
|
78
|
+
:s3_host_name => "s3-ap-northeast-1.amazonaws.com"
|
79
|
+
@dummy = Dummy.new
|
80
|
+
@dummy.avatar = StringIO.new(".")
|
81
|
+
end
|
82
|
+
|
83
|
+
should "return a url based on an :s3_host_name path" do
|
84
|
+
assert_match %r{^http://s3-ap-northeast-1.amazonaws.com/bucket/avatars/stringio.txt}, @dummy.avatar.url
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context "An attachment that uses S3 for storage and has styles that return different file types" do
|
89
|
+
setup do
|
90
|
+
AWS::S3::Base.stubs(:establish_connection!)
|
91
|
+
rebuild_model :styles => { :large => ['500x500#', :jpg] },
|
92
|
+
:storage => :s3,
|
93
|
+
:bucket => "bucket",
|
94
|
+
:path => ":attachment/:basename.:extension",
|
95
|
+
:s3_credentials => {
|
96
|
+
'access_key_id' => "12345",
|
97
|
+
'secret_access_key' => "54321"
|
98
|
+
}
|
99
|
+
|
100
|
+
@dummy = Dummy.new
|
101
|
+
@dummy.avatar = File.new(File.join(File.dirname(__FILE__), '..', 'fixtures', '5k.png'), 'rb')
|
102
|
+
end
|
103
|
+
|
104
|
+
should "return a url containing the correct original file mime type" do
|
105
|
+
assert_match /.+\/5k.png/, @dummy.avatar.url
|
106
|
+
end
|
107
|
+
|
108
|
+
should "return a url containing the correct processed file mime type" do
|
109
|
+
assert_match /.+\/5k.jpg/, @dummy.avatar.url(:large)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
context "An attachment that uses S3 for storage and has spaces in file name" do
|
114
|
+
setup do
|
115
|
+
AWS::S3::Base.stubs(:establish_connection!)
|
116
|
+
rebuild_model :styles => { :large => ['500x500#', :jpg] },
|
117
|
+
:storage => :s3,
|
118
|
+
:bucket => "bucket",
|
119
|
+
:path => ":attachment/:basename.:extension",
|
120
|
+
:s3_credentials => {
|
121
|
+
'access_key_id' => "12345",
|
122
|
+
'secret_access_key' => "54321"
|
123
|
+
}
|
124
|
+
|
125
|
+
@dummy = Dummy.new
|
126
|
+
@dummy.avatar = File.new(File.join(File.dirname(__FILE__), '..', 'fixtures', 'spaced file.png'), 'rb')
|
127
|
+
end
|
128
|
+
|
129
|
+
should "return an escaped version of url" do
|
130
|
+
assert_match /.+\/spaced%20file\.png/, @dummy.avatar.url
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
context "" do
|
135
|
+
setup do
|
136
|
+
AWS::S3::Base.stubs(:establish_connection!)
|
137
|
+
rebuild_model :storage => :s3,
|
138
|
+
:s3_credentials => {},
|
139
|
+
:bucket => "bucket",
|
140
|
+
:path => ":attachment/:basename.:extension",
|
141
|
+
:url => ":s3_domain_url"
|
142
|
+
@dummy = Dummy.new
|
143
|
+
@dummy.avatar = StringIO.new(".")
|
144
|
+
end
|
145
|
+
|
146
|
+
should "return a url based on an S3 subdomain" do
|
147
|
+
assert_match %r{^http://bucket.s3.amazonaws.com/avatars/stringio.txt}, @dummy.avatar.url
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
context "" do
|
152
|
+
setup do
|
153
|
+
AWS::S3::Base.stubs(:establish_connection!)
|
154
|
+
rebuild_model :storage => :s3,
|
155
|
+
:s3_credentials => {
|
156
|
+
:production => { :bucket => "prod_bucket" },
|
157
|
+
:development => { :bucket => "dev_bucket" }
|
158
|
+
},
|
159
|
+
:s3_host_alias => "something.something.com",
|
160
|
+
:path => ":attachment/:basename.:extension",
|
161
|
+
:url => ":s3_alias_url"
|
162
|
+
@dummy = Dummy.new
|
163
|
+
@dummy.avatar = StringIO.new(".")
|
164
|
+
end
|
165
|
+
|
166
|
+
should "return a url based on the host_alias" do
|
167
|
+
assert_match %r{^http://something.something.com/avatars/stringio.txt}, @dummy.avatar.url
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
context "generating a url with a proc as the host alias" do
|
172
|
+
setup do
|
173
|
+
AWS::S3::Base.stubs(:establish_connection!)
|
174
|
+
rebuild_model :storage => :s3,
|
175
|
+
:s3_credentials => { :bucket => "prod_bucket" },
|
176
|
+
:s3_host_alias => Proc.new{|atch| "cdn#{atch.instance.counter % 4}.example.com"},
|
177
|
+
:path => ":attachment/:basename.:extension",
|
178
|
+
:url => ":s3_alias_url"
|
179
|
+
Dummy.class_eval do
|
180
|
+
def counter
|
181
|
+
@counter ||= 0
|
182
|
+
@counter += 1
|
183
|
+
@counter
|
184
|
+
end
|
185
|
+
end
|
186
|
+
@dummy = Dummy.new
|
187
|
+
@dummy.avatar = StringIO.new(".")
|
188
|
+
end
|
189
|
+
|
190
|
+
should "return a url based on the host_alias" do
|
191
|
+
assert_match %r{^http://cdn1.example.com/avatars/stringio.txt}, @dummy.avatar.url
|
192
|
+
assert_match %r{^http://cdn2.example.com/avatars/stringio.txt}, @dummy.avatar.url
|
193
|
+
end
|
194
|
+
|
195
|
+
should "still return the bucket name" do
|
196
|
+
assert_equal "prod_bucket", @dummy.avatar.bucket_name
|
197
|
+
end
|
198
|
+
|
199
|
+
end
|
200
|
+
|
201
|
+
context "" do
|
202
|
+
setup do
|
203
|
+
AWS::S3::Base.stubs(:establish_connection!)
|
204
|
+
rebuild_model :storage => :s3,
|
205
|
+
:s3_credentials => {},
|
206
|
+
:bucket => "bucket",
|
207
|
+
:path => ":attachment/:basename.:extension",
|
208
|
+
:url => ":asset_host"
|
209
|
+
@dummy = Dummy.new
|
210
|
+
@dummy.avatar = StringIO.new(".")
|
211
|
+
end
|
212
|
+
|
213
|
+
should "return a relative URL for Rails to calculate assets host" do
|
214
|
+
assert_match %r{^avatars/stringio\.txt}, @dummy.avatar.url
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
context "Generating a secure url with an expiration" do
|
219
|
+
setup do
|
220
|
+
AWS::S3::Base.stubs(:establish_connection!)
|
221
|
+
rebuild_model :storage => :s3,
|
222
|
+
:s3_credentials => {
|
223
|
+
:production => { :bucket => "prod_bucket" },
|
224
|
+
:development => { :bucket => "dev_bucket" }
|
225
|
+
},
|
226
|
+
:s3_host_alias => "something.something.com",
|
227
|
+
:s3_permissions => "private",
|
228
|
+
:path => ":attachment/:basename.:extension",
|
229
|
+
:url => ":s3_alias_url"
|
230
|
+
|
231
|
+
rails_env("production")
|
232
|
+
|
233
|
+
@dummy = Dummy.new
|
234
|
+
@dummy.avatar = StringIO.new(".")
|
235
|
+
|
236
|
+
AWS::S3::S3Object.expects(:url_for).with("avatars/stringio.txt", "prod_bucket", { :expires_in => 3600, :use_ssl => true })
|
237
|
+
|
238
|
+
@dummy.avatar.expiring_url
|
239
|
+
end
|
240
|
+
|
241
|
+
should "should succeed" do
|
242
|
+
assert true
|
243
|
+
end
|
244
|
+
end
|
245
|
+
|
246
|
+
context "Generating a url with an expiration" do
|
247
|
+
setup do
|
248
|
+
AWS::S3::Base.stubs(:establish_connection!)
|
249
|
+
rebuild_model :storage => :s3,
|
250
|
+
:s3_credentials => {
|
251
|
+
:production => { :bucket => "prod_bucket" },
|
252
|
+
:development => { :bucket => "dev_bucket" }
|
253
|
+
},
|
254
|
+
:s3_permissions => :private,
|
255
|
+
:s3_host_alias => "something.something.com",
|
256
|
+
:path => ":attachment/:style/:basename.:extension",
|
257
|
+
:url => ":s3_alias_url"
|
258
|
+
|
259
|
+
rails_env("production")
|
260
|
+
|
261
|
+
@dummy = Dummy.new
|
262
|
+
@dummy.avatar = StringIO.new(".")
|
263
|
+
|
264
|
+
AWS::S3::S3Object.expects(:url_for).with("avatars/original/stringio.txt", "prod_bucket", { :expires_in => 3600, :use_ssl => true })
|
265
|
+
@dummy.avatar.expiring_url
|
266
|
+
|
267
|
+
AWS::S3::S3Object.expects(:url_for).with("avatars/thumb/stringio.txt", "prod_bucket", { :expires_in => 1800, :use_ssl => true })
|
268
|
+
@dummy.avatar.expiring_url(1800, :thumb)
|
269
|
+
end
|
270
|
+
|
271
|
+
should "should succeed" do
|
272
|
+
assert true
|
273
|
+
end
|
274
|
+
end
|
275
|
+
|
276
|
+
context "Parsing S3 credentials with a bucket in them" do
|
277
|
+
setup do
|
278
|
+
AWS::S3::Base.stubs(:establish_connection!)
|
279
|
+
rebuild_model :storage => :s3,
|
280
|
+
:s3_credentials => {
|
281
|
+
:production => { :bucket => "prod_bucket" },
|
282
|
+
:development => { :bucket => "dev_bucket" }
|
283
|
+
}
|
284
|
+
@dummy = Dummy.new
|
285
|
+
end
|
286
|
+
|
287
|
+
should "get the right bucket in production" do
|
288
|
+
rails_env("production")
|
289
|
+
assert_equal "prod_bucket", @dummy.avatar.bucket_name
|
290
|
+
end
|
291
|
+
|
292
|
+
should "get the right bucket in development" do
|
293
|
+
rails_env("development")
|
294
|
+
assert_equal "dev_bucket", @dummy.avatar.bucket_name
|
295
|
+
end
|
296
|
+
end
|
297
|
+
|
298
|
+
context "Parsing S3 credentials with a s3_host_name in them" do
|
299
|
+
setup do
|
300
|
+
AWS::S3::Base.stubs(:establish_connection!)
|
301
|
+
rebuild_model :storage => :s3,
|
302
|
+
:s3_credentials => {
|
303
|
+
:production => { :s3_host_name => "s3-world-end.amazonaws.com" },
|
304
|
+
:development => { :s3_host_name => "s3-ap-northeast-1.amazonaws.com" }
|
305
|
+
}
|
306
|
+
@dummy = Dummy.new
|
307
|
+
end
|
308
|
+
|
309
|
+
should "get the right s3_host_name in production" do
|
310
|
+
rails_env("production")
|
311
|
+
assert_match %r{^s3-world-end.amazonaws.com}, @dummy.avatar.s3_host_name
|
312
|
+
end
|
313
|
+
|
314
|
+
should "get the right s3_host_name in development" do
|
315
|
+
rails_env("development")
|
316
|
+
assert_match %r{^s3-ap-northeast-1.amazonaws.com}, @dummy.avatar.s3_host_name
|
317
|
+
end
|
318
|
+
|
319
|
+
should "get the right s3_host_name if the key does not exist" do
|
320
|
+
rails_env("test")
|
321
|
+
assert_match %r{^s3.amazonaws.com}, @dummy.avatar.s3_host_name
|
322
|
+
end
|
323
|
+
end
|
324
|
+
|
325
|
+
context "An attachment with S3 storage" do
|
326
|
+
setup do
|
327
|
+
rebuild_model :storage => :s3,
|
328
|
+
:bucket => "testing",
|
329
|
+
:path => ":attachment/:style/:basename.:extension",
|
330
|
+
:s3_credentials => {
|
331
|
+
'access_key_id' => "12345",
|
332
|
+
'secret_access_key' => "54321"
|
333
|
+
}
|
334
|
+
end
|
335
|
+
|
336
|
+
should "be extended by the S3 module" do
|
337
|
+
assert Dummy.new.avatar.is_a?(Paperclip::Storage::S3)
|
338
|
+
end
|
339
|
+
|
340
|
+
should "not be extended by the Filesystem module" do
|
341
|
+
assert ! Dummy.new.avatar.is_a?(Paperclip::Storage::Filesystem)
|
342
|
+
end
|
343
|
+
|
344
|
+
context "when assigned" do
|
345
|
+
setup do
|
346
|
+
@file = File.new(File.join(File.dirname(__FILE__), '..', 'fixtures', '5k.png'), 'rb')
|
347
|
+
@dummy = Dummy.new
|
348
|
+
@dummy.avatar = @file
|
349
|
+
end
|
350
|
+
|
351
|
+
teardown { @file.close }
|
352
|
+
|
353
|
+
should "not get a bucket to get a URL" do
|
354
|
+
@dummy.avatar.expects(:s3).never
|
355
|
+
@dummy.avatar.expects(:s3_bucket).never
|
356
|
+
assert_match %r{^http://s3\.amazonaws\.com/testing/avatars/original/5k\.png}, @dummy.avatar.url
|
357
|
+
end
|
358
|
+
|
359
|
+
context "and saved" do
|
360
|
+
setup do
|
361
|
+
AWS::S3::S3Object.stubs(:store).with(@dummy.avatar.path, anything, 'testing', :content_type => 'image/png', :access => :public_read)
|
362
|
+
@dummy.save
|
363
|
+
end
|
364
|
+
|
365
|
+
should "succeed" do
|
366
|
+
assert true
|
367
|
+
end
|
368
|
+
end
|
369
|
+
|
370
|
+
should "delete tempfiles" do
|
371
|
+
AWS::S3::S3Object.stubs(:store).with(@dummy.avatar.path, anything, 'testing', :content_type => 'image/png', :access => :public_read)
|
372
|
+
File.stubs(:exist?).returns(true)
|
373
|
+
Paperclip::Tempfile.any_instance.expects(:close).at_least_once()
|
374
|
+
Paperclip::Tempfile.any_instance.expects(:unlink).at_least_once()
|
375
|
+
|
376
|
+
@dummy.save!
|
377
|
+
end
|
378
|
+
|
379
|
+
context "and saved without a bucket" do
|
380
|
+
setup do
|
381
|
+
class AWS::S3::NoSuchBucket < AWS::S3::ResponseError
|
382
|
+
# Force the class to be created as a proper subclass of ResponseError thanks to AWS::S3's autocreation of exceptions
|
383
|
+
end
|
384
|
+
AWS::S3::Bucket.expects(:create).with("testing")
|
385
|
+
AWS::S3::S3Object.stubs(:store).raises(AWS::S3::NoSuchBucket.new(:message, :response)).then.returns(true)
|
386
|
+
@dummy.save
|
387
|
+
end
|
388
|
+
|
389
|
+
should "succeed" do
|
390
|
+
assert true
|
391
|
+
end
|
392
|
+
end
|
393
|
+
|
394
|
+
context "and remove" do
|
395
|
+
setup do
|
396
|
+
AWS::S3::S3Object.stubs(:exists?).returns(true)
|
397
|
+
AWS::S3::S3Object.stubs(:delete)
|
398
|
+
@dummy.destroy_attached_files
|
399
|
+
end
|
400
|
+
|
401
|
+
should "succeed" do
|
402
|
+
assert true
|
403
|
+
end
|
404
|
+
end
|
405
|
+
end
|
406
|
+
end
|
407
|
+
|
408
|
+
context "An attachment with S3 storage and bucket defined as a Proc" do
|
409
|
+
setup do
|
410
|
+
AWS::S3::Base.stubs(:establish_connection!)
|
411
|
+
rebuild_model :storage => :s3,
|
412
|
+
:bucket => lambda { |attachment| "bucket_#{attachment.instance.other}" },
|
413
|
+
:s3_credentials => {:not => :important}
|
414
|
+
end
|
415
|
+
|
416
|
+
should "get the right bucket name" do
|
417
|
+
assert "bucket_a", Dummy.new(:other => 'a').avatar.bucket_name
|
418
|
+
assert "bucket_b", Dummy.new(:other => 'b').avatar.bucket_name
|
419
|
+
end
|
420
|
+
end
|
421
|
+
|
422
|
+
context "An attachment with S3 storage and specific s3 headers set" do
|
423
|
+
setup do
|
424
|
+
AWS::S3::Base.stubs(:establish_connection!)
|
425
|
+
rebuild_model :storage => :s3,
|
426
|
+
:bucket => "testing",
|
427
|
+
:path => ":attachment/:style/:basename.:extension",
|
428
|
+
:s3_credentials => {
|
429
|
+
'access_key_id' => "12345",
|
430
|
+
'secret_access_key' => "54321"
|
431
|
+
},
|
432
|
+
:s3_headers => {'Cache-Control' => 'max-age=31557600'}
|
433
|
+
end
|
434
|
+
|
435
|
+
context "when assigned" do
|
436
|
+
setup do
|
437
|
+
@file = File.new(File.join(File.dirname(__FILE__), '..', 'fixtures', '5k.png'), 'rb')
|
438
|
+
@dummy = Dummy.new
|
439
|
+
@dummy.avatar = @file
|
440
|
+
end
|
441
|
+
|
442
|
+
teardown { @file.close }
|
443
|
+
|
444
|
+
context "and saved" do
|
445
|
+
setup do
|
446
|
+
AWS::S3::Base.stubs(:establish_connection!)
|
447
|
+
AWS::S3::S3Object.stubs(:store).with(@dummy.avatar.path,
|
448
|
+
anything,
|
449
|
+
'testing',
|
450
|
+
:content_type => 'image/png',
|
451
|
+
:access => :public_read,
|
452
|
+
'Cache-Control' => 'max-age=31557600')
|
453
|
+
@dummy.save
|
454
|
+
end
|
455
|
+
|
456
|
+
should "succeed" do
|
457
|
+
assert true
|
458
|
+
end
|
459
|
+
end
|
460
|
+
end
|
461
|
+
end
|
462
|
+
|
463
|
+
context "with S3 credentials supplied as Pathname" do
|
464
|
+
setup do
|
465
|
+
ENV['S3_KEY'] = 'pathname_key'
|
466
|
+
ENV['S3_BUCKET'] = 'pathname_bucket'
|
467
|
+
ENV['S3_SECRET'] = 'pathname_secret'
|
468
|
+
|
469
|
+
rails_env('test')
|
470
|
+
|
471
|
+
rebuild_model :storage => :s3,
|
472
|
+
:s3_credentials => Pathname.new(File.join(File.dirname(__FILE__))).join("../fixtures/s3.yml")
|
473
|
+
|
474
|
+
Dummy.delete_all
|
475
|
+
@dummy = Dummy.new
|
476
|
+
end
|
477
|
+
|
478
|
+
should "parse the credentials" do
|
479
|
+
assert_equal 'pathname_bucket', @dummy.avatar.bucket_name
|
480
|
+
assert_equal 'pathname_key', AWS::S3::Base.connection.options[:access_key_id]
|
481
|
+
assert_equal 'pathname_secret', AWS::S3::Base.connection.options[:secret_access_key]
|
482
|
+
end
|
483
|
+
end
|
484
|
+
|
485
|
+
context "with S3 credentials in a YAML file" do
|
486
|
+
setup do
|
487
|
+
ENV['S3_KEY'] = 'env_key'
|
488
|
+
ENV['S3_BUCKET'] = 'env_bucket'
|
489
|
+
ENV['S3_SECRET'] = 'env_secret'
|
490
|
+
|
491
|
+
rails_env('test')
|
492
|
+
|
493
|
+
rebuild_model :storage => :s3,
|
494
|
+
:s3_credentials => File.new(File.join(File.dirname(__FILE__), "../fixtures/s3.yml"))
|
495
|
+
|
496
|
+
Dummy.delete_all
|
497
|
+
|
498
|
+
@dummy = Dummy.new
|
499
|
+
end
|
500
|
+
|
501
|
+
should "run the file through ERB" do
|
502
|
+
assert_equal 'env_bucket', @dummy.avatar.bucket_name
|
503
|
+
assert_equal 'env_key', AWS::S3::Base.connection.options[:access_key_id]
|
504
|
+
assert_equal 'env_secret', AWS::S3::Base.connection.options[:secret_access_key]
|
505
|
+
end
|
506
|
+
end
|
507
|
+
|
508
|
+
context "S3 Permissions" do
|
509
|
+
context "defaults to public-read" do
|
510
|
+
setup do
|
511
|
+
rebuild_model :storage => :s3,
|
512
|
+
:bucket => "testing",
|
513
|
+
:path => ":attachment/:style/:basename.:extension",
|
514
|
+
:s3_credentials => {
|
515
|
+
'access_key_id' => "12345",
|
516
|
+
'secret_access_key' => "54321"
|
517
|
+
}
|
518
|
+
end
|
519
|
+
|
520
|
+
context "when assigned" do
|
521
|
+
setup do
|
522
|
+
@file = File.new(File.join(File.dirname(__FILE__), '..', 'fixtures', '5k.png'), 'rb')
|
523
|
+
@dummy = Dummy.new
|
524
|
+
@dummy.avatar = @file
|
525
|
+
end
|
526
|
+
|
527
|
+
teardown { @file.close }
|
528
|
+
|
529
|
+
context "and saved" do
|
530
|
+
setup do
|
531
|
+
AWS::S3::Base.stubs(:establish_connection!)
|
532
|
+
AWS::S3::S3Object.expects(:store).with(@dummy.avatar.path,
|
533
|
+
anything,
|
534
|
+
'testing',
|
535
|
+
:content_type => 'image/png',
|
536
|
+
:access => :public_read)
|
537
|
+
@dummy.save
|
538
|
+
end
|
539
|
+
|
540
|
+
should "succeed" do
|
541
|
+
assert true
|
542
|
+
end
|
543
|
+
end
|
544
|
+
end
|
545
|
+
end
|
546
|
+
|
547
|
+
context "string permissions set" do
|
548
|
+
setup do
|
549
|
+
rebuild_model :storage => :s3,
|
550
|
+
:bucket => "testing",
|
551
|
+
:path => ":attachment/:style/:basename.:extension",
|
552
|
+
:s3_credentials => {
|
553
|
+
'access_key_id' => "12345",
|
554
|
+
'secret_access_key' => "54321"
|
555
|
+
},
|
556
|
+
:s3_permissions => 'private'
|
557
|
+
end
|
558
|
+
|
559
|
+
context "when assigned" do
|
560
|
+
setup do
|
561
|
+
@file = File.new(File.join(File.dirname(__FILE__), '..', 'fixtures', '5k.png'), 'rb')
|
562
|
+
@dummy = Dummy.new
|
563
|
+
@dummy.avatar = @file
|
564
|
+
end
|
565
|
+
|
566
|
+
teardown { @file.close }
|
567
|
+
|
568
|
+
context "and saved" do
|
569
|
+
setup do
|
570
|
+
AWS::S3::Base.stubs(:establish_connection!)
|
571
|
+
AWS::S3::S3Object.expects(:store).with(@dummy.avatar.path,
|
572
|
+
anything,
|
573
|
+
'testing',
|
574
|
+
:content_type => 'image/png',
|
575
|
+
:access => 'private')
|
576
|
+
@dummy.save
|
577
|
+
end
|
578
|
+
|
579
|
+
should "succeed" do
|
580
|
+
assert true
|
581
|
+
end
|
582
|
+
end
|
583
|
+
end
|
584
|
+
end
|
585
|
+
|
586
|
+
context "hash permissions set" do
|
587
|
+
setup do
|
588
|
+
rebuild_model :storage => :s3,
|
589
|
+
:bucket => "testing",
|
590
|
+
:path => ":attachment/:style/:basename.:extension",
|
591
|
+
:styles => {
|
592
|
+
:thumb => "80x80>"
|
593
|
+
},
|
594
|
+
:s3_credentials => {
|
595
|
+
'access_key_id' => "12345",
|
596
|
+
'secret_access_key' => "54321"
|
597
|
+
},
|
598
|
+
:s3_permissions => {
|
599
|
+
:original => 'private',
|
600
|
+
:thumb => 'public-read'
|
601
|
+
}
|
602
|
+
end
|
603
|
+
|
604
|
+
context "when assigned" do
|
605
|
+
setup do
|
606
|
+
@file = File.new(File.join(File.dirname(__FILE__), '..', 'fixtures', '5k.png'), 'rb')
|
607
|
+
@dummy = Dummy.new
|
608
|
+
@dummy.avatar = @file
|
609
|
+
end
|
610
|
+
|
611
|
+
teardown { @file.close }
|
612
|
+
|
613
|
+
context "and saved" do
|
614
|
+
setup do
|
615
|
+
AWS::S3::Base.stubs(:establish_connection!)
|
616
|
+
[:thumb, :original].each do |style|
|
617
|
+
AWS::S3::S3Object.expects(:store).with("avatars/#{style}/5k.png",
|
618
|
+
anything,
|
619
|
+
'testing',
|
620
|
+
:content_type => 'image/png',
|
621
|
+
:access => style == :thumb ? 'public-read' : 'private')
|
622
|
+
end
|
623
|
+
@dummy.save
|
624
|
+
end
|
625
|
+
|
626
|
+
should "succeed" do
|
627
|
+
assert true
|
628
|
+
end
|
629
|
+
end
|
630
|
+
end
|
631
|
+
end
|
632
|
+
end
|
633
|
+
end
|