Browse Source

CLDC-1378 scheme start date validation (#760)

* testing location validity

* testing for invalid startdate

* testing for invalid startdate in controller

* redundant method

* redundant if

* added tests

* using blank

* using || insetad of &&
pull/766/head
J G 2 years ago committed by GitHub
parent
commit
a85a858ac8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 21
      app/controllers/locations_controller.rb
  2. 3
      config/locales/en.yml
  3. 88
      spec/requests/locations_controller_spec.rb

21
app/controllers/locations_controller.rb

@ -16,13 +16,19 @@ class LocationsController < ApplicationController
end end
def create def create
if date_params_missing?(location_params) || valid_date_params?(location_params)
@location = Location.new(location_params) @location = Location.new(location_params)
if @location.save 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)) 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 else
render :new, status: :unprocessable_entity render :new, status: :unprocessable_entity
end 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 end
def edit; end def edit; end
@ -46,6 +52,19 @@ class LocationsController < ApplicationController
private 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 date_params_missing?(location_params)
location_params["startdate(1i)"].blank? || location_params["startdate(2i)"].blank? || location_params["startdate(3i)"].blank?
end
def is_integer?(string)
string.to_i.to_s == string
end
def find_scheme def find_scheme
@scheme = if %w[new create index edit_name].include?(action_name) @scheme = if %w[new create index edit_name].include?(action_name)
Scheme.find(params[:id]) Scheme.find(params[:id])

3
config/locales/en.yml

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

88
spec/requests/locations_controller_spec.rb

@ -200,6 +200,50 @@ RSpec.describe LocationsController, type: :request do
expect(page).to have_content(I18n.t("activerecord.errors.models.location.attributes.type_of_unit.blank")) expect(page).to have_content(I18n.t("activerecord.errors.models.location.attributes.type_of_unit.blank"))
end end
end end
context "when invalid time is supplied" do
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)
expect(page).to have_content(I18n.t("validations.date.invalid_date"))
end
end
context "when no startdate is supplied" do
let(:params) do
{ location: {
name: "Test",
units: "5",
type_of_unit: "Bungalow",
wheelchair_adaptation: "No",
add_another_location: "No",
postcode: "ZZ1 1ZZ",
"startdate(3i)" => "",
"startdate(2i)" => "",
"startdate(1i)" => "",
} }
end
it "creates a new location for scheme with valid params and redirects to correct page" do
expect { post "/schemes/#{scheme.id}/locations", params: }.to change(Location, :count).by(1)
follow_redirect!
expect(response).to have_http_status(:ok)
expect(page).to have_content("Check your answers before creating this scheme")
end
end
end end
context "when signed in as a support user" do context "when signed in as a support user" do
@ -310,6 +354,50 @@ RSpec.describe LocationsController, type: :request do
expect(page).to have_content(I18n.t("activerecord.errors.models.location.attributes.type_of_unit.blank")) expect(page).to have_content(I18n.t("activerecord.errors.models.location.attributes.type_of_unit.blank"))
end end
end end
context "when invalid time is supplied" do
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)
expect(page).to have_content(I18n.t("validations.date.invalid_date"))
end
end
context "when no startdate is supplied" do
let(:params) do
{ location: {
name: "Test",
units: "5",
type_of_unit: "Bungalow",
wheelchair_adaptation: "No",
add_another_location: "No",
postcode: "ZZ1 1ZZ",
"startdate(3i)" => "",
"startdate(2i)" => "",
"startdate(1i)" => "",
} }
end
it "creates a new location for scheme with valid params and redirects to correct page" do
expect { post "/schemes/#{scheme.id}/locations", params: }.to change(Location, :count).by(1)
follow_redirect!
expect(response).to have_http_status(:ok)
expect(page).to have_content("Check your answers before creating this scheme")
end
end
end end
end end

Loading…
Cancel
Save