From dd14fd864938df3ac2dacf324a977691875c662a Mon Sep 17 00:00:00 2001 From: taitus Date: Thu, 25 Apr 2019 16:57:17 +0200 Subject: [PATCH] Add new model presence validates - Only validate :date_of_birth and :postal_code presence when the application has configured Custom Census and their alias fields has values. - Only validate :year_of_birth presence when :date_of_birth is not configured to send to Custom Census --- app/models/officing/residence.rb | 4 +- spec/models/officing/residence_spec.rb | 75 ++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/app/models/officing/residence.rb b/app/models/officing/residence.rb index cc32fc862..346cd410d 100644 --- a/app/models/officing/residence.rb +++ b/app/models/officing/residence.rb @@ -10,7 +10,9 @@ class Officing::Residence validates :document_number, presence: true validates :document_type, presence: true - validates :year_of_birth, presence: true + validates :date_of_birth, presence: true, if: -> { Setting.force_presence_date_of_birth? } + validates :postal_code, presence: true, if: -> { Setting.force_presence_postal_code? } + validates :year_of_birth, presence: true, unless: -> { Setting.force_presence_date_of_birth? } validate :allowed_age validate :residence_in_madrid diff --git a/spec/models/officing/residence_spec.rb b/spec/models/officing/residence_spec.rb index f8c627fac..341b210ed 100644 --- a/spec/models/officing/residence_spec.rb +++ b/spec/models/officing/residence_spec.rb @@ -36,6 +36,81 @@ describe Officing::Residence do expect(residence).to be_valid end + describe "custom validations" do + + let(:custom_residence) { build(:officing_residence, + document_number: "12345678Z", + date_of_birth: "01/01/1980", + postal_code: "28001") } + + 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 + + after do + Setting["feature.remote_census"] = nil + Setting["remote_census.request.date_of_birth"] = nil + Setting["remote_census.request.postal_code"] = nil + end + + it "is valid" do + expect(custom_residence).to be_valid + end + + it "is not valid without a document number" do + custom_residence.document_number = nil + expect(custom_residence).not_to be_valid + end + + it "is not valid without a document type" do + custom_residence.document_type = nil + expect(custom_residence).not_to be_valid + end + + it "is valid without a year of birth when date_of_birth is present" do + custom_residence.year_of_birth = nil + + expect(custom_residence).to be_valid + end + + it "is not valid without a date of birth" do + custom_residence.date_of_birth = nil + + expect(custom_residence).not_to be_valid + end + + it "is not valid without a postal_code" do + custom_residence.postal_code = nil + + expect(custom_residence).not_to be_valid + end + + describe "dates" do + it "is valid with a valid date of birth" do + custom_residence = described_class.new("date_of_birth(3i)" => "1", + "date_of_birth(2i)" => "1", + "date_of_birth(1i)" => "1980") + expect(custom_residence.errors[:date_of_birth].size).to eq(0) + end + + it "is not valid without a date of birth" do + custom_residence = described_class.new("date_of_birth(3i)" => "", + "date_of_birth(2i)" => "", + "date_of_birth(1i)" => "") + expect(custom_residence).not_to be_valid + expect(custom_residence.errors[:date_of_birth]).to include("can't be blank") + end + end + + end + describe "allowed age" do it "is not valid if user is under allowed age" do allow_any_instance_of(described_class).to receive(:date_of_birth).and_return(15.years.ago)