Skip to content

Commit 04bfab3

Browse files
committed
Follow-up for #2763
- Raises an error instead of returning nil - Reorganize spec/storage/fog_helper.rb so it doesn't have high cyclomatic complexity
1 parent 054bd3d commit 04bfab3

File tree

4 files changed

+10
-15
lines changed

4 files changed

+10
-15
lines changed

.rubocop.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Metrics/ClassLength:
9090
Enabled: false
9191

9292
Metrics/CyclomaticComplexity:
93-
Max: 28
93+
Max: 18
9494

9595
Metrics/MethodLength:
9696
Enabled: false

lib/carrierwave/storage/fog.rb

+3-8
Original file line numberDiff line numberDiff line change
@@ -376,21 +376,15 @@ def public_url
376376
when 'AWS'
377377
# check if some endpoint is set in fog_credentials
378378
if @uploader.fog_credentials.has_key?(:endpoint)
379-
if !@uploader.fog_aws_fips
380-
"#{@uploader.fog_credentials[:endpoint]}/#{@uploader.fog_directory}/#{encoded_path}"
381-
else
382-
warn 'Use of options :endpoint and :fog_aws_fips=true together will fail, as FIPS endpoints do not support path-style URLs.'
383-
nil
384-
end
379+
raise 'fog_aws_fips = true is incompatible with :endpoint, as FIPS endpoints do not support path-style URLs.' if @uploader.fog_aws_fips
380+
"#{@uploader.fog_credentials[:endpoint]}/#{@uploader.fog_directory}/#{encoded_path}"
385381
else
386382
protocol = @uploader.fog_use_ssl_for_aws ? "https" : "http"
387383

388384
subdomain_regex = /^(?:[a-z]|\d(?!\d{0,2}(?:\d{1,3}){3}$))(?:[a-z0-9\.]|(?![\-])|\-(?![\.])){1,61}[a-z0-9]$/
389385
# To use the virtual-hosted style, the bucket name needs to be representable as a subdomain
390386
use_virtual_hosted_style = @uploader.fog_directory.to_s =~ subdomain_regex && !(protocol == 'https' && @uploader.fog_directory =~ /\./)
391387

392-
return nil if !use_virtual_hosted_style && @uploader.fog_aws_fips # FIPS Endpoints can only be used with Virtual Hosted-Style addressing.
393-
394388
region = @uploader.fog_credentials[:region].to_s
395389
regional_host = 's3.amazonaws.com' # used for DEFAULT_S3_REGION or no region set
396390
if @uploader.fog_aws_fips
@@ -403,6 +397,7 @@ def public_url
403397
regional_host = 's3-accelerate.amazonaws.com' if @uploader.fog_aws_accelerate
404398
"#{protocol}://#{@uploader.fog_directory}.#{regional_host}/#{encoded_path}"
405399
else # directory is not a valid subdomain, so use path style for access
400+
raise 'FIPS Endpoints can only be used with Virtual Hosted-Style addressing.' if @uploader.fog_aws_fips
406401
"#{protocol}://#{regional_host}/#{@uploader.fog_directory}/#{encoded_path}"
407402
end
408403
end

spec/storage/fog_helper.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
def fog_tests(fog_credentials)
1+
shared_examples "Fog storage" do |fog_credentials|
22
describe "with #{fog_credentials[:provider]} provider", with_retry: !Fog.mocking do
33
before do
44
WebMock.disable! unless Fog.mocking?
@@ -506,11 +506,11 @@ def check_file
506506
expect(@fog_file.public_url).to include("https://#{CARRIERWAVE_DIRECTORY}.s3-accelerate.amazonaws.com")
507507
end
508508

509-
it 'returns nil when both :endpoint and :fog_aws_fips=true' do
509+
it 'raises an error when both :endpoint and :fog_aws_fips=true' do
510510
allow(@uploader).to receive(:fog_credentials).and_return(@uploader.fog_credentials.merge(endpoint: 'https://custom-endpoint.example.com'))
511511
allow(@uploader).to receive(:fog_directory).and_return('SiteAssets')
512512
allow(@uploader).to receive(:fog_aws_fips).and_return(true)
513-
expect(@fog_file.url).to be nil
513+
expect { @fog_file.url }.to raise_error RuntimeError, /incompatible/
514514
end
515515

516516
it 'returns endpoint+bucket when :endpoint and !:fog_aws_fips' do
@@ -562,13 +562,13 @@ def check_file
562562
'us-east-2',
563563
'us-gov-west-1'
564564
].each do |region|
565-
it "public_url should be nil" do
565+
it "raises an error" do
566566
allow(@uploader).to receive(:fog_use_ssl_for_aws).and_return(true)
567567
allow(@uploader).to receive(:fog_directory).and_return('foo.bar')
568568
allow(@uploader).to receive(:fog_aws_fips).and_return(true)
569569
allow(@uploader).to receive(:fog_credentials).and_return(@uploader.fog_credentials.merge(region: region))
570570

571-
expect(@fog_file.public_url).to be_nil
571+
expect { @fog_file.url }.to raise_error RuntimeError, /Virtual Hosted-Style/
572572
end
573573
end
574574
end

spec/storage/fog_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
describe CarrierWave::Storage::Fog do
1919
FOG_CREDENTIALS.each do |credential|
20-
fog_tests(credential)
20+
include_examples 'Fog storage', credential
2121
end
2222

2323
describe '.eager_load' do

0 commit comments

Comments
 (0)