diff --git a/app/helpers/audio_visual_helper.rb b/app/helpers/audio_visual_helper.rb index 2346880a2..f7d548149 100644 --- a/app/helpers/audio_visual_helper.rb +++ b/app/helpers/audio_visual_helper.rb @@ -7,6 +7,10 @@ module AudioVisualHelper # @note For development environments, we need to subsitute the service hostname of S3 ('minio') # with 'localhost' for links to resolve. In production, 'AWS_ENDPOINT_URL's hostname is valid. def s3_url(key) + if ENV['AWS_ENDPOINT_URL'].blank? + Rails.logger.warn('AWS_ENDPOINT_URL environment variable is not defined.') + return "" + end client_opts = {} client_opts = { endpoint: ENV['AWS_ENDPOINT_URL']&.sub('minio', 'localhost') } if Rails.env.development? client = Aws::S3::Client.new(**client_opts) diff --git a/spec/helpers/audio_visual_helper_spec.rb b/spec/helpers/audio_visual_helper_spec.rb index 771ac97bd..f04de3ffb 100644 --- a/spec/helpers/audio_visual_helper_spec.rb +++ b/spec/helpers/audio_visual_helper_spec.rb @@ -11,78 +11,96 @@ before do stub_env('AWS_AV_ASSET_BUCKET', 'av-derivatives') - stub_env('AWS_ENDPOINT_URL', 'http://minio:9000') allow(Aws::S3::Object).to receive(:new).with(bucket_name: s3_bucket, key: key, client: mock_s3_client).and_return(mock_s3_object) allow(mock_s3_object).to receive(:presigned_url).with(:get, expires_in: 3600).and_return(url) end - context 'env is development' do - let(:client_opts) { { endpoint: "http://localhost:9000" } } - + context 'the env field does not exist' do before do - allow(Rails.env).to receive(:development?).and_return(true) - allow(Aws::S3::Client).to receive(:new).with(client_opts).and_return(mock_s3_client) + stub_env('AWS_ENDPOINT_URL', '') + allow(Rails.logger).to receive(:warn) end - context 'the object exists' do - before do - allow(mock_s3_object).to receive(:data).and_return("something") - helper.s3_url(key) - end - - it 'is expected to receive the client opts' do - expect(Aws::S3::Client).to have_received(:new).with(client_opts) - end + it 'logs a warning and returns the empty string' do + expect(helper.s3_url(key)).to eq "" + expect(Rails.logger).to have_received(:warn) + .with('AWS_ENDPOINT_URL environment variable is not defined.') + end + end - it { is_expected.to eq url } + context 'the env field exists' do + before do + stub_env('AWS_ENDPOINT_URL', 'http://minio:9000') end - context 'the object does not exist' do + context 'env is in development' do + let(:client_opts) { { endpoint: "http://localhost:9000" } } + before do - allow(mock_s3_object).to receive(:data).and_return(nil) - helper.s3_url(key) + allow(Rails.env).to receive(:development?).and_return(true) + allow(Aws::S3::Client).to receive(:new).with(client_opts).and_return(mock_s3_client) end - it 'is expected to receive the client opts' do - expect(Aws::S3::Client).to have_received(:new).with(client_opts) + context 'the object exists' do + before do + allow(mock_s3_object).to receive(:data).and_return("something") + helper.s3_url(key) + end + + it 'is expected to receive the client opts' do + expect(Aws::S3::Client).to have_received(:new).with(client_opts) + end + + it { is_expected.to eq url } end - it { is_expected.to eq "" } - end - end + context 'the object does not exist' do + before do + allow(mock_s3_object).to receive(:data).and_return(nil) + helper.s3_url(key) + end - context 'env is development' do - let(:client_opts) { {} } + it 'is expected to receive the client opts' do + expect(Aws::S3::Client).to have_received(:new).with(client_opts) + end - before do - allow(Rails.env).to receive(:development?).and_return(false) - allow(Aws::S3::Client).to receive(:new).with(client_opts).and_return(mock_s3_client) + it { is_expected.to eq "" } + end end - context 'the object exists' do + context 'env is not in development' do + let(:client_opts) { {} } + before do - allow(mock_s3_object).to receive(:data).and_return("something") - helper.s3_url(key) + allow(Rails.env).to receive(:development?).and_return(false) + allow(Aws::S3::Client).to receive(:new).with(client_opts).and_return(mock_s3_client) end - it 'is expected to receive the empty client opts' do - expect(Aws::S3::Client).to have_received(:new).with(client_opts) - end + context 'the object exists' do + before do + allow(mock_s3_object).to receive(:data).and_return("something") + helper.s3_url(key) + end - it { is_expected.to eq url } - end + it 'is expected to receive the empty client opts' do + expect(Aws::S3::Client).to have_received(:new).with(client_opts) + end - context 'the object does not exist' do - before do - allow(mock_s3_object).to receive(:data).and_return(nil) - helper.s3_url(key) + it { is_expected.to eq url } end - it 'is expected to receive the empty client opts' do - expect(Aws::S3::Client).to have_received(:new).with(client_opts) - end + context 'the object does not exist' do + before do + allow(mock_s3_object).to receive(:data).and_return(nil) + helper.s3_url(key) + end - it { is_expected.to eq "" } + it 'is expected to receive the empty client opts' do + expect(Aws::S3::Client).to have_received(:new).with(client_opts) + end + + it { is_expected.to eq "" } + end end end end