diff --git a/lib/census_caller.rb b/lib/census_caller.rb
index c6f55d530..1ad81c4ff 100644
--- a/lib/census_caller.rb
+++ b/lib/census_caller.rb
@@ -1,5 +1,7 @@
class CensusCaller
def call(document_type, document_number, date_of_birth, postal_code)
+ return Response.new if document_number.blank? || document_type.blank?
+
if Setting["feature.remote_census"].present?
response = RemoteCensusApi.new.call(document_type, document_number, date_of_birth, postal_code)
else
@@ -9,4 +11,10 @@ class CensusCaller
response
end
+
+ class Response
+ def valid?
+ false
+ end
+ end
end
diff --git a/lib/document_parser.rb b/lib/document_parser.rb
index 60ebd9d59..7fd4cc92f 100644
--- a/lib/document_parser.rb
+++ b/lib/document_parser.rb
@@ -1,5 +1,7 @@
module DocumentParser
def get_document_number_variants(document_type, document_number)
+ return [] if document_number.blank?
+
# Delete all non-alphanumerics
document_number = document_number.to_s.gsub(/[^0-9A-Za-z]/i, "")
variants = []
diff --git a/lib/remote_census_api.rb b/lib/remote_census_api.rb
index 25f3c083a..e91de7e12 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
@@ -73,11 +73,11 @@ 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
- stubbed_response(document_type, document_number)
+ stubbed_invalid_response
end
end
@@ -118,43 +118,11 @@ class RemoteCensusApi
to_set[final_key] = value
end
- def end_point_available?
- Rails.env.staging? || Rails.env.preproduction? || Rails.env.production?
- end
-
- def stubbed_response(document_type, document_number)
- if (document_number == "12345678Z" || document_number == "12345678Y") && document_type == "1"
- stubbed_valid_response
- else
- stubbed_invalid_response
- end
- end
-
- 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"
- }
- }
- }
- }
- }
+ def end_point_defined?
+ Setting["remote_census.general.endpoint"].present?
end
def stubbed_invalid_response
- { get_habita_datos_response: { get_habita_datos_return: { datos_habitante: {}, datos_vivienda: {}}}}
+ {}
end
end
diff --git a/spec/fixtures/files/remote_census_api/invalid.xml b/spec/fixtures/files/remote_census_api/invalid.xml
new file mode 100644
index 000000000..6d8f6d1f5
--- /dev/null
+++ b/spec/fixtures/files/remote_census_api/invalid.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
diff --git a/spec/fixtures/files/remote_census_api/valid.xml b/spec/fixtures/files/remote_census_api/valid.xml
new file mode 100644
index 000000000..0ca6f7360
--- /dev/null
+++ b/spec/fixtures/files/remote_census_api/valid.xml
@@ -0,0 +1,15 @@
+
+
+
+
+ 31/12/1980
+ 12345678Z
+ Male
+ William
+ Widmore
+ 28013
+ 01
+
+
+
+
diff --git a/spec/helpers/signature_sheets_helper_spec.rb b/spec/helpers/signature_sheets_helper_spec.rb
index 8e2e5efcc..e9b6a2953 100644
--- a/spec/helpers/signature_sheets_helper_spec.rb
+++ b/spec/helpers/signature_sheets_helper_spec.rb
@@ -8,12 +8,11 @@ describe SignatureSheetsHelper do
end
end
- describe "#required_fields_to_verify_text_help with remote_census active" do
- before do
- Setting["feature.remote_census"] = true
- end
-
+ describe "#required_fields_to_verify_text_help with remote_census active", :remote_census do
it "returns text help when date_of_birth and postal_code are not required" do
+ Setting["remote_census.request.date_of_birth"] = nil
+ Setting["remote_census.request.postal_code"] = nil
+
text_help_1 = "To verify a user, your application needs: Document number"
text_help_2 = "Required fields for each user must be separated by commas and each user must be separated by semicolons."
text_example = "Example: 12345678Z; 87654321Y"
@@ -24,7 +23,7 @@ describe SignatureSheetsHelper do
end
it "returns text help when date_of_birth is required" do
- Setting["remote_census.request.date_of_birth"] = "some.value"
+ Setting["remote_census.request.postal_code"] = nil
text_help_1 = "To verify a user, your application needs: Document number, Day of birth (dd/mm/yyyy)"
text_help_2 = "Required fields for each user must be separated by commas and each user must be separated by semicolons."
@@ -33,12 +32,10 @@ describe SignatureSheetsHelper do
expect(required_fields_to_verify_text_help).to include(text_help_1)
expect(required_fields_to_verify_text_help).to include(text_help_2)
expect(example_text_help).to include(text_example)
-
- Setting["remote_census.request.date_of_birth"] = nil
end
it "returns text help when postal_code is required" do
- Setting["remote_census.request.postal_code"] = "some.value"
+ Setting["remote_census.request.date_of_birth"] = nil
text_help_1 = "To verify a user, your application needs: Document number and Postal Code"
text_help_2 = "Required fields for each user must be separated by commas and each user must be separated by semicolons."
@@ -47,14 +44,9 @@ describe SignatureSheetsHelper do
expect(required_fields_to_verify_text_help).to include(text_help_1)
expect(required_fields_to_verify_text_help).to include(text_help_2)
expect(example_text_help).to include(text_example)
-
- Setting["remote_census.request.postal_code"] = nil
end
it "returns text help when date_of_birth and postal_code are required" do
- Setting["remote_census.request.date_of_birth"] = "some.value"
- Setting["remote_census.request.postal_code"] = "some.value"
-
text_help_1 = "To verify a user, your application needs: Document number, Day of birth (dd/mm/yyyy) and Postal Code"
text_help_2 = "Required fields for each user must be separated by commas and each user must be separated by semicolons."
text_example = "Example: 12345678Z, 01/01/1980, 28001; 87654321Y, 01/02/1990, 28002"
@@ -62,9 +54,6 @@ describe SignatureSheetsHelper do
expect(required_fields_to_verify_text_help).to include(text_help_1)
expect(required_fields_to_verify_text_help).to include(text_help_2)
expect(example_text_help).to include(text_example)
-
- Setting["remote_census.request.postal_code"] = nil
- Setting["remote_census.request.postal_code"] = nil
end
end
end
diff --git a/spec/lib/census_caller_spec.rb b/spec/lib/census_caller_spec.rb
index 5f65076f2..d4f1d550b 100644
--- a/spec/lib/census_caller_spec.rb
+++ b/spec/lib/census_caller_spec.rb
@@ -13,6 +13,16 @@ describe CensusCaller do
{ get_habita_datos_response: { get_habita_datos_return: { datos_habitante: {}}}}
end
+ it "returns invalid response when document_number or document_type are empty" do
+ response = api.call(1, "", nil, nil)
+
+ expect(response).not_to be_valid
+
+ response = api.call("", "12345678A", nil, nil)
+
+ expect(response).not_to be_valid
+ end
+
it "returns local census response when census api response is invalid" do
census_api_response = CensusApi::Response.new(invalid_body)
allow_any_instance_of(CensusApi).to receive(:call).and_return(census_api_response)
@@ -34,12 +44,9 @@ describe CensusCaller do
end
end
- describe "RemoteCensusApi" do
- before do
- Setting["feature.remote_census"] = true
- access_user_data = "get_habita_datos_response.get_habita_datos_return.datos_habitante.item"
- Setting["remote_census.response.valid"] = access_user_data
- 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)
diff --git a/spec/lib/document_parser_spec.rb b/spec/lib/document_parser_spec.rb
index d44f8c5f0..476f3e2ee 100644
--- a/spec/lib/document_parser_spec.rb
+++ b/spec/lib/document_parser_spec.rb
@@ -3,6 +3,10 @@ include DocumentParser
describe DocumentParser do
describe "#get_document_number_variants" do
+ it "returns no variants when document_number is not defined" do
+ expect(DocumentParser.get_document_number_variants("1", "")).to be_empty
+ end
+
it "trims and cleans up entry" do
expect(DocumentParser.get_document_number_variants(2, " 1 2@ 34")).to eq(["1234"])
end
diff --git a/spec/lib/remote_census_api_spec.rb b/spec/lib/remote_census_api_spec.rb
index 85adafd7c..2e25b6e1e 100644
--- a/spec/lib/remote_census_api_spec.rb
+++ b/spec/lib/remote_census_api_spec.rb
@@ -3,214 +3,100 @@ require "rails_helper"
describe RemoteCensusApi do
let(:api) { RemoteCensusApi.new }
- describe "#call" 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"
- }
- }
- }
+ describe "#call", :remote_census do
+ it "returns a valid response correctly fullfilled when remote response returns a valid response" do
+ %w[12345678 12345678z].each { mock_invalid_remote_census_response }
+ %w[12345678Z].each { mock_valid_remote_census_response }
+
+ response = api.call("1", "12345678Z", Date.parse("31/12/1980"), "28013")
+
+ expect(response).to be_valid
+ expect(response.date_of_birth).to eq Time.zone.local(1980, 12, 31).to_date
+ expect(response.postal_code).to eq "28013"
+ expect(response.gender).to eq "male"
+ expect(response.name).to eq "William Widmore"
+ end
+
+ it "returns an invalid response all variants return invalid responses" do
+ %w[99999999 99999999z 99999999Z].each { mock_invalid_remote_census_response }
+
+ response = api.call("1", "99999999Z", Date.parse("31/12/1980"), "28013")
+
+ expect(response).not_to be_valid
+ end
+
+ describe "request messages" do
+ let(:valid_response) { File.read("spec/fixtures/files/remote_census_api/valid.xml") }
+
+ def request_with(params)
+ { "request" => params }
+ end
+
+ it "includes date_of_birth and postal_code when request structure is configured" do
+ params = {
+ "document_type" => "1",
+ "date_of_birth" => "1980-12-31",
+ "postal_code" => "28013"
}
- }
+
+ savon.expects(:verify_residence)
+ .with(message: request_with(params.merge("document_number" => "12345678")))
+ .returns(valid_response)
+
+ api.call("1", "12345678Z", Date.parse("31/12/1980"), "28013")
+ end
+
+ it "does not include date_of_birth and postal_code when not configured" do
+ Setting["remote_census.request.date_of_birth"] = nil
+ Setting["remote_census.request.postal_code"] = nil
+ Setting["remote_census.request.structure"] = '{ "request":
+ {
+ "document_number": "nil",
+ "document_type": "null"
+ }
+ }'
+
+ params = { "document_type" => "1" }
+
+ savon.expects(:verify_residence)
+ .with(message: request_with(params.merge("document_number" => "12345678")))
+ .returns(valid_response)
+
+ api.call("1", "12345678Z", Date.parse("31/12/1980"), "28013")
+ end
+
+ it "includes custom parameters when configured" do
+ Setting["remote_census.request.structure"] = '{ "request":
+ {
+ "document_type": "null",
+ "document_number": "nil",
+ "date_of_birth": "null",
+ "postal_code": "nil",
+ "api_key": "your_api_key"
+ }
+ }'
+
+ params = {
+ "document_type" => "1",
+ "date_of_birth" => "1980-12-31",
+ "postal_code" => "28013",
+ "api_key" => "your_api_key"
+ }
+
+ savon.expects(:verify_residence)
+ .with(message: request_with(params.merge("document_number" => "12345678")))
+ .returns(valid_response)
+
+ api.call("1", "12345678Z", Date.parse("31/12/1980"), "28013")
+ end
end
- 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
+ it "returns an invalid response when endpoint is not defined" do
+ allow_any_instance_of(RemoteCensusApi).to receive(:end_point_defined?).and_return(false)
- 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)
- 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", 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
- 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")
+ response = api.call("1", "12345678Z", Date.parse("01/01/1983"), "28013")
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": null,
- "tipo_documento": null,
- "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": "null",
- "fecha_nacimiento": "null",
- "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
diff --git a/spec/models/officing/residence_spec.rb b/spec/models/officing/residence_spec.rb
index 1925ac6f7..621e24ab8 100644
--- a/spec/models/officing/residence_spec.rb
+++ b/spec/models/officing/residence_spec.rb
@@ -34,26 +34,17 @@ describe Officing::Residence do
expect(residence).to be_valid
end
- describe "custom validations" do
+ describe "custom validations", :remote_census do
let(:custom_residence) do
build(:officing_residence,
document_number: "12345678Z",
- date_of_birth: "01/01/1980",
+ date_of_birth: Date.parse("01/01/1980"),
postal_code: "28001")
end
- before do
- Setting["feature.remote_census"] = true
- Setting["remote_census.request.date_of_birth"] = "some.value"
- Setting["remote_census.request.postal_code"] = "some.value"
- 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
-
it "is valid" do
+ mock_valid_remote_census_response
+
expect(custom_residence).to be_valid
end
@@ -70,27 +61,34 @@ describe Officing::Residence do
it "is valid without a year of birth when date_of_birth is present" do
custom_residence.year_of_birth = nil
+ mock_valid_remote_census_response
+
expect(custom_residence).to be_valid
end
it "is not valid without a date of birth" do
custom_residence.date_of_birth = nil
+ mock_valid_remote_census_response
+
expect(custom_residence).not_to be_valid
end
it "is not valid without a postal_code" do
custom_residence.postal_code = nil
+ mock_valid_remote_census_response
+
expect(custom_residence).not_to be_valid
end
describe "dates" do
- it "is valid with a valid date of birth" do
+ it "is not valid but not because date of birth" do
custom_residence = Officing::Residence.new("date_of_birth(3i)" => "1",
"date_of_birth(2i)" => "1",
"date_of_birth(1i)" => "1980")
+ expect(custom_residence).not_to be_valid
expect(custom_residence.errors[:date_of_birth]).to be_empty
end
@@ -110,6 +108,11 @@ describe Officing::Residence do
:invalid,
document_number: "12345678Z",
postal_code: "00001")
+
+ %w[12345678 12345678z 12345678Z].each do
+ mock_invalid_remote_census_response
+ end
+
residence.save
expect(FailedCensusCall.count).to eq(1)
diff --git a/spec/models/signature_sheet_spec.rb b/spec/models/signature_sheet_spec.rb
index 8eb16b6d3..c32e94b5c 100644
--- a/spec/models/signature_sheet_spec.rb
+++ b/spec/models/signature_sheet_spec.rb
@@ -112,14 +112,15 @@ describe SignatureSheet do
expect(signature_sheet.processed).to eq(true)
end
- context "with remote census active" do
- before do
- Setting["feature.remote_census"] = true
- end
-
+ context "with remote census active", :remote_census do
it "creates signatures for each group with document_number" do
+ Setting["remote_census.request.date_of_birth"] = nil
+ Setting["remote_census.request.postal_code"] = nil
+
required_fields_to_verify = "123A; 456B"
signature_sheet = create(:signature_sheet, required_fields_to_verify: required_fields_to_verify)
+
+ %w[123A 456B].each { mock_valid_remote_census_response }
signature_sheet.verify_signatures
expect(Signature.count).to eq(2)
@@ -132,10 +133,12 @@ describe SignatureSheet do
end
it "creates signatures for each group with document_number and date_of_birth" do
- Setting["remote_census.request.date_of_birth"] = "some.value"
+ Setting["remote_census.request.postal_code"] = nil
required_fields_to_verify = "123A, 01/01/1980; 456B, 01/02/1980"
signature_sheet = create(:signature_sheet, required_fields_to_verify: required_fields_to_verify)
+
+ %w[123A 456B].each { mock_valid_remote_census_response }
signature_sheet.verify_signatures
expect(Signature.count).to eq(2)
@@ -145,15 +148,15 @@ describe SignatureSheet do
expect(Signature.last.document_number).to eq("456B")
expect(Signature.last.date_of_birth).to eq(Date.parse("01/02/1980"))
expect(Signature.last.postal_code).to eq(nil)
-
- Setting["remote_census.request.date_of_birth"] = nil
end
it "creates signatures for each group with document_number and postal_code" do
- Setting["remote_census.request.postal_code"] = "some.value"
+ Setting["remote_census.request.date_of_birth"] = nil
required_fields_to_verify = "123A, 28001; 456B, 28002"
signature_sheet = create(:signature_sheet, required_fields_to_verify: required_fields_to_verify)
+
+ %w[123A 456B].each { mock_valid_remote_census_response }
signature_sheet.verify_signatures
expect(Signature.count).to eq(2)
@@ -163,16 +166,13 @@ describe SignatureSheet do
expect(Signature.last.document_number).to eq("456B")
expect(Signature.last.date_of_birth).to eq(nil)
expect(Signature.last.postal_code).to eq("28002")
-
- Setting["remote_census.request.postal_code"] = nil
end
it "creates signatures for each group with document_number, postal_code and date_of_birth" do
- Setting["remote_census.request.date_of_birth"] = "some.value"
- Setting["remote_census.request.postal_code"] = "some.value"
-
required_fields_to_verify = "123A, 01/01/1980, 28001; 456B, 01/02/1980, 28002"
signature_sheet = create(:signature_sheet, required_fields_to_verify: required_fields_to_verify)
+
+ %w[123A 456B].each { mock_valid_remote_census_response }
signature_sheet.verify_signatures
expect(Signature.count).to eq(2)
@@ -182,9 +182,6 @@ describe SignatureSheet do
expect(Signature.last.document_number).to eq("456B")
expect(Signature.last.date_of_birth).to eq(Date.parse("01/02/1980"))
expect(Signature.last.postal_code).to eq("28002")
-
- Setting["remote_census.request.date_of_birth"] = nil
- Setting["remote_census.request.postal_code"] = nil
end
end
end
diff --git a/spec/models/signature_spec.rb b/spec/models/signature_spec.rb
index c55136d47..aa524a670 100644
--- a/spec/models/signature_spec.rb
+++ b/spec/models/signature_spec.rb
@@ -225,22 +225,13 @@ describe Signature do
end
describe "document in census throught CustomCensusApi" do
- before do
- Setting["feature.remote_census"] = true
- Setting["remote_census.request.date_of_birth"] = "some.value"
- Setting["remote_census.request.postal_code"] = "some.value"
- 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
-
- it "calls assign_vote_to_user" do
+ it "calls assign_vote_to_user", :remote_census do
signature = create(:signature, document_number: "12345678Z",
date_of_birth: "31/12/1980",
postal_code: "28013")
+ mock_valid_remote_census_response
+
expect_any_instance_of(Signature).to receive(:assign_vote_to_user).exactly(1).times
signature.verify
diff --git a/spec/models/verification/management/document_spec.rb b/spec/models/verification/management/document_spec.rb
index 5e8752108..f01a915d4 100644
--- a/spec/models/verification/management/document_spec.rb
+++ b/spec/models/verification/management/document_spec.rb
@@ -28,18 +28,7 @@ describe Verification::Management::Document do
expect(verification_document).to be_valid
end
- describe "custom validations with RemoteCensus enabled" do
- before do
- Setting["feature.remote_census"] = true
- Setting["remote_census.request.date_of_birth"] = "some.value"
- Setting["remote_census.request.postal_code"] = "some.value"
- 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
-
+ describe "custom validations with RemoteCensus enabled", :remote_census do
it "is valid" do
expect(verification_document).to be_valid
end
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 1996c2bb1..00b5c8c9e 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -105,6 +105,38 @@ RSpec.configure do |config|
allow(Time).to receive(:zone).and_return(application_zone)
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":
+ {
+ "document_type": "null",
+ "document_number": "nil",
+ "date_of_birth": "null",
+ "postal_code": "nil"
+ }
+ }'
+
+ 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"
+
+ savon.mock!
+ end
+
+ config.after(:each, :remote_census) do
+ savon.unmock!
+ end
+
# Allows RSpec to persist some state between runs in order to support
# the `--only-failures` and `--next-failure` CLI options.
config.example_status_persistence_file_path = "spec/examples.txt"
diff --git a/spec/support/common_actions.rb b/spec/support/common_actions.rb
index 42d5432b7..ab84b1500 100644
--- a/spec/support/common_actions.rb
+++ b/spec/support/common_actions.rb
@@ -8,6 +8,7 @@ module CommonActions
include Notifications
include Polls
include Proposals
+ include RemoteCensusMock
include Tags
include Translations
include Users
diff --git a/spec/support/common_actions/remote_census_mock.rb b/spec/support/common_actions/remote_census_mock.rb
new file mode 100644
index 000000000..b140c17e4
--- /dev/null
+++ b/spec/support/common_actions/remote_census_mock.rb
@@ -0,0 +1,30 @@
+require "savon/mock/spec_helper"
+
+module RemoteCensusMock
+ include Savon::SpecHelper
+ include DocumentParser
+
+ def mock_valid_remote_census_response
+ mock_remote_census_response(File.read("spec/fixtures/files/remote_census_api/valid.xml"))
+ end
+
+ def mock_invalid_remote_census_response
+ mock_remote_census_response(File.read("spec/fixtures/files/remote_census_api/invalid.xml"))
+ end
+
+ def mock_invalid_signature_sheet_remote_census_response
+ xml = File.read("spec/fixtures/files/remote_census_api/invalid.xml")
+
+ Signature.new.document_types.each do |document_type|
+ get_document_number_variants(document_type, "12345678Z").each do
+ mock_remote_census_response(xml)
+ end
+ end
+ end
+
+ def mock_remote_census_response(xml)
+ savon.expects(Setting["remote_census.request.method_name"].to_sym)
+ .with(message: :any)
+ .returns(xml)
+ end
+end
diff --git a/spec/system/admin/signature_sheets_spec.rb b/spec/system/admin/signature_sheets_spec.rb
index 57d57d350..254d156c4 100644
--- a/spec/system/admin/signature_sheets_spec.rb
+++ b/spec/system/admin/signature_sheets_spec.rb
@@ -76,16 +76,10 @@ describe "Signature sheets" do
end
end
- context "Create throught all required_fields_to_verify of custom census api" do
+ context "Create throught all required_fields_to_verify of custom census api", :remote_census do
before do
- Setting["feature.remote_census"] = true
- Setting["remote_census.request.date_of_birth"] = "some.value"
- Setting["remote_census.request.postal_code"] = "some.value"
- 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
+ mock_valid_remote_census_response
+ mock_invalid_signature_sheet_remote_census_response
end
scenario "Proposal" do
diff --git a/spec/system/management/document_verifications_spec.rb b/spec/system/management/document_verifications_spec.rb
index c27f5395d..53a2c0c7e 100644
--- a/spec/system/management/document_verifications_spec.rb
+++ b/spec/system/management/document_verifications_spec.rb
@@ -53,18 +53,7 @@ describe "DocumentVerifications" do
end
end
- context "Remote Census API" do
- before do
- Setting["feature.remote_census"] = true
- Setting["remote_census.request.date_of_birth"] = "some.value"
- Setting["remote_census.request.postal_code"] = "some.value"
- 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
-
+ context "Remote Census API", :remote_census do
scenario "Verifying a user which does not exist and is not in the census shows an error" do
expect_any_instance_of(Verification::Management::Document).to receive(:in_census?).
and_return(false)
@@ -78,7 +67,10 @@ describe "DocumentVerifications" do
expect(page).to have_content "This document is not registered"
end
- scenario "Verifying a user which does exists in the census but not in the db redirects allows sending an email" do
+ scenario "Verifying a user which does exists in the census but not in the db
+ redirects allows sending an email" do
+ mock_valid_remote_census_response
+
visit management_document_verifications_path
fill_in "document_verification_document_number", with: "12345678Z"
select_date "31-December-1980", from: "document_verification_date_of_birth"
diff --git a/spec/system/officing/residence_spec.rb b/spec/system/officing/residence_spec.rb
index aa63dcf21..6facb7c43 100644
--- a/spec/system/officing/residence_spec.rb
+++ b/spec/system/officing/residence_spec.rb
@@ -111,11 +111,8 @@ describe "Residence", :with_frozen_time do
expect(page).to have_content "Vote introduced!"
end
- context "With remote census configuration" do
+ context "With remote census configuration", :remote_census do
before do
- Setting["feature.remote_census"] = true
- Setting["remote_census.request.date_of_birth"] = "some.value"
- Setting["remote_census.request.postal_code"] = "some.value"
create(:poll_officer_assignment, officer: officer)
login_through_form_as_officer(officer.user)
visit officing_root_path
@@ -137,9 +134,6 @@ describe "Residence", :with_frozen_time do
end
scenario "with all custom census not display year_of_birth" do
- Setting["remote_census.request.date_of_birth"] = "some.value"
- Setting["remote_census.request.postal_code"] = "some.value"
-
within("#side_menu") do
click_link "Validate document"
end
@@ -153,11 +147,8 @@ describe "Residence", :with_frozen_time do
end
scenario "can verify voter with date_of_birth and postal_code fields" 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
+ mock_valid_remote_census_response
+
within("#side_menu") do
click_link "Validate document"
end
@@ -165,7 +156,7 @@ describe "Residence", :with_frozen_time do
select "DNI", from: "residence_document_type"
fill_in "residence_document_number", with: "12345678Z"
select_date "31-December-1980", from: "residence_date_of_birth"
- fill_in "residence_postal_code", with: "28001"
+ fill_in "residence_postal_code", with: "28013"
click_button "Validate document"
diff --git a/spec/system/verification/residence_spec.rb b/spec/system/verification/residence_spec.rb
index a08ed5b23..7b72e5523 100644
--- a/spec/system/verification/residence_spec.rb
+++ b/spec/system/verification/residence_spec.rb
@@ -20,16 +20,10 @@ describe "Residence" do
expect(page).to have_content "Residence verified"
end
- scenario "Verify resident throught RemoteCensusApi" 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
+ scenario "Verify resident throught RemoteCensusApi", :remote_census do
user = create(:user)
login_as(user)
+ mock_valid_remote_census_response
visit account_path
click_link "Verify my account"
@@ -42,7 +36,6 @@ describe "Residence" do
click_button "Verify residence"
expect(page).to have_content "Residence verified"
- Setting["feature.remote_census"] = nil
end
scenario "Residence form use min age to participate" do