Skip to content

Commit

Permalink
BE | Ask VA Api: Refactor classes and specs (#16542)
Browse files Browse the repository at this point in the history
* Update `StaticDataController`

- removed `test_create` endpoint

* Updated `InquiriesController` and `Attachments::Retriever`

- removed `Inquiries#index` action, it was used as a test `endpoint`
- Reworked `Attachments::Retriever` error handling

---------

Co-authored-by: khoa-v-nguyen <[email protected]>
  • Loading branch information
Khoa-V-Nguyen and Khoa-V-Nguyen authored Apr 29, 2024
1 parent f9a61a1 commit ea18c8f
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 13 additions & 1 deletion modules/ask_va_api/app/lib/ask_va_api/attachments/retriever.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module AskVAApi
module Attachments
ENDPOINT = 'attachment'
class AttachmentsRetrieverError < StandardError; end

class Retriever
attr_reader :id, :service
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand All @@ -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
Expand Down
42 changes: 23 additions & 19 deletions modules/ask_va_api/spec/requests/v0/inquiries_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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' } }

Expand Down
24 changes: 0 additions & 24 deletions modules/ask_va_api/spec/requests/v0/static_data_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ea18c8f

Please sign in to comment.