Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 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
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ def show
:url,
)
document_capture_session.save

# useful for analytics
@msg = document_response[:msg]
@reference_id = document_response[:referenceId]
Expand Down
33 changes: 23 additions & 10 deletions app/services/doc_auth/socure/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ class Request
def fetch
# return DocAuth::Response with DocAuth::Error if workflow is invalid
http_response = send_http_request
return handle_invalid_response(http_response) unless http_response.success?

handle_http_response(http_response)
if http_response&.success? && http_response.body.present?
handle_http_response(http_response)
else
handle_invalid_response(http_response)
end
rescue Faraday::ConnectionFailed, Faraday::TimeoutError, Faraday::SSLError => e
handle_connection_error(exception: e)
end
Expand All @@ -33,16 +35,27 @@ def handle_http_response(_response)
end

def handle_invalid_response(http_response)
begin
if http_response.body.present?
warn(http_response.body)
JSON.parse(http_response.body)
else
{}
end
message = [
self.class.name,
'Unexpected HTTP response',
http_response&.status,
].join(' ')
exception = DocAuth::RequestError.new(message, http_response&.status)

response_body = begin
http_response&.body.present? ? JSON.parse(http_response.body) : {}
rescue JSON::JSONError
{}
end
handle_connection_error(
exception: exception,
status: response_body.dig('status'),
status_message: response_body.dig('msg'),
)
end

def handle_connection_error(exception:, status: nil, status_message: nil)
raise NotImplementedError
end

def send_http_get_request
Expand Down
14 changes: 14 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,20 @@ def handle_http_response(http_response)
JSON.parse(http_response.body, symbolize_names: true)
end

def handle_connection_error(exception:, status: nil, status_message: nil)
NewRelic::Agent.notice_error(exception)
{
success: false,
errors: { network: true },
exception: exception,
extra: {
vendor: 'Socure',
vendor_status: status,
vendor_status_message: status_message,
}.compact,
}
end

def method
:post
end
Expand Down
14 changes: 14 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,20 @@ def handle_http_response(http_response)
)
end

def handle_connection_error(exception:, status: nil, status_message: nil)
NewRelic::Agent.notice_error(exception)
DocAuth::Response.new(
success: false,
errors: { network: true },
exception: exception,
extra: {
vendor: 'Socure',
vendor_status: status,
vendor_status_message: status_message,
}.compact,
)
end

def document_capture_session
@document_capture_session ||=
DocumentCaptureSession.find_by!(uuid: document_capture_session_uuid)
Expand Down
14 changes: 10 additions & 4 deletions app/services/doc_auth/socure/responses/docv_result_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class DocvResultResponse < DocAuth::Response
expiration_date: %w[documentVerification documentData expirationDate],
}.freeze

def initialize(http_response: nil, biometric_comparison_required: false)
def initialize(http_response:, biometric_comparison_required: false)
@http_response = http_response
@biometric_comparison_required = biometric_comparison_required
@pii_from_doc = read_pii
Expand Down Expand Up @@ -110,17 +110,23 @@ def get_data(path)
end

def parsed_response_body
@parsed_response_body ||= JSON.parse(http_response.body).with_indifferent_access
@parsed_response_body ||= begin
http_response&.body.present? ? JSON.parse(
http_response.body,
).with_indifferent_access : {}
rescue JSON::JSONError
{}
end
end

def state_id_type
type = get_data(DATA_PATHS[:id_type])
type.gsub(/\W/, '').underscore
type&.gsub(/\W/, '')&.underscore
end

def parse_date(date_string)
Date.parse(date_string)
rescue ArgumentError
rescue ArgumentError, TypeError
message = {
event: 'Failure to parse Socure ID+ date',
}.to_json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
include FlowPolicyHelper

let(:idv_vendor) { Idp::Constants::Vendors::SOCURE }
let(:fake_socure_endpoint) { 'https://fake-socure.com' }
let(:fake_socure_endpoint) { 'https://fake-socure.test' }
let(:user) { create(:user) }
let(:stored_result) { nil }
let(:socure_enabled) { true }
Expand Down Expand Up @@ -91,6 +91,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 @@ -189,6 +190,74 @@
expect(response).to be_not_found
end
end

context 'when socure error encountered' do
let(:fake_socure_endpoint) { 'https://fake-socure.test/' }
let(:failed_response_body) do
{ 'status' => 'Error',
'referenceId' => '1cff6d33-1cc0-4205-b740-c9a9e6b8bd66',
'data' => {},
'msg' => 'No active account is associated with this request' }
end
let(:response_body_401) do
{
status: 'Error',
referenceId: '7ff0cdc5-395e-45d1-8467-0ff1b41c11dc',
msg: 'string',
}
end
let(:no_doc_found_response_body) do
{
referenceId: '0dc21b0d-04df-4dd5-8533-ec9ecdafe0f4',
msg: {
status: 400,
msg: 'No Documents found',
},
}
end
before do
allow(IdentityConfig.store).to receive(:socure_document_request_endpoint).
and_return(fake_socure_endpoint)
end
it 'connection timeout still responds to user' do
stub_request(:post, fake_socure_endpoint).to_raise(Faraday::ConnectionFailed)
get(:show)
expect(response).to redirect_to(idv_unavailable_path)
end

