Browse Source

testing for invalid startdate in controller

pull/760/head
JG 3 years ago
parent
commit
5357a29979
  1. 33
      app/controllers/locations_controller.rb
  2. 9
      app/models/location.rb
  3. 3
      config/locales/en.yml
  4. 14
      spec/requests/locations_controller_spec.rb

33
app/controllers/locations_controller.rb

@ -15,15 +15,29 @@ class LocationsController < ApplicationController
@location = Location.new
end
def create
@location = if Date.valid_date?(location_params["startdate(3i)"].to_i, location_params["startdate(2i)"].to_i, location_params["startdate(1i)"].to_i)
Location.new(location_params.except("startdate(3i)", "startdate(2i)", "startdate(1i)"))
else
Location.new(location_params) end
def validation_errors(scheme_params)
scheme_params.each_key do |key|
if key == "support_services_provider"
@scheme.errors.add("support_services_provider_before_type_cast".to_sym) if scheme_params[key].to_s.empty?
elsif scheme_params[key].to_s.empty?
@scheme.errors.add(key.to_sym)
end
end
end
def create
if valid_date_params?(location_params)
@location = Location.new(location_params)
if @location.save
location_params[:add_another_location] == "Yes" ? redirect_to(new_location_path(id: @scheme.id)) : redirect_to(scheme_check_answers_path(scheme_id: @scheme.id))
else
@location.errors.add(:startdate) unless Date.valid_date?(location_params["startdate(3i)"].to_i, location_params["startdate(2i)"].to_i, location_params["startdate(1i)"].to_i)
render :new, status: :unprocessable_entity
end
else
@location = Location.new(location_params.except("startdate(3i)", "startdate(2i)", "startdate(1i)"))
@location.valid?
@location.errors.add(:startdate)
render :new, status: :unprocessable_entity
end
end
@ -49,6 +63,15 @@ class LocationsController < ApplicationController
private
def valid_date_params?(location_params)
is_integer?(location_params["startdate(1i)"]) && is_integer?(location_params["startdate(2i)"]) && is_integer?(location_params["startdate(3i)"]) &&
Date.valid_date?(location_params["startdate(1i)"].to_i, location_params["startdate(2i)"].to_i, location_params["startdate(3i)"].to_i)
end
def is_integer?(string)
string.to_i.to_s == string
end
def find_scheme
@scheme = if %w[new create index edit_name].include?(action_name)
Scheme.find(params[:id])

9
app/models/location.rb

@ -1,5 +1,5 @@
class Location < ApplicationRecord
validate :validate_postcode, :validate_startdate
validate :validate_postcode
validates :units, :type_of_unit, presence: true
belongs_to :scheme
@ -49,13 +49,6 @@ private
PIO = PostcodeService.new
def validate_startdate
unless startdate.nil? || (startdate.is_a?(ActiveSupport::TimeWithZone) && !startdate.year.zero?)
error_message = I18n.t("validations.date.invalid_date")
errors.add :startdate, error_message
end
end
def validate_postcode
if postcode.nil? || !postcode&.match(POSTCODE_REGEXP)
error_message = I18n.t("validations.postcode")

3
config/locales/en.yml

@ -69,6 +69,9 @@ en:
invalid: "Select who provides the support services used by this scheme"
location:
attributes:
startdate:
invalid: "Enter a date in the correct format, for example 1 9 2022"
units:
blank: "Enter total number of units at this location"
type_of_unit:

14
spec/requests/locations_controller_spec.rb

@ -202,7 +202,19 @@ RSpec.describe LocationsController, type: :request do
end
context "when invalid time is supplied" do
let(:params) { { location: { "startdate(3i)" => "1", "startdate(2i)" => "1", "startdate(1i)" => "w" } } }
let(:params) do
{ location: {
name: "Test",
units: "5",
type_of_unit: "Bungalow",
wheelchair_adaptation: "No",
add_another_location: "No",
postcode: "ZZ1 1ZZ",
"startdate(3i)" => "1",
"startdate(2i)" => "1",
"startdate(1i)" => "w",
} }
end
it "displays the new page with an error message" do
expect(response).to have_http_status(:unprocessable_entity)

Loading…
Cancel
Save