From ae9c392e359a3e32f8d1a4dd5c43208323ba5c62 Mon Sep 17 00:00:00 2001 From: JG Date: Tue, 5 Jul 2022 09:00:58 +0100 Subject: [PATCH] testing postcode missing --- app/controllers/locations_controller.rb | 2 +- app/models/location.rb | 2 +- spec/requests/locations_controller_spec.rb | 10 ++++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index a0217e3e0..84afc3c57 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -44,7 +44,7 @@ private def location_params required_params = params.require(:location).permit(:postcode, :name, :total_units, :type_of_unit, :wheelchair_adaptation, :add_another_location).merge(scheme_id: @scheme.id) - required_params[:postcode] = required_params[:postcode].gsub(" ", "").encode("ASCII", "UTF-8", invalid: :replace, undef: :replace, replace: "") + required_params[:postcode] = required_params[:postcode].gsub(" ", "").encode("ASCII", "UTF-8", invalid: :replace, undef: :replace, replace: "") if required_params[:postcode] required_params end end diff --git a/app/models/location.rb b/app/models/location.rb index 5afb3a700..e93703a6c 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -36,7 +36,7 @@ class Location < ApplicationRecord private def validate_postcode - unless postcode.match(Validations::PropertyValidations::POSTCODE_REGEXP) + if postcode.nil? || !postcode&.match(Validations::PropertyValidations::POSTCODE_REGEXP) error_message = I18n.t("validations.postcode") errors.add :postcode, error_message end diff --git a/spec/requests/locations_controller_spec.rb b/spec/requests/locations_controller_spec.rb index d0a9bb581..8cce906a5 100644 --- a/spec/requests/locations_controller_spec.rb +++ b/spec/requests/locations_controller_spec.rb @@ -103,6 +103,16 @@ RSpec.describe LocationsController, type: :request do expect(Location.last.wheelchair_adaptation).to eq("No") end + context "when required organisation id param is missing" do + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No" } } } + + it "displays the new page with an error message" do + post "/schemes/#{scheme.id}/location/create", params: params + expect(response).to have_http_status(:unprocessable_entity) + expect(page).to have_content(I18n.t("validations.postcode")) + end + end + context "when do you want to add another location is selected as yes" do let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "Yes", postcode: "ZZ1 1ZZ" } } }