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

add failing spec for 2fa behavior when deleting methods

Klaus Zandersde14ecc29637
diff --git a/modules/two_factor_authentication/spec/features/login/login_with_deleted_2fa_spec.rb b/modules/two_factor_authentication/spec/features/login/login_with_deleted_2fa_spec.rb
new file mode 100644
index 00000000000..16ea18886bc
--- /dev/null
+++ b/modules/two_factor_authentication/spec/features/login/login_with_deleted_2fa_spec.rb
@@ -0,0 +1,53 @@
+require_relative "../../spec_helper"
+require_relative "../shared_2fa_examples"
+
+RSpec.describe "Login after 2FA deleted 2FA was deleted (REGRESSION)",
+ :js,
+ :with_cuprite,
+ with_settings: {
+ plugin_openproject_two_factor_authentication: {
+ "active_strategies" => %i[developer totp]
+ }
+ } do
+ let(:user_password) { "bob!" * 4 }
+ let(:user) do
+ create(:user,
+ login: "bob",
+ password: user_password,
+ password_confirmation: user_password)
+ end
+
+ let!(:device1) { create(:two_factor_authentication_device_sms, user:, active: true, default: false) }
+ let!(:device2) { create(:two_factor_authentication_device_totp, user:, active: true, default: true) }
+
+ it "works correctly when not switching 2fa method" do
+ first_login_step
+
+ # ensure that no 2fa device is stored in the session
+ session_data = Sessions::UserSession.last.data
+ expect(session_data["two_factor_authentication_device_id"]).to be_nil
+
+ # destroy all 2fa devices
+ user.otp_devices.destroy_all
+
+ # make sure we can sign in without 2fa
+ first_login_step
+ expect_logged_in
+ end
+
+ it "works correctly when the 2fa method was switched before deleting" do
+ first_login_step
+ switch_two_factor_device(device1)
+
+ # ensure that the selected 2fa device is stored in the session
+ session_data = Sessions::UserSession.last.data
+ expect(session_data["two_factor_authentication_device_id"]).to eq(device1.id)
+
+ # destroy all 2fa devices
+ user.otp_devices.destroy_all
+
+ # make sure we can sign in without 2fa
+ first_login_step
+ expect_logged_in
+ end
+end
diff --git a/modules/two_factor_authentication/spec/features/shared_2fa_examples.rb b/modules/two_factor_authentication/spec/features/shared_2fa_examples.rb
index 2d0a94a2465..367cfbe75b1 100644
--- a/modules/two_factor_authentication/spec/features/shared_2fa_examples.rb
+++ b/modules/two_factor_authentication/spec/features/shared_2fa_examples.rb
@@ -8,6 +8,13 @@ def first_login_step
wait_for_network_idle
end
+def switch_two_factor_device(device)
+ within("#login-form") do
+ click_link_or_button I18n.t(:text_otp_not_receive)
+ click_link_or_button device.redacted_identifier
+ end
+end
+
def two_factor_step(token)
expect(page).to have_css("input#otp")
fill_in "otp", with: token