From d19ad2840c5d3449db408f8c7a73900e8a31028d Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Mon, 18 Oct 2021 15:09:09 +0200 Subject: [PATCH] rename preexisting to targeted, remove duplicated test --- app/controllers/manager/users_controller.rb | 21 +++--- .../manager/users_controller_spec.rb | 66 ++++++------------- 2 files changed, 29 insertions(+), 58 deletions(-) diff --git a/app/controllers/manager/users_controller.rb b/app/controllers/manager/users_controller.rb index 6fbf9c74a..db8395995 100644 --- a/app/controllers/manager/users_controller.rb +++ b/app/controllers/manager/users_controller.rb @@ -2,10 +2,9 @@ module Manager class UsersController < Manager::ApplicationController def update user = User.find(params[:id]) - preexisting_user = User.find_by(email: targeted_email) + targeted_user = User.find_by(email: targeted_email) - elsif preexisting_user.nil? - if preexisting_user.nil? + if targeted_user.nil? user.skip_reconfirmation! user.update(email: targeted_email) @@ -15,17 +14,17 @@ module Manager flash[:error] = user.errors.full_messages.to_sentence end else - user.dossiers.update_all(user_id: preexisting_user.id) + user.dossiers.update_all(user_id: targeted_user.id) [ - [user.instructeur, preexisting_user.instructeur], - [user.expert, preexisting_user.expert], - [user.administrateur, preexisting_user.administrateur] - ].each do |old_role, preexisting_role| - if preexisting_role.nil? - old_role&.update(user: preexisting_user) + [user.instructeur, targeted_user.instructeur], + [user.expert, targeted_user.expert], + [user.administrateur, targeted_user.administrateur] + ].each do |old_role, targeted_role| + if targeted_role.nil? + old_role&.update(user: targeted_user) else - preexisting_role.merge(old_role) + targeted_role.merge(old_role) end end diff --git a/spec/controllers/manager/users_controller_spec.rb b/spec/controllers/manager/users_controller_spec.rb index 896cbc21b..22d77bd2c 100644 --- a/spec/controllers/manager/users_controller_spec.rb +++ b/spec/controllers/manager/users_controller_spec.rb @@ -17,7 +17,7 @@ describe Manager::UsersController, type: :controller do end describe '#update' do - let(:user) { create(:user, email: 'ancien.email@domaine.fr') } + let(:user) { create(:user, email: 'ancien.email@domaine.fr', password: '{My-$3cure-p4ssWord}') } subject { patch :update, params: { id: user.id, user: { email: nouvel_email } } } @@ -45,8 +45,8 @@ describe Manager::UsersController, type: :controller do end context 'when the targeted email exists' do - let(:preexisting_user) { create(:user, email: 'email.existant@domaine.fr') } - let(:nouvel_email) { preexisting_user.email } + let(:targeted_user) { create(:user, email: 'email.existant@domaine.fr', password: '{My-$3cure-p4ssWord}') } + let(:nouvel_email) { targeted_user.email } context 'and the old account has a dossier' do let!(:dossier) { create(:dossier, user: user) } @@ -54,63 +54,35 @@ describe Manager::UsersController, type: :controller do it 'transfers the dossier' do subject - expect(preexisting_user.dossiers).to match([dossier]) + expect(targeted_user.dossiers).to match([dossier]) end end - context 'and the old account belongs to an instructeur and expert' do + context 'and the old account belongs to an instructeur, expert and administrateur' do let!(:instructeur) { create(:instructeur, user: user) } let!(:expert) { create(:expert, user: user) } + let!(:administrateur) { create(:administrateur, user: user) } it 'transfers instructeur account' do subject - preexisting_user.reload + targeted_user.reload - expect(preexisting_user.instructeur).to match(instructeur) - expect(preexisting_user.expert).to match(expert) + expect(targeted_user.instructeur).to match(instructeur) + expect(targeted_user.expert).to match(expert) + expect(targeted_user.administrateur).to match(administrateur) expect(flash[:notice]).to match("Le compte « email.existant@domaine.fr » a absorbé le compte « ancien.email@domaine.fr ».") end - context 'and the preexisting account owns an instructeur and expert as well' do - let!(:preexisting_instructeur) { create(:instructeur, user: preexisting_user) } - let!(:preexisting_expert) { create(:expert, user: preexisting_user) } + context 'and the targeted account owns an instructeur and expert as well' do + let!(:targeted_instructeur) { create(:instructeur, user: targeted_user) } + let!(:targeted_expert) { create(:expert, user: targeted_user) } + let!(:targeted_administrateur) { create(:administrateur, user: targeted_user) } - context 'and the source instructeur has some procedures and dossiers' do - let!(:procedure) { create(:procedure, instructeurs: [instructeur]) } - let(:dossier) { create(:dossier) } - let(:administrateur) { create(:administrateur) } - let!(:commentaire) { create(:commentaire, instructeur: instructeur, dossier: dossier) } - let!(:bulk_message) { BulkMessage.create!(instructeur: instructeur, body: 'body', sent_at: Time.zone.now) } - - before do - user.instructeur.followed_dossiers << dossier - user.instructeur.administrateurs << administrateur - end - - it 'transferts all the stuff' do - subject - preexisting_user.reload - - expect(procedure.instructeurs).to match([preexisting_user.instructeur]) - expect(preexisting_user.instructeur.followed_dossiers).to match([dossier]) - expect(preexisting_user.instructeur.administrateurs).to match([administrateur]) - expect(preexisting_user.instructeur.commentaires).to match([commentaire]) - expect(preexisting_user.instructeur.bulk_messages).to match([bulk_message]) - end - end - - context 'and the source expert has some avis and commentaires' do - let(:dossier) { create(:dossier) } - let(:experts_procedure) { create(:experts_procedure, expert: user.expert, procedure: dossier.procedure) } - let!(:avis) { create(:avis, dossier: dossier, claimant: create(:instructeur), experts_procedure: experts_procedure) } - let!(:commentaire) { create(:commentaire, expert: expert, dossier: dossier) } - - it 'transfers the avis' do - subject - - expect(preexisting_user.expert.avis).to match([avis]) - expect(preexisting_user.expert.commentaires).to match([commentaire]) - end + it 'merge the account' do + expect_any_instance_of(Instructeur).to receive(:merge) + expect_any_instance_of(Expert).to receive(:merge) + expect_any_instance_of(Administrateur).to receive(:merge) + subject end end end