Merge pull request #10661 from demarches-simplifiees/feat/10460

ETQ admin, je veux que l'email du déposant ne puisse pas être similaire à l'email du mandataire
This commit is contained in:
Colin Darie 2024-08-20 09:34:03 +00:00 committed by GitHub
commit 1e53e3bfef
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 45 additions and 6 deletions

View file

@ -16,6 +16,7 @@ class Individual < ApplicationRecord
on: :update on: :update
validates :email, strict_email: true, presence: true, if: -> { dossier.for_tiers? && self.email? }, on: :update validates :email, strict_email: true, presence: true, if: -> { dossier.for_tiers? && self.email? }, on: :update
validate :email_different_from_mandataire, on: :update
after_commit -> { dossier.index_search_terms_later }, if: -> { nom_previously_changed? || prenom_previously_changed? } after_commit -> { dossier.index_search_terms_later }, if: -> { nom_previously_changed? || prenom_previously_changed? }
@ -31,4 +32,10 @@ class Individual < ApplicationRecord
end end
def unverified_email? = !email_verified_at? def unverified_email? = !email_verified_at?
def email_different_from_mandataire
if email.present? && email.casecmp?(dossier.user.email)
errors.add(:email, :must_be_different_from_mandataire)
end
end
end end

View file

@ -1,5 +1,11 @@
en: en:
activerecord: activerecord:
errors:
models:
individual:
attributes:
email:
must_be_different_from_mandataire: "must be different from the agent's email"
attributes: attributes:
individual: individual:
gender: Gender gender: Gender

View file

@ -1,5 +1,11 @@
fr: fr:
activerecord: activerecord:
errors:
models:
individual:
attributes:
email:
must_be_different_from_mandataire: "doit être différent de celui du mandataire"
attributes: attributes:
individual: individual:
gender: Civilité gender: Civilité

View file

@ -44,13 +44,33 @@ describe Individual do
end end
end end
context 'when an individual has an invalid notification_method' do describe 'validate_mandant_email' do
let(:invalid_individual) { build(:individual, notification_method: 'invalid_method') } let(:user) { create(:user, email: 'mandataire@example.com') }
let(:dossier) { create(:dossier, :for_tiers_with_notification, user: user) }
let(:individual) { dossier.individual }
it 'raises an ArgumentError' do context 'when validating email' do
expect { it 'is valid when email is different from the mandataire' do
invalid_individual.valid? individual.email = 'different@example.com'
}.to raise_error(ArgumentError, "'invalid_method' is not a valid notification_method") expect(individual).to be_valid
end
it 'is invalid when email is the same as the mandataire' do
individual.email = 'mandataire@example.com'
expect(individual).not_to be_valid
expect(individual.errors[:email]).to include(
I18n.t('activerecord.errors.models.individual.attributes.email.must_be_different_from_mandataire')
)
end
it 'is valid when email is not required (notification_method is not email)' do
dossier_without_notification = create(:dossier, :for_tiers_without_notification, user: user)
individual_without_notification = dossier_without_notification.individual
expect(individual_without_notification).to be_valid
expect(individual_without_notification.email).to be_nil
expect(individual_without_notification.notification_method).to eq('no_notification')
end
end end
end end
end end