diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-10-20 11:43:02 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-10-20 11:43:02 +0300 |
commit | d9ab72d6080f594d0b3cae15f14b3ef2c6c638cb (patch) | |
tree | 2341ef426af70ad1e289c38036737e04b0aa5007 /spec/services/clusters | |
parent | d6e514dd13db8947884cd58fe2a9c2a063400a9b (diff) |
Add latest changes from gitlab-org/gitlab@14-4-stable-eev14.4.0-rc42
Diffstat (limited to 'spec/services/clusters')
3 files changed, 151 insertions, 0 deletions
diff --git a/spec/services/clusters/agent_tokens/create_service_spec.rb b/spec/services/clusters/agent_tokens/create_service_spec.rb new file mode 100644 index 00000000000..92629af06c8 --- /dev/null +++ b/spec/services/clusters/agent_tokens/create_service_spec.rb @@ -0,0 +1,64 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Clusters::AgentTokens::CreateService do + subject(:service) { described_class.new(container: project, current_user: user, params: params) } + + let_it_be(:user) { create(:user) } + + let(:cluster_agent) { create(:cluster_agent) } + let(:project) { cluster_agent.project } + let(:params) { { agent_id: cluster_agent.id, description: 'token description', name: 'token name' } } + + 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 + + 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 + + it 'returns validation errors', :aggregate_failures do + expect(subject.status).to eq(:error) + expect(subject.message).to eq(["Agent must exist", "Name can't be blank"]) + end + end + end + end +end diff --git a/spec/services/clusters/agents/create_service_spec.rb b/spec/services/clusters/agents/create_service_spec.rb new file mode 100644 index 00000000000..2b3bbcae13c --- /dev/null +++ b/spec/services/clusters/agents/create_service_spec.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Clusters::Agents::CreateService do + subject(:service) { described_class.new(project, user) } + + let(:project) { create(:project, :public, :repository) } + let(:user) { create(:user) } + + describe '#execute' do + context 'without user permissions' do + it 'returns errors when user does not have permissions' do + expect(service.execute(name: 'missing-permissions')).to eq({ + status: :error, + message: 'You have insufficient permissions to create a cluster agent for this project' + }) + end + end + + context 'with user permissions' do + before do + project.add_maintainer(user) + end + + it 'creates a new clusters_agent' do + expect { service.execute(name: 'with-user') }.to change { ::Clusters::Agent.count }.by(1) + end + + it 'returns success status', :aggregate_failures do + result = service.execute(name: 'success') + + expect(result[:status]).to eq(:success) + expect(result[:message]).to be_nil + end + + it 'returns agent values', :aggregate_failures do + new_agent = service.execute(name: 'new-agent')[:cluster_agent] + + expect(new_agent.name).to eq('new-agent') + expect(new_agent.created_by_user).to eq(user) + end + + it 'generates an error message when name is invalid' do + expect(service.execute(name: '@bad_agent_name!')).to eq({ + status: :error, + message: ["Name can contain only lowercase letters, digits, and '-', but cannot start or end with '-'"] + }) + end + end + end +end diff --git a/spec/services/clusters/agents/delete_service_spec.rb b/spec/services/clusters/agents/delete_service_spec.rb new file mode 100644 index 00000000000..1d6bc9618dd --- /dev/null +++ b/spec/services/clusters/agents/delete_service_spec.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Clusters::Agents::DeleteService do + subject(:service) { described_class.new(container: project, current_user: user) } + + let(:cluster_agent) { create(:cluster_agent) } + let(:project) { cluster_agent.project } + let(:user) { create(:user) } + + describe '#execute' do + context 'without user permissions' do + it 'fails to delete when the user has no permissions', :aggregate_failures do + response = service.execute(cluster_agent) + + expect(response.status).to eq(:error) + expect(response.message).to eq('You have insufficient permissions to delete this cluster agent') + + expect { cluster_agent.reload }.not_to raise_error(ActiveRecord::RecordNotFound) + end + end + + context 'with user permissions' do + before do + project.add_maintainer(user) + end + + it 'deletes a cluster agent', :aggregate_failures do + expect { service.execute(cluster_agent) }.to change { ::Clusters::Agent.count }.by(-1) + expect { cluster_agent.reload }.to raise_error(ActiveRecord::RecordNotFound) + end + end + end +end |