Add fci valid_for_merge

This commit is contained in:
simon lehericey 2021-10-13 00:14:12 +02:00
parent 2e118a8f5b
commit 34862f41e0
2 changed files with 30 additions and 0 deletions

View file

@ -18,6 +18,8 @@
# user_id :integer # user_id :integer
# #
class FranceConnectInformation < ApplicationRecord class FranceConnectInformation < ApplicationRecord
MERGE_VALIDITY = 15.minutes
belongs_to :user, optional: true belongs_to :user, optional: true
validates :france_connect_particulier_id, presence: true, allow_blank: false, allow_nil: false validates :france_connect_particulier_id, presence: true, allow_blank: false, allow_nil: false
@ -39,4 +41,8 @@ class FranceConnectInformation < ApplicationRecord
update_attribute('user_id', user.id) update_attribute('user_id', user.id)
touch # needed to update updated_at column touch # needed to update updated_at column
end end
def valid_for_merge?
(MERGE_VALIDITY.ago < merge_token_created_at) && user_id.nil?
end
end end

View file

@ -19,4 +19,28 @@ describe FranceConnectInformation, type: :model do
end end
end end
end end
describe '#valid_for_merge?' do
let(:fci) { create(:france_connect_information) }
subject { fci.valid_for_merge? }
context 'when the merge token is young enough' do
before { fci.merge_token_created_at = 1.minute.ago }
it { is_expected.to be(true) }
context 'but the fci is already linked to an user' do
before { fci.update(user: create(:user)) }
it { is_expected.to be(false) }
end
end
context 'when the merge token is too old' do
before { fci.merge_token_created_at = (FranceConnectInformation::MERGE_VALIDITY + 1.minute).ago }
it { is_expected.to be(false) }
end
end
end end