Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bugfix] Fusion usager qui plante quand on choisit un usager sans annotation #5143

Merged
merged 1 commit into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/services/merge_users_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def merge_annotations
current_territory = @organisation.territory

annotation_to_merge = @user_to_merge.annotations.find_by(territory: current_territory)
@user_target.annotate!(annotation_to_merge.content, territory: current_territory)
annotation_to_merge.destroy!
@user_target.annotate!(annotation_to_merge&.content, territory: current_territory)
annotation_to_merge&.destroy!
end

def merge_user_attributes
Expand Down
48 changes: 44 additions & 4 deletions spec/services/merge_users_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
# defaults
let!(:organisation) { create(:organisation) }
let(:attributes_to_merge) { [] }
let(:user_target) { create(:user, organisations: [organisation]) }
let(:user_to_merge) { create(:user, organisations: [organisation]) }
let!(:user_target) { create(:user, organisations: [organisation]) }
let!(:user_to_merge) { create(:user, organisations: [organisation]) }

context "simply merge first_name" do
let(:user_target) { create(:user, first_name: "Jean", last_name: "PAUL", email: "[email protected]", organisations: [organisation]) }
Expand Down Expand Up @@ -179,8 +179,8 @@
let!(:organisation2) { create(:organisation) }
let!(:agent1) { create(:agent, basic_role_in_organisations: [organisation]) }
let!(:agent2) { create(:agent, basic_role_in_organisations: [organisation2]) }
let(:user_target) { create(:user, referent_agents: [agent1], organisations: [organisation]) }
let(:user_to_merge) { create(:user, referent_agents: [agent2], organisations: [organisation, organisation2]) }
let!(:user_target) { create(:user, referent_agents: [agent1], organisations: [organisation]) }
let!(:user_to_merge) { create(:user, referent_agents: [agent2], organisations: [organisation, organisation2]) }

it "does not move the agent from the other orga anything" do
perform
Expand Down Expand Up @@ -249,6 +249,46 @@
end
end

context "only user to merge has an annotation" do
before do
user_to_merge.annotations.create!(territory: organisation.territory, content: "user to merge")
end

it "deletes the annotation along with the merged user by default" do
expect { perform }.to change(Annotation, :count).by(-1).and(change(User, :count).by(-1))
expect(user_target.annotations.find_by(territory: organisation.territory)).to be_nil
end

context "when merging annotations" do
let(:attributes_to_merge) { [:annotation_content] }

it "moves the annotation to the target user" do
perform
expect(user_target.annotation_for(organisation.territory)).to eq("user to merge")
end
end
end

context "only target user has an annotation" do
before do
user_target.annotations.create!(territory: organisation.territory, content: "target user")
end

it "keeps the annotation by default" do
expect { perform }.not_to change(Annotation, :count)
expect(user_target.annotation_for(organisation.territory)).to eq("target user")
end

context "when merging annotations" do
let(:attributes_to_merge) { [:annotation_content] }

it "deletes the annotation of the target user, to replace it with with the absence of annotation of the user to merge" do
expect { perform }.to change(Annotation, :count).by(-1)
expect(user_target.annotations.find_by(territory: organisation.territory)).to be_nil
end
end
end

context "when one user is connected by FranceConnect" do
it "keep FranceConnect attributes when merged user logged once with franceconnect" do
user_to_merge = create(:user, logged_once_with_franceconnect: true, franceconnect_openid_sub: "unechainedecharacteres", organisations: [organisation])
Expand Down
Loading