From dd0f56f85f9aa7360308d0769780a68c03d98213 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= Date: Tue, 27 Oct 2020 23:45:50 +0100 Subject: [PATCH] Use shorter responses and configuration for specs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit By simplyfing the responses the configuration for specs can be simpler too. We're also using more generic terms instead of the ones used in Madrid's Census API. Co-Authored-By: Javi Martín --- lib/remote_census_api.rb | 32 ++++++++------------ spec/lib/census_caller_spec.rb | 3 ++ spec/lib/remote_census_api_spec.rb | 47 ++++++++++-------------------- spec/spec_helper.rb | 41 +++++++++++--------------- 4 files changed, 47 insertions(+), 76 deletions(-) diff --git a/lib/remote_census_api.rb b/lib/remote_census_api.rb index 25f3c083a..ceb1f8c98 100644 --- a/lib/remote_census_api.rb +++ b/lib/remote_census_api.rb @@ -51,9 +51,9 @@ class RemoteCensusApi path_value = Setting["remote_census.response.gender"] case extract_value(path_value) - when "Varón" + when "Male", "Varón" "male" - when "Mujer" + when "Female", "Mujer" "female" end end @@ -132,29 +132,21 @@ class RemoteCensusApi def stubbed_valid_response { - get_habita_datos_response: { - get_habita_datos_return: { - datos_habitante: { - item: { - fecha_nacimiento_string: "31-12-1980", - identificador_documento: "12345678Z", - descripcion_sexo: "Varón", - nombre: "José", - apellido1: "García" - } - }, - datos_vivienda: { - item: { - codigo_postal: "28013", - codigo_distrito: "01" - } - } + response: { + data: { + date_of_birth: "31-12-1980", + document_number: "12345678Z", + gender: "Male", + name: "William", + surname: "Widmore", + postal_code: "28013", + district_code: "01" } } } end def stubbed_invalid_response - { get_habita_datos_response: { get_habita_datos_return: { datos_habitante: {}, datos_vivienda: {}}}} + { response: { data: {}}} end end diff --git a/spec/lib/census_caller_spec.rb b/spec/lib/census_caller_spec.rb index 6be2b0794..d4f1d550b 100644 --- a/spec/lib/census_caller_spec.rb +++ b/spec/lib/census_caller_spec.rb @@ -45,6 +45,9 @@ describe CensusCaller do end describe "RemoteCensusApi", :remote_census do + let(:valid_body) { { response: { data: { document_number: "12345678" }}} } + let(:invalid_body) { { response: { data: {}}} } + it "returns remote census api response when it's available and response is valid" do remote_census_api_response = RemoteCensusApi::Response.new(valid_body) allow_any_instance_of(RemoteCensusApi).to receive(:call).and_return(remote_census_api_response) diff --git a/spec/lib/remote_census_api_spec.rb b/spec/lib/remote_census_api_spec.rb index 3e4a6b2a9..50aec27ea 100644 --- a/spec/lib/remote_census_api_spec.rb +++ b/spec/lib/remote_census_api_spec.rb @@ -4,21 +4,13 @@ describe RemoteCensusApi do let(:api) { RemoteCensusApi.new } describe "#call", :remote_census do - let(:invalid_body) { { get_habita_datos_response: { get_habita_datos_return: { datos_habitante: {}}}} } - let(:valid_body) do - { - get_habita_datos_response: { - get_habita_datos_return: { - datos_habitante: { - item: { - fecha_nacimiento_string: "1-1-1980" - } - } - } - } - } + before do + Setting["remote_census.response.valid"] = "response.data.date_of_birth" end + let(:invalid_body) { { response: { data: {}}} } + let(:valid_body) { { response: { data: { date_of_birth: "1-1-1980" }}} } + it "returns the response for the first valid variant" do date = Date.parse("01/01/1983") allow(api).to receive(:get_response_body).with(1, "00123456", date, "28001").and_return(invalid_body) @@ -153,23 +145,15 @@ describe RemoteCensusApi do response = RemoteCensusApi.new.send(:get_response_body, document_type, document_number, nil, nil) - expect(response).to eq({ get_habita_datos_response: { - get_habita_datos_return: { - datos_habitante: { - item: { - fecha_nacimiento_string: "31-12-1980", - identificador_documento: "12345678Z", - descripcion_sexo: "Varón", - nombre: "José", - apellido1: "García" - } - }, - datos_vivienda: { - item: { - codigo_postal: "28013", - codigo_distrito: "01" - } - } + expect(response).to eq({ response: { + data: { + date_of_birth: "31-12-1980", + document_number: "12345678Z", + gender: "Male", + name: "William", + surname: "Widmore", + postal_code: "28013", + district_code: "01" } } }) @@ -187,9 +171,8 @@ describe RemoteCensusApi do expect(response.valid?).to eq true expect(response.date_of_birth).to eq Time.zone.local(1980, 12, 31).to_date expect(response.postal_code).to eq "28013" - expect(response.district_code).to eq "01" expect(response.gender).to eq "male" - expect(response.name).to eq "José García" + expect(response.name).to eq "William Widmore" end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 79358cf14..868931d90 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -106,35 +106,28 @@ RSpec.configure do |config| end config.before(:each, :remote_census) do |example| - Setting["remote_census.request.method_name"] = "get_habita_datos" - Setting["remote_census.request.document_type"] = "request.tipo_documento" - Setting["remote_census.request.document_number"] = "request.documento" - Setting["remote_census.request.date_of_birth"] = "request.fecha_nacimiento" - Setting["remote_census.request.postal_code"] = "request.codigo_postal" + Setting["feature.remote_census"] = true + Setting["remote_census.request.method_name"] = "verify_residence" Setting["remote_census.request.structure"] = '{ "request": { - "codigo_institucion": 1, - "codigo_portal": 1, - "codigo_usuario": 1, - "documento": "nil", - "tipo_documento": "null", - "fecha_nacimiento": "null", - "codigo_postal": "nil", - "codigo_idioma": 102, - "nivel": 3 + "document_type": "null", + "document_number": "nil", + "date_of_birth": "null", + "postal_code": "nil" } }' - access_user_data = "get_habita_datos_response.get_habita_datos_return.datos_habitante.item" - access_residence_data = "get_habita_datos_response.get_habita_datos_return.datos_vivienda.item" - Setting["remote_census.response.date_of_birth"] = "#{access_user_data}.fecha_nacimiento_string" - Setting["remote_census.response.postal_code"] = "#{access_residence_data}.codigo_postal" - Setting["remote_census.response.district"] = "#{access_residence_data}.codigo_distrito" - Setting["remote_census.response.gender"] = "#{access_user_data}.descripcion_sexo" - Setting["remote_census.response.name"] = "#{access_user_data}.nombre" - Setting["remote_census.response.surname"] = "#{access_user_data}.apellido1" - Setting["remote_census.response.valid"] = access_user_data - Setting["feature.remote_census"] = true + Setting["remote_census.request.document_type"] = "request.document_type" + Setting["remote_census.request.document_number"] = "request.document_number" + Setting["remote_census.request.date_of_birth"] = "request.date_of_birth" + Setting["remote_census.request.postal_code"] = "request.postal_code" + Setting["remote_census.response.date_of_birth"] = "response.data.date_of_birth" + Setting["remote_census.response.postal_code"] = "response.data.postal_code" + Setting["remote_census.response.district"] = "response.data.district_code" + Setting["remote_census.response.gender"] = "response.data.gender" + Setting["remote_census.response.name"] = "response.data.name" + Setting["remote_census.response.surname"] = "response.data.surname" + Setting["remote_census.response.valid"] = "response.data.document_number" end # Allows RSpec to persist some state between runs in order to support