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

lg-14790 handle network failures for socure #11430

Merged
merged 18 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
4 changes: 4 additions & 0 deletions app/services/doc_auth/socure/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ def handle_invalid_response(http_response)
end
end

def handle_connection_error
raise NotImplementedError
end

AShukla-GSA marked this conversation as resolved.
Show resolved Hide resolved
def send_http_get_request
faraday_connection.get do |req|
req.options.context = { service_name: metric_name }
Expand Down
37 changes: 37 additions & 0 deletions app/services/doc_auth/socure/requests/document_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,43 @@ def handle_http_response(http_response)
JSON.parse(http_response.body, symbolize_names: true)
end

def handle_invalid_response(http_response)
message = [
self.class.name,
'Unexpected HTTP response',
http_response.status,
AShukla-GSA marked this conversation as resolved.
Show resolved Hide resolved
].join(' ')
exception = DocAuth::RequestError.new(message, http_response.status)
AShukla-GSA marked this conversation as resolved.
Show resolved Hide resolved

response_body = begin
http_response.body.present? ? JSON.parse(http_response.body) : {}
rescue JSON::JSONError
{}
end

handle_connection_error(
exception: exception,
status_code: response_body.dig('status', 'code'),
status_message: response_body.dig('status', 'message'),
)
AShukla-GSA marked this conversation as resolved.
Show resolved Hide resolved
end

def handle_connection_error(exception:, status_code: nil, status_message: nil)
NewRelic::Agent.notice_error(exception)
{
success: false,
errors: { network: true },
exception: exception,
extra: {
vendor: 'Socure',
selfie_live: false,
selfie_quality_good: false,
AShukla-GSA marked this conversation as resolved.
Show resolved Hide resolved
vendor_status_code: status_code,
vendor_status_message: status_message,
}.compact,
}
end

def method
:post
end
Expand Down
8 changes: 8 additions & 0 deletions app/services/doc_auth/socure/requests/docv_result_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ def handle_http_response(http_response)
)
end

def handle_connection_error(exception:)
NewRelic::Agent.notice_error(exception)
DocAuth::Socure::Responses::DocvResultResponse.new(
http_response: nil,
AShukla-GSA marked this conversation as resolved.
Show resolved Hide resolved
biometric_comparison_required: @biometric_comparison_required,
)
end

def document_capture_session
@document_capture_session ||=
DocumentCaptureSession.find_by!(uuid: document_capture_session_uuid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
before do
allow(I18n).to receive(:locale).and_return(expected_language)
allow(request_class).to receive(:new).and_call_original
allow(request_class).to receive(:handle_connection_error).and_call_original
get(:show)
end

Expand Down Expand Up @@ -175,6 +176,20 @@
expect(response).to be_not_found
end
end

context 'when socure connection error encountered' do
let(:fake_socure_endpoint) { 'https://fake-socure.com/' }
AShukla-GSA marked this conversation as resolved.
Show resolved Hide resolved
before do
allow(IdentityConfig.store).to receive(:socure_document_request_endpoint).
and_return(fake_socure_endpoint)
stub_request(:post, fake_socure_endpoint).to_raise(Faraday::ConnectionFailed)
end
it 'timeout still responds to user' do
get(:show)

expect(response).not_to be_nil
AShukla-GSA marked this conversation as resolved.
Show resolved Hide resolved
end
end
end

describe '#update' do
Expand Down
14 changes: 14 additions & 0 deletions spec/controllers/idv/socure/document_capture_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,20 @@
expect(response).to be_not_found
end
end

context 'when socure connection error encountered' do
let(:fake_socure_endpoint) { 'https://fake-socure.com/' }
AShukla-GSA marked this conversation as resolved.
Show resolved Hide resolved
before do
allow(IdentityConfig.store).to receive(:socure_document_request_endpoint).
and_return(fake_socure_endpoint)
stub_request(:post, fake_socure_endpoint).to_raise(Faraday::ConnectionFailed)
end
it 'timeout still responds to user' do
get(:show)

expect(response).not_to be_nil
AShukla-GSA marked this conversation as resolved.
Show resolved Hide resolved
end
end
end

describe '#update' do
Expand Down
28 changes: 28 additions & 0 deletions spec/services/doc_auth/socure/requests/document_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,33 @@
expect { document_request.fetch }.not_to raise_error
end
end
context 'with timeout exception' do
let(:response) { nil }
let(:response_status) { 403 }
let(:faraday_connection_failed_exception) { Faraday::ConnectionFailed }

before do
stub_request(:post, fake_socure_endpoint).to_raise(faraday_connection_failed_exception)
end
it 'expect handle_connection_error method to be called' do
connection_error_attributes = {
success: false,
errors: { network: true },
exception: faraday_connection_failed_exception,
extra: {
vendor: 'Socure',
selfie_live: false,
selfie_quality_good: false,
vendor_status_code: nil,
vendor_status_message: nil,
}.compact,
}
result = document_request.fetch
expect(result[:success]).to eq(connection_error_attributes[:success])
expect(result[:errors]).to eq(connection_error_attributes[:errors])
expect(result[:exception]).to be_a Faraday::ConnectionFailed
expect(result[:extra]).to eq(connection_error_attributes[:extra])
end
end
end
end
51 changes: 51 additions & 0 deletions spec/services/doc_auth/socure/requests/docv_result_request_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require 'rails_helper'

RSpec.describe DocAuth::Socure::Requests::DocvResultRequest do
let(:document_capture_session_uuid) { 'fake uuid' }
let(:biometric_comparison_required) { false }

subject(:docv_result_request) do
described_class.new(
document_capture_session_uuid:,
biometric_comparison_required: biometric_comparison_required,
)
end

describe '#fetch' do
let(:fake_socure_endpoint) { 'https://fake-socure.com/' }
AShukla-GSA marked this conversation as resolved.
Show resolved Hide resolved
let(:fake_socure_api_endpoint) { 'https://fake-socure.com/api/3.0/EmailAuthScore' }
AShukla-GSA marked this conversation as resolved.
Show resolved Hide resolved
let(:docv_transaction_token) { 'fake docv transaction token' }
let(:user) { create(:user) }
let(:document_capture_session) do
DocumentCaptureSession.create(user:).tap do |dcs|
dcs.socure_docv_transaction_token = docv_transaction_token
end
end

before do
allow(IdentityConfig.store).to receive(:socure_idplus_base_url).
and_return(fake_socure_endpoint)
allow(DocumentCaptureSession).to receive(:find_by).and_return(document_capture_session)
stub_request(:post, fake_socure_api_endpoint).to_raise(Faraday::ConnectionFailed)
end

context 'with timeout exception' do
let(:fake_socure_response) { {} }
let(:fake_socure_status) { 500 }

it 'expect handle_connection_error method to be called' do
connection_error_attributes = {
http_response: nil,
biometric_comparison_required: biometric_comparison_required,
}
failed_response = DocAuth::Socure::Responses::DocvResultResponse.new(
**connection_error_attributes,
)
allow(DocAuth::Socure::Responses::DocvResultResponse).to receive(:new).
with(**connection_error_attributes).
and_return(failed_response)
expect(docv_result_request.fetch).to eq(failed_response)
end
end
end
end