Skip to content

Commit

Permalink
s3 url tests take 3
Browse files Browse the repository at this point in the history
  • Loading branch information
jwellnit committed Sep 11, 2024
1 parent 254a941 commit 2853a15
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 45 deletions.
4 changes: 4 additions & 0 deletions app/helpers/audio_visual_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
108 changes: 63 additions & 45 deletions spec/helpers/audio_visual_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2853a15

Please sign in to comment.