From 794b09d6054e7e65111c574222e219f7120cea89 Mon Sep 17 00:00:00 2001 From: baarkerlounger <5101747+baarkerlounger@users.noreply.github.com> Date: Mon, 20 Jun 2022 13:41:10 +0100 Subject: [PATCH] Validate required fields when creating Organisations (#677) * Validate required fields * Model test * Rubocop --- app/controllers/organisations_controller.rb | 4 ++-- app/models/organisation.rb | 3 ++- config/locales/en.yml | 4 ++++ spec/models/organisation_spec.rb | 2 +- spec/requests/organisations_controller_spec.rb | 12 ++++++++++++ 5 files changed, 21 insertions(+), 4 deletions(-) diff --git a/app/controllers/organisations_controller.rb b/app/controllers/organisations_controller.rb index 7a92e5811..4721a31ed 100644 --- a/app/controllers/organisations_controller.rb +++ b/app/controllers/organisations_controller.rb @@ -49,8 +49,8 @@ class OrganisationsController < ApplicationController end def create - organisation = Organisation.create(org_params) - if organisation.persisted? + @resource = Organisation.new(org_params) + if @resource.save redirect_to organisations_path else render :new, status: :unprocessable_entity diff --git a/app/models/organisation.rb b/app/models/organisation.rb index 0319befa4..10e720458 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -18,7 +18,8 @@ class Organisation < ApplicationRecord enum provider_type: PROVIDER_TYPE - validates :provider_type, presence: true + validates :name, presence: { message: I18n.t("validations.organisation.name_missing") } + validates :provider_type, presence: { message: I18n.t("validations.organisation.provider_type_missing") } def case_logs CaseLog.filter_by_organisation(self) diff --git a/config/locales/en.yml b/config/locales/en.yml index 3e9c8898e..e6f94d28b 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -39,6 +39,10 @@ en: reset_password: "Reset your password" validations: + organisation: + name_missing: "Enter the organisation’s name" + provider_type_missing: "Select the organisation’s type" + other_field_missing: "If %{main_field_label} is other then %{other_field_label} must be provided" other_field_not_required: "%{other_field_label} must not be provided if %{main_field_label} was not other" numeric: diff --git a/spec/models/organisation_spec.rb b/spec/models/organisation_spec.rb index ac6a0fc6c..7062f3f51 100644 --- a/spec/models/organisation_spec.rb +++ b/spec/models/organisation_spec.rb @@ -20,7 +20,7 @@ RSpec.describe Organisation, type: :model do it "validates provider_type presence" do expect { FactoryBot.create(:organisation, provider_type: nil) } - .to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Provider type can’t be blank") + .to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Provider type #{I18n.t('validations.organisation.provider_type_missing')}") end context "with data protection confirmations" do diff --git a/spec/requests/organisations_controller_spec.rb b/spec/requests/organisations_controller_spec.rb index 18fd10204..7ad145d66 100644 --- a/spec/requests/organisations_controller_spec.rb +++ b/spec/requests/organisations_controller_spec.rb @@ -1008,6 +1008,18 @@ RSpec.describe OrganisationsController, type: :request do request expect(response).to redirect_to("/organisations") end + + context "when required params are missing" do + let(:name) { "" } + let(:provider_type) { "" } + + it "displays the form with an error message" do + request + expect(response).to have_http_status(:unprocessable_entity) + expect(page).to have_content(I18n.t("validations.organisation.name_missing")) + expect(page).to have_content(I18n.t("validations.organisation.provider_type_missing")) + end + end end end end