diff --git a/app/models/user.rb b/app/models/user.rb index 35c0edd5e..6d2c6db70 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -108,7 +108,7 @@ class User < ApplicationRecord dossiers.each do |dossier| dossier.delete_and_keep_track(administration) end - dossiers.unscoped.destroy_all + dossiers.with_hidden.destroy_all destroy! end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 19b317f40..2de7cfccd 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -273,6 +273,9 @@ describe User, type: :model do let!(:dossier_cache) do create(:dossier, :en_construction, user: user) end + let!(:dossier_from_another_user) do + create(:dossier, :en_construction, user: create(:user)) + end it "keep track of dossiers and delete user" do dossier_cache.delete_and_keep_track(administration) @@ -282,6 +285,13 @@ describe User, type: :model do expect(DeletedDossier.find_by(dossier_id: dossier_brouillon)).to be_present expect(User.find_by(id: user.id)).to be_nil end + + it "doesn't destroy dossiers of another user" do + dossier_cache.delete_and_keep_track(administration) + user.delete_and_keep_track_dossiers(administration) + + expect(Dossier.find_by(id: dossier_from_another_user.id)).not_to be_nil + end end end end