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

Refactor GCP redirect test suite

parent 0b294fc2
...@@ -137,15 +137,17 @@ describe Projects::Clusters::GcpController do ...@@ -137,15 +137,17 @@ describe Projects::Clusters::GcpController 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
allow_any_instance_of(described_class).to receive(:authorize_google_project_billing)
end end
context 'when google project billing is enabled' do context 'when google project billing is enabled' do
before do before do
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).with(CheckGcpProjectBillingWorker.redis_shared_state_key_for('token')).and_return('true')
end end
it 'creates a new cluster' do it 'creates a new cluster' do
expect(CheckGcpProjectBillingWorker).to receive(:store_session_token)
expect(ClusterProvisionWorker).to receive(:perform_async) expect(ClusterProvisionWorker).to receive(:perform_async)
expect { go }.to change { Clusters::Cluster.count } expect { go }.to change { Clusters::Cluster.count }
.and change { Clusters::Providers::Gcp.count } .and change { Clusters::Providers::Gcp.count }
...@@ -157,9 +159,6 @@ describe Projects::Clusters::GcpController do ...@@ -157,9 +159,6 @@ describe Projects::Clusters::GcpController do
context 'when google project billing is not enabled' do context 'when google project billing is not enabled' do
it 'renders the cluster form with an error' do it 'renders the cluster form with an error' do
expect(CheckGcpProjectBillingWorker).to receive(:perform_async)
expect(CheckGcpProjectBillingWorker).to receive(:store_session_token)
go go
expect(response).to set_flash[:alert] expect(response).to set_flash[:alert]
......
...@@ -13,6 +13,8 @@ feature 'Gcp Cluster', :js do ...@@ -13,6 +13,8 @@ feature 'Gcp Cluster', :js do
end end
context 'when user has signed with Google' do context 'when user has signed with Google' do
let(:project_id) { 'test-project-1234' }
before do before do
allow_any_instance_of(Projects::Clusters::GcpController) allow_any_instance_of(Projects::Clusters::GcpController)
.to receive(:token_in_session).and_return('token') .to receive(:token_in_session).and_return('token')
...@@ -23,7 +25,7 @@ feature 'Gcp Cluster', :js do ...@@ -23,7 +25,7 @@ feature 'Gcp Cluster', :js do
context 'when user has a GCP project with billing enabled' do context 'when user has a GCP project with billing enabled' do
before do before do
allow_any_instance_of(Projects::Clusters::GcpController).to receive(:authorize_google_project_billing) allow_any_instance_of(Projects::Clusters::GcpController).to receive(:authorize_google_project_billing)
stub_google_project_billing_status allow_any_instance_of(Projects::Clusters::GcpController).to receive(:google_project_billing_status).and_return('true')
end end
context 'when user does not have a cluster and visits cluster index page' do context 'when user does not have a cluster and visits cluster index page' do
...@@ -131,15 +133,41 @@ feature 'Gcp Cluster', :js do ...@@ -131,15 +133,41 @@ feature 'Gcp Cluster', :js do
context 'when user does not have a GCP project with billing enabled' do context 'when user does not have a GCP project with billing enabled' do
before do before do
allow_any_instance_of(Projects::Clusters::GcpController).to receive(:authorize_google_project_billing)
allow_any_instance_of(Projects::Clusters::GcpController).to receive(:google_project_billing_status).and_return('false')
visit project_clusters_path(project) visit project_clusters_path(project)
click_link 'Add cluster' click_link 'Add cluster'
click_link 'Create on GKE' click_link 'Create on GKE'
fill_in 'cluster_provider_gcp_attributes_gcp_project_id', with: 'gcp-project-123'
fill_in 'cluster_name', with: 'dev-cluster'
click_button 'Create cluster'
end
it 'user sees form with error' do
expect(page).to have_content('Please enable billing for one of your projects to be able to create a cluster. Please try again.')
end
end
context 'when gcp billing status is not in redis' do
before do
allow_any_instance_of(Projects::Clusters::GcpController).to receive(:authorize_google_project_billing)
allow_any_instance_of(Projects::Clusters::GcpController).to receive(:google_project_billing_status).and_return(nil)
visit project_clusters_path(project)
click_link 'Add cluster'
click_link 'Create on GKE'
fill_in 'cluster_provider_gcp_attributes_gcp_project_id', with: 'gcp-project-123'
fill_in 'cluster_name', with: 'dev-cluster'
click_button 'Create cluster'
end end
it 'user sees a check page' do it 'user sees form with error' do
pending 'the frontend still has not been implemented' expect(page).to have_content('We could not verify that one of your projects on GCP has billing enabled. Please try again.')
expect(page).to have_link('Continue')
end end
end end
end end
......
require 'spec_helper' require 'spec_helper'
describe CheckGcpProjectBillingService do describe CheckGcpProjectBillingService do
include GoogleApi::CloudPlatformHelpers
let(:service) { described_class.new } let(:service) { described_class.new }
let(:projects) { [double(name: 'first_project', project_id: 'first_project-1234'), double(name: 'second_project', project_id: 'second_project-1234')] } let(:project_id) { 'test-project-1234' }
describe '#execute' do describe '#execute' do
before do before do
expect_any_instance_of(GoogleApi::CloudPlatform::Client) stub_cloud_platform_projects_list(project_id: project_id)
.to receive(:projects_list).and_return(projects)
allow_any_instance_of(GoogleApi::CloudPlatform::Client)
.to receive_message_chain(:projects_get_billing_info, :billing_enabled)
.and_return(project_billing_enabled)
end end
subject { service.execute('bogustoken') } subject { service.execute('bogustoken') }
context 'google account has a billing enabled gcp project' do context 'google account has a billing enabled gcp project' do
let(:project_billing_enabled) { true } before do
stub_cloud_platform_projects_get_billing_info(project_id, true)
end
it { is_expected.to eq(projects) } it { is_expected.to all(satisfy { |project| project.project_id == project_id }) }
end end
context 'google account does not have a billing enabled gcp project' do context 'google account does not have a billing enabled gcp project' do
let(:project_billing_enabled) { false } before do
stub_cloud_platform_projects_get_billing_info(project_id, false)
end
it { is_expected.to eq([]) } it { is_expected.to eq([]) }
end end
......
...@@ -10,10 +10,14 @@ module GoogleApi ...@@ -10,10 +10,14 @@ 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 def stub_cloud_platform_projects_list(options)
redis_double = double WebMock.stub_request(:get, cloud_platform_projects_list_url)
allow(Gitlab::Redis::SharedState).to receive(:with).and_yield(redis_double) .to_return(cloud_platform_response(cloud_platform_projects_body(options)))
allow(redis_double).to receive(:get).with(CheckGcpProjectBillingWorker.redis_shared_state_key_for('token')).and_return('true') end
def stub_cloud_platform_projects_get_billing_info(project_id, billing_enabled)
WebMock.stub_request(:get, cloud_platform_projects_get_billing_info_url(project_id))
.to_return(cloud_platform_response(cloud_platform_projects_billing_info_body(project_id, billing_enabled)))
end 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)
...@@ -46,6 +50,14 @@ module GoogleApi ...@@ -46,6 +50,14 @@ module GoogleApi
.to_return(status: [500, "Internal Server Error"]) .to_return(status: [500, "Internal Server Error"])
end end
def cloud_platform_projects_list_url
"https://cloudresourcemanager.googleapis.com/v1/projects"
end
def cloud_platform_projects_get_billing_info_url(project_id)
"https://cloudbilling.googleapis.com/v1/projects/#{project_id}/billingInfo"
end
def cloud_platform_get_zone_cluster_url(project_id, zone, cluster_id) def cloud_platform_get_zone_cluster_url(project_id, zone, cluster_id)
"https://container.googleapis.com/v1/projects/#{project_id}/zones/#{zone}/clusters/#{cluster_id}" "https://container.googleapis.com/v1/projects/#{project_id}/zones/#{zone}/clusters/#{cluster_id}"
end end
...@@ -121,5 +133,32 @@ module GoogleApi ...@@ -121,5 +133,32 @@ module GoogleApi
"endTime": options[:endTime] || '' "endTime": options[:endTime] || ''
} }
end end
def cloud_platform_projects_body(**options)
{
"projects": [
{
"projectNumber": options[:project_number] || "1234",
"projectId": options[:project_id] || "test-project-1234",
"lifecycleState": "ACTIVE",
"name": options[:name] || "test-project",
"createTime": "2017-12-16T01:48:29.129Z",
"parent": {
"type": "organization",
"id": "12345"
}
}
]
}
end
def cloud_platform_projects_billing_info_body(project_id, billing_enabled)
{
"name": "projects/#{project_id}/billingInfo",
"projectId": "#{project_id}",
"billingAccountName": "account-name",
"billingEnabled": billing_enabled
}
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