From 265f8dbf015c9e22f34e58f1182b11c940d15e01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= Date: Fri, 30 Oct 2020 12:25:30 +0100 Subject: [PATCH] Connect to the service only when remote census endpoint is defined Also return an invalid response when the endpoint is not defined. This will allow to test remote web services on development environments. --- lib/remote_census_api.rb | 6 +++--- spec/lib/remote_census_api_spec.rb | 8 ++++++++ spec/spec_helper.rb | 1 + 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/remote_census_api.rb b/lib/remote_census_api.rb index 5cea6142e..e91de7e12 100644 --- a/lib/remote_census_api.rb +++ b/lib/remote_census_api.rb @@ -73,7 +73,7 @@ class RemoteCensusApi private def get_response_body(document_type, document_number, date_of_birth, postal_code) - if end_point_available? + if end_point_defined? request = request(document_type, document_number, date_of_birth, postal_code) client.call(Setting["remote_census.request.method_name"].to_sym, message: request).body else @@ -118,8 +118,8 @@ class RemoteCensusApi to_set[final_key] = value end - def end_point_available? - !Rails.env.development? + def end_point_defined? + Setting["remote_census.general.endpoint"].present? end def stubbed_invalid_response diff --git a/spec/lib/remote_census_api_spec.rb b/spec/lib/remote_census_api_spec.rb index 152d1b946..2e25b6e1e 100644 --- a/spec/lib/remote_census_api_spec.rb +++ b/spec/lib/remote_census_api_spec.rb @@ -90,5 +90,13 @@ describe RemoteCensusApi do api.call("1", "12345678Z", Date.parse("31/12/1980"), "28013") end end + + it "returns an invalid response when endpoint is not defined" do + allow_any_instance_of(RemoteCensusApi).to receive(:end_point_defined?).and_return(false) + + response = api.call("1", "12345678Z", Date.parse("01/01/1983"), "28013") + + expect(response).not_to be_valid + end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index df40b8c01..00b5c8c9e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -106,6 +106,7 @@ RSpec.configure do |config| end config.before(:each, :remote_census) do |example| + allow_any_instance_of(RemoteCensusApi).to receive(:end_point_defined?).and_return(true) Setting["feature.remote_census"] = true Setting["remote_census.request.method_name"] = "verify_residence" Setting["remote_census.request.structure"] = '{ "request":