[Fix #1479] Sanitize Avis email before validation

This commit is contained in:
Mathieu Magnin 2018-02-28 15:46:27 +01:00
parent a00114d03d
commit 69f8353b2f
2 changed files with 4 additions and 8 deletions

View file

@ -1,11 +1,13 @@
class Avis < ApplicationRecord class Avis < ApplicationRecord
include EmailSanitizableConcern
belongs_to :dossier, touch: true belongs_to :dossier, touch: true
belongs_to :gestionnaire belongs_to :gestionnaire
belongs_to :claimant, class_name: 'Gestionnaire' belongs_to :claimant, class_name: 'Gestionnaire'
validates :email, format: { with: Devise.email_regexp, message: "n'est pas valide" }, allow_nil: true validates :email, format: { with: Devise.email_regexp, message: "n'est pas valide" }, allow_nil: true
before_save :clean_email before_validation -> { sanitize_email(:email) }
before_create :try_to_assign_gestionnaire before_create :try_to_assign_gestionnaire
after_create :notify_gestionnaire after_create :notify_gestionnaire
@ -31,12 +33,6 @@ class Avis < ApplicationRecord
private private
def clean_email
if email.present?
self.email = email.downcase.strip
end
end
def notify_gestionnaire def notify_gestionnaire
AvisMailer.avis_invitation(self).deliver_now AvisMailer.avis_invitation(self).deliver_now
end end

View file

@ -118,7 +118,7 @@ RSpec.describe Avis, type: :model do
end end
end end
describe "#clean_email" do describe "email sanitization" do
subject { Avis.create(claimant: claimant, email: email, dossier: create(:dossier), gestionnaire: create(:gestionnaire)) } subject { Avis.create(claimant: claimant, email: email, dossier: create(:dossier), gestionnaire: create(:gestionnaire)) }
context "when there is no email" do context "when there is no email" do