Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

105852 add specs for upstream failures in appointment booking flow #21377

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions modules/vaos/app/errors/eps/service_error.rb

This file was deleted.

2 changes: 0 additions & 2 deletions modules/vaos/app/services/eps/appointment_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ def get_appointment(appointment_id:, retrieve_latest_details: false)

response = perform(:get, "/#{config.base_path}/appointments/#{appointment_id}#{query_params}", {}, headers)
OpenStruct.new(response.body)
rescue => e
raise Eps::ServiceError, "Error fetching appointment details: #{e.message}"
end

##
Expand Down
2 changes: 1 addition & 1 deletion modules/vaos/app/sidekiq/eps/eps_appointment_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def perform(appointment_id, user, retry_count = 0)
else
send_vanotify_message(user:, error: 'Could not complete booking')
end
rescue Eps::AppointmentService::ServiceError
rescue Common::Exceptions::BackendServiceException
send_vanotify_message(user:, error: 'Service error, please contact support')
rescue => e
send_vanotify_message(user:, error: e.message)
Expand Down
38 changes: 38 additions & 0 deletions modules/vaos/spec/requests/vaos/v2/appointments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1555,6 +1555,44 @@ def stub_clinics
end
end

context 'when the upstream service returns a 500 error' do
it 'returns a bad_gateway status and appropriate error message' do
VCR.use_cassette('vaos/eps/get_appointments/500_error') do
VCR.use_cassette('vaos/v2/appointments/get_appointments_200') do
VCR.use_cassette('vaos/eps/get_drive_times/200') do
VCR.use_cassette 'vaos/eps/get_provider_slots/200' do
VCR.use_cassette 'vaos/eps/get_provider_service/200' do
VCR.use_cassette 'vaos/eps/draft_appointment/200' do
VCR.use_cassette 'vaos/eps/token/token_200' do
post '/vaos/v2/appointments/draft', params: draft_params, headers: inflection_header

expect(response).to have_http_status(:bad_gateway)
response_body = JSON.parse(response.body)
expect(response_body).to have_key('errors')
expect(response_body['errors']).to be_an(Array)

error = response_body['errors'].first
expect(error).to include(
'title' => 'Bad Gateway',
'detail' => 'Received an an invalid response from the upstream server',
'code' => 'VAOS_502',
'status' => '502',
'source' => {
'vamfUrl' => 'https://api.wellhive.com/care-navigation/v1/appointments?patientId=care-nav-patient-casey',
'vamfBody' => '{"isFault": true,"isTemporary": true,"name": "Internal Server Error"}',
'vamfStatus' => 500
}
)
end
end
end
end
end
end
end
end
end

context 'when Redis connection fails' do
it 'returns a bad_gateway status and appropriate error message' do
# Mock the Redis client to raise a connection error
Expand Down
26 changes: 25 additions & 1 deletion modules/vaos/spec/requests/vaos/v2/eps_appointments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
let(:current_user) { build(:user, :vaos, icn: 'care-nav-patient-casey') }

