diff --git a/modules/ask_va_api/app/lib/ask_va_api/correspondences/creator.rb b/modules/ask_va_api/app/lib/ask_va_api/correspondences/creator.rb index 9cdf1ad1759..3e57c1fd81a 100644 --- a/modules/ask_va_api/app/lib/ask_va_api/correspondences/creator.rb +++ b/modules/ask_va_api/app/lib/ask_va_api/correspondences/creator.rb @@ -35,7 +35,8 @@ def post_data(payload: {}) def handle_response_data(response) if response[:Data].nil? - raise CorrespondencesCreatorError, response[:Message] + error = JSON.parse(response[:body], symbolize_names: true) + raise CorrespondencesCreatorError, error[:Message] else response[:Data] end diff --git a/modules/ask_va_api/app/lib/ask_va_api/inquiries/creator.rb b/modules/ask_va_api/app/lib/ask_va_api/inquiries/creator.rb index 5accb20ee13..918601ea690 100644 --- a/modules/ask_va_api/app/lib/ask_va_api/inquiries/creator.rb +++ b/modules/ask_va_api/app/lib/ask_va_api/inquiries/creator.rb @@ -32,7 +32,8 @@ def post_data(payload: {}) def handle_response_data(response) if response[:Data].nil? - raise InquiriesCreatorError, response[:Message] + error = JSON.parse(response[:body], symbolize_names: true) + raise InquiriesCreatorError, error[:Message] else response[:Data] end diff --git a/modules/ask_va_api/spec/app/lib/ask_va_api/correspondences/creator_spec.rb b/modules/ask_va_api/spec/app/lib/ask_va_api/correspondences/creator_spec.rb index 6136b5c1609..bea0c25bd50 100644 --- a/modules/ask_va_api/spec/app/lib/ask_va_api/correspondences/creator_spec.rb +++ b/modules/ask_va_api/spec/app/lib/ask_va_api/correspondences/creator_spec.rb @@ -22,9 +22,23 @@ module Correspondences end context 'when not successful' do + let(:body) do + '{"Data":null,"Message":"Data Validation: Missing Reply"' \ + ',"ExceptionOccurred":true,"ExceptionMessage":"Data Validation: ' \ + 'Missing Reply","MessageId":"e2cbe041-df91-41f4-8bd2-8b6d9dbb2e38"}' + 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_any_instance_of(Crm::Service).to receive(:call).and_return({ Data: nil, Message: 'Error has occur' }) + allow_any_instance_of(Crm::Service).to receive(:call).and_return(failure) end it 'raise CorrespondenceCreatorError' do diff --git a/modules/ask_va_api/spec/app/lib/ask_va_api/inquiries/creator_spec.rb b/modules/ask_va_api/spec/app/lib/ask_va_api/inquiries/creator_spec.rb index 91029a32a3d..793ebba7057 100644 --- a/modules/ask_va_api/spec/app/lib/ask_va_api/inquiries/creator_spec.rb +++ b/modules/ask_va_api/spec/app/lib/ask_va_api/inquiries/creator_spec.rb @@ -36,12 +36,22 @@ end context 'when the API call fails' do + let(:body) do + '{"Data":null,"Message":"Data Validation: missing InquiryCategory"' \ + ',"ExceptionOccurred":true,"ExceptionMessage":"Data Validation: missing' \ + 'InquiryCategory","MessageId":"cb0dd954-ef25-4e56-b0d9-41925e5a190c"}' + end + let(:failure) do + { + status: 400, + body:, + response_headers: nil, + url: nil + } + end + before do - allow(service).to receive(:call).and_return({ Data: nil, - Message: 'Data Validation: missing InquiryCategory', - ExceptionOccurred: true, - ExceptionMessage: 'Data Validation: missing InquiryCategory', - MessageId: '13bc59ea-c90a-4d48-8979-fe71e0f7ddeb' }) + allow(service).to receive(:call).and_return(failure) end it 'raise InquiriesCreatorError' 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 3d0a6216ddc..25ff307fb6f 100644 --- a/modules/ask_va_api/spec/requests/v0/inquiries_spec.rb +++ b/modules/ask_va_api/spec/requests/v0/inquiries_spec.rb @@ -357,14 +357,24 @@ context 'when crm api fail' do context 'when the API call fails' do + let(:body) do + '{"Data":null,"Message":"Data Validation: missing InquiryCategory"' \ + ',"ExceptionOccurred":true,"ExceptionMessage":"Data Validation: missing' \ + 'InquiryCategory","MessageId":"cb0dd954-ef25-4e56-b0d9-41925e5a190c"}' + end + let(:failure) do + { + status: 400, + body:, + response_headers: nil, + url: nil + } + end + before do allow_any_instance_of(Crm::Service).to receive(:call) .with(endpoint:, method: :put, - payload:).and_return({ Data: nil, - Message: 'Data Validation: missing InquiryCategory', - ExceptionOccurred: true, - ExceptionMessage: 'Data Validation: missing InquiryCategory', - MessageId: '13bc59ea-c90a-4d48-8979-fe71e0f7ddeb' }) + payload:).and_return(failure) sign_in(authorized_user) post '/ask_va_api/v0/inquiries/auth', params: payload end @@ -404,14 +414,24 @@ context 'when crm api fail' do context 'when the API call fails' do + let(:body) do + '{"Data":null,"Message":"Data Validation: missing InquiryCategory"' \ + ',"ExceptionOccurred":true,"ExceptionMessage":"Data Validation: missing' \ + 'InquiryCategory","MessageId":"cb0dd954-ef25-4e56-b0d9-41925e5a190c"}' + end + let(:failure) do + { + status: 400, + body:, + response_headers: nil, + url: nil + } + end + before do allow_any_instance_of(Crm::Service).to receive(:call) .with(endpoint:, method: :put, - payload:).and_return({ Data: nil, - Message: 'Data Validation: missing InquiryCategory', - ExceptionOccurred: true, - ExceptionMessage: 'Data Validation: missing InquiryCategory', - MessageId: '13bc59ea-c90a-4d48-8979-fe71e0f7ddeb' }) + payload:).and_return(failure) post '/ask_va_api/v0/inquiries', params: payload end @@ -472,7 +492,7 @@ def json_response describe 'POST #test_create' do before do - allow_any_instance_of(Crm::Service).to receive(:call).and_return({ message: 'success' }) + allow_any_instance_of(Crm::Service).to receive(:call).and_return({ body: { message: 'success' } }) post '/ask_va_api/v0/test_create', params: { 'reply' => 'test', 'endpoint' => 'inquiries/id/reply/new' }, as: :json @@ -486,14 +506,50 @@ def json_response describe 'POST #create_reply' do let(:payload) { { 'reply' => 'this is my reply' } } - before do - allow_any_instance_of(Crm::Service).to receive(:call).and_return({ Data: { Id: '123' } }) - sign_in(authorized_user) - post '/ask_va_api/v0/inquiries/123/reply/new', params: payload + context 'when successful' do + before do + allow_any_instance_of(Crm::Service).to receive(:call).and_return({ Data: { Id: '123' } }) + sign_in(authorized_user) + post '/ask_va_api/v0/inquiries/123/reply/new', params: payload + end + + it 'returns status 200' do + expect(response).to have_http_status(:ok) + end end - it 'returns status 200' do - expect(response).to have_http_status(:ok) + context 'when crm api fail' do + context 'when the API call fails' do + let(:endpoint) { 'inquiries/123/reply/new' } + let(:body) do + '{"Data":null,"Message":"Data Validation: Missing Reply"' \ + ',"ExceptionOccurred":true,"ExceptionMessage":"Data Validation: ' \ + 'Missing Reply","MessageId":"e2cbe041-df91-41f4-8bd2-8b6d9dbb2e38"}' + end + let(:failure) do + { + status: 400, + body:, + response_headers: nil, + url: nil + } + end + + before do + sign_in(authorized_user) + allow_any_instance_of(Crm::Service).to receive(:call) + .with(endpoint:, method: :put, + payload: { Reply: 'this is my reply' }).and_return(failure) + post '/ask_va_api/v0/inquiries/123/reply/new', params: payload + end + + it 'raise InquiriesCreatorError' do + expect(response).to have_http_status(:unprocessable_entity) + end + + it_behaves_like 'common error handling', :unprocessable_entity, 'service_error', + 'AskVAApi::Correspondences::CorrespondencesCreatorError: Data Validation: Missing Reply' + end end end end