debian-mirror-gitlab/spec/requests/projects/google_cloud/databases_controller_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

201 lines
6.6 KiB
Ruby
Raw Permalink Normal View History

2022-08-13 15:12:31 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-06-20 00:43:36 +05:30
RSpec.describe Projects::GoogleCloud::DatabasesController, :snowplow, feature_category: :deployment_management do
2022-10-11 01:57:18 +05:30
shared_examples 'shared examples for database controller endpoints' do
include_examples 'requires `admin_project_google_cloud` role'
2022-08-13 15:12:31 +05:30
2022-10-11 01:57:18 +05:30
include_examples 'requires feature flag `incubation_5mp_google_cloud` enabled'
2022-08-13 15:12:31 +05:30
2022-10-11 01:57:18 +05:30
include_examples 'requires valid Google OAuth2 configuration'
2022-08-13 15:12:31 +05:30
2022-10-11 01:57:18 +05:30
include_examples 'requires valid Google Oauth2 token' do
let_it_be(:mock_gcp_projects) { [{}, {}, {}] }
let_it_be(:mock_branches) { [] }
let_it_be(:mock_tags) { [] }
end
end
context '-/google_cloud/databases' do
let_it_be(:project) { create(:project) }
let_it_be(:user) { create(:user) }
let_it_be(:renders_template) { 'projects/google_cloud/databases/index' }
let_it_be(:redirects_to) { nil }
2022-08-13 15:12:31 +05:30
2022-10-11 01:57:18 +05:30
subject { get project_google_cloud_databases_path(project) }
include_examples 'shared examples for database controller endpoints'
2022-08-13 15:12:31 +05:30
end
2022-10-11 01:57:18 +05:30
context '-/google_cloud/databases/new/postgres' do
let_it_be(:project) { create(:project) }
let_it_be(:user) { create(:user) }
let_it_be(:renders_template) { 'projects/google_cloud/databases/cloudsql_form' }
let_it_be(:redirects_to) { nil }
2022-08-13 15:12:31 +05:30
2022-10-11 01:57:18 +05:30
subject { get new_project_google_cloud_database_path(project, :postgres) }
2022-08-13 15:12:31 +05:30
2022-10-11 01:57:18 +05:30
include_examples 'shared examples for database controller endpoints'
2022-08-13 15:12:31 +05:30
end
2022-10-11 01:57:18 +05:30
context '-/google_cloud/databases/new/mysql' do
let_it_be(:project) { create(:project) }
let_it_be(:user) { create(:user) }
let_it_be(:renders_template) { 'projects/google_cloud/databases/cloudsql_form' }
let_it_be(:redirects_to) { nil }
2022-08-13 15:12:31 +05:30
2022-10-11 01:57:18 +05:30
subject { get new_project_google_cloud_database_path(project, :mysql) }
2022-08-13 15:12:31 +05:30
2022-10-11 01:57:18 +05:30
include_examples 'shared examples for database controller endpoints'
end
2022-08-13 15:12:31 +05:30
2022-10-11 01:57:18 +05:30
context '-/google_cloud/databases/new/sqlserver' do
let_it_be(:project) { create(:project) }
let_it_be(:user) { create(:user) }
let_it_be(:renders_template) { 'projects/google_cloud/databases/cloudsql_form' }
let_it_be(:redirects_to) { nil }
2022-08-13 15:12:31 +05:30
2022-10-11 01:57:18 +05:30
subject { get new_project_google_cloud_database_path(project, :sqlserver) }
2022-08-13 15:12:31 +05:30
2022-10-11 01:57:18 +05:30
include_examples 'shared examples for database controller endpoints'
end
2022-08-13 15:12:31 +05:30
2022-10-11 01:57:18 +05:30
context '-/google_cloud/databases/create' do
let_it_be(:project) { create(:project) }
let_it_be(:user) { create(:user) }
let_it_be(:renders_template) { nil }
let_it_be(:redirects_to) { project_google_cloud_databases_path(project) }
subject { post project_google_cloud_databases_path(project) }
include_examples 'shared examples for database controller endpoints'
context 'when the request is valid' do
before do
project.add_maintainer(user)
sign_in(user)
allow_next_instance_of(GoogleApi::CloudPlatform::Client) do |client|
allow(client).to receive(:validate_token).and_return(true)
allow(client).to receive(:list_projects).and_return(mock_gcp_projects)
end
allow_next_instance_of(BranchesFinder) do |finder|
allow(finder).to receive(:execute).and_return(mock_branches)
end
allow_next_instance_of(TagsFinder) do |finder|
allow(finder).to receive(:execute).and_return(mock_branches)
2022-08-13 15:12:31 +05:30
end
end
2022-10-11 01:57:18 +05:30
subject do
post project_google_cloud_databases_path(project)
2022-08-13 15:12:31 +05:30
end
2023-04-23 21:23:45 +05:30
context 'when EnableCloudsqlService fails' do
before do
allow_next_instance_of(::GoogleCloud::EnableCloudsqlService) do |service|
allow(service).to receive(:execute)
.and_return({ status: :error, message: 'error' })
end
2022-10-11 01:57:18 +05:30
end
2022-08-13 15:12:31 +05:30
2023-04-23 21:23:45 +05:30
it 'redirects and track event on error' do
subject
expect(response).to redirect_to(project_google_cloud_databases_path(project))
expect_snowplow_event(
category: 'Projects::GoogleCloud::DatabasesController',
action: 'error_enable_cloudsql_services',
label: nil,
project: project,
user: user
)
end
2022-08-13 15:12:31 +05:30
2023-04-23 21:23:45 +05:30
it 'shows a flash alert' do
subject
2022-10-11 01:57:18 +05:30
2023-04-23 21:23:45 +05:30
expect(flash[:alert]).to eq(s_('CloudSeed|Google Cloud Error - error'))
end
2022-08-13 15:12:31 +05:30
end
2022-10-11 01:57:18 +05:30
context 'when EnableCloudsqlService is successful' do
before do
allow_next_instance_of(::GoogleCloud::EnableCloudsqlService) do |service|
allow(service).to receive(:execute)
.and_return({ status: :success, message: 'success' })
end
2022-08-13 15:12:31 +05:30
end
2023-04-23 21:23:45 +05:30
context 'when CreateCloudsqlInstanceService fails' do
before do
allow_next_instance_of(::GoogleCloud::CreateCloudsqlInstanceService) do |service|
allow(service).to receive(:execute)
.and_return({ status: :error, message: 'error' })
end
2022-10-11 01:57:18 +05:30
end
2023-04-23 21:23:45 +05:30
it 'redirects and track event on error' do
subject
2022-08-13 15:12:31 +05:30
2023-04-23 21:23:45 +05:30
expect(response).to redirect_to(project_google_cloud_databases_path(project))
2022-08-13 15:12:31 +05:30
2023-04-23 21:23:45 +05:30
expect_snowplow_event(
category: 'Projects::GoogleCloud::DatabasesController',
action: 'error_create_cloudsql_instance',
label: nil,
project: project,
user: user
)
end
it 'shows a flash warning' do
subject
expect(flash[:warning]).to eq(s_('CloudSeed|Google Cloud Error - error'))
end
2022-08-13 15:12:31 +05:30
end
2022-10-11 01:57:18 +05:30
context 'when CreateCloudsqlInstanceService is successful' do
before do
allow_next_instance_of(::GoogleCloud::CreateCloudsqlInstanceService) do |service|
allow(service).to receive(:execute)
.and_return({ status: :success, message: 'success' })
end
end
it 'redirects as expected' do
subject
expect(response).to redirect_to(project_google_cloud_databases_path(project))
expect_snowplow_event(
category: 'Projects::GoogleCloud::DatabasesController',
action: 'create_cloudsql_instance',
label: "{}",
project: project,
user: user
)
end
2023-04-23 21:23:45 +05:30
it 'shows a flash notice' do
subject
expect(flash[:notice])
.to eq(
s_(
'CloudSeed|Cloud SQL instance creation request successful. ' \
'Expected resolution time is ~5 minutes.'
)
)
end
2022-10-11 01:57:18 +05:30
end
2022-08-13 15:12:31 +05:30
end
end
end
end