describe 'get eps appointment' do
context 'booked appointment' do
context 'when a booked appointment corresponding to the referral exists' do
let(:expected_response) do
{
'data' => {
Expand Down Expand Up @@ -98,6 +98,30 @@
end
end

context 'when a booked appointment corresponding to the referral is not found' do
it 'returns a 404 error' do
VCR.use_cassette('vaos/eps/token/token_200', match_requests_on: %i[method path query]) do
VCR.use_cassette('vaos/eps/get_appointment/404', match_requests_on: %i[method path query]) do
get '/vaos/v2/eps_appointments/qdm61cJ5', headers: inflection_header

expect(response).to have_http_status(:not_found)
end
end
end
end

context 'when the upstream service returns a 500 error' do
it 'returns a 502 error' do
VCR.use_cassette('vaos/eps/token/token_200', match_requests_on: %i[method path query]) do
VCR.use_cassette('vaos/eps/get_appointment/500', match_requests_on: %i[method path query]) do
get '/vaos/v2/eps_appointments/qdm61cJ5', headers: inflection_header

expect(response).to have_http_status(:bad_gateway)
end
end
end
end

context 'draft appointment' do
it 'returns 404' do
VCR.use_cassette('vaos/eps/token/token_200', match_requests_on: %i[method path query]) do
Expand Down
6 changes: 3 additions & 3 deletions modules/vaos/spec/services/eps/appointment_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@
end

it 'throws exception' do
expect { service.get_appointment(appointment_id:) }.to raise_error(Eps::ServiceError, /VA900/)
expect { service.get_appointment(appointment_id:) }.to raise_error(Common::Exceptions::BackendServiceException,
/VA900/)
end
end
end
Expand Down Expand Up @@ -143,8 +144,7 @@
it 'throws exception' do
expect do
service.create_draft_appointment(referral_id:)
end.to raise_error(Common::Exceptions::BackendServiceException,
/VA900/)
end.to raise_error(Common::Exceptions::BackendServiceException, /VA900/)
end
end
end
Expand Down
50 changes: 46 additions & 4 deletions modules/vaos/spec/sidekiq/eps_appointment_worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,52 @@
end

it 'sends failure message after max retries' do
# rubocop:disable RSpec/SubjectStub
expect(worker).to receive(:send_vanotify_message).with(user:, error: 'Could not complete booking')
expect(va_notify_service).to receive(:send_email).with(
email_address: user.va_profile_email,
template_id: Settings.vanotify.services.va_gov.template_id.va_appointment_failure,
parameters: {
'error' => 'Could not complete booking'
}
)
worker.perform(appointment_id, user, Eps::EpsAppointmentWorker::MAX_RETRIES)
end
end

context 'when the appointment is not found' do
before do
allow(service).to receive(:get_appointment).with(appointment_id:).and_raise(
Common::Exceptions::BackendServiceException.new(nil, {}, 404, 'Appointment not found')
)
end

it 'sends failure message after max retries' do
expect(va_notify_service).to receive(:send_email).with(
email_address: user.va_profile_email,
template_id: Settings.vanotify.services.va_gov.template_id.va_appointment_failure,
parameters: {
'error' => 'Service error, please contact support'
}
)
worker.perform(appointment_id, user, Eps::EpsAppointmentWorker::MAX_RETRIES)
end
end

context 'when the upstream service returns a 500 error' do
before do
allow(service).to receive(:get_appointment).with(appointment_id:).and_raise(
Common::Exceptions::BackendServiceException.new(nil, {}, 500, 'Internal server error')
)
end

it 'sends failure message after max retries' do
expect(va_notify_service).to receive(:send_email).with(
email_address: user.va_profile_email,
template_id: Settings.vanotify.services.va_gov.template_id.va_appointment_failure,
parameters: {
'error' => 'Service error, please contact support'
}
)
worker.perform(appointment_id, user, Eps::EpsAppointmentWorker::MAX_RETRIES)
# rubocop:enable RSpec/SubjectStub
end
end

Expand All @@ -61,7 +103,7 @@
'error' => nil
}
)
worker.__send__(:send_vanotify_message, user:)
worker.send(:send_vanotify_message, user:)
end
end
end
Expand Down
52 changes: 52 additions & 0 deletions spec/support/vcr_cassettes/vaos/eps/get_appointment/404.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions spec/support/vcr_cassettes/vaos/eps/get_appointment/500.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
http_interactions:
- request:
method: get
uri: https://api.wellhive.com/care-navigation/v1/appointments/qdm61cJ5?retrieveLatestDetails=true
body:
encoding: US-ASCII
string: ''
headers:
Accept:
- application/json
Content-Type:
- application/json
User-Agent:
- Vets.gov Agent
Referer:
- https://review-instance.va.gov
X-Vamf-Jwt:
- stubbed-token
X-Request-Id:
- ''
Accept-Encoding:
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
response:
status:
code: 500
message: Internal Server Error
headers:
Date:
- Wed, 26 May 2021 22:47:08 GMT
Content-Type:
- application/json
Content-Length:
- '260'
Server:
- openresty
X-Vamf-Version:
- 1.5.0
B3:
- edb650c5fed99511-10c51619b9c07470-1
Access-Control-Allow-Headers:
- x-vamf-jwt
X-Vamf-Build:
- d8af5ed
X-Vamf-Timestamp:
- '2021-05-24T22:00:44+0000'
Access-Control-Allow-Origin:
- "*"
Access-Control-Allow-Methods:
- GET,OPTIONS
Access-Control-Max-Age:
- '3600'
body:
encoding: UTF-8
string: '{"isFault": true,"isTemporary": true,"name": "Internal Server Error"}'
recorded_at: Wed, 26 May 2021 22:47:08 GMT

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading