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

Update `User::ChangePasswordService` to activate the current user

Aaron Contreras90dca8c8c8e6
diff --git a/app/services/users/change_password_service.rb b/app/services/users/change_password_service.rb
index 889a118fd48..b76168efbc6 100644
--- a/app/services/users/change_password_service.rb
+++ b/app/services/users/change_password_service.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2023 the OpenProject GmbH
@@ -40,9 +42,11 @@ module Users
current_user.password = params[:new_password]
current_user.password_confirmation = params[:new_password_confirmation]
current_user.force_password_change = false
+ current_user.activate if current_user.invited?
if current_user.save
invalidate_recovery_tokens
+ invalidate_invitation_tokens
log_success
::ServiceResult.new success: true,
@@ -64,6 +68,10 @@ module Users
Token::Recovery.where(user: current_user).delete_all
end
+ def invalidate_invitation_tokens
+ Token::Invitation.where(user: current_user).delete_all
+ end
+
def invalidate_session_result
update_message = I18n.t(:notice_account_password_updated)
diff --git a/spec/services/users/change_password_service_spec.rb b/spec/services/users/change_password_service_spec.rb
index cf5a8889cfd..8066a7dd0d6 100644
--- a/spec/services/users/change_password_service_spec.rb
+++ b/spec/services/users/change_password_service_spec.rb
@@ -28,7 +28,11 @@
require 'spec_helper'
RSpec.describe Users::ChangePasswordService do
- let(:user) { create(:user, password: old_password, password_confirmation: old_password) }
+ let(:user) do
+ create(:invited_user,
+ password: old_password,
+ password_confirmation: old_password)
+ end
let(:old_password) { 'AdminAdmin42' }
let(:new_password) { 'SoreThroat33' }
let(:session) { ActionController::TestSession.new }
@@ -38,18 +42,53 @@ RSpec.describe Users::ChangePasswordService do
instance.call new_password:, new_password_confirmation: new_password
end
- it "changes the user's password" do
+ it 'is successful' do
expect(result).to be_success
+ end
+
+ it "changes the user's password" do
+ result
expect(user.check_password?(old_password)).to be false
expect(user.check_password?(new_password)).to be true
end
- context "with existing password recovery tokens" do
+ describe 'activating the user' do
+ context 'when the user is "invited"' do
+ it 'activates the user' do
+ result
+ expect(user).to be_active
+ end
+ end
+
+ context 'when the user is not "invited"' do
+ let(:user) do
+ create(:locked_user,
+ password: old_password,
+ password_confirmation: old_password)
+ end
+
+ it 'does not activate the user' do
+ result
+ expect(user).not_to be_active
+ end
+ end
+ end
+
+ context 'with existing invitation tokens' do
+ let!(:invitation_token) { create(:invitation_token, user:) }
+
+ it 'invalidates the existing token' do
+ expect(result).to be_success
+ expect(Token::Invitation.where(user:)).to be_empty
+ end
+ end
+
+ context 'with existing password recovery tokens' do
let!(:recovery_token) { create(:recovery_token, user:) }
it 'invalidates the existing tokens' do
expect(result).to be_success
- expect(Token::Recovery.where(user:).count).to eq 0
+ expect(Token::Recovery.where(user:)).to be_empty
end
end
end