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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Add and adapt specs
Oliver Günther69899243ce9a
diff --git a/spec/features/my/sessions_management_spec.rb b/spec/features/my/sessions_management_spec.rbindex c65c73278d6..202e504334c 100644--- a/spec/features/my/sessions_management_spec.rb+++ b/spec/features/my/sessions_management_spec.rb@@ -32,6 +32,7 @@ require "spec_helper"RSpec.describe "My account session management", :js, :selenium doinclude Redmine::I18n+let(:user) { create(:user) }let(:old_session_time) { 5.days.ago }@@ -50,6 +51,20 @@ RSpec.describe "My account session management", :js, :selenium doexpires_on: 4.days.from_now)end+ let!(:linked_sql_session) do+ create(:user_session,+ user:,+ data: { browser: "Firefox", browser_version: "142", platform: "macOS", updated_at: 2.days.ago })+ end++ let!(:linked_session) do+ Sessions::UserSession.find_by(session_id: linked_sql_session.session_id)+ end++ let!(:autologin_session_link) do+ Sessions::AutologinSessionLink.create(token: autologin_token, session: linked_session)+ end+before dologin_as(user)Sessions::UserSession.where(id: user_session.id).update_all(updated_at: 5.days.ago)@@ -59,7 +74,7 @@ RSpec.describe "My account session management", :js, :selenium doit "can list and terminate sessions and remembered devices" dopage.within_test_selector("Users::Sessions::TableComponent") do- # Current session + old session + remembered device+ # Current session + old session + remembered device (linked session is aggregated with token)expect(page).to have_css(".session-row", count: 3)trs = page.all(".session-row")@@ -70,13 +85,13 @@ RSpec.describe "My account session management", :js, :selenium doexpect(trs[0]).to have_text("unknown operating system")expect(trs[0]).to have_no_test_selector("session-revoke-button")- # Old session+ # Old session (unmapped session)expect(trs[1]).to have_text("Mozilla Firefox (Version 12.3)")expect(trs[1]).to have_text("Linux")expect(trs[1]).to have_text format_time(old_session_time)expect(trs[1]).to have_test_selector("session-revoke-button")- # Remembered device (token)+ # Remembered device (token with aggregated linked session)expect(trs[2]).to have_text("Firefox (Version 142)")expect(trs[2]).to have_text("macOS")expect(trs[2]).to have_text(format_time(autologin_token.created_at))@@ -92,7 +107,7 @@ RSpec.describe "My account session management", :js, :selenium dopage.within_test_selector("Users::Sessions::TableComponent") dotrs = page.all(".session-row")- # Revoke the remembered device+ # Revoke the remembered device (this will also delete the linked session)within trs[1] dofind_test_selector("session-revoke-button").clickend@@ -105,8 +120,9 @@ RSpec.describe "My account session management", :js, :selenium doexpect(page).to have_css(".session-row", count: 1)end- # Both old session and token are gone+ # Both old session, token, and linked session are goneexpect { user_session.reload }.to raise_error(ActiveRecord::RecordNotFound)expect { autologin_token.reload }.to raise_error(ActiveRecord::RecordNotFound)+ expect { linked_session.reload }.to raise_error(ActiveRecord::RecordNotFound)endenddiff --git a/spec/models/token/auto_login_spec.rb b/spec/models/token/auto_login_spec.rbnew file mode 100644index 00000000000..783e1fb23a4--- /dev/null+++ b/spec/models/token/auto_login_spec.rb@@ -0,0 +1,209 @@+# frozen_string_literal: true++# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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 Token::AutoLogin do+ let(:user) { create(:user) }++ before do+ # Enable autologin for tests+ allow(Setting).to receive(:autologin).and_return(30)+ end++ describe "inheritance and behavior" do+ it "inherits from HashedToken" do+ expect(described_class.superclass).to eq(Token::HashedToken)+ end++ it "includes ExpirableToken" do+ expect(described_class.included_modules).to include(Token::ExpirableToken)+ end++ it "allows multiple values" do+ token = described_class.new(user:)+ expect(token.send(:single_value?)).to be false+ end+ end++ describe "creation" do+ it "creates a valid autologin token" do+ token = described_class.create(user:)++ expect(token).to be_valid+ expect(token.user).to eq(user)+ expect(token.value).to be_present+ expect(token.plain_value).to be_present+ end++ it "generates a unique token value" do+ token1 = described_class.create(user:)+ token2 = described_class.create(user:)++ expect(token1.value).not_to eq(token2.value)+ expect(token1.plain_value).not_to eq(token2.plain_value)+ end++ it "sets expiration based on autologin setting" do+ allow(Setting).to receive(:autologin).and_return(30)++ token = described_class.create(user:)++ expect(token.expires_on).to be_within(1.second).of(30.days.from_now)+ end++ it "can store browser and platform data" do+ data = { browser: "Firefox", browser_version: "142", platform: "macOS" }+ token = described_class.create(user:, data:)++ expect(token.data[:browser]).to eq("Firefox")+ expect(token.data[:browser_version]).to eq("142")+ expect(token.data[:platform]).to eq("macOS")+ end+ end++ describe "validation" do+ it "requires a user" do+ token = described_class.new++ expect(token).not_to be_valid+ expect(token.errors[:user]).to include("must exist")+ end++ it "validates token value uniqueness" do+ token1 = described_class.create(user:)+ token2 = described_class.new(user:, value: token1.value)++ expect(token2).not_to be_valid+ expect(token2.errors[:value]).to include("has already been taken.")+ end+ end++ describe ".find_valid_token" do+ let(:token) { described_class.create(user:) }++ it "finds a valid token with correct plaintext value" do+ found_token = described_class.find_valid_token(token.plain_value)+ expect(found_token).to eq(token)+ end++ it "returns nil for blank key" do+ expect(described_class.find_valid_token("")).to be_nil+ expect(described_class.find_valid_token(nil)).to be_nil+ end++ it "returns nil for non-existent token" do+ expect(described_class.find_valid_token("nonexistent")).to be_nil+ end++ it "returns nil for expired token" do+ token.update!(expires_on: 1.day.ago)+ expect(described_class.find_valid_token(token.plain_value)).to be_nil+ end++ it "returns nil for inactive user" do+ user.update!(status: User.statuses[:locked])+ expect(described_class.find_valid_token(token.plain_value)).to be_nil+ end++ it "returns nil for deleted user" do+ # Create a new user and token for this test+ test_user = create(:user)+ test_token = described_class.create(user: test_user)++ # Delete the token first, then the user+ test_token.destroy+ test_user.destroy++ # The token should no longer exist+ expect(described_class.find_valid_token(test_token.plain_value)).to be_nil+ end+ end++ describe "expiration" do+ it "is not expired when created" do+ token = described_class.create(user:)+ expect(token.expired?).to be false+ end++ it "is expired when expires_on is in the past" do+ token = described_class.create(user:)+ token.update!(expires_on: 1.day.ago)+ expect(token.expired?).to be true+ end++ it "uses validity_time for expiration calculation" do+ allow(described_class).to receive(:validity_time).and_return(7.days)+ token = described_class.create(user:)++ expect(token.expires_on).to be_within(1.second).of(7.days.from_now)+ end+ end++ describe "associations" do+ it "belongs to a user" do+ token = described_class.create(user:)+ expect(token.user).to eq(user)+ end++ it "can have multiple autologin session links" do+ token = described_class.create(user:)++ # Create SqlBypass sessions first+ sql_session1 = create(:user_session, user:)+ sql_session2 = create(:user_session, user:)++ # Get the corresponding UserSession records+ session1 = Sessions::UserSession.find_by(session_id: sql_session1.session_id)+ session2 = Sessions::UserSession.find_by(session_id: sql_session2.session_id)++ link1 = Sessions::AutologinSessionLink.create(token:, session: session1)+ link2 = Sessions::AutologinSessionLink.create(token:, session: session2)++ expect(token.autologin_session_links).to include(link1, link2)+ end+ end++ describe "deletion behavior" do+ it "deletes associated session links when token is destroyed" do+ token = described_class.create(user:)++ # Create SqlBypass session first+ sql_session = create(:user_session, user:)++ # Get the corresponding UserSession record+ session = Sessions::UserSession.find_by(session_id: sql_session.session_id)+ link = Sessions::AutologinSessionLink.create(token:, session:)++ expect { token.destroy }.to change(Sessions::AutologinSessionLink, :count).by(-1)+ expect { link.reload }.to raise_error(ActiveRecord::RecordNotFound)+ end+ end+enddiff --git a/spec/models/token/base_token_spec.rb b/spec/models/token/base_token_spec.rbindex af506f092c9..00fce765734 100644--- a/spec/models/token/base_token_spec.rb+++ b/spec/models/token/base_token_spec.rb@@ -42,9 +42,9 @@ RSpec.describe Token::Base doit "create_should_remove_existing_tokenses" dosubject.save!- t2 = Token::AutoLogin.create(user:)+ t2 = described_class.create(user:)expect(subject.value).not_to eq(t2.value)- expect(Token::AutoLogin.exists?(subject.id)).to be false- expect(Token::AutoLogin.exists?(t2.id)).to be true+ expect(described_class.exists?(subject.id)).to be false+ expect(described_class.exists?(t2.id)).to be trueendenddiff --git a/spec/requests/my/auto_login_tokens_spec.rb b/spec/requests/my/auto_login_tokens_spec.rbnew file mode 100644index 00000000000..72fcc420c7d--- /dev/null+++ b/spec/requests/my/auto_login_tokens_spec.rb@@ -0,0 +1,154 @@+# frozen_string_literal: true++# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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 "My::AutoLoginTokensController",+ :skip_csrf,+ type: :rails_request,+ with_settings: { autologin: 7 } do+ let(:user) { create(:user) }++ before do+ login_as(user)+ end++ describe "DELETE /my/auto_login_tokens/:id" do+ let!(:autologin_token) do+ create(:autologin_token,+ user:,+ data: { browser: "Firefox", browser_version: "142", platform: "macOS" })+ end++ let!(:linked_sql_session1) do+ create(:user_session,+ user:,+ data: { browser: "Firefox", browser_version: "142", platform: "macOS" })+ end++ let!(:linked_sql_session2) do+ create(:user_session,+ user:,+ data: { browser: "Firefox", browser_version: "142", platform: "macOS" })+ end++ let!(:unlinked_sql_session) do+ create(:user_session,+ user:,+ data: { browser: "Chrome", browser_version: "120", platform: "Windows" })+ end++ let!(:linked_session1) do+ Sessions::UserSession.find_by(session_id: linked_sql_session1.session_id)+ end++ let!(:linked_session2) do+ Sessions::UserSession.find_by(session_id: linked_sql_session2.session_id)+ end++ let!(:unlinked_session) do+ Sessions::UserSession.find_by(session_id: unlinked_sql_session.session_id)+ end++ let!(:autologin_session_link1) do+ Sessions::AutologinSessionLink.create(token: autologin_token, session: linked_session1)+ end++ let!(:autologin_session_link2) do+ Sessions::AutologinSessionLink.create(token: autologin_token, session: linked_session2)+ end++ let!(:other_token) { create(:autologin_token, user:) }+ let!(:other_sql_session) { create(:user_session, user:) }+ let!(:other_session) { Sessions::UserSession.find_by(session_id: other_sql_session.session_id) }+ let!(:other_link) { Sessions::AutologinSessionLink.create(token: other_token, session: other_session) }++ let!(:other_user) { create(:user) }+ let!(:other_user_token) { create(:autologin_token, user: other_user) }++ it "deletes the autologin token and all linked sessions" do+ delete "/my/auto_login_tokens/#{autologin_token.id}"++ # the subject token is deleted+ expect { autologin_token.reload }.to raise_error(ActiveRecord::RecordNotFound)++ # linked sessions are deleted+ expect { linked_session1.reload }.to raise_error(ActiveRecord::RecordNotFound)+ expect { linked_session2.reload }.to raise_error(ActiveRecord::RecordNotFound)++ # session links are deleted+ expect { autologin_session_link1.reload }.to raise_error(ActiveRecord::RecordNotFound)+ expect { autologin_session_link2.reload }.to raise_error(ActiveRecord::RecordNotFound)++ # unlinked session remains+ expect(unlinked_session.reload).to be_present++ # other token and its linked session remain+ expect(other_token.reload).to be_present+ expect(other_session.reload).to be_present+ expect(other_link.reload).to be_present+ end++ it "prevents deletion of tokens belonging to other users" do+ expect do+ delete "/my/auto_login_tokens/#{other_user_token.id}"+ end.not_to change(Token::AutoLogin, :count)++ expect(response).to have_http_status(:not_found)+ end++ it "handles non-existent token gracefully" do+ expect do+ delete "/my/auto_login_tokens/99999"+ end.not_to change(Token::AutoLogin, :count)++ expect(response).to have_http_status(:not_found)+ end++ context "when token has no linked sessions" do+ let!(:autologin_token_without_sessions) do+ create(:autologin_token, user:)+ end++ it "deletes only the token" do+ delete "/my/auto_login_tokens/#{autologin_token_without_sessions.id}"++ expect { autologin_token_without_sessions.reload }.to raise_error(ActiveRecord::RecordNotFound)++ expect { autologin_token.reload }.not_to raise_error+ expect { linked_session1.reload }.not_to raise_error+ expect { linked_session2.reload }.not_to raise_error++ expect(response).to redirect_to(my_sessions_path)+ expect(flash[:notice]).to eq(I18n.t(:notice_successful_delete))+ end+ end+ end+enddiff --git a/spec/services/users/logout_service_spec.rb b/spec/services/users/logout_service_spec.rbindex 63e0ccf6c1d..0946d8a633d 100644--- a/spec/services/users/logout_service_spec.rb+++ b/spec/services/users/logout_service_spec.rb@@ -105,11 +105,48 @@ RSpec.describe Users::LogoutService, type: :model doexpect(Sessions::UserSession.for_user(user).count).to eq(2)end- describe "autologin cookie" do+ describe "autologin cookie", with_settings: { autologin: 7 } dolet!(:token) { create(:autologin_token, user:) }++ let!(:linked_sql_session1) do+ create(:user_session,+ user:,+ data: { browser: "Firefox", browser_version: "142", platform: "macOS" })+ end++ let!(:linked_sql_session2) do+ create(:user_session,+ user:,+ data: { browser: "Firefox", browser_version: "142", platform: "macOS" })+ end++ let!(:other_linked_sql_session) do+ create(:user_session,+ user:,+ data: { browser: "Firefox", browser_version: "142", platform: "macOS" })+ end++ let!(:linked_session1) do+ Sessions::UserSession.find_by(session_id: linked_sql_session1.session_id)+ end++ let!(:linked_session2) do+ Sessions::UserSession.find_by(session_id: linked_sql_session2.session_id)+ end++ let!(:other_session) do+ Sessions::UserSession.find_by(session_id: other_linked_sql_session.session_id)+ end+let!(:other_token) { create(:autologin_token, user:) }- it "removes the matching autologin cookie and token" do+ before do+ Sessions::AutologinSessionLink.create(token:, session: linked_session1)+ Sessions::AutologinSessionLink.create(token:, session: linked_session2)+ Sessions::AutologinSessionLink.create(token: other_token, session: other_session)+ end++ it "removes only the matching autologin cookie and token, with associated sessions" docookies[OpenProject::Configuration.autologin_cookie_name] = token.plain_valuesubject@@ -117,6 +154,12 @@ RSpec.describe Users::LogoutService, type: :model doexpect(cookies[OpenProject::Configuration.autologin_cookie_name]).to be_nilexpect { token.reload }.to raise_error(ActiveRecord::RecordNotFound)expect(Token::AutoLogin.where(user_id: user.id).all).to contain_exactly other_token++ expect { linked_session1.reload }.to raise_error(ActiveRecord::RecordNotFound)+ expect { linked_session2.reload }.to raise_error(ActiveRecord::RecordNotFound)++ expect { other_token.reload }.not_to raise_error+ expect { other_session.reload }.not_to raise_errorendendend