simple cases when the preexisting targeted account does not have instructeur or profile profile

This commit is contained in:
simon lehericey 2021-10-04 14:00:32 +02:00
parent c56199e8f7
commit 9a6a53349f
2 changed files with 64 additions and 17 deletions

View file

@ -2,14 +2,25 @@ module Manager
class UsersController < Manager::ApplicationController
def update
user = User.find(params[:id])
new_email = params[:user][:email]
user.skip_reconfirmation!
user.update(email: new_email)
if (user.valid?)
flash[:notice] = "L'email a été modifié en « #{new_email} » sans notification ni validation par email."
preexisting_user = User.find_by(email: targeted_email)
if preexisting_user.nil?
user.skip_reconfirmation!
user.update(email: targeted_email)
if (user.valid?)
flash[:notice] = "L'email a été modifié en « #{targeted_email} » sans notification ni validation par email."
else
flash[:error] = user.errors.full_messages.to_sentence
end
else
flash[:error] = "« #{new_email} » nest pas une adresse valide."
user.dossiers.update_all(user_id: preexisting_user.id)
user.instructeur&.update(user: preexisting_user)
user.expert&.update(user: preexisting_user)
end
redirect_to edit_manager_user_path(user)
end
@ -72,5 +83,11 @@ module Manager
end
redirect_to emails_manager_user_path(@user)
end
private
def targeted_email
params[:user][:email]
end
end
end

View file

@ -21,24 +21,54 @@ describe Manager::UsersController, type: :controller do
subject { patch :update, params: { id: user.id, user: { email: nouvel_email } } }
describe 'with a valid email' do
let(:nouvel_email) { 'nouvel.email@domaine.fr' }
context 'when the targeted email does not exist' do
describe 'with a valid email' do
let(:nouvel_email) { 'nouvel.email@domaine.fr' }
it 'updates the user email' do
subject
it 'updates the user email' do
subject
expect(User.find_by(id: user.id).email).to eq(nouvel_email)
expect(User.find_by(id: user.id).email).to eq(nouvel_email)
end
end
describe 'with an invalid email' do
let(:nouvel_email) { 'plop' }
it 'does not update the user email' do
subject
expect(User.find_by(id: user.id).email).not_to eq(nouvel_email)
expect(flash[:error]).to match("Courriel invalide")
end
end
end
describe 'with an invalid email' do
let(:nouvel_email) { 'plop' }
context 'when the targeted email exists' do
let(:preexisting_user) { create(:user, email: 'email.existant@domaine.fr') }
let(:nouvel_email) { preexisting_user.email }
it 'does not update the user email' do
subject
context 'and the old account has a dossier' do
let!(:dossier) { create(:dossier, user: user) }
expect(User.find_by(id: user.id).email).not_to eq(nouvel_email)
expect(flash[:error]).to match("« #{nouvel_email} » nest pas une adresse valide.")
it 'transfers the dossier' do
subject
expect(preexisting_user.dossiers).to match([dossier])
end
end
context 'and the old account belongs to an instructeur and expert' do
let!(:instructeur) { create(:instructeur, user: user) }
let!(:expert) { create(:expert, user: user) }
it 'transfers instructeur account' do
subject
preexisting_user.reload
expect(preexisting_user.instructeur).to match(instructeur)
expect(preexisting_user.expert).to match(expert)
end
end
end
end