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
This commit is contained in:
taitus
2019-04-25 16:57:17 +02:00
committed by Javi Martín
parent 30c9445c53
commit dd14fd8649
2 changed files with 78 additions and 1 deletions

View File

@@ -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

View File

@@ -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)