2023-01-13 00:05:48 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe Clusters::AgentTokensFinder do
|
|
|
|
describe '#execute' do
|
|
|
|
let_it_be(:project) { create(:project) }
|
2023-03-04 22:38:38 +05:30
|
|
|
let_it_be(:agent) { create(:cluster_agent, project: project) }
|
2023-01-13 00:05:48 +05:30
|
|
|
let(:user) { create(:user, maintainer_projects: [project]) }
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
let_it_be(:active_agent_tokens) do
|
2023-01-13 00:05:48 +05:30
|
|
|
[
|
|
|
|
create(:cluster_agent_token, agent: agent),
|
2023-03-04 22:38:38 +05:30
|
|
|
create(:cluster_agent_token, agent: agent)
|
2023-01-13 00:05:48 +05:30
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
let_it_be(:revoked_agent_tokens) do
|
|
|
|
[
|
|
|
|
create(:cluster_agent_token, :revoked, agent: agent),
|
|
|
|
create(:cluster_agent_token, :revoked, agent: agent)
|
|
|
|
]
|
|
|
|
end
|
2023-01-13 00:05:48 +05:30
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
before_all do
|
|
|
|
# set up a token under a different agent as a way to verify
|
|
|
|
# that only tokens of a given agent are included in the result
|
2023-01-13 00:05:48 +05:30
|
|
|
create(:cluster_agent_token, agent: create(:cluster_agent))
|
2023-03-04 22:38:38 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
subject(:execute) { described_class.new(agent, user).execute }
|
|
|
|
|
|
|
|
it { is_expected.to match_array(active_agent_tokens + revoked_agent_tokens) }
|
2023-01-13 00:05:48 +05:30
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
context 'when filtering by status=active' do
|
|
|
|
subject(:execute) { described_class.new(agent, user, status: 'active').execute }
|
|
|
|
|
|
|
|
it { is_expected.to match_array(active_agent_tokens) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when filtering by status=revoked' do
|
|
|
|
subject(:execute) { described_class.new(agent, user, status: 'revoked').execute }
|
|
|
|
|
|
|
|
it { is_expected.to match_array(revoked_agent_tokens) }
|
2023-01-13 00:05:48 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user does not have permission' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_reporter(user)
|
|
|
|
end
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
it { is_expected.to eq ::Clusters::AgentToken.none }
|
2023-01-13 00:05:48 +05:30
|
|
|
end
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
context 'when current_user is nil' do
|
|
|
|
it 'returns an empty list' do
|
|
|
|
result = described_class.new(agent, nil).execute
|
|
|
|
expect(result).to eq ::Clusters::AgentToken.none
|
|
|
|
end
|
|
|
|
end
|
2023-01-13 00:05:48 +05:30
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
context 'when agent is nil' do
|
|
|
|
it 'returns an empty list' do
|
|
|
|
result = described_class.new(nil, user).execute
|
|
|
|
expect(result).to eq ::Clusters::AgentToken.none
|
2023-01-13 00:05:48 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|