rename preexisting to targeted, remove duplicated test

This commit is contained in:
simon lehericey 2021-10-18 15:09:09 +02:00
parent 52b7e85954
commit d19ad2840c
2 changed files with 29 additions and 58 deletions

View file

@ -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

View file

@ -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
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
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
end
end
end