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 12f91508ed1..075985d3d5c 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 @@ -17,14 +17,6 @@ def show render json: @inquiry.payload, status: @inquiry.status end - def test_create - service = Crm::Service.new(icn: nil) - payload = { reply: params[:reply] } - response = service.call(endpoint: params[:endpoint], method: :put, payload:) - - render json: response.to_json, status: :ok - end - def create response = Inquiries::Creator.new(icn: current_user.icn).call(payload: inquiry_params) render json: response.to_json, status: :created diff --git a/modules/ask_va_api/app/controllers/ask_va_api/v0/static_data_controller.rb b/modules/ask_va_api/app/controllers/ask_va_api/v0/static_data_controller.rb index 94b79ea9234..ecff99a6a06 100644 --- a/modules/ask_va_api/app/controllers/ask_va_api/v0/static_data_controller.rb +++ b/modules/ask_va_api/app/controllers/ask_va_api/v0/static_data_controller.rb @@ -6,17 +6,6 @@ class StaticDataController < ApplicationController skip_before_action :authenticate around_action :handle_exceptions, except: %i[index] - def index - icn = YAML.load_file('./modules/ask_va_api/config/locales/constants.yml')['test_users']['crm_test_user_icn'] - service = Crm::Service.new(icn:) - options = if params[:key] - key = params[:key].to_sym - { key => params[:value] } - end - data = service.call(endpoint: params[:endpoint], payload: options) - render json: data.to_json, status: :ok - end - def announcements get_resource('announcements', user_mock_data: params[:user_mock_data]) render_result(@announcements) diff --git a/modules/ask_va_api/app/lib/ask_va_api/attachments/retriever.rb b/modules/ask_va_api/app/lib/ask_va_api/attachments/retriever.rb index a2ab5729a37..88178dc9d6e 100644 --- a/modules/ask_va_api/app/lib/ask_va_api/attachments/retriever.rb +++ b/modules/ask_va_api/app/lib/ask_va_api/attachments/retriever.rb @@ -3,6 +3,7 @@ module AskVAApi module Attachments ENDPOINT = 'attachment' + class AttachmentsRetrieverError < StandardError; end class Retriever attr_reader :id, :service @@ -29,12 +30,23 @@ def default_service end def fetch_data(payload: {}) - service.call(endpoint: ENDPOINT, payload:)[:Data] + response = service.call(endpoint: ENDPOINT, payload:) + handle_response_data(response) end def validate_input(input, error_message) raise ArgumentError, error_message if input.blank? end + + def handle_response_data(response) + case response + when Hash + response[:Data] + else + error = JSON.parse(response.body, symbolize_names: true) + raise(AttachmentsRetrieverError, error[:Message]) + end + end end end end diff --git a/modules/ask_va_api/spec/app/lib/ask_va_api/attachments/retriever_spec.rb b/modules/ask_va_api/spec/app/lib/ask_va_api/attachments/retriever_spec.rb index d6a027fd495..3b5f3629042 100644 --- a/modules/ask_va_api/spec/app/lib/ask_va_api/attachments/retriever_spec.rb +++ b/modules/ask_va_api/spec/app/lib/ask_va_api/attachments/retriever_spec.rb @@ -10,12 +10,12 @@ let(:entity) { instance_double(AskVAApi::Attachments::Entity) } before do - allow(Crm::Service).to receive(:new).and_return(service) allow(AskVAApi::Attachments::Entity).to receive(:new).and_return(entity) end context 'when successful' do before do + allow(Crm::Service).to receive(:new).and_return(service) allow(service).to receive(:call) .with(endpoint: 'attachment', payload: { id: '1' }) .and_return({ Data: double }) @@ -27,20 +27,21 @@ end context 'when Crm raise an error' do - let(:response) { instance_double(Faraday::Response, status: 400, body: 'Bad Request') } - let(:endpoint) { AskVAApi::Attachments::ENDPOINT } - let(:error_message) { "Bad request to #{endpoint}: #{response.body}" } + let(:body) do + '{"Data":null,"Message":"Data Validation: Invalid GUID, Parsing Failed",' \ + '"ExceptionOccurred":true,"ExceptionMessage":"Data Validation: Invalid GUID,' \ + ' Parsing Failed","MessageId":"c14c61c4-a3a8-4200-8c86-bdc09c261308"}' + end + let(:failure) { Faraday::Response.new(response_body: body, status: 400) } before do - allow(service).to receive(:call) - .with(endpoint: 'attachment', payload: { id: '1' }) - .and_raise(Crm::ErrorHandler::ServiceError, error_message) + allow_any_instance_of(Crm::CrmToken).to receive(:call).and_return('token') + allow_any_instance_of(Crm::Service).to receive(:call) + .with(endpoint: 'attachment', payload: { id: '1' }).and_return(failure) end - it 'raises an Error' do - expect do - retriever.call - end.to raise_error(ErrorHandler::ServiceError, "Crm::ErrorHandler::ServiceError: #{error_message}") + it 'raise the error' do + expect { retriever.call }.to raise_error(ErrorHandler::ServiceError) end end end 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 b94d93d6044..53f71a84c92 100644 --- a/modules/ask_va_api/spec/requests/v0/inquiries_spec.rb +++ b/modules/ask_va_api/spec/requests/v0/inquiries_spec.rb @@ -237,17 +237,34 @@ before do sign_in(authorized_user) - get '/ask_va_api/v0/download_attachment', params: { id:, mock: true } end - it 'response with 200' do - expect(response).to have_http_status(:ok) + context 'when successful' do + before do + get '/ask_va_api/v0/download_attachment', params: { id:, mock: true } + end + + it 'response with 200' do + expect(response).to have_http_status(:ok) + end end - context 'when attachment is not found' do - let(:id) { 'not_valid' } + context 'when Crm raise an error' do + let(:body) do + '{"Data":null,"Message":"Data Validation: Invalid GUID, Parsing Failed",' \ + '"ExceptionOccurred":true,"ExceptionMessage":"Data Validation: Invalid GUID,' \ + ' Parsing Failed","MessageId":"c14c61c4-a3a8-4200-8c86-bdc09c261308"}' + end + let(:failure) { Faraday::Response.new(response_body: body, status: 400) } + + before do + allow_any_instance_of(Crm::CrmToken).to receive(:call).and_return('token') + allow_any_instance_of(Crm::Service).to receive(:call) + .with(endpoint: 'attachment', payload: { id: '1' }).and_return(failure) + get '/ask_va_api/v0/download_attachment', params: { id:, mock: nil } + end - it 'responds with 500' do + it 'raise the error' do expect(response).to have_http_status(:unprocessable_entity) end end @@ -467,19 +484,6 @@ def json_response end end - describe 'POST #test_create' do - before do - 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 - end - - it 'response with 200' do - expect(response).to have_http_status(:ok) - end - end - describe 'POST #create_reply' do let(:payload) { { 'reply' => 'this is my reply' } } diff --git a/modules/ask_va_api/spec/requests/v0/static_data_spec.rb b/modules/ask_va_api/spec/requests/v0/static_data_spec.rb index 19647366682..041801ca51c 100644 --- a/modules/ask_va_api/spec/requests/v0/static_data_spec.rb +++ b/modules/ask_va_api/spec/requests/v0/static_data_spec.rb @@ -25,30 +25,6 @@ end end - describe 'GET #index' do - let(:index_path) { '/ask_va_api/v0/static_data?key=name&value=irish_country' } - let(:expected_response) { 'pong' } - let(:authorized_user) do - build(:user, :accountable_with_sec_id, - icn: YAML.load_file('./modules/ask_va_api/config/locales/constants.yml')['test_users']['test_user_228_icn']) - end - - before do - sign_in(authorized_user) - entity = OpenStruct.new(id: nil, info: 'pong') - allow_any_instance_of(Crm::Service).to receive(:call).and_return(entity) - get index_path - end - - context 'when successful' do - it 'returns status of 200 and the correct response data' do - result = JSON.parse(response.body)['table']['info'] - expect(response).to have_http_status(:ok) - expect(result).to eq(expected_response) - end - end - end - describe 'GET #announcements' do let(:announcements_path) { '/ask_va_api/v0/announcements' } let(:expected_hash) do