From 4ee9a78fef997ceb5b01a1babefdad17f286299f Mon Sep 17 00:00:00 2001 From: Trevor Bosaw Date: Fri, 12 Apr 2024 11:10:02 -0700 Subject: [PATCH] Fixing flaky specs related to pdf generation (#16310) --- .../concerns/mhv_fhir_session_client.rb | 1 + .../spec/requests/v1/uploads_spec.rb | 26 ++++-------- .../virtual_agent/virtual_agent_claim_spec.rb | 36 +++++------------ .../concerns/mhv_fhir_session_client_spec.rb | 2 +- .../service_spec.rb | 40 ++++++++++--------- 5 files changed, 43 insertions(+), 62 deletions(-) diff --git a/lib/common/client/concerns/mhv_fhir_session_client.rb b/lib/common/client/concerns/mhv_fhir_session_client.rb index 389d9739e60..a7d65a7dd4e 100644 --- a/lib/common/client/concerns/mhv_fhir_session_client.rb +++ b/lib/common/client/concerns/mhv_fhir_session_client.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'common/client/concerns/mhv_jwt_session_client' +require 'medical_records/patient_not_found' module Common module Client diff --git a/modules/simple_forms_api/spec/requests/v1/uploads_spec.rb b/modules/simple_forms_api/spec/requests/v1/uploads_spec.rb index 2d8c3c709b2..7663eac253f 100644 --- a/modules/simple_forms_api/spec/requests/v1/uploads_spec.rb +++ b/modules/simple_forms_api/spec/requests/v1/uploads_spec.rb @@ -2,6 +2,7 @@ require 'rails_helper' require 'simple_forms_api_submission/metadata_validator' +require 'common/file_helpers' RSpec.describe 'Forms uploader', type: :request do non_ivc_forms = [ @@ -30,6 +31,13 @@ ] describe '#submit' do + let(:metadata_file) { "#{file_seed}.SimpleFormsApi.metadata.json" } + let(:file_seed) { 'tmp/some-unique-simple-forms-file-seed' } + + before { allow(Common::FileHelpers).to receive(:random_file_path).and_return(file_seed) } + + after { Common::FileHelpers.delete_file_if_exists(metadata_file) } + non_ivc_forms.each do |form| fixture_path = Rails.root.join('modules', 'simple_forms_api', 'spec', 'fixtures', 'form_json', form) data = JSON.parse(fixture_path.read) @@ -43,9 +51,6 @@ expect(SimpleFormsApiSubmission::MetadataValidator).to have_received(:validate) expect(response).to have_http_status(:ok) - ensure - metadata_file = Dir['tmp/*.SimpleFormsApi.metadata.json'][0] - Common::FileHelpers.delete_file_if_exists(metadata_file) if defined?(metadata_file) end end end @@ -58,9 +63,6 @@ expect do post '/simple_forms_api/v1/simple_forms', params: data end.to change(FormSubmissionAttempt, :count).by(1) - ensure - metadata_file = Dir['tmp/*.SimpleFormsApi.metadata.json'][0] - Common::FileHelpers.delete_file_if_exists(metadata_file) if defined?(metadata_file) end end end @@ -85,9 +87,6 @@ expect do post '/simple_forms_api/v1/simple_forms', params: data end.to change(InProgressForm, :count).by(-1) - ensure - metadata_file = Dir['tmp/*.SimpleFormsApi.metadata.json'][0] - Common::FileHelpers.delete_file_if_exists(metadata_file) if defined?(metadata_file) end end end @@ -199,9 +198,6 @@ expect_any_instance_of(SimpleFormsApi::PdfFiller).to receive(:generate).with(3) post '/simple_forms_api/v1/simple_forms', params: data - ensure - metadata_file = Dir['tmp/*.SimpleFormsApi.metadata.json'][0] - Common::FileHelpers.delete_file_if_exists(metadata_file) if defined?(metadata_file) end end end @@ -223,9 +219,6 @@ post '/simple_forms_api/v1/simple_forms', params: data expect(response).to have_http_status(:ok) - ensure - metadata_file = Dir['tmp/*.SimpleFormsApi.metadata.json'][0] - Common::FileHelpers.delete_file_if_exists(metadata_file) if defined?(metadata_file) end end end @@ -242,9 +235,6 @@ expect(PersistentAttachment).to receive(:where).with(guid: ['a-random-uuid']).and_return([attachment]) post '/simple_forms_api/v1/simple_forms', params: data expect(response).to have_http_status(:ok) - ensure - metadata_file = Dir['tmp/*.SimpleFormsApi.metadata.json'][0] - Common::FileHelpers.delete_file_if_exists(metadata_file) if defined?(metadata_file) end end end diff --git a/spec/controllers/v0/virtual_agent/virtual_agent_claim_spec.rb b/spec/controllers/v0/virtual_agent/virtual_agent_claim_spec.rb index 251c50b5c2b..5a8c675c2fd 100644 --- a/spec/controllers/v0/virtual_agent/virtual_agent_claim_spec.rb +++ b/spec/controllers/v0/virtual_agent/virtual_agent_claim_spec.rb @@ -19,14 +19,9 @@ end describe 'GET /v0/virtual_agent/claim' do - let!(:claim) do - FactoryBot.create(:evss_claim, id: 11, evss_id: 600_118_854, - user_uuid: user.uuid) - FactoryBot.create(:evss_claim, id: 22, evss_id: 600_118_855, - user_uuid: user.uuid) - FactoryBot.create(:evss_claim, id: 33, evss_id: 600_118_851, - user_uuid: user.uuid) - end + let!(:claim1) { FactoryBot.create(:evss_claim, evss_id: 600_118_854, user_uuid: user.uuid) } + let!(:claim2) { FactoryBot.create(:evss_claim, evss_id: 600_118_855, user_uuid: user.uuid) } + let!(:claim3) { FactoryBot.create(:evss_claim, evss_id: 600_118_851, user_uuid: user.uuid) } it 'returns information on multiple open compensation claims in descending chronological order by updated date' do sign_in_as(user) @@ -41,13 +36,13 @@ EVSS::RetrieveClaimsFromRemoteJob.new.perform(user.uuid) end VCR.use_cassette('evss/claims/claim_with_docs1') do - EVSS::UpdateClaimFromRemoteJob.new.perform(user.uuid, 11) + EVSS::UpdateClaimFromRemoteJob.new.perform(user.uuid, claim1.id) end VCR.use_cassette('evss/claims/claim_with_docs2') do - EVSS::UpdateClaimFromRemoteJob.new.perform(user.uuid, 22) + EVSS::UpdateClaimFromRemoteJob.new.perform(user.uuid, claim2.id) end VCR.use_cassette('evss/claims/claim_with_docs3') do - EVSS::UpdateClaimFromRemoteJob.new.perform(user.uuid, 33) + EVSS::UpdateClaimFromRemoteJob.new.perform(user.uuid, claim3.id) end get '/v0/virtual_agent/claim' @@ -81,10 +76,7 @@ end describe 'for a single claim' do - let!(:claim) do - FactoryBot.create(:evss_claim, id: 3, evss_id: 600_118_851, - user_uuid: user.uuid) - end + let!(:claim) { FactoryBot.create(:evss_claim, evss_id: 600_118_851, user_uuid: user.uuid) } it 'returns information on single open compensation claim' do sign_in_as(user) @@ -99,7 +91,7 @@ EVSS::RetrieveClaimsFromRemoteJob.new.perform(user.uuid) end VCR.use_cassette('evss/claims/claim_with_docs1') do - EVSS::UpdateClaimFromRemoteJob.new.perform(user.uuid, 3) + EVSS::UpdateClaimFromRemoteJob.new.perform(user.uuid, claim.id) end get '/v0/virtual_agent/claim' @@ -183,10 +175,7 @@ end describe 'for a user who has non-compensation and compensation claims' do - let!(:claim) do - FactoryBot.create(:evss_claim, id: 3, evss_id: 600_114_693, - user_uuid: user.uuid) - end + let!(:claim) { FactoryBot.create(:evss_claim, evss_id: 600_114_693, user_uuid: user.uuid) } it 'returns information when there is a more recent non-compensation open claim' do sign_in_as(user) @@ -200,7 +189,7 @@ EVSS::RetrieveClaimsFromRemoteJob.new.perform(user.uuid) end VCR.use_cassette('evss/claims/claim_with_docs4') do - EVSS::UpdateClaimFromRemoteJob.new.perform(user.uuid, 3) + EVSS::UpdateClaimFromRemoteJob.new.perform(user.uuid, claim.id) end get '/v0/virtual_agent/claim' @@ -218,10 +207,7 @@ end describe 'GET /v0/virtual_agent/claim/{EVSS_ID}' do - let!(:claim) do - FactoryBot.create(:evss_claim, id: 1, evss_id: 600_117_255, - user_uuid: user.uuid) - end + let!(:claim) { FactoryBot.create(:evss_claim, evss_id: 600_117_255, user_uuid: user.uuid) } it 'returns claims details of a specific claim' do sign_in_as(user) diff --git a/spec/lib/common/client/concerns/mhv_fhir_session_client_spec.rb b/spec/lib/common/client/concerns/mhv_fhir_session_client_spec.rb index 88271047aef..a9371307fd9 100644 --- a/spec/lib/common/client/concerns/mhv_fhir_session_client_spec.rb +++ b/spec/lib/common/client/concerns/mhv_fhir_session_client_spec.rb @@ -2,7 +2,7 @@ require 'rails_helper' require 'medical_records/client_session' -require_relative '../../../../../lib/common/client/concerns/mhv_fhir_session_client' +require 'common/client/concerns/mhv_fhir_session_client' describe Common::Client::Concerns::MhvFhirSessionClient do let(:dummy_class) do diff --git a/spec/lib/simple_forms_api_submission/service_spec.rb b/spec/lib/simple_forms_api_submission/service_spec.rb index 6864c3532cc..f7495a07983 100644 --- a/spec/lib/simple_forms_api_submission/service_spec.rb +++ b/spec/lib/simple_forms_api_submission/service_spec.rb @@ -5,47 +5,51 @@ require 'simple_forms_api_submission/service' describe SimpleFormsApiSubmission::Service do - mock_metadata = ' - {"veteranFirstName"=>"Veteran", "veteranLastName"=>"Surname", "fileNumber"=>"123456789", - "zipCode"=>"12345", "source"=>"test", "docType"=>"test", "businessLine"=>"OTH"} - ' - - before(:all) do - @service = SimpleFormsApiSubmission::Service.new + let(:mock_metadata) do + '{"veteranFirstName"=>"Veteran", "veteranLastName"=>"Surname", "fileNumber"=>"123456789", + "zipCode"=>"12345", "source"=>"test", "docType"=>"test", "businessLine"=>"OTH"}' end + let(:simple_forms_service) { SimpleFormsApiSubmission::Service.new } + let(:file_seed) { 'some-unique-simple-forms-service-spec-file-seed' } + + before { allow(SecureRandom).to receive(:hex).and_return(file_seed) } + describe 'get uuid and upload location' do it 'retrieves uuid and upload location from the Lighthouse API' do VCR.use_cassette('lighthouse/benefits_intake/200_lighthouse_intake_upload_location') do - response = @service.get_upload_location + response = simple_forms_service.get_upload_location expect(response.status).to equal(200) end end end describe 'generate metadata file' do + let(:mock_file_path_metadata) { "#{file_seed}.SimpleFormsApi.metadata.json" } + it 'generates a json file from the metadata' do - @service.generate_tmp_metadata(mock_metadata) + simple_forms_service.generate_tmp_metadata(mock_metadata) expect(Dir['tmp/*.SimpleFormsApi.metadata.json'].any?).to equal(true) ensure - metadata_file = Dir['tmp/*.SimpleFormsApi.metadata.json'][0] - Common::FileHelpers.delete_file_if_exists(metadata_file) if defined?(metadata_file) + Common::FileHelpers.delete_file_if_exists(mock_file_path_metadata) end end describe 'upload doc' do + let(:mock_file) { Common::FileHelpers.random_file_path } + let(:mock_file_path_pdf) { "#{mock_file}-mock-upload.pdf" } + let(:mock_file_path_metadata) { "#{mock_file}.SimpleFormsApi.metadata.json" } + it 'upload doc to mock location' do VCR.use_cassette('lighthouse/benefits_intake/200_lighthouse_intake_upload') do mock_upload_url = 'https://sandbox-api.va.gov/services_user_content/vba_documents/id-path-doesnt-matter' - mock_file_path = "#{Common::FileHelpers.random_file_path}-mock-upload.pdf" - Prawn::Document.new.render_file mock_file_path - response = @service.upload_doc(upload_url: mock_upload_url, file: mock_file_path, metadata: mock_metadata) + Prawn::Document.new.render_file mock_file_path_pdf + response = simple_forms_service.upload_doc(upload_url: mock_upload_url, + file: mock_file_path_pdf, + metadata: mock_metadata) expect(response.status).to equal(200) ensure - metadata_file = Dir['tmp/*.SimpleFormsApi.metadata.json'][0] - pdf_file = Dir['tmp/*-mock-upload.pdf'][0] - Common::FileHelpers.delete_file_if_exists(metadata_file) if defined?(metadata_file) - Common::FileHelpers.delete_file_if_exists(pdf_file) if defined?(pdf_file) + Common::FileHelpers.delete_file_if_exists(mock_file_path_pdf) end end end