Commit b69a0474 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Merge branch 'refactor-project-cluster-controller-specs' into 'master'

Project::ClustersController spec improvement

See merge request gitlab-org/gitlab-ce!22346
parents e347170c 0d7bb4f6
...@@ -6,15 +6,19 @@ describe Projects::ClustersController do ...@@ -6,15 +6,19 @@ describe Projects::ClustersController do
set(:project) { create(:project) } set(:project) { create(:project) }
describe 'GET index' do let(:user) { create(:user) }
describe 'functionality' do
let(:user) { create(:user) }
before do before do
project.add_maintainer(user) project.add_maintainer(user)
sign_in(user) sign_in(user)
end end
describe 'GET index' do
def go(params = {})
get :index, params.reverse_merge(namespace_id: project.namespace.to_param, project_id: project)
end
describe 'functionality' do
context 'when project has one or more clusters' do context 'when project has one or more clusters' do
let(:project) { create(:project) } let(:project) { create(:project) }
let!(:enabled_cluster) { create(:cluster, :provided_by_gcp, projects: [project]) } let!(:enabled_cluster) { create(:cluster, :provided_by_gcp, projects: [project]) }
...@@ -33,10 +37,11 @@ describe Projects::ClustersController do ...@@ -33,10 +37,11 @@ describe Projects::ClustersController do
before do before do
allow(Clusters::Cluster).to receive(:paginates_per).and_return(1) allow(Clusters::Cluster).to receive(:paginates_per).and_return(1)
create_list(:cluster, 2, :provided_by_gcp, :production_environment, projects: [project]) create_list(:cluster, 2, :provided_by_gcp, :production_environment, projects: [project])
get :index, namespace_id: project.namespace, project_id: project, page: last_page
end end
it 'redirects to the page' do it 'redirects to the page' do
go(page: last_page)
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(assigns(:clusters).current_page).to eq(last_page) expect(assigns(:clusters).current_page).to eq(last_page)
end end
...@@ -68,21 +73,14 @@ describe Projects::ClustersController do ...@@ -68,21 +73,14 @@ describe Projects::ClustersController do
it { expect { go }.to be_denied_for(:user) } it { expect { go }.to be_denied_for(:user) }
it { expect { go }.to be_denied_for(:external) } it { expect { go }.to be_denied_for(:external) }
end end
end
describe 'GET new' do
def go def go
get :index, namespace_id: project.namespace.to_param, project_id: project get :new, namespace_id: project.namespace, project_id: project
end end
end
describe 'GET new' do
describe 'functionality for new cluster' do describe 'functionality for new cluster' do
let(:user) { create(:user) }
before do
project.add_maintainer(user)
sign_in(user)
end
context 'when omniauth has been configured' do context 'when omniauth has been configured' do
let(:key) { 'secret-key' } let(:key) { 'secret-key' }
let(:session_key_for_redirect_uri) do let(:session_key_for_redirect_uri) do
...@@ -139,13 +137,6 @@ describe Projects::ClustersController do ...@@ -139,13 +137,6 @@ describe Projects::ClustersController do
end end
describe 'functionality for existing cluster' do describe 'functionality for existing cluster' do
let(:user) { create(:user) }
before do
project.add_maintainer(user)
sign_in(user)
end
it 'has new object' do it 'has new object' do
go go
...@@ -163,10 +154,6 @@ describe Projects::ClustersController do ...@@ -163,10 +154,6 @@ describe Projects::ClustersController do
it { expect { go }.to be_denied_for(:user) } it { expect { go }.to be_denied_for(:user) }
it { expect { go }.to be_denied_for(:external) } it { expect { go }.to be_denied_for(:external) }
end end
def go
get :new, namespace_id: project.namespace, project_id: project
end
end end
describe 'POST create for new cluster' do describe 'POST create for new cluster' do
...@@ -183,14 +170,11 @@ describe Projects::ClustersController do ...@@ -183,14 +170,11 @@ describe Projects::ClustersController do
} }
end end
describe 'functionality' do def go
let(:user) { create(:user) } post :create_gcp, params.merge(namespace_id: project.namespace, project_id: project)
end
before do
project.add_maintainer(user)
sign_in(user)
end
describe 'functionality' do
context 'when access token is valid' do context 'when access token is valid' do
before do before do
stub_google_api_validate_token stub_google_api_validate_token
...@@ -257,10 +241,6 @@ describe Projects::ClustersController do ...@@ -257,10 +241,6 @@ describe Projects::ClustersController do
it { expect { go }.to be_denied_for(:user) } it { expect { go }.to be_denied_for(:user) }
it { expect { go }.to be_denied_for(:external) } it { expect { go }.to be_denied_for(:external) }
end end
def go
post :create_gcp, params.merge(namespace_id: project.namespace, project_id: project)
end
end end
describe 'POST create for existing cluster' do describe 'POST create for existing cluster' do
...@@ -277,14 +257,11 @@ describe Projects::ClustersController do ...@@ -277,14 +257,11 @@ describe Projects::ClustersController do
} }
end end
describe 'functionality' do def go
let(:user) { create(:user) } post :create_user, params.merge(namespace_id: project.namespace, project_id: project)
end
before do
project.add_maintainer(user)
sign_in(user)
end
describe 'functionality' do
context 'when creates a cluster' do context 'when creates a cluster' do
it 'creates a new cluster' do it 'creates a new cluster' do
expect(ClusterProvisionWorker).to receive(:perform_async) expect(ClusterProvisionWorker).to receive(:perform_async)
...@@ -339,23 +316,19 @@ describe Projects::ClustersController do ...@@ -339,23 +316,19 @@ describe Projects::ClustersController do
it { expect { go }.to be_denied_for(:user) } it { expect { go }.to be_denied_for(:user) }
it { expect { go }.to be_denied_for(:external) } it { expect { go }.to be_denied_for(:external) }
end end
def go
post :create_user, params.merge(namespace_id: project.namespace, project_id: project)
end
end end
describe 'GET status' do describe 'GET status' do
let(:cluster) { create(:cluster, :providing_by_gcp, projects: [project]) } let(:cluster) { create(:cluster, :providing_by_gcp, projects: [project]) }
describe 'functionality' do def go
let(:user) { create(:user) } get :status, namespace_id: project.namespace,
project_id: project,
before do id: cluster,
project.add_maintainer(user) format: :json
sign_in(user) end
end
describe 'functionality' do
it "responds with matching schema" do it "responds with matching schema" do
go go
...@@ -380,26 +353,18 @@ describe Projects::ClustersController do ...@@ -380,26 +353,18 @@ describe Projects::ClustersController do
it { expect { go }.to be_denied_for(:user) } it { expect { go }.to be_denied_for(:user) }
it { expect { go }.to be_denied_for(:external) } it { expect { go }.to be_denied_for(:external) }
end end
def go
get :status, namespace_id: project.namespace,
project_id: project,
id: cluster,
format: :json
end
end end
describe 'GET show' do describe 'GET show' do
let(:cluster) { create(:cluster, :provided_by_gcp, projects: [project]) } let(:cluster) { create(:cluster, :provided_by_gcp, projects: [project]) }
describe 'functionality' do def go
let(:user) { create(:user) } get :show, namespace_id: project.namespace,
project_id: project,
before do id: cluster
project.add_maintainer(user) end
sign_in(user)
end
describe 'functionality' do
it "renders view" do it "renders view" do
go go
...@@ -418,73 +383,85 @@ describe Projects::ClustersController do ...@@ -418,73 +383,85 @@ describe Projects::ClustersController do
it { expect { go }.to be_denied_for(:user) } it { expect { go }.to be_denied_for(:user) }
it { expect { go }.to be_denied_for(:external) } it { expect { go }.to be_denied_for(:external) }
end end
def go
get :show, namespace_id: project.namespace,
project_id: project,
id: cluster
end
end end
describe 'PUT update' do describe 'PUT update' do
context 'when cluster is provided by GCP' do let(:cluster) { create(:cluster, :provided_by_gcp, projects: [project]) }
let(:cluster) { create(:cluster, :provided_by_gcp, projects: [project]) }
let(:user) { create(:user) }
before do
project.add_maintainer(user)
sign_in(user)
end
context 'when changing parameters' do let(:params) do
let(:params) do {
{ cluster: {
cluster: { enabled: false,
enabled: false, name: 'my-new-cluster-name',
name: 'my-new-cluster-name', platform_kubernetes_attributes: {
platform_kubernetes_attributes: { namespace: 'my-namespace'
namespace: 'my-namespace'
}
}
} }
end }
}
end
it "updates and redirects back to show page" do def go(format: :html)
go put :update, params.merge(namespace_id: project.namespace,
project_id: project,
id: cluster,
format: format
)
end
cluster.reload context 'when cluster is provided by GCP' do
expect(response).to redirect_to(project_cluster_path(project, cluster)) it "updates and redirects back to show page" do
expect(flash[:notice]).to eq('Kubernetes cluster was successfully updated.') go
expect(cluster.enabled).to be_falsey
end
it "does not change cluster name" do cluster.reload
go expect(response).to redirect_to(project_cluster_path(project, cluster))
expect(flash[:notice]).to eq('Kubernetes cluster was successfully updated.')
expect(cluster.enabled).to be_falsey
end
expect(cluster.name).to eq('test-cluster') it "does not change cluster name" do
end go
context 'when cluster is being created' do cluster.reload
let(:cluster) { create(:cluster, :providing_by_gcp, projects: [project]) } expect(cluster.name).to eq('test-cluster')
end
it "rejects changes" do context 'when cluster is being created' do
go let(:cluster) { create(:cluster, :providing_by_gcp, projects: [project]) }
expect(response).to have_gitlab_http_status(:ok) it "rejects changes" do
expect(response).to render_template(:show) go
expect(cluster.enabled).to be_truthy
end expect(response).to have_gitlab_http_status(:ok)
expect(response).to render_template(:show)
expect(cluster.enabled).to be_truthy
end end
end end
end end
context 'when cluster is provided by user' do context 'when cluster is provided by user' do
let(:cluster) { create(:cluster, :provided_by_user, projects: [project]) } let(:cluster) { create(:cluster, :provided_by_user, projects: [project]) }
let(:user) { create(:user) }
before do let(:params) do
project.add_maintainer(user) {
sign_in(user) cluster: {
enabled: false,
name: 'my-new-cluster-name',
platform_kubernetes_attributes: {
namespace: 'my-namespace'
}
}
}
end
it "updates and redirects back to show page" do
go
cluster.reload
expect(response).to redirect_to(project_cluster_path(project, cluster))
expect(flash[:notice]).to eq('Kubernetes cluster was successfully updated.')
expect(cluster.enabled).to be_falsey
expect(cluster.name).to eq('my-new-cluster-name')
expect(cluster.platform_kubernetes.namespace).to eq('my-namespace')
end end
context 'when format is json' do context 'when format is json' do
...@@ -503,7 +480,7 @@ describe Projects::ClustersController do ...@@ -503,7 +480,7 @@ describe Projects::ClustersController do
end end
it "updates and redirects back to show page" do it "updates and redirects back to show page" do
go_json go(format: :json)
cluster.reload cluster.reload
expect(response).to have_http_status(:no_content) expect(response).to have_http_status(:no_content)
...@@ -526,49 +503,18 @@ describe Projects::ClustersController do ...@@ -526,49 +503,18 @@ describe Projects::ClustersController do
end end
it "rejects changes" do it "rejects changes" do
go_json go(format: :json)
expect(response).to have_http_status(:bad_request) expect(response).to have_http_status(:bad_request)
end end
end end
end end
end end
context 'when format is html' do
context 'when update enabled' do
let(:params) do
{
cluster: {
enabled: false,
name: 'my-new-cluster-name',
platform_kubernetes_attributes: {
namespace: 'my-namespace'
}
}
}
end
it "updates and redirects back to show page" do
go
cluster.reload
expect(response).to redirect_to(project_cluster_path(project, cluster))
expect(flash[:notice]).to eq('Kubernetes cluster was successfully updated.')
expect(cluster.enabled).to be_falsey
expect(cluster.name).to eq('my-new-cluster-name')
expect(cluster.platform_kubernetes.namespace).to eq('my-namespace')
end
end
end
end end
describe 'security' do describe 'security' do
set(:cluster) { create(:cluster, :provided_by_gcp, projects: [project]) } set(:cluster) { create(:cluster, :provided_by_gcp, projects: [project]) }
let(:params) do
{ cluster: { enabled: false } }
end
it { expect { go }.to be_allowed_for(:admin) } it { expect { go }.to be_allowed_for(:admin) }
it { expect { go }.to be_allowed_for(:owner).of(project) } it { expect { go }.to be_allowed_for(:owner).of(project) }
it { expect { go }.to be_allowed_for(:maintainer).of(project) } it { expect { go }.to be_allowed_for(:maintainer).of(project) }
...@@ -578,34 +524,20 @@ describe Projects::ClustersController do ...@@ -578,34 +524,20 @@ describe Projects::ClustersController do
it { expect { go }.to be_denied_for(:user) } it { expect { go }.to be_denied_for(:user) }
it { expect { go }.to be_denied_for(:external) } it { expect { go }.to be_denied_for(:external) }
end end
def go
put :update, params.merge(namespace_id: project.namespace,
project_id: project,
id: cluster)
end
def go_json
put :update, params.merge(namespace_id: project.namespace,
project_id: project,
id: cluster,
format: :json)
end
end end
describe 'DELETE destroy' do describe 'DELETE destroy' do
describe 'functionality' do let!(:cluster) { create(:cluster, :provided_by_gcp, :production_environment, projects: [project]) }
let(:user) { create(:user) }
before do def go
project.add_maintainer(user) delete :destroy, namespace_id: project.namespace,
sign_in(user) project_id: project,
end id: cluster
end
describe 'functionality' do
context 'when cluster is provided by GCP' do context 'when cluster is provided by GCP' do
context 'when cluster is created' do context 'when cluster is created' do
let!(:cluster) { create(:cluster, :provided_by_gcp, :production_environment, projects: [project]) }
it "destroys and redirects back to clusters list" do it "destroys and redirects back to clusters list" do
expect { go } expect { go }
.to change { Clusters::Cluster.count }.by(-1) .to change { Clusters::Cluster.count }.by(-1)
...@@ -658,11 +590,5 @@ describe Projects::ClustersController do ...@@ -658,11 +590,5 @@ describe Projects::ClustersController do
it { expect { go }.to be_denied_for(:user) } it { expect { go }.to be_denied_for(:user) }
it { expect { go }.to be_denied_for(:external) } it { expect { go }.to be_denied_for(:external) }
end end
def go
delete :destroy, namespace_id: project.namespace,
project_id: project,
id: cluster
end
end end
end end
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