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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

77 lines
2.4 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
2022-03-02 08:16:31 +05:30
it_behaves_like 'having unique enum values'
2021-06-08 01:23:25 +05:30
describe 'scopes' do
describe '.order_last_used_at_desc' do
2022-03-02 08:16:31 +05:30
let_it_be(:agent) { create(:cluster_agent) }
let_it_be(:token_1) { create(:cluster_agent_token, agent: agent, last_used_at: 7.days.ago) }
let_it_be(:token_2) { create(:cluster_agent_token, agent: agent, last_used_at: nil) }
let_it_be(:token_3) { create(:cluster_agent_token, agent: agent, last_used_at: 2.days.ago) }
2021-06-08 01:23:25 +05:30
it 'sorts by last_used_at descending, with null values at last' do
expect(described_class.order_last_used_at_desc)
.to eq([token_3, token_1, token_2])
end
end
2022-03-02 08:16:31 +05:30
2023-07-09 08:55:56 +05:30
describe 'status-related scopes' do
2022-03-02 08:16:31 +05:30
let!(:active_token) { create(:cluster_agent_token) }
let!(:revoked_token) { create(:cluster_agent_token, :revoked) }
2023-07-09 08:55:56 +05:30
describe '.with_status' do
context 'when filtering by active status' do
subject { described_class.with_status(:active) }
2022-03-02 08:16:31 +05:30
2023-07-09 08:55:56 +05:30
it { is_expected.to contain_exactly(active_token) }
end
context 'when filtering by revoked status' do
subject { described_class.with_status(:revoked) }
it { is_expected.to contain_exactly(revoked_token) }
end
end
describe '.active' do
subject { described_class.active }
it { is_expected.to contain_exactly(active_token) }
end
2022-03-02 08:16:31 +05:30
end
2021-06-08 01:23:25 +05:30
end
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
2023-03-04 22:38:38 +05:30
describe '#to_ability_name' do
it 'is :cluster' do
agent_token = build(:cluster_agent_token)
expect(agent_token.to_ability_name).to eq :cluster
end
end
2020-10-24 23:57:45 +05:30
end