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

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.rb
index dc0857fb466..c9a9dcd0a60 100644
--- a/app/models/token/expirable_token.rb
+++ b/app/models/token/expirable_token.rb
@@ -22,7 +22,7 @@ module Token
included do
# Set the expiration time
- before_create :set_expiration_time
+ after_initialize :set_expiration_time, if: :new_record?
# Remove outdated token
after_save :delete_expired_tokens
@@ -34,7 +34,7 @@ module Token
end
def expired?
- expires_on && Time.now > expires_on
+ expires_on.nil? || Time.now > expires_on
end
def validity_time
diff --git a/modules/two_factor_authentication/spec/models/login_token_spec.rb b/modules/two_factor_authentication/spec/models/login_token_spec.rb
index 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
+ end
end
it "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
+ end
end
it "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_nil
end
end
diff --git a/spec/contracts/backups/create_contract_spec.rb b/spec/contracts/backups/create_contract_spec.rb
index 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 do
include_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' do
let(: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.rb
index 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 do
it_behaves_like 'BaseServices create service' do
let(:instance) { service }
+ let(:backup_token) { build_stubbed :backup_token, user: }
let(:contract_options) { { backup_token: backup_token.plain_value } }
end