Commit 1d9c109e authored by Kamil Trzciński's avatar Kamil Trzciński

Merge branch '56110-cluster-kubernetes-api-500-error-on-post-request-ee' into 'master'

EE Port: Improves restriction of multiple Kubernetes clusters via API

Closes gitlab-ce#56110

See merge request gitlab-org/gitlab-ee!9088
parents b64f59d1 faa5b22e
---
title: Improves restriction of multiple Kubernetes clusters through API
merge_request: 24251
author:
type: fixed
......@@ -56,12 +56,10 @@ describe API::ProjectClusters do
}
end
before do
post api("/projects/#{project.id}/clusters/user", current_user), params: cluster_params
end
context 'when user sets specific environment scope' do
it 'should create a cluster with that specific environment' do
post api("/projects/#{project.id}/clusters/user", current_user), params: cluster_params
expect(json_response['environment_scope']).to eq('production/*')
end
end
......@@ -75,9 +73,30 @@ describe API::ProjectClusters do
end
it 'should set default environment' do
post api("/projects/#{project.id}/clusters/user", current_user), params: cluster_params
expect(json_response['environment_scope']).to eq('*')
end
end
context 'when license has multiple clusters feature' do
before do
stub_licensed_features(multiple_clusters: true)
create(:cluster, :provided_by_gcp, :project,
projects: [project])
post api("/projects/#{project.id}/clusters/user", current_user), params: cluster_params
end
it 'should respond with 201' do
expect(response).to have_gitlab_http_status(201)
end
it 'should allow to associate multiple cluster to project' do
expect(project.reload.clusters.count).to eq(2)
end
end
end
describe 'PUT /projects/:id/clusters/:cluster_id' do
......
......@@ -248,8 +248,8 @@ module API
forbidden! unless current_user.admin?
end
def authorize!(action, subject = :global)
forbidden! unless can?(current_user, action, subject)
def authorize!(action, subject = :global, reason = nil)
forbidden!(reason) unless can?(current_user, action, subject)
end
def authorize_push_project
......
......@@ -65,7 +65,7 @@ module API
use :create_params_ee
end
post ':id/clusters/user' do
authorize! :create_cluster, user_project
authorize! :add_cluster, user_project, 'Instance does not support multiple Kubernetes clusters'
user_cluster = ::Clusters::CreateService
.new(current_user, create_cluster_user_params)
......
......@@ -266,6 +266,23 @@ describe API::ProjectClusters do
end
end
end
context 'when user tries to add multiple clusters' do
before do
create(:cluster, :provided_by_gcp, :project,
projects: [project])
post api("/projects/#{project.id}/clusters/user", current_user), params: cluster_params
end
it 'should respond with 403' do
expect(response).to have_gitlab_http_status(403)
end
it 'should return an appropriate message' do
expect(json_response['message']).to include('Instance does not support multiple Kubernetes clusters')
end
end
end
describe 'PUT /projects/:id/clusters/:cluster_id' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment