Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Don't use locked! on user to avoid validation errors on delete

Oliver Günthercf7f31ece79d
diff --git a/app/services/users/delete_service.rb b/app/services/users/delete_service.rb
index 4a6565dd32b..9fdc66ec248 100644
--- a/app/services/users/delete_service.rb
+++ b/app/services/users/delete_service.rb
@@ -39,7 +39,8 @@ module Users
def destroy(user_object)
# as destroying users is a lengthy process we handle it in the background
# and lock the account now so that no action can be performed with it
- user_object.locked!
+ # don't use "locked!" handle as it will raise on invalid users
+ user_object.update_column(:status, User.statuses[:locked])
::Principals::DeleteJob.perform_later(user_object)
logout! if self_delete?
diff --git a/spec/services/users/delete_service_integration_spec.rb b/spec/services/users/delete_service_integration_spec.rb
new file mode 100644
index 00000000000..d79cf021810
--- /dev/null
+++ b/spec/services/users/delete_service_integration_spec.rb
@@ -0,0 +1,53 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require 'spec_helper'
+
+describe ::Users::DeleteService, 'Integration', type: :model do
+ let(:input_user) { FactoryBot.create(:user) }
+ let(:actor) { FactoryBot.build_stubbed(:admin) }
+
+ let(:instance) { described_class.new(model: input_user, user: actor) }
+
+ subject { instance.call }
+
+ context 'when input user is invalid',
+ with_settings: { users_deletable_by_admins: true } do
+ before do
+ input_user.update_column(:mail, '')
+ end
+
+ it 'can still delete the user' do
+ expect(input_user).not_to be_valid
+
+ expect(subject).to be_success
+
+ expect(Principals::DeleteJob).to have_been_enqueued.with(input_user)
+ end
+ end
+end
diff --git a/spec/services/users/delete_service_spec.rb b/spec/services/users/delete_service_spec.rb
index 17c57667b67..535e9270d1b 100644
--- a/spec/services/users/delete_service_spec.rb
+++ b/spec/services/users/delete_service_spec.rb
@@ -38,17 +38,19 @@ describe ::Users::DeleteService, type: :model do
shared_examples 'deletes the user' do
it do
- expect(input_user).to receive(:locked!)
+ allow(input_user).to receive(:update_column).with(:status, 3)
expect(Principals::DeleteJob).to receive(:perform_later).with(input_user)
expect(subject).to be_success
+ expect(input_user).to have_received(:update_column).with(:status, 3)
end
end
shared_examples 'does not delete the user' do
it do
- expect(input_user).not_to receive(:locked!)
+ allow(input_user).to receive(:update_column).with(:status, 3)
expect(Principals::DeleteJob).not_to receive(:perform_later)
expect(subject).not_to be_success
+ expect(input_user).not_to have_received(:update_column).with(:status, 3)
end
end
@@ -72,13 +74,7 @@ describe ::Users::DeleteService, type: :model do
context 'with privileged system user' do
let(:actor) { User.system }
- it 'performs deletion' do
- actor.run_given do
- expect(input_user).to receive(:locked!)
- expect(Principals::DeleteJob).to receive(:perform_later).with(input_user)
- expect(subject).to be_success
- end
- end
+ it_behaves_like 'deletes the user'
end
end