2021-11-18 22:05:49 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
RSpec.describe Clusters::AgentTokens::CreateService, feature_category: :deployment_management do
|
2023-05-27 22:25:52 +05:30
|
|
|
subject(:service) { described_class.new(agent: cluster_agent, current_user: user, params: params) }
|
2021-11-18 22:05:49 +05:30
|
|
|
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
|
|
|
|
let(:cluster_agent) { create(:cluster_agent) }
|
|
|
|
let(:project) { cluster_agent.project }
|
2023-05-27 22:25:52 +05:30
|
|
|
let(:params) { { description: 'token description', name: 'token name' } }
|
2021-11-18 22:05:49 +05:30
|
|
|
|
|
|
|
describe '#execute' do
|
|
|
|
subject { service.execute }
|
|
|
|
|
|
|
|
it 'does not create a new token due to user permissions' do
|
|
|
|
expect { subject }.not_to change(::Clusters::AgentToken, :count)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns permission errors', :aggregate_failures do
|
|
|
|
expect(subject.status).to eq(:error)
|
|
|
|
expect(subject.message).to eq('User has insufficient permissions to create a token for this project')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with user permissions' do
|
|
|
|
before do
|
|
|
|
project.add_maintainer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a new token' do
|
|
|
|
expect { subject }.to change { ::Clusters::AgentToken.count }.by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns success status', :aggregate_failures do
|
|
|
|
expect(subject.status).to eq(:success)
|
|
|
|
expect(subject.message).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns token information', :aggregate_failures do
|
|
|
|
token = subject.payload[:token]
|
|
|
|
|
|
|
|
expect(subject.payload[:secret]).not_to be_nil
|
|
|
|
|
|
|
|
expect(token.created_by_user).to eq(user)
|
|
|
|
expect(token.description).to eq(params[:description])
|
|
|
|
expect(token.name).to eq(params[:name])
|
|
|
|
end
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
it 'creates an activity event' do
|
|
|
|
expect { subject }.to change { ::Clusters::Agents::ActivityEvent.count }.by(1)
|
|
|
|
|
|
|
|
token = subject.payload[:token].reload
|
|
|
|
event = cluster_agent.activity_events.last
|
|
|
|
|
|
|
|
expect(event).to have_attributes(
|
|
|
|
kind: 'token_created',
|
|
|
|
level: 'info',
|
|
|
|
recorded_at: token.created_at,
|
|
|
|
user: token.created_by_user,
|
|
|
|
agent_token: token
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
context 'when params are invalid' do
|
|
|
|
let(:params) { { agent_id: 'bad_id' } }
|
|
|
|
|
|
|
|
it 'does not create a new token' do
|
|
|
|
expect { subject }.not_to change(::Clusters::AgentToken, :count)
|
|
|
|
end
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
it 'does not create an activity event' do
|
|
|
|
expect { subject }.not_to change { ::Clusters::Agents::ActivityEvent.count }
|
|
|
|
end
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
it 'returns validation errors', :aggregate_failures do
|
|
|
|
expect(subject.status).to eq(:error)
|
2023-05-27 22:25:52 +05:30
|
|
|
expect(subject.message).to eq(["Name can't be blank"])
|
2021-11-18 22:05:49 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|