Browse Source

More robust date validation (#830)

* More robust date validation

* Update app/controllers/form_controller.rb

Co-authored-by: James Rose <james@jbpr.net>

Co-authored-by: James Rose <james@jbpr.net>
pull/834/head
baarkerlounger 2 years ago committed by GitHub
parent
commit
bf7c81592d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      app/controllers/form_controller.rb
  2. 21
      spec/requests/form_controller_spec.rb

2
app/controllers/form_controller.rb

@ -89,7 +89,7 @@ private
year = params["case_log"]["#{question.id}(1i)"] year = params["case_log"]["#{question.id}(1i)"]
next unless [day, month, year].any?(&:present?) next unless [day, month, year].any?(&:present?)
result[question.id] = if day.to_i.between?(1, 31) && month.to_i.between?(1, 12) && year.to_i.between?(2000, 2200) result[question.id] = if Date.valid_date?(year.to_i, month.to_i, day.to_i) && year.to_i.between?(2000, 2200)
Date.new(year.to_i, month.to_i, day.to_i) Date.new(year.to_i, month.to_i, day.to_i)
else else
Date.new(0, 1, 1) Date.new(0, 1, 1)

21
spec/requests/form_controller_spec.rb

@ -255,6 +255,27 @@ RSpec.describe FormController, type: :request do
expect(Rails.logger).to receive(:info).with("User triggered validation(s) on: age1").once expect(Rails.logger).to receive(:info).with("User triggered validation(s) on: age1").once
post "/logs/#{case_log.id}/form", params: params post "/logs/#{case_log.id}/form", params: params
end end
context "when the number of days is too high for the month" do
let(:page_id) { "tenancy_start_date" }
let(:params) do
{
id: case_log.id,
case_log: {
page: page_id,
"startdate(3i)" => 31,
"startdate(2i)" => 6,
"startdate(1i)" => 2022,
},
}
end
it "validates the date correctly" do
post "/logs/#{case_log.id}/form", params: params
follow_redirect!
expect(page).to have_content("There is a problem")
end
end
end end
context "with valid answers" do context "with valid answers" do

Loading…
Cancel
Save