Skip to content

Commit

Permalink
Fix/blank contact email (#1246)
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulDoyle-DEFRA committed Aug 8, 2022
1 parent 58a2c17 commit 5619c09
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ def self.custom_error_messages(attribute, *errors)
messages = {}

errors.each do |error|
messages[error] = I18n.t("activemodel.errors.models."\
"waste_carriers_engine/#{form_name}"\
messages[error] = I18n.t("activemodel.errors.models." \
"waste_carriers_engine/#{form_name}" \
".attributes.#{attribute}.#{error}")
end

Expand Down
4 changes: 4 additions & 0 deletions app/forms/waste_carriers_engine/contact_email_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ class ContactEmailForm < ::WasteCarriersEngine::BaseForm
after_initialize :populate_confirmed_email

def submit(params)
# Blank email address vluaes should be processed as nil
params[:contact_email] = nil if params[:contact_email].blank?
params[:confirmed_email] = nil if params[:confirmed_email].blank?

# Assign the params for validation and pass them to the BaseForm method for updating
self.confirmed_email = params[:confirmed_email]
self.no_contact_email = params[:no_contact_email]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ContactEmailValidator < ActiveModel::EachValidator

def validate(record)
if WasteCarriersEngine.configuration.host_is_back_office? && record.no_contact_email == "1"
if record.contact_email.present?
unless record.contact_email.nil?
add_validation_error(record, :no_contact_email, :not_blank)
return false
end
Expand Down
22 changes: 17 additions & 5 deletions spec/forms/waste_carriers_engine/contact_email_forms_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ module WasteCarriersEngine
end

context "when the form is valid" do
before { contact_email_form.transient_registration.contact_email = nil }

context "when running in the front office" do
before { allow(WasteCarriersEngine.configuration).to receive(:host_is_back_office?).and_return(false) }

context "with an email address" do
let(:contact_email) { contact_email_form.contact_email }
let(:contact_email) { Faker::Internet.email }

it_behaves_like "should submit"
end
Expand All @@ -44,16 +46,26 @@ module WasteCarriersEngine
before { allow(WasteCarriersEngine.configuration).to receive(:host_is_back_office?).and_return(true) }

context "with an email address" do
let(:contact_email) { contact_email_form.contact_email }
let(:contact_email) { Faker::Internet.email }

it_behaves_like "should submit"

it "populates contact_email" do
expect { contact_email_form.submit(ActionController::Parameters.new(params)) }
.to change { contact_email_form.transient_registration.contact_email }.to(contact_email)
end
end

context "without an email address" do
let(:contact_email) { nil }
context "with a blank email address" do
let(:contact_email) { "" }
let(:no_contact_email) { "1" }

it_behaves_like "should submit"

it "does not populate contact_email" do
expect { contact_email_form.submit(ActionController::Parameters.new(params)) }
.not_to change { contact_email_form.transient_registration.contact_email }.from(nil)
end
end
end
end
Expand All @@ -80,7 +92,7 @@ module WasteCarriersEngine
end

context "with an email address and with the no-email-address option selected" do
let(:contact_email) { contact_email_form.contact_email }
let(:contact_email) { Faker::Internet.email }
let(:no_contact_email) { "1" }

it_behaves_like "should not submit"
Expand Down

0 comments on commit 5619c09

Please sign in to comment.