Browse Source

CLDC-700: date validation (#97)

* Add date validation for mrc date

* validate the rest of the date fields
pull/99/head
kosiakkatrina 3 years ago committed by GitHub
parent
commit
26e423aaf7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      app/controllers/case_logs_controller.rb
  2. 1
      app/models/case_log.rb
  3. 21
      app/validations/date_validations.rb
  4. 2
      spec/controllers/case_logs_controller_spec.rb
  5. 52
      spec/features/case_log_spec.rb
  6. 4
      spec/fixtures/forms/test_form.json

8
app/controllers/case_logs_controller.rb

@ -107,9 +107,13 @@ private
day = params["case_log"]["#{question_key}(3i)"]
month = params["case_log"]["#{question_key}(2i)"]
year = params["case_log"]["#{question_key}(1i)"]
next unless day.present? && month.present? && year.present?
next unless [day, month, year].any?(&:present?)
result[question_key] = Date.new(year.to_i, month.to_i, day.to_i)
result[question_key] = if day.to_i.between?(1, 31) && month.to_i.between?(1, 12) && year.to_i.between?(2000, 2200)
Date.new(year.to_i, month.to_i, day.to_i)
else
Date.new(0, 1, 1)
end
end
next unless question_params

1
app/models/case_log.rb

@ -5,6 +5,7 @@ class CaseLogValidator < ActiveModel::Validator
include PropertyValidations
include FinancialValidations
include TenancyValidations
include DateValidations
def validate(record)
# If we've come from the form UI we only want to validate the specific fields

21
app/validations/date_validations.rb

@ -0,0 +1,21 @@
module DateValidations
def validate_property_major_repairs(record)
date_valid?("mrcdate", record)
end
def validate_tenancy_start_date(record)
date_valid?("startdate", record)
end
def validate_sale_completion_date(record)
date_valid?("sale_completion_date", record)
end
private
def date_valid?(question, record)
if record[question].is_a?(ActiveSupport::TimeWithZone) && record[question].year.zero?
record.errors.add question, "Please enter a valid date"
end
end
end

2
spec/controllers/case_logs_controller_spec.rb

@ -184,7 +184,7 @@ RSpec.describe CaseLogsController, type: :controller do
"mrcdate(1i)": "2021",
"mrcdate(2i)": "05",
"mrcdate(3i)": "04",
page: "major_repairs_date",
page: "property_major_repairs",
}
end
it "saves full and partial dates" do

52
spec/features/case_log_spec.rb

@ -489,4 +489,56 @@ RSpec.describe "Test Features" do
expect(page).to have_current_path("/case_logs/#{id}/rent")
end
end
describe "date validation", js: true do
def fill_in_date(case_log_id, question, day, month, year, path)
visit("/case_logs/#{case_log_id}/#{path}")
fill_in("#{question}_1i", with: year)
fill_in("#{question}_2i", with: month)
fill_in("#{question}_3i", with: day)
end
it "does not allow out of range dates to be submitted" do
fill_in_date(id, "case_log_mrcdate", 3100, 12, 2000, "property_major_repairs")
click_button("Save and continue")
expect(page).to have_current_path("/case_logs/#{id}/property_major_repairs")
fill_in_date(id, "case_log_mrcdate", 12, 1, 20_000, "property_major_repairs")
click_button("Save and continue")
expect(page).to have_current_path("/case_logs/#{id}/property_major_repairs")
fill_in_date(id, "case_log_mrcdate", 13, 100, 2020, "property_major_repairs")
click_button("Save and continue")
expect(page).to have_current_path("/case_logs/#{id}/property_major_repairs")
fill_in_date(id, "case_log_mrcdate", 21, 11, 2020, "property_major_repairs")
click_button("Save and continue")
expect(page).to have_current_path("/case_logs/#{id}/local_authority/check_answers")
end
it "does not allow non numeric inputs to be submitted" do
fill_in_date(id, "case_log_mrcdate", "abc", "de", "ff", "property_major_repairs")
click_button("Save and continue")
expect(page).to have_current_path("/case_logs/#{id}/property_major_repairs")
end
it "does not allow partial inputs to be submitted" do
fill_in_date(id, "case_log_mrcdate", 21, 12, nil, "property_major_repairs")
click_button("Save and continue")
expect(page).to have_current_path("/case_logs/#{id}/property_major_repairs")
fill_in_date(id, "case_log_mrcdate", 12, nil, 2000, "property_major_repairs")
click_button("Save and continue")
expect(page).to have_current_path("/case_logs/#{id}/property_major_repairs")
fill_in_date(id, "case_log_mrcdate", nil, 10, 2020, "property_major_repairs")
click_button("Save and continue")
expect(page).to have_current_path("/case_logs/#{id}/property_major_repairs")
end
it "allows valid inputs to be submitted" do
fill_in_date(id, "case_log_mrcdate", 21, 11, 2020, "property_major_repairs")
click_button("Save and continue")
expect(page).to have_current_path("/case_logs/#{id}/local_authority/check_answers")
end
end
end

4
spec/fixtures/forms/test_form.json vendored

@ -511,7 +511,7 @@
}
}
},
"major_repairs_date": {
"property_major_repairs": {
"questions": {
"mrcdate": {
"check_answer_label": "What was the major repairs completion date?",
@ -545,4 +545,4 @@
}
}
}
}
}

Loading…
Cancel
Save