[Fix #721] If an avis is created with uppercase email, downcase it before save

This commit is contained in:
Mathieu Magnin 2017-09-12 17:02:45 +02:00
parent 39a53ff9b9
commit 31995c270c
3 changed files with 30 additions and 1 deletions

View file

@ -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)

View file

@ -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

View file

@ -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