2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
RSpec.describe Keys::LastUsedService, feature_category: :source_code_management do
|
2018-03-17 18:26:18 +05:30
|
|
|
describe '#execute', :clean_gitlab_redis_shared_state do
|
|
|
|
it 'updates the key when it has not been used recently' do
|
|
|
|
key = create(:key, last_used_at: 1.year.ago)
|
|
|
|
time = Time.zone.now
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
travel_to(time) { described_class.new(key).execute }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
expect(key.reload.last_used_at).to be_like_time(time)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not update the key when it has been used recently' do
|
|
|
|
time = 1.minute.ago
|
|
|
|
key = create(:key, last_used_at: time)
|
|
|
|
|
|
|
|
described_class.new(key).execute
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
expect(key.last_used_at).to be_like_time(time)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not update the updated_at field' do
|
|
|
|
# Since a lot of these updates could happen in parallel for different keys
|
|
|
|
# we want these updates to be as lightweight as possible, hence we want to
|
|
|
|
# make sure we _only_ update last_used_at and not always updated_at.
|
|
|
|
key = create(:key, last_used_at: 1.year.ago)
|
|
|
|
|
|
|
|
expect { described_class.new(key).execute }.not_to change { key.updated_at }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#update?', :clean_gitlab_redis_shared_state do
|
|
|
|
it 'returns true when no last used timestamp is present' do
|
|
|
|
key = build(:key, last_used_at: nil)
|
|
|
|
service = described_class.new(key)
|
|
|
|
|
|
|
|
expect(service.update?).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true when the key needs to be updated' do
|
|
|
|
key = build(:key, last_used_at: 1.year.ago)
|
|
|
|
service = described_class.new(key)
|
|
|
|
|
|
|
|
expect(service.update?).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false when a lease has already been obtained' do
|
|
|
|
key = build(:key, last_used_at: 1.year.ago)
|
|
|
|
service = described_class.new(key)
|
|
|
|
|
|
|
|
expect(service.update?).to eq(true)
|
|
|
|
expect(service.update?).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false when the key does not yet need to be updated' do
|
|
|
|
key = build(:key, last_used_at: 1.minute.ago)
|
|
|
|
service = described_class.new(key)
|
|
|
|
|
|
|
|
expect(service.update?).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|