From 4dfce4f245718bd8eb561bcb879a8448882eaa13 Mon Sep 17 00:00:00 2001 From: taitus Date: Wed, 17 Apr 2019 14:05:48 +0200 Subject: [PATCH] RemoteCensusAPI accept :date_of_birth and :postal_code New RemoteCensusAPI allow receive :date_of_birth and :postal_code and use in request to endpoint always that have been configured on remote_census_configuration: - Setting["remote_census.request.date_of_birth"] - Setting["remote_census.request.postal_code"] Add new params to CensusCaller 'call' method. --- lib/census_caller.rb | 4 +- lib/remote_census_api.rb | 17 ++- spec/lib/census_caller_spec.rb | 74 ++++++++---- spec/lib/remote_census_api_spec.rb | 188 +++++++++++++++++++++++++++-- 4 files changed, 246 insertions(+), 37 deletions(-) diff --git a/lib/census_caller.rb b/lib/census_caller.rb index c0a7f1c89..71b3404b5 100644 --- a/lib/census_caller.rb +++ b/lib/census_caller.rb @@ -1,8 +1,8 @@ class CensusCaller - def call(document_type, document_number) + def call(document_type, document_number, date_of_birth, postal_code) if Setting["feature.remote_census"].present? - response = RemoteCensusApi.new.call(document_type, document_number) + response = RemoteCensusApi.new.call(document_type, document_number, date_of_birth, postal_code) else response = CensusApi.new.call(document_type, document_number) end diff --git a/lib/remote_census_api.rb b/lib/remote_census_api.rb index ae8b124e5..ae2c1c4ad 100644 --- a/lib/remote_census_api.rb +++ b/lib/remote_census_api.rb @@ -1,10 +1,10 @@ include DocumentParser class RemoteCensusApi - def call(document_type, document_number) + def call(document_type, document_number, date_of_birth, postal_code) response = nil get_document_number_variants(document_type, document_number).each do |variant| - response = Response.new(get_response_body(document_type, variant)) + response = Response.new(get_response_body(document_type, variant, date_of_birth, postal_code)) return response if response.valid? end response @@ -70,9 +70,10 @@ class RemoteCensusApi private - def get_response_body(document_type, document_number) + def get_response_body(document_type, document_number, date_of_birth, postal_code) if end_point_available? - client.call(Setting["remote_census.request.method_name"].to_sym, message: request(document_type, document_number)).body + 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 stubbed_response(document_type, document_number) end @@ -82,11 +83,17 @@ class RemoteCensusApi @client = Savon.client(wsdl: Setting["remote_census.general.endpoint"]) end - def request(document_type, document_number) + def request(document_type, document_number, date_of_birth, postal_code) structure = eval(Setting["remote_census.request.structure"]) fill_in(structure, Setting["remote_census.request.document_type"], document_type) fill_in(structure, Setting["remote_census.request.document_number"], document_number) + fill_in(structure, Setting["remote_census.request.postal_code"], postal_code) + if date_of_birth.present? + fill_in(structure, + Setting["remote_census.request.date_of_birth"], + I18n.l(date_of_birth, format: :default)) + end structure end diff --git a/spec/lib/census_caller_spec.rb b/spec/lib/census_caller_spec.rb index 9d0646854..a680df52e 100644 --- a/spec/lib/census_caller_spec.rb +++ b/spec/lib/census_caller_spec.rb @@ -18,7 +18,7 @@ describe CensusCaller do allow(CensusApi).to receive(:call).with(1, "12345678A") allow(LocalCensus).to receive(:call).with(1, "12345678A") - response = api.call(1, "12345678A") + response = api.call(1, "12345678A", nil, nil) expect(response).to eq(local_census_response) end @@ -38,38 +38,68 @@ describe CensusCaller do allow(CensusApi).to receive(:call).with(1, "12345678A") allow(LocalCensus).to receive(:call).with(1, "12345678A") - response = api.call(1, "12345678A") + response = api.call(1, "12345678A", nil, nil) expect(response).to eq(census_api_response) end - it "returns data from Remote Census API if it's available and valid" do - Setting["feature.remote_census"] = true - 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.valid"] = access_user_data + describe "RemoteCensusApi" do - remote_census_api_response = RemoteCensusApi::Response.new(get_habita_datos_response: { - get_habita_datos_return: { - datos_habitante: { item: { fecha_nacimiento_string: "1-1-1980" } } - } - }) + before do + 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.valid"] = access_user_data + end - local_census_response = LocalCensus::Response.new(create(:local_census_record)) + it "returns data from Remote Census API if it's available and valid" do + Setting["feature.remote_census"] = true - expect_any_instance_of(RemoteCensusApi).to receive(:call).and_return(remote_census_api_response) - allow_any_instance_of(LocalCensus).to receive(:call).and_return(local_census_response) + remote_census_api_response = RemoteCensusApi::Response.new(get_habita_datos_response: { + get_habita_datos_return: { + datos_habitante: { item: { fecha_nacimiento_string: "1-1-1980" } } + } + }) - allow(RemoteCensusApi).to receive(:call).with(1, "12345678A") - allow(LocalCensus).to receive(:call).with(1, "12345678A") + local_census_response = LocalCensus::Response.new(create(:local_census_record)) - response = api.call(1, "12345678A") + expect_any_instance_of(RemoteCensusApi).to receive(:call).and_return(remote_census_api_response) + allow_any_instance_of(LocalCensus).to receive(:call).and_return(local_census_response) - expect(response).to eq(remote_census_api_response) + allow(RemoteCensusApi).to receive(:call).with(1, "12345678A", Date.parse("01/01/1983"), "28001") + allow(LocalCensus).to receive(:call).with(1, "12345678A") - Setting["feature.remote_census"] = nil + response = api.call(1, "12345678A", Date.parse("01/01/1983"), "28001") + + expect(response).to eq(remote_census_api_response) + + Setting["feature.remote_census"] = nil + end + + it "returns data from Remote Census API if it's available and valid without send date_of_birth and postal_code" do + Setting["feature.remote_census"] = true + + remote_census_api_response = RemoteCensusApi::Response.new(get_habita_datos_response: { + get_habita_datos_return: { + datos_habitante: { item: { fecha_nacimiento_string: "1-1-1980" } } + } + }) + + local_census_response = LocalCensus::Response.new(create(:local_census_record)) + + expect_any_instance_of(RemoteCensusApi).to receive(:call).and_return(remote_census_api_response) + allow_any_instance_of(LocalCensus).to receive(:call).and_return(local_census_response) + + allow(RemoteCensusApi).to receive(:call).with(1, "12345678A", nil, nil) + allow(LocalCensus).to receive(:call).with(1, "12345678A") + + response = api.call(1, "12345678A", nil, nil) + + expect(response).to eq(remote_census_api_response) + + Setting["feature.remote_census"] = nil + end end end diff --git a/spec/lib/remote_census_api_spec.rb b/spec/lib/remote_census_api_spec.rb index c023ce1b0..786616918 100644 --- a/spec/lib/remote_census_api_spec.rb +++ b/spec/lib/remote_census_api_spec.rb @@ -28,24 +28,196 @@ describe RemoteCensusApi do end it "returns the response for the first valid variant" do - allow(api).to receive(:get_response_body).with(1, "00123456").and_return(invalid_body) - allow(api).to receive(:get_response_body).with(1, "123456").and_return(invalid_body) - allow(api).to receive(:get_response_body).with(1, "0123456").and_return(valid_body) + date = Date.parse("01/01/1983") + allow(api).to receive(:get_response_body).with(1, "00123456", date, "28001").and_return(invalid_body) + allow(api).to receive(:get_response_body).with(1, "123456", date, "28001").and_return(invalid_body) + allow(api).to receive(:get_response_body).with(1, "0123456", date, "28001").and_return(valid_body) - response = api.call(1, "123456") + response = api.call(1, "123456", date, "28001") + + expect(response).to be_valid + expect(response.date_of_birth).to eq(Date.new(1980, 1, 1)) + end + + it "returns the response for the first valid variant without send date_of_birth and postal_code" do + allow(api).to receive(:get_response_body).with(1, "00123456", nil, nil).and_return(invalid_body) + allow(api).to receive(:get_response_body).with(1, "123456", nil, nil).and_return(invalid_body) + allow(api).to receive(:get_response_body).with(1, "0123456", nil, nil).and_return(valid_body) + + response = api.call(1, "123456", nil, nil) expect(response).to be_valid expect(response.date_of_birth).to eq(Date.new(1980, 1, 1)) end it "returns the last failed response" do - allow(api).to receive(:get_response_body).with(1, "00123456").and_return(invalid_body) - allow(api).to receive(:get_response_body).with(1, "123456").and_return(invalid_body) - allow(api).to receive(:get_response_body).with(1, "0123456").and_return(invalid_body) - response = api.call(1, "123456") + date = Date.parse("01/01/1983") + allow(api).to receive(:get_response_body).with(1, "00123456", date, "28001").and_return(invalid_body) + allow(api).to receive(:get_response_body).with(1, "123456", date, "28001").and_return(invalid_body) + allow(api).to receive(:get_response_body).with(1, "0123456", date, "28001").and_return(invalid_body) + response = api.call(1, "123456", date, "28001") expect(response).not_to be_valid end end + describe "request structure correctly filled" do + + before do + Setting["feature.remote_census"] = true + Setting["remote_census.request.structure"] = "{ request: + { codigo_institucion: 1, + codigo_portal: 1, + codigo_usuario: 1, + documento: 'xxx', + tipo_documento: 'xxx', + codigo_idioma: '102', + nivel: '3' } + }" + Setting["remote_census.request.document_type"] = "request.tipo_documento" + Setting["remote_census.request.document_number"] = "request.documento" + Setting["remote_census.request.date_of_birth"] = nil + Setting["remote_census.request.postal_code"] = nil + end + + it "with default values" do + document_type = "1" + document_number = "0123456" + + request = RemoteCensusApi.new.send(:request, document_type, document_number, nil, nil) + + expect(request).to eq ({:request=> + {:codigo_institucion=>1, + :codigo_portal=>1, + :codigo_usuario=>1, + :documento=>"0123456", + :tipo_documento=>"1", + :codigo_idioma=>"102", + :nivel=>"3"} + }) + end + + it "when send date_of_birth and postal_code but are not configured" do + document_type = "1" + document_number = "0123456" + date_of_birth = Date.new(1980, 1, 1) + postal_code = "28001" + + request = RemoteCensusApi.new.send(:request, document_type, document_number, date_of_birth, postal_code) + + expect(request).to eq ({:request=> + {:codigo_institucion=>1, + :codigo_portal=>1, + :codigo_usuario=>1, + :documento=>"0123456", + :tipo_documento=>"1", + :codigo_idioma=>"102", + :nivel=>"3"} + }) + end + + it "when send date_of_birth and postal_code but are configured" do + Setting["remote_census.request.structure"] = "{ request: + { codigo_institucion: 1, + codigo_portal: 1, + codigo_usuario: 1, + documento: nil, + tipo_documento: nil, + fecha_nacimiento: nil, + codigo_postal: nil, + codigo_idioma: '102', + nivel: '3' } + }" + Setting["remote_census.request.date_of_birth"] = "request.fecha_nacimiento" + Setting["remote_census.request.postal_code"] = "request.codigo_postal" + document_type = "1" + document_number = "0123456" + date_of_birth = Date.new(1980, 1, 1) + postal_code = "28001" + + request = RemoteCensusApi.new.send(:request, document_type, document_number, date_of_birth, postal_code) + + expect(request).to eq ({:request=> + {:codigo_institucion=>1, + :codigo_portal=>1, + :codigo_usuario=>1, + :documento=>"0123456", + :tipo_documento=>"1", + :fecha_nacimiento=>"1980-01-01", + :codigo_postal=>"28001", + :codigo_idioma=>"102", + :nivel=>"3"} + }) + end + + end + + describe "get_response_body" do + + before do + Setting["feature.remote_census"] = true + end + + it "return expected stubbed_response" do + document_type = "1" + document_number = "12345678Z" + + 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" + } + } + } + } + }) + end + + end + + describe "RemoteCensusApi::Response" do + + before do + Setting["feature.remote_census"] = true + 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 + end + + it "return expected response methods with default values" do + document_type = "1" + document_number = "12345678Z" + + get_response_body = RemoteCensusApi.new.send(:get_response_body, document_type, document_number, nil, nil) + response = RemoteCensusApi::Response.new(get_response_body) + + 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" + end + + end + end