it 'socure error response still gives a result to user' do
stub_request(:post, fake_socure_endpoint).to_return(
status: 401,
body: JSON.generate(failed_response_body),
)
get(:show)
expect(response).to redirect_to(idv_unavailable_path)
end
it 'socure nil response still gives a result to user' do
stub_request(:post, fake_socure_endpoint).to_return(
status: 500,
body: nil,
)
get(:show)
expect(response).to redirect_to(idv_unavailable_path)
end
it 'socure nil response still gives a result to user' do
stub_request(:post, fake_socure_endpoint).to_return(
status: 401,
body: JSON.generate(response_body_401),
)
get(:show)
expect(response).to redirect_to(idv_unavailable_path)
end
it 'socure nil response still gives a result to user' do
stub_request(:post, fake_socure_endpoint).to_return(
status: 401,
body: JSON.generate(no_doc_found_response_body),
)
get(:show)
expect(response).to redirect_to(idv_unavailable_path)
end
end
end

describe '#update' do
Expand Down
70 changes: 69 additions & 1 deletion spec/controllers/idv/socure/document_capture_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
include FlowPolicyHelper

let(:idv_vendor) { Idp::Constants::Vendors::SOCURE }
let(:fake_socure_endpoint) { 'https://fake-socure.com' }
let(:fake_socure_endpoint) { 'https://fake-socure.test' }
let(:user) { create(:user) }
let(:stored_result) { nil }
let(:socure_enabled) { true }
Expand Down Expand Up @@ -192,6 +192,74 @@
expect(response).to be_not_found
end
end

context 'when socure error encountered' do
let(:fake_socure_endpoint) { 'https://fake-socure.test/' }
let(:failed_response_body) do
{ 'status' => 'Error',
'referenceId' => '1cff6d33-1cc0-4205-b740-c9a9e6b8bd66',
'data' => {},
'msg' => 'No active account is associated with this request' }
end
let(:response_body_401) do
{
status: 'Error',
referenceId: '7ff0cdc5-395e-45d1-8467-0ff1b41c11dc',
msg: 'string',
}
end
let(:no_doc_found_response_body) do
{
referenceId: '0dc21b0d-04df-4dd5-8533-ec9ecdafe0f4',
msg: {
status: 400,
msg: 'No Documents found',
},
}
end
before do
allow(IdentityConfig.store).to receive(:socure_document_request_endpoint).
and_return(fake_socure_endpoint)
end
it 'connection timeout still responds to user' do
stub_request(:post, fake_socure_endpoint).to_raise(Faraday::ConnectionFailed)
get(:show)
expect(response).to redirect_to(idv_unavailable_path)
end

it 'socure error response still gives a result to user' do
stub_request(:post, fake_socure_endpoint).to_return(
status: 401,
body: JSON.generate(failed_response_body),
)
get(:show)
expect(response).to redirect_to(idv_unavailable_path)
end
it 'socure nil response still gives a result to user' do
stub_request(:post, fake_socure_endpoint).to_return(
status: 500,
body: nil,
)
get(:show)
expect(response).to redirect_to(idv_unavailable_path)
end
it 'socure nil response still gives a result to user' do
stub_request(:post, fake_socure_endpoint).to_return(
status: 401,
body: JSON.generate(response_body_401),
)
get(:show)
expect(response).to redirect_to(idv_unavailable_path)
end
it 'socure nil response still gives a result to user' do
stub_request(:post, fake_socure_endpoint).to_return(
status: 401,
body: JSON.generate(no_doc_found_response_body),
)
get(:show)
expect(response).to redirect_to(idv_unavailable_path)
end
end
end

describe '#update' do
Expand Down
8 changes: 5 additions & 3 deletions spec/services/doc_auth/socure/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
end

describe '#fetch' do
let(:fake_socure_endpoint) { 'https://fake-socure.com/' }
let(:fake_socure_endpoint) { 'https://fake-socure.test/' }
let(:fake_metric_name) { 'fake metric' }

before do
Expand Down Expand Up @@ -40,8 +40,10 @@
let(:response) { nil }
let(:response_status) { 403 }

it 'returns {}' do
expect(request.fetch).to eq({})
# Because we have not implemented handle_connection_error at this level
# (defined in docv_result and document_request)
it 'raises a NotImplementedError' do
expect { request.fetch }.to raise_error NotImplementedError
end
end
end
Expand Down
28 changes: 27 additions & 1 deletion spec/services/doc_auth/socure/requests/document_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

describe '#fetch' do
let(:document_type) { 'license' }
let(:fake_socure_endpoint) { 'https://fake-socure.com/' }
let(:fake_socure_endpoint) { 'https://fake-socure.test/' }
let(:fake_socure_document_capture_app_url) { 'https://verify.socure.us/something' }
let(:docv_transaction_token) { 'fake docv transaction token' }
let(:fake_socure_response) do
Expand Down Expand Up @@ -99,5 +99,31 @@
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',
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
Loading