diff --git a/modules/ask_va_api/app/controllers/ask_va_api/v0/inquiries_controller.rb b/modules/ask_va_api/app/controllers/ask_va_api/v0/inquiries_controller.rb index 9cc82b5a60e..12f91508ed1 100644 --- a/modules/ask_va_api/app/controllers/ask_va_api/v0/inquiries_controller.rb +++ b/modules/ask_va_api/app/controllers/ask_va_api/v0/inquiries_controller.rb @@ -6,7 +6,7 @@ class InquiriesController < ApplicationController around_action :handle_exceptions before_action :get_inquiries_by_icn, only: [:index] before_action :get_inquiry_by_id, only: [:show] - skip_before_action :authenticate, only: %i[unauth_create upload_attachment test_create] + skip_before_action :authenticate, only: %i[unauth_create upload_attachment test_create show] skip_before_action :verify_authenticity_token, only: %i[unauth_create upload_attachment test_create] def index @@ -63,6 +63,8 @@ def create_reply private def get_inquiry_by_id + entity_class = AskVAApi::Inquiries::Entity + retriever = Inquiries::Retriever.new(user_mock_data: params[:mock], entity_class:) inq = retriever.fetch_by_id(id: params[:id]) @inquiry = Result.new(payload: Inquiries::Serializer.new(inq).serializable_hash, status: :ok) end diff --git a/modules/ask_va_api/app/lib/ask_va_api/inquiries/retriever.rb b/modules/ask_va_api/app/lib/ask_va_api/inquiries/retriever.rb index 0951d724eb8..073c35f8832 100644 --- a/modules/ask_va_api/app/lib/ask_va_api/inquiries/retriever.rb +++ b/modules/ask_va_api/app/lib/ask_va_api/inquiries/retriever.rb @@ -57,7 +57,12 @@ def filter_data(data, id = nil) end def handle_response_data(response) - response[:Data].presence || raise(InquiriesRetrieverError, response[:Message]) + if response[:Data].nil? + error = JSON.parse(response[:body], symbolize_names: true) + raise InquiriesRetrieverError, error[:Message] + else + response[:Data] + end end end end diff --git a/modules/ask_va_api/spec/app/lib/ask_va_api/inquiries/retriever_spec.rb b/modules/ask_va_api/spec/app/lib/ask_va_api/inquiries/retriever_spec.rb index b848c81cb93..ee1f4e45987 100644 --- a/modules/ask_va_api/spec/app/lib/ask_va_api/inquiries/retriever_spec.rb +++ b/modules/ask_va_api/spec/app/lib/ask_va_api/inquiries/retriever_spec.rb @@ -20,17 +20,23 @@ describe '#call' do context 'when Crm raise an error' do let(:icn) { '123' } - let(:response) do - { Data: nil, - Message: 'Data Validation: No Contact found by ICN', - ExceptionOccurred: true, - ExceptionMessage: 'Data Validation: No Contact found by ICN', - MessageId: '2733ca25-7e64-4fbc-af2c-366f4bd2e3dc' } + let(:body) do + '{"Data":null,"Message":"Data Validation: No Inquiries found by ID A-20240423-30709"' \ + ',"ExceptionOccurred":true,"ExceptionMessage":"Data Validation: No Inquiries found by ' \ + 'ID A-20240423-30709","MessageId":"ca5b990a-63fe-407d-a364-46caffce12c1"}' + end + let(:failure) do + { + status: 400, + body:, + response_headers: nil, + url: nil + } end before do allow_any_instance_of(Crm::CrmToken).to receive(:call).and_return('Token') - allow(service).to receive(:call).and_return(response) + allow(service).to receive(:call).and_return(failure) end it 'raise CorrespondenceRetrieverrError' do diff --git a/modules/ask_va_api/spec/requests/v0/inquiries_spec.rb b/modules/ask_va_api/spec/requests/v0/inquiries_spec.rb index 25ff307fb6f..03eec8510ca 100644 --- a/modules/ask_va_api/spec/requests/v0/inquiries_spec.rb +++ b/modules/ask_va_api/spec/requests/v0/inquiries_spec.rb @@ -207,19 +207,25 @@ end context 'when the id is invalid' do - let(:crm_response) do - { Data: nil, - Message: 'Data Validation: No Inquiries found by ID A-20230305-30617', - ExceptionOccurred: true, - ExceptionMessage: 'Data Validation: No Inquiries found by ID A-20230305-30617', - MessageId: 'e6024ccb-e19b-4bc6-990c-667e7ebab4ec' } + let(:body) do + '{"Data":null,"Message":"Data Validation: No Inquiries found by ID A-20240423-30709"' \ + ',"ExceptionOccurred":true,"ExceptionMessage":"Data Validation: No Inquiries found by ' \ + 'ID A-20240423-30709","MessageId":"ca5b990a-63fe-407d-a364-46caffce12c1"}' + end + let(:failure) do + { + status: 400, + body:, + response_headers: nil, + url: nil + } end let(:service) { instance_double(Crm::Service) } before do allow(Crm::Service).to receive(:new).and_return(service) allow_any_instance_of(Crm::CrmToken).to receive(:call).and_return('Token') - allow(service).to receive(:call).and_return(crm_response) + allow(service).to receive(:call).and_return(failure) sign_in(authorized_user) get "#{inquiry_path}/#{invalid_id}" end @@ -228,17 +234,9 @@ it_behaves_like 'common error handling', :unprocessable_entity, 'service_error', 'AskVAApi::Inquiries::InquiriesRetrieverError: ' \ - 'Data Validation: No Inquiries found by ID A-20230305-30617' + 'Data Validation: No Inquiries found by ID A-20240423-30709' end end - - context 'when user is not signed in' do - before do - get "#{inquiry_path}/#{valid_id}" - end - - it { expect(response).to have_http_status(:unauthorized) } - end end describe 'GET #download_attachment' do