From 3e383d7c1f20b2dac1cecfeb8f3da7e2e57191fa Mon Sep 17 00:00:00 2001 From: taitus Date: Thu, 25 Apr 2019 16:27:40 +0200 Subject: [PATCH] Add new fields to form - Add :date_of_birth and :postal_code - Only display new fields when aplication has configured the custom census API and contains alias values for fields. Add 2 class Setting methods to check this feature: - force_presence_date_of_birth? - force_presence_postal_code? --- .../officing/residence_controller.rb | 3 +- app/models/setting.rb | 10 ++++ app/views/officing/residence/new.html.erb | 13 ++++- spec/features/officing/residence_spec.rb | 57 +++++++++++++++++++ spec/models/setting_spec.rb | 46 +++++++++++++++ 5 files changed, 127 insertions(+), 2 deletions(-) diff --git a/app/controllers/officing/residence_controller.rb b/app/controllers/officing/residence_controller.rb index cb7b96a64..38f7e3d9d 100644 --- a/app/controllers/officing/residence_controller.rb +++ b/app/controllers/officing/residence_controller.rb @@ -20,7 +20,8 @@ class Officing::ResidenceController < Officing::BaseController private def residence_params - params.require(:residence).permit(:document_number, :document_type, :year_of_birth) + params.require(:residence).permit(:document_number, :document_type, :year_of_birth, + :date_of_birth, :postal_code) end end diff --git a/app/models/setting.rb b/app/models/setting.rb index 61a6ebf30..3770aca1d 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -197,5 +197,15 @@ class Setting < ApplicationRecord self[name] = value unless find_by(key: name) end end + + def force_presence_date_of_birth? + Setting["feature.remote_census"].present? && + Setting["remote_census.request.date_of_birth"].present? + end + + def force_presence_postal_code? + Setting["feature.remote_census"].present? && + Setting["remote_census.request.postal_code"].present? + end end end diff --git a/app/views/officing/residence/new.html.erb b/app/views/officing/residence/new.html.erb index c736b1f23..c039e570a 100644 --- a/app/views/officing/residence/new.html.erb +++ b/app/views/officing/residence/new.html.erb @@ -12,7 +12,18 @@ placeholder: t("officing.residence.new.document_number"), autocomplete: "off" %> - + <% if Setting.force_presence_date_of_birth? %> +
+ <%= f.date_select :date_of_birth, + prompt: true, + start_year: 1900, end_year: minimum_required_age.years.ago.year %> +
+ <% end %> + <% if Setting.force_presence_postal_code? %> +
+ <%= f.text_field :postal_code, aria: {describedby: "postal-code-help-text"} %> +
+ <% end %>
<%= f.text_field :year_of_birth, type: "number", autocomplete: "off" %>
diff --git a/spec/features/officing/residence_spec.rb b/spec/features/officing/residence_spec.rb index 475835523..604aa36b4 100644 --- a/spec/features/officing/residence_spec.rb +++ b/spec/features/officing/residence_spec.rb @@ -116,4 +116,61 @@ describe "Residence", :with_frozen_time do expect(page).to have_content "Vote introduced!" end + context "With remote census configuration" 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 + end + + after do + Setting["feature.remote_census"] = nil + Setting["remote_census.request.date_of_birth"] = nil + Setting["remote_census.request.postal_code"] = nil + end + + describe "Display form fields according to the remote census configuration" do + + scenario "by default (without custom census) not display date_of_birth and postal_code" do + Setting["feature.remote_census"] = false + + within("#side_menu") do + click_link "Validate document" + end + + expect(page).to have_css("#residence_document_type") + expect(page).to have_css("#residence_document_number") + expect(page).to have_css("#residence_year_of_birth") + expect(page).not_to have_content("Date of birth") + expect(page).not_to have_css("#residence_postal_code") + end + + 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 + within("#side_menu") do + click_link "Validate document" + end + + 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" + + click_button "Validate document" + + expect(page).to have_content "Document verified with Census" + end + + end + end diff --git a/spec/models/setting_spec.rb b/spec/models/setting_spec.rb index 19d7b5374..282dc6d47 100644 --- a/spec/models/setting_spec.rb +++ b/spec/models/setting_spec.rb @@ -236,4 +236,50 @@ describe Setting do end end end + + describe ".force_presence_date_of_birth?" do + + it "return false when feature remote_census is not active" do + Setting["feature.remote_census"] = false + + expect(Setting.force_presence_date_of_birth?).to eq false + end + + it "return false when feature remote_census is active and date_of_birth is nil" do + Setting["feature.remote_census"] = true + Setting["remote_census.request.date_of_birth"] = nil + + expect(Setting.force_presence_date_of_birth?).to eq false + end + + it "return true when feature remote_census is active and date_of_birth is empty" do + Setting["feature.remote_census"] = true + Setting["remote_census.request.date_of_birth"] = "some.value" + + expect(Setting.force_presence_date_of_birth?).to eq true + end + end + + describe ".force_presence_postal_code?" do + + it "return false when feature remote_census is not active" do + Setting["feature.remote_census"] = false + + expect(Setting.force_presence_postal_code?).to eq false + end + + it "return false when feature remote_census is active and postal_code is nil" do + Setting["feature.remote_census"] = true + Setting["remote_census.request.postal_code"] = nil + + expect(Setting.force_presence_postal_code?).to eq false + end + + it "return true when feature remote_census is active and postal_code is empty" do + Setting["feature.remote_census"] = true + Setting["remote_census.request.postal_code"] = "some.value" + + expect(Setting.force_presence_postal_code?).to eq true + end + end end