-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
fog_helper.rb
565 lines (462 loc) · 20.1 KB
/
fog_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
def fog_tests(fog_credentials)
describe CarrierWave::Storage::Fog do
shared_examples_for "#{fog_credentials[:provider]} storage" do
before do
WebMock.disable! unless Fog.mocking?
CarrierWave.configure do |config|
config.reset_config
config.fog_provider = "fog/#{fog_credentials[:provider].downcase}"
config.fog_attributes = {}
config.fog_credentials = fog_credentials
config.fog_directory = CARRIERWAVE_DIRECTORY
config.fog_public = true
config.fog_use_ssl_for_aws = true
config.cache_storage = :fog
end
eval <<-RUBY
class FogSpec#{fog_credentials[:provider]}Uploader < CarrierWave::Uploader::Base
storage :fog
end
RUBY
@provider = fog_credentials[:provider]
# @uploader = FogSpecUploader.new
@uploader = eval("FogSpec#{@provider}Uploader")
allow(@uploader).to receive(:store_path).and_return('uploads/test.jpg')
@storage = CarrierWave::Storage::Fog.new(@uploader)
@directory = @storage.connection.directories.get(CARRIERWAVE_DIRECTORY) || @storage.connection.directories.create(:key => CARRIERWAVE_DIRECTORY, :public => true)
end
after do
CarrierWave.configure do |config|
config.reset_config
end
WebMock.enable! unless Fog.mocking?
end
describe '#cache_stored_file!' do
it "should cache_stored_file! after store!" do
uploader = @uploader.new
uploader.store!(file)
expect { uploader.cache_stored_file! }.not_to raise_error
end
end
context '#acl_header' do
let(:store_path) { 'uploads/test+.jpg' }
before do
allow(@uploader).to receive(:store_path).and_return(store_path)
end
it 'includes acl_header when necessary' do
if file.is_a?(CarrierWave::Storage::Fog::File)
if @provider == 'AWS'
expect(@storage.connection).to receive(:copy_object)
.with(anything, anything, anything, anything, { "x-amz-acl"=>"public-read" }).and_call_original
else
expect(@storage.connection).to receive(:copy_object)
.with(anything, anything, anything, anything, {}).and_call_original
end
end
@storage.store!(file)
end
end
describe '#store!' do
let(:store_path) { 'uploads/test+.jpg' }
context 'when file is ::File' do
before do
allow(@uploader).to receive(:store_path).and_return(store_path)
@fog_file = @storage.store!(file)
end
it "should upload the file" do
# reading the file after upload should return the body, not a closed tempfile
expect(@fog_file.read).to eq('this is stuff')
# make sure it actually uploaded to the service, too
expect(@directory.files.get(store_path).body).to eq('this is stuff')
end
it "should have a path" do
expect(@fog_file.path).to eq(store_path)
end
it "should have a content_type" do
expect(@fog_file.content_type).to eq(file.content_type)
expect(@directory.files.get(store_path).content_type).to eq(file.content_type)
end
it "should have an extension" do
expect(@fog_file.extension).to eq("jpg")
end
context "without asset_host" do
it "should have a public_url" do
unless fog_credentials[:provider] == 'Local'
expect(@fog_file.public_url).not_to be_nil
end
end
it "should have a url" do
unless fog_credentials[:provider] == 'Local'
expect(@fog_file.url).not_to be_nil
end
end
context "directory is a valid subdomain" do
before do
allow(@uploader).to receive(:fog_directory).and_return('assets-site-com')
end
it "should use a subdomain URL for AWS" do
if @provider == 'AWS'
expect(@fog_file.public_url).to include('https://assets-site-com.s3.amazonaws.com')
end
end
it "should use accelerate domain if fog_aws_accelerate is true" do
if @provider == 'AWS'
allow(@uploader).to receive(:fog_aws_accelerate).and_return(true)
expect(@fog_file.public_url).to include('https://assets-site-com.s3-accelerate.amazonaws.com')
end
end
end
it "should not use a subdomain URL for AWS if the directory is not a valid subdomain" do
if @provider == 'AWS'
allow(@uploader).to receive(:fog_directory).and_return('SiteAssets')
expect(@fog_file.public_url).to include('https://s3.amazonaws.com/SiteAssets')
end
end
it "should not use a subdomain URL for AWS if https && the directory is not accessible over https as a virtual hosted bucket" do
if @provider == 'AWS'
allow(@uploader).to receive(:fog_use_ssl_for_aws).and_return(true)
allow(@uploader).to receive(:fog_directory).and_return('foo.bar')
expect(@fog_file.public_url).to include('https://s3.amazonaws.com/foo.bar')
end
end
it "should use a subdomain URL for AWS if http && the directory is not accessible over https as a virtual hosted bucket" do
if @provider == 'AWS'
allow(@uploader).to receive(:fog_use_ssl_for_aws).and_return(false)
allow(@uploader).to receive(:fog_directory).and_return('foo.bar')
expect(@fog_file.public_url).to include('http://foo.bar.s3.amazonaws.com/')
end
end
it "should use https as a default protocol" do
if @provider == 'AWS'
expect(@fog_file.public_url).to start_with 'https://'
end
end
it "should use https as a default protocol" do
if @provider == 'AWS'
allow(@uploader).to receive(:fog_use_ssl_for_aws).and_return(false)
expect(@fog_file.public_url).to start_with 'http://'
end
end
it "should use the google public url if available" do
if @provider == 'Google'
allow(@uploader).to receive(:fog_directory).and_return('SiteAssets')
expect(@fog_file.public_url).to include('https://storage.googleapis.com/SiteAssets')
end
end
end
context "with asset_host" do
before { allow(@uploader).to receive(:asset_host).and_return(asset_host) }
context "when a asset_host is a proc" do
let(:asset_host) { proc { "http://foo.bar" } }
describe "args passed to proc" do
let(:asset_host) { proc { |storage| expect(storage).to be_instance_of ::CarrierWave::Storage::Fog::File } }
it "should be the uploader" do
@fog_file.public_url
end
end
it "should have a asset_host rooted public_url" do
expect(@fog_file.public_url).to eq('http://foo.bar/uploads/test%2B.jpg')
end
it "should have a asset_host rooted url" do
expect(@fog_file.url).to eq('http://foo.bar/uploads/test%2B.jpg')
end
it "should always have the same asset_host rooted url" do
expect(@fog_file.url).to eq('http://foo.bar/uploads/test%2B.jpg')
expect(@fog_file.url).to eq('http://foo.bar/uploads/test%2B.jpg')
end
it 'should retrieve file name' do
expect(@fog_file.filename).to eq('test+.jpg')
end
end
context "when a string" do
let(:asset_host) { "http://foo.bar" }
it "should have a asset_host rooted public_url" do
expect(@fog_file.public_url).to eq('http://foo.bar/uploads/test%2B.jpg')
end
it "should have a asset_host rooted url" do
expect(@fog_file.url).to eq('http://foo.bar/uploads/test%2B.jpg')
end
it "should always have the same asset_host rooted url" do
expect(@fog_file.url).to eq('http://foo.bar/uploads/test%2B.jpg')
expect(@fog_file.url).to eq('http://foo.bar/uploads/test%2B.jpg')
end
end
end
context "without extension" do
let(:store_path) { 'uploads/test' }
it "should have no extension" do
expect(@fog_file.extension).to be_nil
end
end
it "should return filesize" do
expect(@fog_file.size).to eq(13)
end
it "should be deletable" do
@fog_file.delete
expect(@directory.files.head(store_path)).to eq(nil)
end
context "when the file has been deleted" do
before { @fog_file.delete }
it "should not error getting the file size" do
expect { @fog_file.size }.not_to raise_error
end
it "should not error getting the content type" do
expect { @fog_file.content_type }.not_to raise_error
end
it "should not return false for content type" do
expect(@fog_file.content_type).not_to be false
end
end
end
context 'when file is ::StringIO' do
let(:file) do
CarrierWave::SanitizedFile.new(
:tempfile => StringIO.new('Test StringIO texts'),
:filename => 'test.jpg',
:content_type => 'image/jpeg'
)
end
before do
allow(@uploader).to receive(:store_path).and_return(store_path)
@fog_file = @storage.store!(file)
end
it "should upload the file" do
# reading the file after upload should return the body, not a closed tempfile
expect(@fog_file.read).to eq('Test StringIO texts')
# make sure it actually uploaded to the service, too
expect(@directory.files.get(store_path).body).to eq('Test StringIO texts')
end
end
end
describe '#retrieve!' do
before do
@directory.files.create(:key => 'uploads/test.jpg', :body => 'A test, 1234', :public => true)
allow(@uploader).to receive(:store_path).with('test.jpg').and_return('uploads/test.jpg')
@fog_file = @storage.retrieve!('test.jpg')
end
it "should retrieve the file contents" do
expect(@fog_file.read.chomp).to eq("A test, 1234")
end
it "should have a path" do
expect(@fog_file.path).to eq('uploads/test.jpg')
end
it "should have a public url" do
unless fog_credentials[:provider] == 'Local'
expect(@fog_file.public_url).not_to be_nil
end
end
it "should return filesize" do
expect(@fog_file.size).to eq(12)
end
it "should be deletable" do
@fog_file.delete
expect(@directory.files.head('uploads/test.jpg')).to eq(nil)
end
end
describe '#cache!' do
before do
allow(@uploader).to receive(:cache_path).and_return('uploads/tmp/test+.jpg')
@fog_file = @storage.cache!(file)
end
it "should upload the file", focus: true do
expect(@directory.files.get('uploads/tmp/test+.jpg').body).to eq('this is stuff')
end
it 'should preserve content type' do
expect(@fog_file.content_type).to eq(file.content_type)
end
end
describe '#retrieve_from_cache!' do
before do
@directory.files.create(:key => 'uploads/tmp/test.jpg', :body => 'A test, 1234', :public => true)
allow(@uploader).to receive(:cache_path).with('test.jpg').and_return('uploads/tmp/test.jpg')
@fog_file = @storage.retrieve_from_cache!('test.jpg')
end
it "should retrieve the file contents" do
expect(@fog_file.read.chomp).to eq("A test, 1234")
end
end
describe '#delete_dir' do
it "should do nothing" do
expect(running{ @storage.delete_dir!('foobar') }).not_to raise_error
end
end
describe '#clean_cache!' do
let(:today) { Time.now.round }
let(:five_days_ago) { today.ago(5.days) }
let(:three_days_ago) { today.ago(3.days) }
let(:yesterday) { today.yesterday }
before do
# clean up
@directory.files.each{|file| file.destroy }
# We can't use simple time freezing because of AWS request time check
[five_days_ago, three_days_ago, yesterday, (today - 1.minute)].each do |created_date|
key = nil
Timecop.freeze created_date do
key = "uploads/tmp/#{CarrierWave.generate_cache_id}/test.jpg"
end
@directory.files.create(:key => key, :body => 'A test, 1234', :public => true)
end
end
it "should clear all files older than now in the default cache directory" do
Timecop.freeze(today) do
@uploader.clean_cached_files!(0)
end
expect(@directory.files.all(:prefix => 'uploads/tmp').size).to eq(0)
end
it "should clear all files older than, by defaul, 24 hours in the default cache directory" do
Timecop.freeze(today) do
@uploader.clean_cached_files!
end
expect(@directory.files.all(:prefix => 'uploads/tmp').size).to eq(2)
end
it "should permit to set since how many seconds delete the cached files" do
Timecop.freeze(today) do
@uploader.clean_cached_files!(4.days)
end
expect(@directory.files.all(:prefix => 'uploads/tmp').size).to eq(3)
end
it "should be aliased on the CarrierWave module" do
Timecop.freeze(today) do
CarrierWave.clean_cached_files!
end
expect(@directory.files.all(:prefix => 'uploads/tmp').size).to eq(2)
end
end
describe 'fog_public' do
context "true" do
before do
directory_key = "#{CARRIERWAVE_DIRECTORY}public"
@directory = @storage.connection.directories.create(:key => directory_key, :public => true)
allow(@uploader).to receive(:fog_directory).and_return(directory_key)
allow(@uploader).to receive(:store_path).and_return('uploads/public.txt')
@fog_file = @storage.store!(file)
end
after do
@directory.files.new(:key => 'uploads/public.txt').destroy
@directory.files.new(:key => 'test.jpg').destroy
@directory.destroy
end
it "should be available at public URL" do
unless Fog.mocking? || fog_credentials[:provider] == 'Local'
expect(open(@fog_file.public_url).read).to eq('this is stuff')
end
end
end
context "false" do
before do
directory_key = "#{CARRIERWAVE_DIRECTORY}private"
@directory = @storage.connection.directories.create(:key => directory_key, :public => true)
allow(@uploader).to receive(:fog_directory).and_return(directory_key)
allow(@uploader).to receive(:fog_public).and_return(false)
allow(@uploader).to receive(:store_path).and_return('uploads/private.txt')
@fog_file = @storage.store!(file)
end
after do
@directory.files.new(:key => 'uploads/private.txt').destroy
@directory.files.new(:key => 'test.jpg').destroy
@directory.destroy
end
it "should not be available at public URL" do
unless Fog.mocking? || fog_credentials[:provider] == 'Local'
expect(running{ open(@fog_file.public_url) }).to raise_error OpenURI::HTTPError
end
end
it "should have an authenticated_url" do
if ['AWS', 'Rackspace', 'Google', 'OpenStack', 'AzureRM', 'Aliyun'].include?(@provider)
expect(@fog_file.authenticated_url).not_to be_nil
end
end
it "should have an custom authenticated_url" do
if ['AWS', 'Rackspace', 'Google', 'OpenStack', 'AzureRM'].include?(@provider)
timestamp = ::Fog::Time.now + 999
if @provider == "AWS"
expect(@fog_file.authenticated_url({expire_at: timestamp })).to include("Expires=999&")
elsif @provider == "Google"
expect(@fog_file.authenticated_url({expire_at: timestamp })).to include("Expires=#{timestamp.to_i}")
end
end
end
it 'should generate correct filename' do
expect(@fog_file.filename).to eq('private.txt')
end
it "should handle query params" do
if ['AWS', 'Google'].include?(@provider)
url = @fog_file.url(:query => {"response-content-disposition" => "attachment"})
expect(url).to match(/response-content-disposition=attachment/)
unless Fog.mocking?
# Workaround for S3 SignatureDoesNotMatch issue
# https://github.com/excon/excon/issues/475
Excon.defaults[:omit_default_port] = true
response = Excon.get(url)
expect(response.status).to be 200
expect(response.headers["Content-Disposition"]).to eq("attachment")
end
end
end
it "should not use #file to get signed url" do
if ['AWS', 'Google'].include?(@provider)
allow(@fog_file).to receive(:file).and_return(nil)
expect { @fog_file.url }.not_to raise_error
end
end
end
end
end
describe "with a valid Hash" do
let(:file) do
CarrierWave::SanitizedFile.new(
:tempfile => stub_merb_tempfile('test.jpg'),
:filename => 'test.jpg',
:content_type => 'image/jpeg'
)
end
it_should_behave_like "#{fog_credentials[:provider]} storage"
end
describe "with a valid Tempfile" do
let(:file) do
CarrierWave::SanitizedFile.new(stub_tempfile('test.jpg', 'image/jpeg'))
end
it_should_behave_like "#{fog_credentials[:provider]} storage"
end
describe "with a valid StringIO" do
let(:file) do
CarrierWave::SanitizedFile.new(stub_stringio('test.jpg', 'image/jpeg'))
end
it_should_behave_like "#{fog_credentials[:provider]} storage"
end
describe "with a valid File object" do
let(:file) do
CarrierWave::SanitizedFile.new(stub_file('test.jpg', 'image/jpeg'))
end
it_should_behave_like "#{fog_credentials[:provider]} storage"
end
describe "with a valid File object with an explicit content type" do
let(:file) do
CarrierWave::SanitizedFile.new(stub_file('test.jpg', 'image/jpeg')).tap do |f|
f.content_type = 'image/jpg'
end
end
it_should_behave_like "#{fog_credentials[:provider]} storage"
end
describe "with a valid path" do
let(:file) do
CarrierWave::SanitizedFile.new(file_path('test.jpg'))
end
it_should_behave_like "#{fog_credentials[:provider]} storage"
end
describe "with a valid Pathname" do
let(:file) do
CarrierWave::SanitizedFile.new(Pathname.new(file_path('test.jpg')))
end
it_should_behave_like "#{fog_credentials[:provider]} storage"
end
describe "with a CarrierWave::Storage::Fog::File" do
let(:file) do
CarrierWave::Storage::Fog::File.new(@uploader, @storage, 'test.jpg').
tap{|file| file.store(CarrierWave::SanitizedFile.new(
:tempfile => StringIO.new('this is stuff'), :content_type => 'image/jpeg')) }
end
it_should_behave_like "#{fog_credentials[:provider]} storage"
end
end
end