Browse Source

Add date validation for mrc date

pull/97/head
Kat 4 years ago
parent
commit
1e79e3f780
  1. 8
      app/controllers/case_logs_controller.rb
  2. 3
      app/models/case_log.rb
  3. 7
      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)"] day = params["case_log"]["#{question_key}(3i)"]
month = params["case_log"]["#{question_key}(2i)"] month = params["case_log"]["#{question_key}(2i)"]
year = params["case_log"]["#{question_key}(1i)"] 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 end
next unless question_params next unless question_params

3
app/models/case_log.rb

@ -5,6 +5,7 @@ class CaseLogValidator < ActiveModel::Validator
include PropertyValidations include PropertyValidations
include FinancialValidations include FinancialValidations
include TenancyValidations include TenancyValidations
include DateValidations
def validate(record) def validate(record)
# If we've come from the form UI we only want to validate the specific fields # If we've come from the form UI we only want to validate the specific fields
@ -184,7 +185,7 @@ class CaseLog < ApplicationRecord
mrcdate.month mrcdate.month
end end
end end
def mrcyear def mrcyear
if mrcdate.present? if mrcdate.present?
mrcdate.year mrcdate.year

7
app/validations/date_validations.rb

@ -0,0 +1,7 @@
module DateValidations
def validate_property_major_repairs(record)
if record.mrcdate.is_a?(ActiveSupport::TimeWithZone) && record.mrcdate.year.zero?
record.errors.add :mrcdate, "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(1i)": "2021",
"mrcdate(2i)": "05", "mrcdate(2i)": "05",
"mrcdate(3i)": "04", "mrcdate(3i)": "04",
page: "major_repairs_date", page: "property_major_repairs",
} }
end end
it "saves full and partial dates" do 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") expect(page).to have_current_path("/case_logs/#{id}/rent")
end end
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 end

4
spec/fixtures/forms/test_form.json vendored

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

Loading…
Cancel
Save