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
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? }
@ -31,4 +32,10 @@ class Individual < ApplicationRecord
end
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

View file

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

View file

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

View file

@ -44,13 +44,33 @@ describe Individual do
end
end
context 'when an individual has an invalid notification_method' do
let(:invalid_individual) { build(:individual, notification_method: 'invalid_method') }
describe 'validate_mandant_email' do
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
expect {
invalid_individual.valid?
}.to raise_error(ArgumentError, "'invalid_method' is not a valid notification_method")
context 'when validating email' do
it 'is valid when email is different from the mandataire' do
individual.email = 'different@example.com'
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