Commit b2b4f262 authored by Kamil Trzcinski's avatar Kamil Trzcinski

First round of tests

parent c63641ea
...@@ -50,14 +50,6 @@ class Projects::Clusters::GcpController < Projects::ApplicationController ...@@ -50,14 +50,6 @@ class Projects::Clusters::GcpController < Projects::ApplicationController
) )
end end
def update_params
params.require(:cluster).permit(
:enabled,
platform_kubernetes_attributes: [
:namespace
])
end
def authorize_google_api def authorize_google_api
unless GoogleApi::CloudPlatform::Client.new(token_in_session, nil) unless GoogleApi::CloudPlatform::Client.new(token_in_session, nil)
.validate_token(expires_at_in_session) .validate_token(expires_at_in_session)
......
...@@ -77,7 +77,7 @@ class Projects::ClustersController < Projects::ApplicationController ...@@ -77,7 +77,7 @@ class Projects::ClustersController < Projects::ApplicationController
:token, :token,
:ca_cert, :ca_cert,
:namespace :namespace
] ]
) )
end end
end end
......
...@@ -146,7 +146,7 @@ ...@@ -146,7 +146,7 @@
= number_with_delimiter(@project.open_merge_requests_count) = number_with_delimiter(@project.open_merge_requests_count)
- if project_nav_tab? :pipelines - if project_nav_tab? :pipelines
= nav_link(controller: [:pipelines, :builds, :jobs, :pipeline_schedules, :environments, :artifacts, :clusters]) do = nav_link(controller: [:pipelines, :builds, :jobs, :pipeline_schedules, :environments, :artifacts, :clusters, :user, :gcp]) do
= link_to project_pipelines_path(@project), class: 'shortcuts-pipelines' do = link_to project_pipelines_path(@project), class: 'shortcuts-pipelines' do
.nav-icon-container .nav-icon-container
= sprite_icon('pipeline') = sprite_icon('pipeline')
...@@ -154,7 +154,7 @@ ...@@ -154,7 +154,7 @@
CI / CD CI / CD
%ul.sidebar-sub-level-items %ul.sidebar-sub-level-items
= nav_link(controller: [:pipelines, :builds, :jobs, :pipeline_schedules, :environments, :artifacts, :clusters, :kubernetes, :gcp], html_options: { class: "fly-out-top-item" } ) do = nav_link(controller: [:pipelines, :builds, :jobs, :pipeline_schedules, :environments, :artifacts, :clusters, :user, :gcp], html_options: { class: "fly-out-top-item" } ) do
= link_to project_pipelines_path(@project) do = link_to project_pipelines_path(@project) do
%strong.fly-out-top-item-name %strong.fly-out-top-item-name
#{ _('CI / CD') } #{ _('CI / CD') }
...@@ -184,7 +184,7 @@ ...@@ -184,7 +184,7 @@
Environments Environments
- if project_nav_tab? :clusters - if project_nav_tab? :clusters
= nav_link(controller: [:clusters, :kubernetes, :gcp]) do = nav_link(controller: [:clusters, :user, :gcp]) do
= link_to project_clusters_path(@project), title: 'Cluster', class: 'shortcuts-cluster' do = link_to project_clusters_path(@project), title: 'Cluster', class: 'shortcuts-cluster' do
%span %span
Cluster Cluster
......
...@@ -4,6 +4,8 @@ describe Projects::ClustersController do ...@@ -4,6 +4,8 @@ describe Projects::ClustersController do
include AccessMatchersForController include AccessMatchersForController
include GoogleApi::CloudPlatformHelpers include GoogleApi::CloudPlatformHelpers
set(:project) { create(:project) }
describe 'GET index' do describe 'GET index' do
describe 'functionality' do describe 'functionality' do
let(:user) { create(:user) } let(:user) { create(:user) }
...@@ -14,22 +16,20 @@ describe Projects::ClustersController do ...@@ -14,22 +16,20 @@ describe Projects::ClustersController do
end end
context 'when project has a cluster' do context 'when project has a cluster' do
let(:cluster) { create(:cluster, :project, :provided_by_gcp) } let(:cluster) { create(:cluster, :provided_by_gcp, projects: [project]) }
let(:project) { cluster.project }
it { expect(go).to redirect_to(project_cluster_path(project, project.cluster)) } it { expect(go).to redirect_to(project_cluster_path(project, project.cluster)) }
end end
context 'when project does not have a cluster' do context 'when project does not have a cluster' do
let(:project) { create(:project) } let(:cluster) { create(:cluster, :provided_by_gcp) }
it { expect(go).to redirect_to(new_project_cluster_path(project)) } it { expect(go).to redirect_to(new_project_cluster_path(project)) }
end end
end end
describe 'security' do describe 'security' do
let(:cluster) { create(:cluster, :project, :provided_by_gcp) } let(:cluster) { create(:cluster, :provided_by_gcp, projects: [project]) }
let(:project) { cluster.project }
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) }
...@@ -46,198 +46,8 @@ describe Projects::ClustersController do ...@@ -46,198 +46,8 @@ describe Projects::ClustersController do
end end
end end
describe 'GET login' do
let(:project) { create(:project) }
describe 'functionality' do
let(:user) { create(:user) }
before do
project.add_master(user)
sign_in(user)
end
context 'when omniauth has been configured' do
let(:key) { 'secere-key' }
let(:session_key_for_redirect_uri) do
GoogleApi::CloudPlatform::Client.session_key_for_redirect_uri(key)
end
before do
allow(SecureRandom).to receive(:hex).and_return(key)
end
it 'has authorize_url' do
go
expect(assigns(:authorize_url)).to include(key)
expect(session[session_key_for_redirect_uri]).to eq(providers_gcp_new_project_clusters_url(project))
end
end
context 'when omniauth has not configured' do
before do
stub_omniauth_setting(providers: [])
end
it 'does not have authorize_url' do
go
expect(assigns(:authorize_url)).to be_nil
end
end
end
describe 'security' do
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(:master).of(project) }
it { expect { go }.to be_denied_for(:developer).of(project) }
it { expect { go }.to be_denied_for(:reporter).of(project) }
it { expect { go }.to be_denied_for(:guest).of(project) }
it { expect { go }.to be_denied_for(:user) }
it { expect { go }.to be_denied_for(:external) }
end
def go
get :login, namespace_id: project.namespace, project_id: project
end
end
shared_examples 'requires to login' do
it 'redirects to create a cluster' do
subject
expect(response).to redirect_to(login_project_clusters_path(project))
end
end
describe 'GET new_gcp' do
let(:project) { create(:project) }
describe 'functionality' do
let(:user) { create(:user) }
before do
project.add_master(user)
sign_in(user)
end
context 'when access token is valid' do
before do
stub_google_api_validate_token
end
it 'has new object' do
go
expect(assigns(:cluster)).to be_an_instance_of(Clusters::Cluster)
end
end
context 'when access token is expired' do
before do
stub_google_api_expired_token
end
it { expect(go).to redirect_to(login_project_clusters_path(project)) }
end
context 'when access token is not stored in session' do
it { expect(go).to redirect_to(login_project_clusters_path(project)) }
end
end
describe 'security' do
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(:master).of(project) }
it { expect { go }.to be_denied_for(:developer).of(project) }
it { expect { go }.to be_denied_for(:reporter).of(project) }
it { expect { go }.to be_denied_for(:guest).of(project) }
it { expect { go }.to be_denied_for(:user) }
it { expect { go }.to be_denied_for(:external) }
end
def go
get :new_gcp, namespace_id: project.namespace, project_id: project
end
end
describe 'POST create' do
let(:project) { create(:project) }
let(:params) do
{
cluster: {
name: 'new-cluster',
provider_type: :gcp,
provider_gcp_attributes: {
gcp_project_id: '111'
}
}
}
end
describe 'functionality' do
let(:user) { create(:user) }
before do
project.add_master(user)
sign_in(user)
end
context 'when access token is valid' do
before do
stub_google_api_validate_token
end
context 'when creates a cluster on gke' do
it 'creates a new cluster' do
expect(ClusterProvisionWorker).to receive(:perform_async)
expect { go }.to change { Clusters::Cluster.count }
expect(response).to redirect_to(project_cluster_path(project, project.cluster))
end
end
end
context 'when access token is expired' do
before do
stub_google_api_expired_token
end
it 'redirects to login page' do
expect(go).to redirect_to(login_project_clusters_path(project))
end
end
context 'when access token is not stored in session' do
it 'redirects to login page' do
expect(go).to redirect_to(login_project_clusters_path(project))
end
end
end
describe 'security' do
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(:master).of(project) }
it { expect { go }.to be_denied_for(:developer).of(project) }
it { expect { go }.to be_denied_for(:reporter).of(project) }
it { expect { go }.to be_denied_for(:guest).of(project) }
it { expect { go }.to be_denied_for(:user) }
it { expect { go }.to be_denied_for(:external) }
end
def go
post :create, params.merge(namespace_id: project.namespace, project_id: project)
end
end
describe 'GET status' do describe 'GET status' do
let(:cluster) { create(:cluster, :project, :providing_by_gcp) } let(:cluster) { create(:cluster, :providing_by_gcp, projects: [project]) }
let(:project) { cluster.project }
describe 'functionality' do describe 'functionality' do
let(:user) { create(:user) } let(:user) { create(:user) }
...@@ -275,8 +85,7 @@ describe Projects::ClustersController do ...@@ -275,8 +85,7 @@ describe Projects::ClustersController do
end end
describe 'GET show' do describe 'GET show' do
let(:cluster) { create(:cluster, :project, :provided_by_gcp) } let(:cluster) { create(:cluster, :provided_by_gcp, projects: [project]) }
let(:project) { cluster.project }
describe 'functionality' do describe 'functionality' do
let(:user) { create(:user) } let(:user) { create(:user) }
...@@ -313,21 +122,66 @@ describe Projects::ClustersController do ...@@ -313,21 +122,66 @@ describe Projects::ClustersController do
end end
describe 'PUT update' do describe 'PUT update' do
let(:cluster) { create(:cluster, :project, :provided_by_gcp) } context 'Managed' do
let(:project) { cluster.project } let(:cluster) { create(:cluster, :provided_by_gcp, projects: [project]) }
describe 'functionality' do describe 'functionality' do
let(:user) { create(:user) } let(:user) { create(:user) }
before do
project.add_master(user)
sign_in(user)
end
context 'when update enabled' do
let(:params) do
{
cluster: { enabled: false }
}
end
it "updates and redirects back to show page" do
go
cluster.reload
expect(response).to redirect_to(project_cluster_path(project, project.cluster))
expect(flash[:notice]).to eq('Cluster was successfully updated.')
expect(cluster.enabled).to be_falsey
end
context 'when cluster is being created' do
let(:cluster) { create(:cluster, :providing_by_gcp, projects: [project]) }
it "rejects changes" do
go
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
context 'User' do
let(:cluster) { create(:cluster, :provided_by_user, projects: [project]) }
let(:user) { create(:user) }
before do before do
project.add_master(user) project.add_master(user)
sign_in(user) sign_in(user)
end end
context 'when update enabled' do context 'when changing parameters' do
let(:params) do let(:params) do
{ {
cluster: { enabled: false } cluster: {
enabled: false, name: 'my-new-cluster-name',
platform_kubernetes_attributes: [
namespace: 'my-namespace'
]
}
} }
end end
...@@ -338,10 +192,12 @@ describe Projects::ClustersController do ...@@ -338,10 +192,12 @@ describe Projects::ClustersController do
expect(response).to redirect_to(project_cluster_path(project, project.cluster)) expect(response).to redirect_to(project_cluster_path(project, project.cluster))
expect(flash[:notice]).to eq('Cluster was successfully updated.') expect(flash[:notice]).to eq('Cluster was successfully updated.')
expect(cluster.enabled).to be_falsey 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 cluster is being created' do context 'when cluster is being created' do
let(:cluster) { create(:cluster, :project, :providing_by_gcp) } let(:cluster) { create(:cluster, :providing_by_gcp, projects: [project]) }
it "rejects changes" do it "rejects changes" do
go go
...@@ -379,8 +235,7 @@ describe Projects::ClustersController do ...@@ -379,8 +235,7 @@ describe Projects::ClustersController do
end end
describe 'delete update' do describe 'delete update' do
let(:cluster) { create(:cluster, :project, :provided_by_gcp) } let(:cluster) { create(:cluster, :provided_by_gcp, projects: [project]) }
let(:project) { cluster.project }
describe 'functionality' do describe 'functionality' do
let(:user) { create(:user) } let(:user) { create(:user) }
...@@ -401,7 +256,7 @@ describe Projects::ClustersController do ...@@ -401,7 +256,7 @@ describe Projects::ClustersController do
end end
context 'when cluster is being created' do context 'when cluster is being created' do
let(:cluster) { create(:cluster, :project, :providing_by_gcp) } let(:cluster) { create(:cluster, :providing_by_gcp, projects: [project]) }
it "destroys and redirects back to clusters list" do it "destroys and redirects back to clusters list" do
expect { go } expect { go }
...@@ -414,7 +269,7 @@ describe Projects::ClustersController do ...@@ -414,7 +269,7 @@ describe Projects::ClustersController do
end end
context 'when provider is user' do context 'when provider is user' do
let(:cluster) { create(:cluster, :project, :provided_by_user) } let(:cluster) { create(:cluster, :provided_by_user, projects: [project]) }
it "destroys and redirects back to clusters list" do it "destroys and redirects back to clusters list" do
expect { go } expect { go }
......
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