2022-03-02 08:16:31 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
RSpec.describe Projects::GoogleCloud::DeploymentsController, feature_category: :deployment_management do
|
2022-04-04 11:22:00 +05:30
|
|
|
let_it_be(:project) { create(:project, :public, :repository) }
|
|
|
|
let_it_be(:repository) { project.repository }
|
2022-03-02 08:16:31 +05:30
|
|
|
|
|
|
|
let_it_be(:user_guest) { create(:user) }
|
|
|
|
let_it_be(:user_developer) { create(:user) }
|
|
|
|
let_it_be(:user_maintainer) { create(:user) }
|
|
|
|
|
|
|
|
let_it_be(:unauthorized_members) { [user_guest, user_developer] }
|
2022-08-13 15:12:31 +05:30
|
|
|
let_it_be(:authorized_members) { [user_maintainer] }
|
2022-03-02 08:16:31 +05:30
|
|
|
|
|
|
|
let_it_be(:urls_list) { %W[#{project_google_cloud_deployments_cloud_run_path(project)} #{project_google_cloud_deployments_cloud_storage_path(project)}] }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_guest(user_guest)
|
|
|
|
project.add_developer(user_developer)
|
|
|
|
project.add_maintainer(user_maintainer)
|
|
|
|
end
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
describe "Routes must be restricted behind Google OAuth2", :snowplow do
|
2022-03-02 08:16:31 +05:30
|
|
|
context 'when a public request is made' do
|
|
|
|
it 'returns not found on GET request' do
|
|
|
|
urls_list.each do |url|
|
|
|
|
get url
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2022-05-07 20:08:51 +05:30
|
|
|
expect_snowplow_event(
|
2022-10-11 01:57:18 +05:30
|
|
|
category: 'Projects::GoogleCloud::DeploymentsController',
|
|
|
|
action: 'error_invalid_user',
|
|
|
|
label: nil,
|
2022-05-07 20:08:51 +05:30
|
|
|
project: project,
|
|
|
|
user: nil
|
|
|
|
)
|
2022-03-02 08:16:31 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when unauthorized members make requests' do
|
|
|
|
it 'returns not found on GET request' do
|
|
|
|
urls_list.each do |url|
|
|
|
|
unauthorized_members.each do |unauthorized_member|
|
|
|
|
get url
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2022-05-07 20:08:51 +05:30
|
|
|
expect_snowplow_event(
|
2022-10-11 01:57:18 +05:30
|
|
|
category: 'Projects::GoogleCloud::DeploymentsController',
|
|
|
|
action: 'error_invalid_user',
|
|
|
|
label: nil,
|
2022-05-07 20:08:51 +05:30
|
|
|
project: project,
|
|
|
|
user: nil
|
|
|
|
)
|
2022-03-02 08:16:31 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when authorized members make requests' do
|
|
|
|
it 'redirects on GET request' do
|
|
|
|
urls_list.each do |url|
|
|
|
|
authorized_members.each do |authorized_member|
|
|
|
|
sign_in(authorized_member)
|
|
|
|
|
|
|
|
get url
|
|
|
|
|
|
|
|
expect(response).to redirect_to(assigns(:authorize_url))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
describe 'Authorized GET project/-/google_cloud/deployments', :snowplow do
|
|
|
|
before do
|
|
|
|
sign_in(user_maintainer)
|
|
|
|
|
|
|
|
allow_next_instance_of(GoogleApi::CloudPlatform::Client) do |client|
|
|
|
|
allow(client).to receive(:validate_token).and_return(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders template' do
|
2023-01-13 00:05:48 +05:30
|
|
|
get project_google_cloud_deployments_path(project).to_s
|
2022-10-11 01:57:18 +05:30
|
|
|
|
|
|
|
expect(response).to render_template(:index)
|
|
|
|
|
|
|
|
expect_snowplow_event(
|
|
|
|
category: 'Projects::GoogleCloud::DeploymentsController',
|
|
|
|
action: 'render_page',
|
|
|
|
label: nil,
|
|
|
|
project: project,
|
|
|
|
user: user_maintainer
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
describe 'Authorized GET project/-/google_cloud/deployments/cloud_run', :snowplow do
|
2023-01-13 00:05:48 +05:30
|
|
|
let_it_be(:url) { project_google_cloud_deployments_cloud_run_path(project).to_s }
|
2022-03-02 08:16:31 +05:30
|
|
|
|
|
|
|
before do
|
2022-04-04 11:22:00 +05:30
|
|
|
sign_in(user_maintainer)
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
allow_next_instance_of(GoogleApi::CloudPlatform::Client) do |client|
|
|
|
|
allow(client).to receive(:validate_token).and_return(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
context 'when enable service fails' do
|
|
|
|
before do
|
|
|
|
allow_next_instance_of(GoogleCloud::EnableCloudRunService) do |service|
|
|
|
|
allow(service)
|
|
|
|
.to receive(:execute)
|
|
|
|
.and_return(
|
|
|
|
status: :error,
|
|
|
|
message: 'No GCP projects found. Configure a service account or GCP_PROJECT_ID ci variable'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2022-04-04 11:22:00 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
it 'redirects to google cloud deployments and tracks event on enable service error' do
|
|
|
|
get url
|
2022-04-04 11:22:00 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
expect(response).to redirect_to(project_google_cloud_deployments_path(project))
|
|
|
|
# since GPC_PROJECT_ID is not set, enable cloud run service should return an error
|
|
|
|
expect_snowplow_event(
|
|
|
|
category: 'Projects::GoogleCloud::DeploymentsController',
|
|
|
|
action: 'error_enable_services',
|
|
|
|
label: nil,
|
|
|
|
project: project,
|
|
|
|
user: user_maintainer
|
|
|
|
)
|
2022-04-04 11:22:00 +05:30
|
|
|
end
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
it 'shows a flash alert' do
|
|
|
|
get url
|
2022-04-04 11:22:00 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
expect(flash[:alert])
|
|
|
|
.to eq('No GCP projects found. Configure a service account or GCP_PROJECT_ID ci variable')
|
|
|
|
end
|
2022-04-04 11:22:00 +05:30
|
|
|
end
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
context 'when enable service raises an error' do
|
|
|
|
before do
|
|
|
|
mock_gcp_error = Google::Apis::ClientError.new('some_error')
|
2022-04-04 11:22:00 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
allow_next_instance_of(GoogleCloud::EnableCloudRunService) do |service|
|
|
|
|
allow(service).to receive(:execute).and_raise(mock_gcp_error)
|
2022-04-04 11:22:00 +05:30
|
|
|
end
|
2023-05-27 22:25:52 +05:30
|
|
|
end
|
2022-03-02 08:16:31 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
it 'redirects to google cloud deployments with error' do
|
2022-03-02 08:16:31 +05:30
|
|
|
get url
|
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
expect(response).to redirect_to(project_google_cloud_deployments_path(project))
|
2022-05-07 20:08:51 +05:30
|
|
|
expect_snowplow_event(
|
2022-10-11 01:57:18 +05:30
|
|
|
category: 'Projects::GoogleCloud::DeploymentsController',
|
2023-05-27 22:25:52 +05:30
|
|
|
action: 'error_google_api',
|
2022-10-11 01:57:18 +05:30
|
|
|
label: nil,
|
2022-05-07 20:08:51 +05:30
|
|
|
project: project,
|
|
|
|
user: user_maintainer
|
|
|
|
)
|
2022-04-04 11:22:00 +05:30
|
|
|
end
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
it 'shows a flash warning' do
|
|
|
|
get url
|
|
|
|
|
|
|
|
expect(flash[:warning]).to eq(format(_('Google Cloud Error - %{error}'), error: 'some_error'))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'GCP_PROJECT_IDs are defined' do
|
|
|
|
before do
|
|
|
|
allow_next_instance_of(GoogleCloud::EnableCloudRunService) do |enable_cloud_run_service|
|
|
|
|
allow(enable_cloud_run_service).to receive(:execute).and_return({ status: :success })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when generate pipeline service fails' do
|
|
|
|
before do
|
|
|
|
allow_next_instance_of(GoogleCloud::GeneratePipelineService) do |generate_pipeline_service|
|
|
|
|
allow(generate_pipeline_service).to receive(:execute).and_return({ status: :error })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'redirects to google_cloud deployments and tracks event on generate pipeline error' do
|
|
|
|
get url
|
|
|
|
|
|
|
|
expect(response).to redirect_to(project_google_cloud_deployments_path(project))
|
|
|
|
expect_snowplow_event(
|
|
|
|
category: 'Projects::GoogleCloud::DeploymentsController',
|
|
|
|
action: 'error_generate_cloudrun_pipeline',
|
|
|
|
label: nil,
|
|
|
|
project: project,
|
|
|
|
user: user_maintainer
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'shows a flash alert' do
|
|
|
|
get url
|
|
|
|
|
|
|
|
expect(flash[:alert]).to eq('Failed to generate pipeline')
|
2022-04-04 11:22:00 +05:30
|
|
|
end
|
2023-05-27 22:25:52 +05:30
|
|
|
end
|
2022-04-04 11:22:00 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
it 'redirects to create merge request form' do
|
2022-04-04 11:22:00 +05:30
|
|
|
allow_next_instance_of(GoogleCloud::GeneratePipelineService) do |service|
|
|
|
|
allow(service).to receive(:execute).and_return({ status: :success })
|
|
|
|
end
|
|
|
|
|
|
|
|
get url
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:found)
|
|
|
|
expect(response.location).to include(project_new_merge_request_path(project))
|
2022-05-07 20:08:51 +05:30
|
|
|
expect_snowplow_event(
|
2022-10-11 01:57:18 +05:30
|
|
|
category: 'Projects::GoogleCloud::DeploymentsController',
|
|
|
|
action: 'generate_cloudrun_pipeline',
|
|
|
|
label: nil,
|
2022-05-07 20:08:51 +05:30
|
|
|
project: project,
|
|
|
|
user: user_maintainer
|
|
|
|
)
|
2022-03-02 08:16:31 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
describe 'Authorized GET project/-/google_cloud/deployments/cloud_storage', :snowplow do
|
2023-01-13 00:05:48 +05:30
|
|
|
let_it_be(:url) { project_google_cloud_deployments_cloud_storage_path(project).to_s }
|
2022-03-02 08:16:31 +05:30
|
|
|
|
|
|
|
before do
|
|
|
|
allow_next_instance_of(GoogleApi::CloudPlatform::Client) do |client|
|
|
|
|
allow(client).to receive(:validate_token).and_return(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders placeholder' do
|
|
|
|
authorized_members.each do |authorized_member|
|
|
|
|
sign_in(authorized_member)
|
|
|
|
|
|
|
|
get url
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|