Use shorter responses and configuration for specs

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 <javim@elretirao.net>
This commit is contained in:
Senén Rodero Rodríguez
2020-10-27 23:45:50 +01:00
committed by Javi Martín
parent 3ed4bdaff8
commit dd0f56f85f
4 changed files with 47 additions and 76 deletions

View File

@@ -51,9 +51,9 @@ class RemoteCensusApi
path_value = Setting["remote_census.response.gender"] path_value = Setting["remote_census.response.gender"]
case extract_value(path_value) case extract_value(path_value)
when "Varón" when "Male", "Varón"
"male" "male"
when "Mujer" when "Female", "Mujer"
"female" "female"
end end
end end
@@ -132,29 +132,21 @@ class RemoteCensusApi
def stubbed_valid_response def stubbed_valid_response
{ {
get_habita_datos_response: { response: {
get_habita_datos_return: { data: {
datos_habitante: { date_of_birth: "31-12-1980",
item: { document_number: "12345678Z",
fecha_nacimiento_string: "31-12-1980", gender: "Male",
identificador_documento: "12345678Z", name: "William",
descripcion_sexo: "Varón", surname: "Widmore",
nombre: "José", postal_code: "28013",
apellido1: "García" district_code: "01"
}
},
datos_vivienda: {
item: {
codigo_postal: "28013",
codigo_distrito: "01"
}
}
} }
} }
} }
end end
def stubbed_invalid_response def stubbed_invalid_response
{ get_habita_datos_response: { get_habita_datos_return: { datos_habitante: {}, datos_vivienda: {}}}} { response: { data: {}}}
end end
end end

View File

@@ -45,6 +45,9 @@ describe CensusCaller do
end end
describe "RemoteCensusApi", :remote_census do 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 it "returns remote census api response when it's available and response is valid" do
remote_census_api_response = RemoteCensusApi::Response.new(valid_body) remote_census_api_response = RemoteCensusApi::Response.new(valid_body)
allow_any_instance_of(RemoteCensusApi).to receive(:call).and_return(remote_census_api_response) allow_any_instance_of(RemoteCensusApi).to receive(:call).and_return(remote_census_api_response)

View File

@@ -4,21 +4,13 @@ describe RemoteCensusApi do
let(:api) { RemoteCensusApi.new } let(:api) { RemoteCensusApi.new }
describe "#call", :remote_census do describe "#call", :remote_census do
let(:invalid_body) { { get_habita_datos_response: { get_habita_datos_return: { datos_habitante: {}}}} } before do
let(:valid_body) do Setting["remote_census.response.valid"] = "response.data.date_of_birth"
{
get_habita_datos_response: {
get_habita_datos_return: {
datos_habitante: {
item: {
fecha_nacimiento_string: "1-1-1980"
}
}
}
}
}
end 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 it "returns the response for the first valid variant" do
date = Date.parse("01/01/1983") 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, "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) response = RemoteCensusApi.new.send(:get_response_body, document_type, document_number, nil, nil)
expect(response).to eq({ get_habita_datos_response: { expect(response).to eq({ response: {
get_habita_datos_return: { data: {
datos_habitante: { date_of_birth: "31-12-1980",
item: { document_number: "12345678Z",
fecha_nacimiento_string: "31-12-1980", gender: "Male",
identificador_documento: "12345678Z", name: "William",
descripcion_sexo: "Varón", surname: "Widmore",
nombre: "José", postal_code: "28013",
apellido1: "García" district_code: "01"
}
},
datos_vivienda: {
item: {
codigo_postal: "28013",
codigo_distrito: "01"
}
}
} }
} }
}) })
@@ -187,9 +171,8 @@ describe RemoteCensusApi do
expect(response.valid?).to eq true expect(response.valid?).to eq true
expect(response.date_of_birth).to eq Time.zone.local(1980, 12, 31).to_date expect(response.date_of_birth).to eq Time.zone.local(1980, 12, 31).to_date
expect(response.postal_code).to eq "28013" expect(response.postal_code).to eq "28013"
expect(response.district_code).to eq "01"
expect(response.gender).to eq "male" expect(response.gender).to eq "male"
expect(response.name).to eq "José García" expect(response.name).to eq "William Widmore"
end end
end end
end end

View File

@@ -106,35 +106,28 @@ RSpec.configure do |config|
end end
config.before(:each, :remote_census) do |example| config.before(:each, :remote_census) do |example|
Setting["remote_census.request.method_name"] = "get_habita_datos" Setting["feature.remote_census"] = true
Setting["remote_census.request.document_type"] = "request.tipo_documento" Setting["remote_census.request.method_name"] = "verify_residence"
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["remote_census.request.structure"] = '{ "request": Setting["remote_census.request.structure"] = '{ "request":
{ {
"codigo_institucion": 1, "document_type": "null",
"codigo_portal": 1, "document_number": "nil",
"codigo_usuario": 1, "date_of_birth": "null",
"documento": "nil", "postal_code": "nil"
"tipo_documento": "null",
"fecha_nacimiento": "null",
"codigo_postal": "nil",
"codigo_idioma": 102,
"nivel": 3
} }
}' }'
access_user_data = "get_habita_datos_response.get_habita_datos_return.datos_habitante.item" Setting["remote_census.request.document_type"] = "request.document_type"
access_residence_data = "get_habita_datos_response.get_habita_datos_return.datos_vivienda.item" Setting["remote_census.request.document_number"] = "request.document_number"
Setting["remote_census.response.date_of_birth"] = "#{access_user_data}.fecha_nacimiento_string" Setting["remote_census.request.date_of_birth"] = "request.date_of_birth"
Setting["remote_census.response.postal_code"] = "#{access_residence_data}.codigo_postal" Setting["remote_census.request.postal_code"] = "request.postal_code"
Setting["remote_census.response.district"] = "#{access_residence_data}.codigo_distrito" Setting["remote_census.response.date_of_birth"] = "response.data.date_of_birth"
Setting["remote_census.response.gender"] = "#{access_user_data}.descripcion_sexo" Setting["remote_census.response.postal_code"] = "response.data.postal_code"
Setting["remote_census.response.name"] = "#{access_user_data}.nombre" Setting["remote_census.response.district"] = "response.data.district_code"
Setting["remote_census.response.surname"] = "#{access_user_data}.apellido1" Setting["remote_census.response.gender"] = "response.data.gender"
Setting["remote_census.response.valid"] = access_user_data Setting["remote_census.response.name"] = "response.data.name"
Setting["feature.remote_census"] = true Setting["remote_census.response.surname"] = "response.data.surname"
Setting["remote_census.response.valid"] = "response.data.document_number"
end end
# Allows RSpec to persist some state between runs in order to support # Allows RSpec to persist some state between runs in order to support