debian-mirror-gitlab/spec/models/clusters/agent_token_spec.rb

77 lines
2.1 KiB
Ruby
Raw Normal View History

2020-10-24 23:57:45 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Clusters::AgentToken do
2021-04-17 20:07:23 +05:30
it { is_expected.to belong_to(:agent).class_name('Clusters::Agent').required }
2021-03-11 19:13:27 +05:30
it { is_expected.to belong_to(:created_by_user).class_name('User').optional }
2021-04-17 20:07:23 +05:30
it { is_expected.to validate_length_of(:description).is_at_most(1024) }
it { is_expected.to validate_length_of(:name).is_at_most(255) }
it { is_expected.to validate_presence_of(:name) }
2020-10-24 23:57:45 +05:30
describe '#token' do
it 'is generated on save' do
agent_token = build(:cluster_agent_token, token_encrypted: nil)
expect(agent_token.token).to be_nil
agent_token.save!
expect(agent_token.token).to be_present
end
2021-01-29 00:20:46 +05:30
it 'is at least 50 characters' do
agent_token = create(:cluster_agent_token)
expect(agent_token.token.length).to be >= 50
end
2020-10-24 23:57:45 +05:30
end
2021-04-29 21:17:54 +05:30
describe '#track_usage', :clean_gitlab_redis_cache do
let(:agent_token) { create(:cluster_agent_token) }
subject { agent_token.track_usage }
context 'when last_used_at was updated recently' do
before do
agent_token.update!(last_used_at: 10.minutes.ago)
end
it 'updates cache but not database' do
expect { subject }.not_to change { agent_token.reload.read_attribute(:last_used_at) }
expect_redis_update
end
end
context 'when last_used_at was not updated recently' do
it 'updates cache and database' do
does_db_update
expect_redis_update
end
context 'with invalid token' do
before do
agent_token.description = SecureRandom.hex(2000)
end
it 'still updates caches and database' do
expect(agent_token).to be_invalid
does_db_update
expect_redis_update
end
end
end
def expect_redis_update
Gitlab::Redis::Cache.with do |redis|
redis_key = "cache:#{described_class.name}:#{agent_token.id}:attributes"
expect(redis.get(redis_key)).to be_present
end
end
def does_db_update
expect { subject }.to change { agent_token.reload.read_attribute(:last_used_at) }
end
end
2020-10-24 23:57:45 +05:30
end