Skip to content

Commit

Permalink
Merge pull request #10661 from demarches-simplifiees/feat/10460
Browse files Browse the repository at this point in the history
 ETQ admin, je veux que l'email du déposant ne puisse pas être similaire à l'email du mandataire
  • Loading branch information
colinux authored Aug 20, 2024
2 parents 66bf0cc + 88cd4a9 commit 1e53e3b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
7 changes: 7 additions & 0 deletions app/models/individual.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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? }

Expand All @@ -31,4 +32,10 @@ def self.from_france_connect(fc_information)
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
6 changes: 6 additions & 0 deletions config/locales/models/individual/en.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 6 additions & 0 deletions config/locales/models/individual/fr.yml
Original file line number Diff line number Diff line change
@@ -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é
Expand Down
32 changes: 26 additions & 6 deletions spec/models/individual_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,33 @@
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: '[email protected]') }
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 = '[email protected]'
expect(individual).to be_valid
end

it 'is invalid when email is the same as the mandataire' do
individual.email = '[email protected]'
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

0 comments on commit 1e53e3b

Please sign in to comment.