2020-07-20 05:09:22 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe Clusters::AgentToken do
|
2021-02-19 19:10:55 -05:00
|
|
|
it { is_expected.to belong_to(:agent).class_name('Clusters::Agent').required }
|
2021-02-16 13:09:24 -05:00
|
|
|
it { is_expected.to belong_to(:created_by_user).class_name('User').optional }
|
2021-02-19 19:10:55 -05:00
|
|
|
it { is_expected.to validate_length_of(:description).is_at_most(1024) }
|
2021-03-01 19:11:26 -05:00
|
|
|
it { is_expected.to validate_length_of(:name).is_at_most(255) }
|
|
|
|
it { is_expected.to validate_presence_of(:name) }
|
2020-07-20 05:09:22 -04:00
|
|
|
|
2021-12-21 22:15:20 -05:00
|
|
|
it_behaves_like 'having unique enum values'
|
|
|
|
|
2021-04-21 05:09:15 -04:00
|
|
|
describe 'scopes' do
|
|
|
|
describe '.order_last_used_at_desc' do
|
2022-01-11 10:15:55 -05:00
|
|
|
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-04-21 05:09:15 -04:00
|
|
|
|
|
|
|
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-01-11 10:15:55 -05:00
|
|
|
|
|
|
|
describe '.with_status' do
|
|
|
|
let!(:active_token) { create(:cluster_agent_token) }
|
|
|
|
let!(:revoked_token) { create(:cluster_agent_token, :revoked) }
|
|
|
|
|
|
|
|
subject { described_class.with_status(:active) }
|
|
|
|
|
|
|
|
it { is_expected.to contain_exactly(active_token) }
|
|
|
|
end
|
2021-04-21 05:09:15 -04:00
|
|
|
end
|
|
|
|
|
2020-07-20 05:09:22 -04:00
|
|
|
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
|
2020-10-22 23:08:37 -04:00
|
|
|
|
|
|
|
it 'is at least 50 characters' do
|
|
|
|
agent_token = create(:cluster_agent_token)
|
|
|
|
expect(agent_token.token.length).to be >= 50
|
|
|
|
end
|
2020-07-20 05:09:22 -04:00
|
|
|
end
|
|
|
|
end
|