trustedDeviceToken: move token youth

This commit is contained in:
simon lehericey 2019-02-04 11:04:55 +01:00
parent 23db8a160c
commit d664f130fd
4 changed files with 22 additions and 6 deletions

View file

@ -2,8 +2,6 @@ class Gestionnaire < ApplicationRecord
include CredentialsSyncableConcern
include EmailSanitizableConcern
LOGIN_TOKEN_YOUTH = 15.minutes
devise :database_authenticatable, :registerable, :async,
:recoverable, :rememberable, :trackable, :validatable
@ -204,8 +202,7 @@ class Gestionnaire < ApplicationRecord
def young_login_token?
trusted_device_token = trusted_device_tokens.order(created_at: :desc).first
trusted_device_token.present? &&
LOGIN_TOKEN_YOUTH.ago < trusted_device_token.created_at
trusted_device_token&.token_young?
end
private

View file

@ -1,5 +1,6 @@
class TrustedDeviceToken < ApplicationRecord
LOGIN_TOKEN_VALIDITY = 45.minutes
LOGIN_TOKEN_YOUTH = 15.minutes
belongs_to :gestionnaire
has_secure_token
@ -7,4 +8,8 @@ class TrustedDeviceToken < ApplicationRecord
def token_valid?
LOGIN_TOKEN_VALIDITY.ago < created_at
end
def token_young?
LOGIN_TOKEN_YOUTH.ago < created_at
end
end

View file

@ -403,13 +403,13 @@ describe Gestionnaire, type: :model do
end
context 'when the token is a bit old' do
before { gestionnaire.update(login_token_created_at: (Gestionnaire::LOGIN_TOKEN_YOUTH + 1.minute).ago) }
before { gestionnaire.trusted_device_tokens.first.update(created_at: (TrustedDeviceToken::LOGIN_TOKEN_YOUTH + 1.minute).ago) }
it { expect(gestionnaire.young_login_token?).to be false }
end
end
context 'when there are no token' do
it { expect(gestionnaire.young_login_token?).to be false }
it { expect(gestionnaire.young_login_token?).to be_falsey }
end
end

View file

@ -12,4 +12,18 @@ RSpec.describe TrustedDeviceToken, type: :model do
it { expect(token.token_valid?).to be false }
end
end
describe '#token_young?' do
let(:token) { TrustedDeviceToken.create }
context 'when the token is create after login_token_youth' do
it { expect(token.token_young?).to be true }
end
context 'when the token is create before login_token_youth' do
before { token.update(created_at: (TrustedDeviceToken::LOGIN_TOKEN_YOUTH + 1.minute).ago) }
it { expect(token.token_young?).to be false }
end
end
end