Commit 91426093 authored by Matija Čupić's avatar Matija Čupić

Expand controller test suite matrix

parent 935a27cf
class Projects::Clusters::GcpController < Projects::ApplicationController class Projects::Clusters::GcpController < Projects::ApplicationController
before_action :authorize_read_cluster! before_action :authorize_read_cluster!
before_action :authorize_google_api, except: [:login] before_action :authorize_google_api, except: [:login]
before_action :authorize_google_project_billing, only: [:check] before_action :authorize_google_project_billing, except: [:login, :check]
before_action :authorize_create_cluster!, only: [:new, :create] before_action :authorize_create_cluster!, only: [:new, :create]
STATUS_POLLING_INTERVAL = 1.minute.to_i STATUS_POLLING_INTERVAL = 1.minute.to_i
def login def login
begin begin
state = generate_session_key_redirect(gcp_check_namespace_project_clusters_path.to_s) state = generate_session_key_redirect(gcp_new_namespace_project_clusters_path.to_s)
@authorize_url = GoogleApi::CloudPlatform::Client.new( @authorize_url = GoogleApi::CloudPlatform::Client.new(
nil, callback_google_api_auth_url, nil, callback_google_api_auth_url,
...@@ -76,6 +76,7 @@ class Projects::Clusters::GcpController < Projects::ApplicationController ...@@ -76,6 +76,7 @@ class Projects::Clusters::GcpController < Projects::ApplicationController
Gitlab::Redis::SharedState.with do |redis| Gitlab::Redis::SharedState.with do |redis|
unless redis.get(CheckGcpProjectBillingWorker.redis_shared_state_key_for(token_in_session)) == 'true' unless redis.get(CheckGcpProjectBillingWorker.redis_shared_state_key_for(token_in_session)) == 'true'
CheckGcpProjectBillingWorker.perform_async(token_in_session) CheckGcpProjectBillingWorker.perform_async(token_in_session)
redirect_to action: 'check'
end end
end end
end end
......
...@@ -8,6 +8,6 @@ ...@@ -8,6 +8,6 @@
%h4.prepend-top-0= s_('ClusterIntegration|Choose how to set up cluster integration') %h4.prepend-top-0= s_('ClusterIntegration|Choose how to set up cluster integration')
%p= s_('ClusterIntegration|Create a new cluster on Google Kubernetes Engine right from GitLab') %p= s_('ClusterIntegration|Create a new cluster on Google Kubernetes Engine right from GitLab')
= link_to s_('ClusterIntegration|Create on GKE'), gcp_check_namespace_project_clusters_path(@project.namespace, @project), class: 'btn append-bottom-20' = link_to s_('ClusterIntegration|Create on GKE'), gcp_new_namespace_project_clusters_path(@project.namespace, @project), class: 'btn append-bottom-20'
%p= s_('ClusterIntegration|Enter the details for an existing Kubernetes cluster') %p= s_('ClusterIntegration|Enter the details for an existing Kubernetes cluster')
= link_to s_('ClusterIntegration|Add an existing cluster'), user_new_namespace_project_clusters_path(@project.namespace, @project), class: 'btn append-bottom-20' = link_to s_('ClusterIntegration|Add an existing cluster'), user_new_namespace_project_clusters_path(@project.namespace, @project), class: 'btn append-bottom-20'
...@@ -17,7 +17,6 @@ describe Projects::Clusters::GcpController do ...@@ -17,7 +17,6 @@ describe Projects::Clusters::GcpController do
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
GoogleApi::CloudPlatform::Client.session_key_for_redirect_uri(key) GoogleApi::CloudPlatform::Client.session_key_for_redirect_uri(key)
end end
...@@ -30,7 +29,7 @@ describe Projects::Clusters::GcpController do ...@@ -30,7 +29,7 @@ describe Projects::Clusters::GcpController do
go go
expect(assigns(:authorize_url)).to include(key) expect(assigns(:authorize_url)).to include(key)
expect(session[session_key_for_redirect_uri]).to eq(gcp_check_project_clusters_path(project)) expect(session[session_key_for_redirect_uri]).to eq(gcp_new_project_clusters_path(project))
end end
end end
...@@ -72,47 +71,54 @@ describe Projects::Clusters::GcpController do ...@@ -72,47 +71,54 @@ describe Projects::Clusters::GcpController do
end end
describe 'functionality' do describe 'functionality' do
context 'when redis has wanted billing status' do context 'when access token is valid' do
let(:token) { 'bogustoken' }
before do before do
redis_double = double stub_google_api_validate_token
allow(Gitlab::Redis::SharedState).to receive(:with).and_yield(redis_double)
allow(redis_double).to receive(:get).and_return('true')
end end
it 'should render json with billing status' do context 'when redis has wanted billing status' do
go let(:token) { 'bogustoken' }
before do
redis_double = double
allow(Gitlab::Redis::SharedState).to receive(:with).and_yield(redis_double)
allow(redis_double).to receive(:get).and_return('true')
end
expect(response).to have_http_status(:ok) it 'should render json with billing status' do
expect(response.body).to include_json(billing: 'true') go
expect(response).to have_http_status(:ok)
expect(JSON.parse(response.body)).to include('billing' => 'true')
end
end end
it 'should not start worker' do context 'when redis does not have billing status' do
expect(CheckGcpProjectBillingWorker).not_to receive(:perform_async) before do
redis_double = double
allow(Gitlab::Redis::SharedState).to receive(:with).and_yield(redis_double)
allow(redis_double).to receive(:get).and_return(nil)
end
it 'should render json with null billing status' do
go
go expect(response).to have_http_status(:ok)
expect(JSON.parse(response.body)).to include('billing' => nil)
end
end end
end end
context 'when redis does not have billing status' do context 'when access token is expired' do
before do before do
redis_double = double stub_google_api_expired_token
allow(Gitlab::Redis::SharedState).to receive(:with).and_yield(redis_double)
allow(redis_double).to receive(:get).and_return(nil)
end
it 'should render json with null billing status' do
go
expect(response).to have_http_status(:ok)
expect(response.body).to include_json(billing: nil)
end end
it 'should start worker' do it { expect(go).to redirect_to(gcp_login_project_clusters_path(project)) }
expect(CheckGcpProjectBillingWorker).to receive(:perform_async) end
go context 'when access token is not stored in session' do
end it { expect(go).to redirect_to(gcp_login_project_clusters_path(project)) }
end end
end end
...@@ -146,10 +152,36 @@ describe Projects::Clusters::GcpController do ...@@ -146,10 +152,36 @@ describe Projects::Clusters::GcpController do
stub_google_api_validate_token stub_google_api_validate_token
end end
it 'has new object' do context 'when google project billing status is true' do
go before do
stub_google_project_billing_status
end
it 'has new object' do
go
expect(assigns(:cluster)).to be_an_instance_of(Clusters::Cluster)
end
end
context 'when google project billing status is not true' do
before do
redis_double = double
allow(Gitlab::Redis::SharedState).to receive(:with).and_yield(redis_double)
allow(redis_double).to receive(:get).and_return(nil)
end
it 'redirects to check page' do
allow(CheckGcpProjectBillingWorker).to receive(:perform_async)
expect(go).to redirect_to(gcp_check_project_clusters_path(project))
end
it 'calls gcp project billing check worker' do
expect(CheckGcpProjectBillingWorker).to receive(:perform_async)
expect(assigns(:cluster)).to be_an_instance_of(Clusters::Cluster) go
end
end end
end end
...@@ -207,14 +239,40 @@ describe Projects::Clusters::GcpController do ...@@ -207,14 +239,40 @@ describe Projects::Clusters::GcpController do
stub_google_api_validate_token stub_google_api_validate_token
end end
context 'when creates a cluster on gke' do context 'when google project billing status is true' do
it 'creates a new cluster' do before do
expect(ClusterProvisionWorker).to receive(:perform_async) stub_google_project_billing_status
expect { go }.to change { Clusters::Cluster.count } end
.and change { Clusters::Providers::Gcp.count }
expect(response).to redirect_to(project_cluster_path(project, project.clusters.first)) context 'when creates a cluster on gke' do
expect(project.clusters.first).to be_gcp it 'creates a new cluster' do
expect(project.clusters.first).to be_kubernetes expect(ClusterProvisionWorker).to receive(:perform_async)
expect { go }.to change { Clusters::Cluster.count }
.and change { Clusters::Providers::Gcp.count }
expect(response).to redirect_to(project_cluster_path(project, project.clusters.first))
expect(project.clusters.first).to be_gcp
expect(project.clusters.first).to be_kubernetes
end
end
end
context 'when google project billing status is not true' do
before do
redis_double = double
allow(Gitlab::Redis::SharedState).to receive(:with).and_yield(redis_double)
allow(redis_double).to receive(:get).and_return(nil)
end
it 'redirects to check page' do
allow(CheckGcpProjectBillingWorker).to receive(:perform_async)
expect(go).to redirect_to(gcp_check_project_clusters_path(project))
end
it 'calls gcp project billing check worker' do
expect(CheckGcpProjectBillingWorker).to receive(:perform_async)
go
end end
end end
end end
......
...@@ -10,6 +10,12 @@ module GoogleApi ...@@ -10,6 +10,12 @@ module GoogleApi
request.session[GoogleApi::CloudPlatform::Client.session_key_for_expires_at] = 1.hour.ago.to_i.to_s request.session[GoogleApi::CloudPlatform::Client.session_key_for_expires_at] = 1.hour.ago.to_i.to_s
end end
def stub_google_project_billing_status
redis_double = double
allow(Gitlab::Redis::SharedState).to receive(:with).and_yield(redis_double)
allow(redis_double).to receive(:get).and_return('true')
end
def stub_cloud_platform_get_zone_cluster(project_id, zone, cluster_id, **options) def stub_cloud_platform_get_zone_cluster(project_id, zone, cluster_id, **options)
WebMock.stub_request(:get, cloud_platform_get_zone_cluster_url(project_id, zone, cluster_id)) WebMock.stub_request(:get, cloud_platform_get_zone_cluster_url(project_id, zone, cluster_id))
.to_return(cloud_platform_response(cloud_platform_cluster_body(options))) .to_return(cloud_platform_response(cloud_platform_cluster_body(options)))
......
...@@ -7,7 +7,7 @@ describe CheckGcpProjectBillingWorker do ...@@ -7,7 +7,7 @@ describe CheckGcpProjectBillingWorker do
context 'when there is no lease' do context 'when there is no lease' do
before do before do
allow_any_instance_of(CheckGcpProjectBillingWorker).to receive(:try_obtain_lease_for).and_return('randomuuid') allow_any_instance_of(described_class).to receive(:try_obtain_lease_for).and_return('randomuuid')
end end
it 'calls the service' do it 'calls the service' do
...@@ -21,7 +21,7 @@ describe CheckGcpProjectBillingWorker do ...@@ -21,7 +21,7 @@ describe CheckGcpProjectBillingWorker do
expect(CheckGcpProjectBillingService).to receive_message_chain(:new, :execute).and_return(true) expect(CheckGcpProjectBillingService).to receive_message_chain(:new, :execute).and_return(true)
expect(Gitlab::Redis::SharedState).to receive(:with).and_yield(redis_double) expect(Gitlab::Redis::SharedState).to receive(:with).and_yield(redis_double)
expect(redis_double).to receive(:set).with(CheckGcpProjectBillingWorker.redis_shared_state_key_for(token), anything) expect(redis_double).to receive(:set).with(described_class.redis_shared_state_key_for(token), anything)
subject subject
end end
...@@ -29,7 +29,7 @@ describe CheckGcpProjectBillingWorker do ...@@ -29,7 +29,7 @@ describe CheckGcpProjectBillingWorker do
context 'when there is a lease' do context 'when there is a lease' do
before do before do
allow_any_instance_of(CheckGcpProjectBillingWorker).to receive(:try_obtain_lease_for).and_return(false) allow_any_instance_of(described_class).to receive(:try_obtain_lease_for).and_return(false)
end end
it 'does not call the service' do it 'does not call the service' 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