From 31995c270c68acfa97fa07a9a9a778633afe6377 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Tue, 12 Sep 2017 17:02:45 +0200 Subject: [PATCH] [Fix #721] If an avis is created with uppercase email, downcase it before save --- app/controllers/backoffice/avis_controller.rb | 2 +- app/models/avis.rb | 7 ++++++ spec/models/avis_spec.rb | 22 +++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/app/controllers/backoffice/avis_controller.rb b/app/controllers/backoffice/avis_controller.rb index 5817e0c81..3ed67fa9e 100644 --- a/app/controllers/backoffice/avis_controller.rb +++ b/app/controllers/backoffice/avis_controller.rb @@ -7,7 +7,7 @@ class Backoffice::AvisController < ApplicationController avis = Avis.new(create_params.merge(claimant: current_gestionnaire, dossier: dossier)) if avis.save - flash[:notice] = "Votre demande d'avis a bien été envoyée à #{create_params[:email]}" + flash[:notice] = "Votre demande d'avis a bien été envoyée à #{avis.email}" end redirect_to backoffice_dossier_path(dossier) diff --git a/app/models/avis.rb b/app/models/avis.rb index 89e29c5a3..9d2253a2f 100644 --- a/app/models/avis.rb +++ b/app/models/avis.rb @@ -3,6 +3,7 @@ class Avis < ApplicationRecord belongs_to :gestionnaire belongs_to :claimant, class_name: 'Gestionnaire' + before_save :downcase_email before_create :try_to_assign_gestionnaire after_create :notify_gestionnaire @@ -27,6 +28,12 @@ class Avis < ApplicationRecord private + def downcase_email + if email.present? + email.downcase! + end + end + def notify_gestionnaire AvisMailer.avis_invitation(self).deliver_now end diff --git a/spec/models/avis_spec.rb b/spec/models/avis_spec.rb index cb8de8cce..0dd7ef2ac 100644 --- a/spec/models/avis_spec.rb +++ b/spec/models/avis_spec.rb @@ -117,4 +117,26 @@ RSpec.describe Avis, type: :model do it { expect(avis.email).to eq(email) } end end + + describe "#downcase_email" do + subject { Avis.create(claimant: claimant, email: email, dossier: create(:dossier), gestionnaire: create(:gestionnaire)) } + + context "when there is no email" do + let(:email) { nil } + + it { expect(subject.email).to be_nil } + end + + context "when the email is in lowercase" do + let(:email) { "toto@tps.fr" } + + it { expect(subject.email).to eq("toto@tps.fr") } + end + + context "when the email is not in lowercase" do + let(:email) { "TOTO@tps.fr" } + + it { expect(subject.email).to eq("toto@tps.fr") } + end + end end