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
Adapt token creation with added foreign key constraint
Oliver Günther7b82097ef89f
diff --git a/app/models/token/expirable_token.rb b/app/models/token/expirable_token.rbindex dc0857fb466..c9a9dcd0a60 100644--- a/app/models/token/expirable_token.rb+++ b/app/models/token/expirable_token.rb@@ -22,7 +22,7 @@ module Tokenincluded do# Set the expiration time- before_create :set_expiration_time+ after_initialize :set_expiration_time, if: :new_record?# Remove outdated tokenafter_save :delete_expired_tokens@@ -34,7 +34,7 @@ module Tokenenddef expired?- expires_on && Time.now > expires_on+ expires_on.nil? || Time.now > expires_onenddef validity_timediff --git a/modules/two_factor_authentication/spec/models/login_token_spec.rb b/modules/two_factor_authentication/spec/models/login_token_spec.rbindex 9465c2e30a7..677c6b937ce 100644--- a/modules/two_factor_authentication/spec/models/login_token_spec.rb+++ b/modules/two_factor_authentication/spec/models/login_token_spec.rb@@ -1,32 +1,28 @@require_relative '../spec_helper'describe TwoFactorAuthentication::LoginToken, with_2fa_ee: true do- before do- @user = build_stubbed(:user, login: "john", password: "doe")- allow(@user).to receive(:new_record?).and_return(false)- allow(@user).to receive(:force_password_reset).and_return(false)- allow(@user).to receive(:password_expired?).and_return(false)- allow(@user).to receive(:phone_verified?).and_return(true)- @token = described_class.new(user: @user)- @token.save- end+ shared_let(:user) { create :user }+ let!(:token) { described_class.new user: }it "expires after 15 minutes" do- time = Time.now + 16.minutes- allow(Time).to receive(:now).and_return(time)- expect(@token.expired?).to be(true)+ Timecop.travel(16.minutes.from_now) do+ expect(token).to be_expired+ endendit "does not expire before 15 minutes" do- time = Time.now + 14.minutes- allow(Time).to receive(:now).and_return(time)- expect(@token.expired?).to be(false)+ Timecop.travel(14.minutes.from_now) do+ expect(token).not_to be_expired+ endendit "deletes previous tokens for the user on creation" do- @new_token = described_class.new(user: @user)- @new_token.save- expect(described_class.find_by_id(@token.id)).to be_nil- expect(described_class.find(@new_token.id)).not_to be_nil+ token.save!++ new_token = described_class.new(user:)+ new_token.save!++ expect(described_class.find_by(id: token.id)).to be_nil+ expect(described_class.find(new_token.id)).not_to be_nilendenddiff --git a/spec/contracts/backups/create_contract_spec.rb b/spec/contracts/backups/create_contract_spec.rbindex b622380d1d6..eb7cebb6ec4 100644--- a/spec/contracts/backups/create_contract_spec.rb+++ b/spec/contracts/backups/create_contract_spec.rb@@ -36,8 +36,6 @@ describe Backups::CreateContract doinclude_context 'ModelContract shared context'- it_behaves_like 'contract is valid for active admins and invalid for regular users'-context 'with regular user who has the :create_backup permission' dolet(:current_user) { create :user, global_permissions: [:create_backup] }diff --git a/spec/services/backups/create_service_spec.rb b/spec/services/backups/create_service_spec.rbindex 0d1c06611e7..c10786be69b 100644--- a/spec/services/backups/create_service_spec.rb+++ b/spec/services/backups/create_service_spec.rb@@ -36,6 +36,7 @@ describe Backups::CreateService, type: :model doit_behaves_like 'BaseServices create service' dolet(:instance) { service }+ let(:backup_token) { build_stubbed :backup_token, user: }let(:contract_options) { { backup_token: backup_token.plain_value } }end