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

Proper logout tests

Oliver Güntheraea036079f1a
diff --git a/app/controllers/concerns/accounts/current_user.rb b/app/controllers/concerns/accounts/current_user.rb
index 55103fb01b2..2df6d50c501 100644
--- a/app/controllers/concerns/accounts/current_user.rb
+++ b/app/controllers/concerns/accounts/current_user.rb
@@ -134,7 +134,7 @@ module Accounts::CurrentUser
def logout_user
::Users::LogoutService
.new(controller: self)
- .call(current_user)
+ .call!(current_user)
end
# Redirect the user according to the logout scheme
diff --git a/app/controllers/concerns/auth_source_sso.rb b/app/controllers/concerns/auth_source_sso.rb
index 0eb726e02da..1f4ef875767 100644
--- a/app/controllers/concerns/auth_source_sso.rb
+++ b/app/controllers/concerns/auth_source_sso.rb
@@ -46,7 +46,7 @@ module AuthSourceSSO
return user if user.login.casecmp?(login)
Rails.logger.warn { "Header-based auth source SSO user changed from #{user.login} to #{login}. Re-authenticating" }
- ::Users::LogoutService.new(controller: self).call(user)
+ ::Users::LogoutService.new(controller: self).call!(user)
nil
end
diff --git a/app/services/users/login_service.rb b/app/services/users/login_service.rb
index 11b00d2481d..94bf19c2f66 100644
--- a/app/services/users/login_service.rb
+++ b/app/services/users/login_service.rb
@@ -80,7 +80,7 @@ module Users
end
def reset_session!
- Sessions::DropAllSessionsService.call(user) if drop_old_sessions?
+ ::Sessions::DropAllSessionsService.call(user) if drop_old_sessions?
controller.reset_session
end
diff --git a/app/services/users/logout_service.rb b/app/services/users/logout_service.rb
index 9802138bd9a..b6b531c0456 100644
--- a/app/services/users/logout_service.rb
+++ b/app/services/users/logout_service.rb
@@ -28,38 +28,46 @@
module Users
class LogoutService
- attr_accessor :controller
+ attr_accessor :controller, :cookies
def initialize(controller:)
self.controller = controller
+ self.cookies = controller.send(:cookies)
end
- def call(user)
+ def call!(user)
OpenProject.logger.info { "Logging out ##{user.id}" }
- controller.reset_session
-
- remove_cookies! controller.send(:cookies)
if OpenProject::Configuration.drop_old_sessions_on_logout?
- remove_tokens! user
- remove_sessions! user
+ remove_all_autologin_tokens! user
+ remove_all_sessions! user
+ else
+ remove_matching_autologin_token! user
end
+ controller.reset_session
+
User.current = User.anonymous
end
private
- def remove_sessions!(user)
+ def remove_all_sessions!(user)
::Sessions::UserSession.for_user(user.id).delete_all
end
- def remove_tokens!(user)
+ def remove_all_autologin_tokens!(user)
+ cookies.delete(OpenProject::Configuration.autologin_cookie_name)
Token::AutoLogin.where(user_id: user.id).delete_all
end
- def remove_cookies!(cookies)
- cookies.delete OpenProject::Configuration.autologin_cookie_name
+ def remove_matching_autologin_token!(user)
+ value = cookies.delete(OpenProject::Configuration.autologin_cookie_name)
+ return if value.blank?
+
+ Token::AutoLogin
+ .where(user:)
+ .find_by_plaintext_value(value)&.destroy
end
end
end
diff --git a/spec/factories/token_factory.rb b/spec/factories/token_factory.rb
index f205b400910..c50cbc2776f 100644
--- a/spec/factories/token_factory.rb
+++ b/spec/factories/token_factory.rb
@@ -45,6 +45,10 @@ FactoryBot.define do
user
end
+ factory :autologin_token, class: '::Token::AutoLogin' do
+ user
+ end
+
factory :backup_token, class: '::Token::Backup' do
user
diff --git a/spec/features/security/expire_sessions_spec.rb b/spec/features/security/expire_sessions_spec.rb
index 9934eaf7344..75903cadf4d 100644
--- a/spec/features/security/expire_sessions_spec.rb
+++ b/spec/features/security/expire_sessions_spec.rb
@@ -33,6 +33,9 @@ RSpec.describe 'Expire old user sessions' do
let(:admin_password) { 'adminADMIN!' }
before do
+ # Delete all previous sesssions
+ ::Sessions::UserSession.delete_all
+
login_with(admin.login, admin_password)
# Create a dangling session
diff --git a/spec/models/sessions/sql_bypass_sessions_spec.rb b/spec/models/sessions/sql_bypass_sessions_spec.rb
index a84788b678b..6676619c802 100644
--- a/spec/models/sessions/sql_bypass_sessions_spec.rb
+++ b/spec/models/sessions/sql_bypass_sessions_spec.rb
@@ -51,30 +51,4 @@ RSpec.describe Sessions::SqlBypass do
it_behaves_like 'augments the user_id attribute'
end
-
- describe 'delete other sessions on destroy' do
- let(:user) { build_stubbed(:user) }
- let!(:sessions) { create_list(:user_session, 2, user:) }
-
- context 'when config is enabled',
- with_config: { drop_old_sessions_on_logout: true } do
- it 'destroys both sessions' do
- expect(Sessions::UserSession.for_user(user).count).to eq(2)
- sessions.first.destroy
-
- expect(Sessions::UserSession.count).to eq(0)
- end
- end
-
- context 'when config is disabled',
- with_config: { drop_old_sessions_on_logout: false } do
- it 'destroys only the one session' do
- expect(Sessions::UserSession.for_user(user).count).to eq(2)
- sessions.first.destroy
-
- expect(Sessions::UserSession.count).to eq(1)
- expect(Sessions::UserSession.first.session_id).to eq(sessions[1].session_id)
- end
- end
- end
end
diff --git a/spec/services/users/logout_service_spec.rb b/spec/services/users/logout_service_spec.rb
new file mode 100644
index 00000000000..b8e825fb4d7
--- /dev/null
+++ b/spec/services/users/logout_service_spec.rb
@@ -0,0 +1,123 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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'
+
+RSpec.describe Users::LogoutService, type: :model do
+ shared_let(:user) { create(:user) }
+ let(:request) { {} }
+ let(:browser) do
+ instance_double(Browser::Safari,
+ name: 'Safari',
+ version: '13.2',
+ platform: instance_double(Browser::Platform::Linux, name: 'Linux'))
+ end
+ let(:cookies) { {} }
+ let(:controller) { instance_double(ApplicationController, browser:, cookies:) }
+
+ let(:instance) { described_class.new(controller:) }
+
+ subject { instance.call!(user) }
+
+ before do
+ allow(controller).to(receive(:reset_session))
+ end
+
+ describe 'User.current' do
+ it 'resets it' do
+ User.current = user
+
+ subject
+
+ expect(User.current).to eq User.anonymous
+ end
+ end
+
+ describe 'delete other sessions on destroy' do
+ let!(:sessions) { create_list(:user_session, 2, user:) }
+ let!(:other_session) { create(:user_session) }
+
+ context 'when config is enabled',
+ with_config: { drop_old_sessions_on_logout: true } do
+ it 'destroys both sessions' do
+ expect(Sessions::UserSession.count).to eq(3)
+ expect(Sessions::UserSession.for_user(user).count).to eq(2)
+
+ subject
+
+ expect(Sessions::UserSession.count).to eq(1)
+ expect(Sessions::UserSession.for_user(user).count).to eq(0)
+ end
+
+ describe 'autologin cookie' do
+ let!(:token) { create(:autologin_token, user:) }
+ let!(:other_token) { create(:autologin_token, user:) }
+
+ it 'removes all autologin tokens' do
+ cookies[OpenProject::Configuration.autologin_cookie_name] = token.plain_value
+
+ subject
+
+ expect(cookies[OpenProject::Configuration.autologin_cookie_name]).to be_nil
+ expect { token.reload }.to raise_error(ActiveRecord::RecordNotFound)
+ expect { other_token.reload }.to raise_error(ActiveRecord::RecordNotFound)
+ expect(Token::AutoLogin.where(user_id: user.id)).to be_empty
+ end
+ end
+ end
+
+ context 'when config is disabled',
+ with_config: { drop_old_sessions_on_logout: false } do
+ it 'destroys none of the existing sessions' do
+ expect(Sessions::UserSession.count).to eq(3)
+ expect(Sessions::UserSession.for_user(user).count).to eq(2)
+
+ subject
+
+ expect(Sessions::UserSession.count).to eq(3)
+ expect(Sessions::UserSession.for_user(user).count).to eq(2)
+ end
+
+
+ describe 'autologin cookie' do
+ let!(:token) { create(:autologin_token, user:) }
+ let!(:other_token) { create(:autologin_token, user:) }
+
+ it 'removes the matching autologin cookie and token' do
+ cookies[OpenProject::Configuration.autologin_cookie_name] = token.plain_value
+
+ subject
+
+ expect(cookies[OpenProject::Configuration.autologin_cookie_name]).to be_nil
+ expect { token.reload }.to raise_error(ActiveRecord::RecordNotFound)
+ expect(Token::AutoLogin.where(user_id: user.id).all).to contain_exactly other_token
+ end
+ end
+ end
+ end
+end