2022-03-02 08:16:31 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe GoogleCloud::CreateServiceAccountsService do
|
|
|
|
describe '#execute' do
|
|
|
|
before do
|
2022-04-04 11:22:00 +05:30
|
|
|
mock_google_oauth2_creds = Struct.new(:app_id, :app_secret)
|
|
|
|
.new('mock-app-id', 'mock-app-secret')
|
2022-03-02 08:16:31 +05:30
|
|
|
allow(Gitlab::Auth::OAuth::Provider).to receive(:config_for)
|
|
|
|
.with('google_oauth2')
|
2022-04-04 11:22:00 +05:30
|
|
|
.and_return(mock_google_oauth2_creds)
|
2022-03-02 08:16:31 +05:30
|
|
|
|
|
|
|
allow_next_instance_of(GoogleApi::CloudPlatform::Client) do |client|
|
2022-04-04 11:22:00 +05:30
|
|
|
mock_service_account = Struct.new(:project_id, :unique_id, :email)
|
|
|
|
.new('mock-project-id', 'mock-unique-id', 'mock-email')
|
2022-03-02 08:16:31 +05:30
|
|
|
allow(client).to receive(:create_service_account)
|
2022-04-04 11:22:00 +05:30
|
|
|
.and_return(mock_service_account)
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
allow(client).to receive(:create_service_account_key)
|
|
|
|
.and_return('mock-key')
|
2022-04-04 11:22:00 +05:30
|
|
|
|
|
|
|
allow(client)
|
|
|
|
.to receive(:grant_service_account_roles)
|
2022-03-02 08:16:31 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates unprotected vars', :aggregate_failures do
|
2022-05-07 20:08:51 +05:30
|
|
|
allow(ProtectedBranch).to receive(:protected?).and_return(false)
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
project = create(:project)
|
|
|
|
|
|
|
|
service = described_class.new(
|
|
|
|
project,
|
|
|
|
nil,
|
|
|
|
google_oauth2_token: 'mock-token',
|
|
|
|
gcp_project_id: 'mock-gcp-project-id',
|
|
|
|
environment_name: '*'
|
|
|
|
)
|
|
|
|
|
|
|
|
response = service.execute
|
|
|
|
|
|
|
|
expect(response.status).to eq(:success)
|
|
|
|
expect(response.message).to eq('Service account generated successfully')
|
|
|
|
expect(project.variables.count).to eq(3)
|
|
|
|
expect(project.variables.first.protected).to eq(false)
|
|
|
|
expect(project.variables.second.protected).to eq(false)
|
|
|
|
expect(project.variables.third.protected).to eq(false)
|
|
|
|
end
|
2022-05-07 20:08:51 +05:30
|
|
|
|
|
|
|
it 'creates protected vars', :aggregate_failures do
|
|
|
|
allow(ProtectedBranch).to receive(:protected?).and_return(true)
|
|
|
|
|
|
|
|
project = create(:project)
|
|
|
|
|
|
|
|
service = described_class.new(
|
|
|
|
project,
|
|
|
|
nil,
|
|
|
|
google_oauth2_token: 'mock-token',
|
|
|
|
gcp_project_id: 'mock-gcp-project-id',
|
|
|
|
environment_name: '*'
|
|
|
|
)
|
|
|
|
|
|
|
|
response = service.execute
|
|
|
|
|
|
|
|
expect(response.status).to eq(:success)
|
|
|
|
expect(response.message).to eq('Service account generated successfully')
|
|
|
|
expect(project.variables.count).to eq(3)
|
|
|
|
expect(project.variables.first.protected).to eq(true)
|
|
|
|
expect(project.variables.second.protected).to eq(true)
|
|
|
|
expect(project.variables.third.protected).to eq(true)
|
|
|
|
end
|
2022-03-02 08:16:31 +05:30
|
|
|
end
|
|
|
|
end
|