update update_email to allow merge

This commit is contained in:
simon lehericey 2021-10-26 13:36:14 +02:00
parent 652b8367be
commit b160086cc5
8 changed files with 45 additions and 34 deletions

View file

@ -14,11 +14,12 @@ module Users
end end
def update_email def update_email
if current_user.update(update_email_params) requested_user = User.find_by(email: requested_email)
if requested_user.present?
current_user.ask_for_merge(requested_user)
flash.notice = t('devise.registrations.update_needs_confirmation') flash.notice = t('devise.registrations.update_needs_confirmation')
elsif current_user.errors&.details&.dig(:email)&.any? { |e| e[:error] == :taken } elsif current_user.update(update_email_params)
UserMailer.account_already_taken(current_user, requested_email).deliver_later
# avoid leaking information about whether an account with this email exists or not
flash.notice = t('devise.registrations.update_needs_confirmation') flash.notice = t('devise.registrations.update_needs_confirmation')
else else
flash.alert = current_user.errors.full_messages flash.alert = current_user.errors.full_messages

View file

@ -12,10 +12,10 @@ class UserMailer < ApplicationMailer
mail(to: user.email, subject: @subject, procedure: @procedure) mail(to: user.email, subject: @subject, procedure: @procedure)
end end
def account_already_taken(user, requested_email) def ask_for_merge(user, requested_email)
@user = user @user = user
@requested_email = requested_email @requested_email = requested_email
@subject = "Changement dadresse email" @subject = "Fusion de compte"
mail(to: requested_email, subject: @subject) mail(to: requested_email, subject: @subject)
end end

View file

@ -220,6 +220,11 @@ class User < ApplicationRecord
end end
end end
def ask_for_merge(requested_user)
update(requested_merge_into: requested_user)
UserMailer.ask_for_merge(self, requested_user.email).deliver_later
end
private private
def link_invites! def link_invites!

View file

@ -1,20 +0,0 @@
- content_for(:title, @subject)
%p
Bonjour,
%p
Lutilisateur « #{@user.email} » a demandé le changement de son adresse vers « #{@requested_email} ».
%p
Malheureusement, votre compte « #{@requested_email} » existe déjà. Nous ne pouvons pas fusionner automatiquement vos comptes.
%p
%strong Nous ne pouvons donc pas effectuer le changement dadresse email.
%p
Si vous nêtes pas à lorigine de cette demande, vous pouvez ignorer ce message. Et si vous avez besoin dassistance, nhésitez pas à nous contacter à
= succeed '.' do
= mail_to CONTACT_EMAIL
= render partial: "layouts/mailers/signature"

View file

@ -0,0 +1,22 @@
- content_for(:title, @subject)
%p
Bonjour,
%p
Lutilisateur <b>« #{@user.email} »</b> a demandé la fusion de son compte avec le votre <b>« #{@requested_email} »</b>.
%p
Si vous désirez confirmer la fusion de ces comptes :
%ol
%li connectez-vous avec le compte #{@requested_email}
%li allez sur votre page profil
%li acceptez la fusion
%p
Si vous nêtes pas à lorigine de cette demande, vous pouvez ignorer ce message. Et si vous avez besoin dassistance, nhésitez pas à nous contacter à
= succeed '.' do
= mail_to CONTACT_EMAIL
= render partial: "layouts/mailers/signature"

View file

@ -63,16 +63,19 @@ describe Users::ProfilController, type: :controller do
let(:existing_user) { create(:user) } let(:existing_user) { create(:user) }
before do before do
expect_any_instance_of(User).to receive(:ask_for_merge).with(existing_user)
perform_enqueued_jobs do perform_enqueued_jobs do
patch :update_email, params: { user: { email: existing_user.email } } patch :update_email, params: { user: { email: existing_user.email } }
end end
user.reload user.reload
end end
it { expect(user.unconfirmed_email).to be_nil } it 'launches the merge process' do
it { expect(ActionMailer::Base.deliveries.last.to).to eq([existing_user.email]) } expect(user.unconfirmed_email).to be_nil
it { expect(response).to redirect_to(profil_path) } expect(response).to redirect_to(profil_path)
it { expect(flash.notice).to eq(I18n.t('devise.registrations.update_needs_confirmation')) } expect(flash.notice).to eq(I18n.t('devise.registrations.update_needs_confirmation'))
end
end end
context 'when the mail is incorrect' do context 'when the mail is incorrect' do

View file

@ -8,8 +8,8 @@ class UserMailerPreview < ActionMailer::Preview
UserMailer.new_account_warning(user, procedure) UserMailer.new_account_warning(user, procedure)
end end
def account_already_taken def ask_for_merge
UserMailer.account_already_taken(user, 'dircab@territoires.gouv.fr') UserMailer.ask_for_merge(user, 'dircab@territoires.gouv.fr')
end end
private private

View file

@ -17,10 +17,10 @@ RSpec.describe UserMailer, type: :mailer do
end end
end end
describe '.account_already_taken' do describe '.ask_for_merge' do
let(:requested_email) { 'new@exemple.fr' } let(:requested_email) { 'new@exemple.fr' }
subject { described_class.account_already_taken(user, requested_email) } subject { described_class.ask_for_merge(user, requested_email) }
it { expect(subject.to).to eq([requested_email]) } it { expect(subject.to).to eq([requested_email]) }
it { expect(subject.body).to include(requested_email) } it { expect(subject.body).to include(requested_email) }