From 146c23521f947c62855080de5adeac828c6ff6cb Mon Sep 17 00:00:00 2001 From: Wayne Weibel Date: Thu, 5 Sep 2024 10:50:49 -0400 Subject: [PATCH] Pension 92172 BenefitsIntake Service metadata temp file --- lib/common/file_helpers.rb | 10 ++++++++++ lib/lighthouse/benefits_intake/service.rb | 5 +++-- spec/lib/lighthouse/benefits_intake/service_spec.rb | 8 +++----- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/common/file_helpers.rb b/lib/common/file_helpers.rb index 031f265b451..d42965877cd 100644 --- a/lib/common/file_helpers.rb +++ b/lib/common/file_helpers.rb @@ -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') diff --git a/lib/lighthouse/benefits_intake/service.rb b/lib/lighthouse/benefits_intake/service.rb index a4cd962c57f..7aca24ef10f 100644 --- a/lib/lighthouse/benefits_intake/service.rb +++ b/lib/lighthouse/benefits_intake/service.rb @@ -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') @@ -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 ## diff --git a/spec/lib/lighthouse/benefits_intake/service_spec.rb b/spec/lib/lighthouse/benefits_intake/service_spec.rb index 40a960fec2b..1ce0e6415cc 100644 --- a/spec/lib/lighthouse/benefits_intake/service_spec.rb +++ b/spec/lib/lighthouse/benefits_intake/service_spec.rb @@ -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') @@ -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