Skip to content

Commit

Permalink
Pension 92172 BenefitsIntake Service metadata temp file
Browse files Browse the repository at this point in the history
  • Loading branch information
wayne-weibel committed Sep 5, 2024
1 parent 142e25e commit 146c235
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
10 changes: 10 additions & 0 deletions lib/common/file_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ def random_file_path
"tmp/#{SecureRandom.hex}"
end

def generate_random_file(file_body)
file_path = random_file_path

File.open(file_path, 'wb') do |file|
file.write(file_body)
end

file_path
end

def generate_clamav_temp_file(file_body, file_name = nil)
file_name = SecureRandom.hex if file_name.nil?
clamav_directory = Rails.root.join('clamav_tmp')
Expand Down
5 changes: 3 additions & 2 deletions lib/lighthouse/benefits_intake/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ def perform_upload(metadata:, document:, attachments: [], upload_url: nil)
upload_url, _uuid = request_upload unless upload_url

metadata = JSON.parse(metadata)
meta_tmp = Common::FileHelpers.generate_clamav_temp_file(metadata.to_json,
"#{STATSD_KEY_PREFIX}.#{@uuid}.metadata.json")
meta_tmp = Common::FileHelpers.generate_random_file(metadata.to_json)

params = {}
params[:metadata] = Faraday::UploadIO.new(meta_tmp, Mime[:json].to_s, 'metadata.json')
Expand All @@ -58,6 +57,8 @@ def perform_upload(metadata:, document:, attachments: [], upload_url: nil)
end

perform :put, upload_url, params, { 'Content-Type' => 'multipart/form-data' }
ensure
Common::FileHelpers.delete_file_if_exists(meta_tmp) if meta_tmp
end

##
Expand Down
8 changes: 3 additions & 5 deletions spec/lib/lighthouse/benefits_intake/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,13 @@
service.instance_variable_set(:@location, 'location')
service.instance_variable_set(:@uuid, 'uuid')

allow(Common::FileHelpers).to receive(:generate_clamav_temp_file).and_return 'a-temp-file'
allow(Common::FileHelpers).to receive(:generate_random_file).and_return 'a-temp-file'
end

it 'performs the upload' do
allow(Faraday::UploadIO).to receive(:new).and_return 'a-file-io-object'

expect(Common::FileHelpers).to(
receive(:generate_clamav_temp_file).once.with(metadata.to_json, 'api.benefits_intake.uuid.metadata.json')
)
expect(Common::FileHelpers).to receive(:generate_random_file).once.with(metadata.to_json)

expect(Faraday::UploadIO).to receive(:new).once.with('a-temp-file', mime_json, 'metadata.json')
expect(Faraday::UploadIO).to receive(:new).once.with('file-path', mime_pdf, 'file-path')
Expand All @@ -94,7 +92,7 @@
it 'errors on invalid JSON metadata' do
args[:metadata] = 'not a json string'

expect(Common::FileHelpers).not_to receive(:generate_clamav_temp_file)
expect(Common::FileHelpers).not_to receive(:generate_random_file)
expect(service).not_to receive(:perform)
expect { service.perform_upload(**args) }.to raise_error JSON::ParserError
end
Expand Down

0 comments on commit 146c235

Please sign in to comment.