debian-mirror-gitlab/spec/services/projects/create_service_spec.rb

952 lines
30 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Projects::CreateService, '#execute' do
2019-07-07 11:18:12 +05:30
include ExternalAuthorizationServiceHelpers
2018-12-05 23:21:45 +05:30
include GitHelpers
2017-08-17 22:00:37 +05:30
let(:user) { create :user }
let(:opts) do
{
2017-09-10 17:25:29 +05:30
name: 'GitLab',
namespace_id: user.namespace.id
2017-08-17 22:00:37 +05:30
}
end
2021-01-03 14:25:43 +05:30
context 'with labels' do
subject(:project) { create_project(user, opts) }
before_all do
Label.create!(title: 'bug', template: true)
end
it 'creates labels on project creation' do
created_label = project.labels.last
2017-08-17 22:00:37 +05:30
2021-01-03 14:25:43 +05:30
expect(created_label.type).to eq('ProjectLabel')
expect(created_label.project_id).to eq(project.id)
expect(created_label.title).to eq('bug')
end
context 'using gitlab project import' do
before do
opts[:import_type] = 'gitlab_project'
end
2019-03-02 22:35:43 +05:30
2021-01-03 14:25:43 +05:30
it 'does not creates labels on project creation' do
expect(project.labels.size).to eq(0)
end
end
2017-08-17 22:00:37 +05:30
end
2014-09-02 18:07:02 +05:30
2021-03-11 19:13:27 +05:30
describe 'setting name and path' do
subject(:project) { create_project(user, opts) }
context 'when both are set' do
let(:opts) { { name: 'one', path: 'two' } }
it 'keeps them as specified' do
expect(project.name).to eq('one')
expect(project.path).to eq('two')
end
end
context 'when path is set' do
let(:opts) { { path: 'one.two_three-four' } }
it 'sets name == path' do
expect(project.path).to eq('one.two_three-four')
expect(project.name).to eq(project.path)
end
end
context 'when name is a valid path' do
let(:opts) { { name: 'one.two_three-four' } }
it 'sets path == name' do
expect(project.name).to eq('one.two_three-four')
expect(project.path).to eq(project.name)
end
end
context 'when name is not a valid path' do
let(:opts) { { name: 'one.two_three-four and five' } }
# TODO: Retained for backwards compatibility. Remove in API v5.
# See https://gitlab.com/gitlab-org/gitlab/-/merge_requests/52725
it 'parameterizes the name' do
expect(project.name).to eq('one.two_three-four and five')
expect(project.path).to eq('one-two_three-four-and-five')
end
end
end
2017-08-17 22:00:37 +05:30
context 'user namespace' do
it do
project = create_project(user, opts)
2015-09-11 14:41:01 +05:30
2017-08-17 22:00:37 +05:30
expect(project).to be_valid
expect(project.owner).to eq(user)
2018-11-18 11:00:15 +05:30
expect(project.team.maintainers).to include(user)
2017-08-17 22:00:37 +05:30
expect(project.namespace).to eq(user.namespace)
2015-09-11 14:41:01 +05:30
end
2017-08-17 22:00:37 +05:30
end
2015-09-11 14:41:01 +05:30
2018-05-09 12:01:36 +05:30
describe 'after create actions' do
it 'invalidate personal_projects_count caches' do
expect(user).to receive(:invalidate_personal_projects_count)
create_project(user, opts)
end
2020-03-13 15:44:24 +05:30
2020-06-23 00:09:42 +05:30
it 'builds associated project settings' do
2020-03-13 15:44:24 +05:30
project = create_project(user, opts)
2020-06-23 00:09:42 +05:30
expect(project.project_setting).to be_new_record
2020-03-13 15:44:24 +05:30
end
2020-10-24 23:57:45 +05:30
it_behaves_like 'storing arguments in the application context' do
let(:expected_params) { { project: subject.full_path, related_class: described_class.to_s } }
subject { create_project(user, opts) }
end
2018-05-09 12:01:36 +05:30
end
2017-08-17 22:00:37 +05:30
context "admin creates project with other user's namespace_id" do
2021-01-29 00:20:46 +05:30
context 'when admin mode is enabled', :enable_admin_mode do
it 'sets the correct permissions' do
admin = create(:admin)
project = create_project(admin, opts)
2015-09-25 12:07:36 +05:30
2021-01-29 00:20:46 +05:30
expect(project).to be_persisted
expect(project.owner).to eq(user)
expect(project.team.maintainers).to contain_exactly(user)
expect(project.namespace).to eq(user.namespace)
end
end
context 'when admin mode is disabled' do
it 'is not allowed' do
admin = create(:admin)
project = create_project(admin, opts)
expect(project).not_to be_persisted
end
2015-09-25 12:07:36 +05:30
end
2017-08-17 22:00:37 +05:30
end
2015-09-25 12:07:36 +05:30
2017-08-17 22:00:37 +05:30
context 'group namespace' do
let(:group) do
create(:group).tap do |group|
group.add_owner(user)
2014-09-02 18:07:02 +05:30
end
2017-08-17 22:00:37 +05:30
end
2014-09-02 18:07:02 +05:30
2017-08-17 22:00:37 +05:30
before do
user.refresh_authorized_projects # Ensure cache is warm
2014-09-02 18:07:02 +05:30
end
2017-08-17 22:00:37 +05:30
it do
project = create_project(user, opts.merge!(namespace_id: group.id))
2014-09-02 18:07:02 +05:30
2017-08-17 22:00:37 +05:30
expect(project).to be_valid
expect(project.owner).to eq(group)
expect(project.namespace).to eq(group)
2019-12-04 20:38:33 +05:30
expect(project.team.owners).to include(user)
2017-08-17 22:00:37 +05:30
expect(user.authorized_projects).to include(project)
end
end
2014-09-02 18:07:02 +05:30
2020-06-23 00:09:42 +05:30
context 'group sharing', :sidekiq_inline do
let_it_be(:group) { create(:group) }
let_it_be(:shared_group) { create(:group) }
let_it_be(:shared_group_user) { create(:user) }
let(:opts) do
{
name: 'GitLab',
namespace_id: shared_group.id
}
end
before do
create(:group_group_link, shared_group: shared_group, shared_with_group: group)
shared_group.add_maintainer(shared_group_user)
group.add_developer(user)
end
it 'updates authorization' do
shared_group_project = create_project(shared_group_user, opts)
expect(
Ability.allowed?(shared_group_user, :read_project, shared_group_project)
).to be_truthy
expect(
Ability.allowed?(user, :read_project, shared_group_project)
).to be_truthy
end
end
context 'membership overrides', :sidekiq_inline do
let_it_be(:group) { create(:group, :private) }
let_it_be(:subgroup_for_projects) { create(:group, :private, parent: group) }
let_it_be(:subgroup_for_access) { create(:group, :private, parent: group) }
let_it_be(:group_maintainer) { create(:user) }
let(:group_access_level) { Gitlab::Access::REPORTER }
let(:subgroup_access_level) { Gitlab::Access::DEVELOPER }
let(:share_max_access_level) { Gitlab::Access::MAINTAINER }
let(:opts) do
{
name: 'GitLab',
namespace_id: subgroup_for_projects.id
}
end
before do
group.add_maintainer(group_maintainer)
create(:group_group_link, shared_group: subgroup_for_projects,
shared_with_group: subgroup_for_access,
group_access: share_max_access_level)
end
context 'membership is higher from group hierarchy' do
let(:group_access_level) { Gitlab::Access::MAINTAINER }
it 'updates authorization' do
create(:group_member, access_level: subgroup_access_level, group: subgroup_for_access, user: user)
create(:group_member, access_level: group_access_level, group: group, user: user)
subgroup_project = create_project(group_maintainer, opts)
project_authorization = ProjectAuthorization.where(
project_id: subgroup_project.id,
user_id: user.id,
access_level: group_access_level)
expect(project_authorization).to exist
end
end
context 'membership is higher from group share' do
let(:subgroup_access_level) { Gitlab::Access::MAINTAINER }
context 'share max access level is not limiting' do
it 'updates authorization' do
create(:group_member, access_level: group_access_level, group: group, user: user)
create(:group_member, access_level: subgroup_access_level, group: subgroup_for_access, user: user)
subgroup_project = create_project(group_maintainer, opts)
project_authorization = ProjectAuthorization.where(
project_id: subgroup_project.id,
user_id: user.id,
access_level: subgroup_access_level)
expect(project_authorization).to exist
end
end
context 'share max access level is limiting' do
let(:share_max_access_level) { Gitlab::Access::DEVELOPER }
it 'updates authorization' do
create(:group_member, access_level: group_access_level, group: group, user: user)
create(:group_member, access_level: subgroup_access_level, group: subgroup_for_access, user: user)
subgroup_project = create_project(group_maintainer, opts)
project_authorization = ProjectAuthorization.where(
project_id: subgroup_project.id,
user_id: user.id,
access_level: share_max_access_level)
expect(project_authorization).to exist
end
end
end
end
2017-08-17 22:00:37 +05:30
context 'error handling' do
it 'handles invalid options' do
opts[:default_branch] = 'master'
expect(create_project(user, opts)).to eq(nil)
2014-09-02 18:07:02 +05:30
end
2017-08-17 22:00:37 +05:30
end
2014-09-02 18:07:02 +05:30
2017-08-17 22:00:37 +05:30
context 'wiki_enabled creates repository directory' do
context 'wiki_enabled true creates wiki repository directory' do
it do
project = create_project(user, opts)
2018-03-17 18:26:18 +05:30
expect(wiki_repo(project).exists?).to be_truthy
2015-12-23 02:04:40 +05:30
end
end
2017-08-17 22:00:37 +05:30
context 'wiki_enabled false does not create wiki repository directory' do
it do
opts[:wiki_enabled] = false
project = create_project(user, opts)
2014-09-02 18:07:02 +05:30
2018-03-17 18:26:18 +05:30
expect(wiki_repo(project).exists?).to be_falsey
2014-09-02 18:07:02 +05:30
end
2017-08-17 22:00:37 +05:30
end
2018-03-17 18:26:18 +05:30
def wiki_repo(project)
relative_path = ProjectWiki.new(project).disk_path + '.git'
2019-03-02 22:35:43 +05:30
Gitlab::Git::Repository.new(project.repository_storage, relative_path, 'foobar', project.full_path)
2018-03-17 18:26:18 +05:30
end
2017-08-17 22:00:37 +05:30
end
2014-09-02 18:07:02 +05:30
2018-11-18 11:00:15 +05:30
context 'import data' do
2020-07-28 23:09:34 +05:30
let(:import_data) { { data: { 'test' => 'some data' } } }
let(:imported_project) { create_project(user, { name: 'test', import_url: 'http://import-url', import_data: import_data }) }
it 'does not write repository config' do
expect_next_instance_of(Project) do |project|
expect(project).not_to receive(:write_repository_config)
end
2018-11-18 11:00:15 +05:30
2020-07-28 23:09:34 +05:30
imported_project
end
it 'stores import data and URL' do
expect(imported_project.import_data).to be_persisted
expect(imported_project.import_data.data).to eq(import_data[:data])
expect(imported_project.import_url).to eq('http://import-url')
2018-11-18 11:00:15 +05:30
end
end
2017-08-17 22:00:37 +05:30
context 'builds_enabled global setting' do
let(:project) { create_project(user, opts) }
2014-09-02 18:07:02 +05:30
2017-08-17 22:00:37 +05:30
subject { project.builds_enabled? }
context 'global builds_enabled false does not enable CI by default' do
before do
project.project_feature.update_attribute(:builds_access_level, ProjectFeature::DISABLED)
2015-04-26 12:48:37 +05:30
end
2017-08-17 22:00:37 +05:30
it { is_expected.to be_falsey }
2015-04-26 12:48:37 +05:30
end
2017-08-17 22:00:37 +05:30
context 'global builds_enabled true does enable CI by default' do
it { is_expected.to be_truthy }
end
end
2015-11-26 14:37:03 +05:30
2019-09-04 21:01:54 +05:30
context 'default visibility level' do
let(:group) { create(:group, :private) }
2021-04-17 20:07:23 +05:30
using RSpec::Parameterized::TableSyntax
2015-11-26 14:37:03 +05:30
2021-04-17 20:07:23 +05:30
where(:case_name, :group_level, :project_level) do
[
['in public group', Gitlab::VisibilityLevel::PUBLIC, Gitlab::VisibilityLevel::INTERNAL],
['in internal group', Gitlab::VisibilityLevel::INTERNAL, Gitlab::VisibilityLevel::INTERNAL],
['in private group', Gitlab::VisibilityLevel::PRIVATE, Gitlab::VisibilityLevel::PRIVATE]
]
2017-08-17 22:00:37 +05:30
end
2015-11-26 14:37:03 +05:30
2021-04-17 20:07:23 +05:30
with_them do
before do
stub_application_setting(default_project_visibility: Gitlab::VisibilityLevel::INTERNAL)
group.add_developer(user)
group.update!(visibility_level: group_level)
opts.merge!(
name: 'test',
namespace: group,
path: 'foo'
)
end
2015-11-26 14:37:03 +05:30
2021-04-17 20:07:23 +05:30
it 'creates project with correct visibility level', :aggregate_failures do
project = create_project(user, opts)
2015-11-26 14:37:03 +05:30
2021-04-17 20:07:23 +05:30
expect(project).to respond_to(:errors)
expect(project.errors).to be_blank
expect(project.visibility_level).to eq(project_level)
expect(project).to be_saved
expect(project).to be_valid
end
2019-09-04 21:01:54 +05:30
end
end
context 'restricted visibility level' do
before do
stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::PUBLIC])
end
shared_examples 'restricted visibility' do
it 'does not allow a restricted visibility level for non-admins' do
project = create_project(user, opts)
expect(project).to respond_to(:errors)
expect(project.errors.messages).to have_key(:visibility_level)
expect(project.errors.messages[:visibility_level].first).to(
match('restricted by your GitLab administrator')
)
end
2021-01-29 00:20:46 +05:30
it 'does not allow a restricted visibility level for admins when admin mode is disabled' do
admin = create(:admin)
project = create_project(admin, opts)
expect(project.errors.any?).to be(true)
expect(project.saved?).to be_falsey
end
it 'allows a restricted visibility level for admins when admin mode is enabled', :enable_admin_mode do
2019-09-04 21:01:54 +05:30
admin = create(:admin)
project = create_project(admin, opts)
expect(project.errors.any?).to be(false)
expect(project.saved?).to be(true)
end
end
context 'when visibility is project based' do
before do
opts.merge!(
visibility_level: Gitlab::VisibilityLevel::PUBLIC
)
end
include_examples 'restricted visibility'
end
context 'when visibility is overridden' do
let(:visibility) { 'public' }
before do
opts.merge!(
import_data: {
data: {
override_params: {
visibility: visibility
}
}
}
)
end
include_examples 'restricted visibility'
context 'when visibility is misspelled' do
let(:visibility) { 'publik' }
it 'does not restrict project creation' do
project = create_project(user, opts)
expect(project.errors.any?).to be(false)
expect(project.saved?).to be(true)
end
end
2015-11-26 14:37:03 +05:30
end
2017-08-17 22:00:37 +05:30
end
2015-11-26 14:37:03 +05:30
2017-08-17 22:00:37 +05:30
context 'repository creation' do
it 'synchronously creates the repository' do
2020-03-13 15:44:24 +05:30
expect_next_instance_of(Project) do |instance|
expect(instance).to receive(:create_repository)
end
2015-04-26 12:48:37 +05:30
2017-08-17 22:00:37 +05:30
project = create_project(user, opts)
expect(project).to be_valid
expect(project.owner).to eq(user)
expect(project.namespace).to eq(user.namespace)
end
2017-09-10 17:25:29 +05:30
context 'when another repository already exists on disk' do
let(:opts) do
{
2021-03-11 19:13:27 +05:30
name: 'existing',
2017-09-10 17:25:29 +05:30
namespace_id: user.namespace.id
}
end
2018-03-17 18:26:18 +05:30
context 'with legacy storage' do
2020-04-08 14:13:33 +05:30
let(:fake_repo_path) { File.join(TestEnv.repos_path, user.namespace.full_path, 'existing.git') }
2018-03-17 18:26:18 +05:30
before do
2019-09-04 21:01:54 +05:30
stub_application_setting(hashed_storage_enabled: false)
2020-04-08 14:13:33 +05:30
TestEnv.create_bare_repository(fake_repo_path)
2018-03-17 18:26:18 +05:30
end
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
after do
2020-04-08 14:13:33 +05:30
FileUtils.rm_rf(fake_repo_path)
2018-03-17 18:26:18 +05:30
end
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
it 'does not allow to create a project when path matches existing repository on disk' do
project = create_project(user, opts)
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
expect(project).not_to be_persisted
expect(project).to respond_to(:errors)
expect(project.errors.messages).to have_key(:base)
expect(project.errors.messages[:base].first).to match('There is already a repository with that name on disk')
end
it 'does not allow to import project when path matches existing repository on disk' do
project = create_project(user, opts.merge({ import_url: 'https://gitlab.com/gitlab-org/gitlab-test.git' }))
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
expect(project).not_to be_persisted
expect(project).to respond_to(:errors)
expect(project.errors.messages).to have_key(:base)
expect(project.errors.messages[:base].first).to match('There is already a repository with that name on disk')
end
2017-09-10 17:25:29 +05:30
end
2018-03-17 18:26:18 +05:30
context 'with hashed storage' do
let(:hash) { '6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b' }
let(:hashed_path) { '@hashed/6b/86/6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b' }
2020-04-08 14:13:33 +05:30
let(:fake_repo_path) { File.join(TestEnv.repos_path, "#{hashed_path}.git") }
2018-03-17 18:26:18 +05:30
before do
allow(Digest::SHA2).to receive(:hexdigest) { hash }
2020-04-08 14:13:33 +05:30
TestEnv.create_bare_repository(fake_repo_path)
2018-03-17 18:26:18 +05:30
end
after do
2020-04-08 14:13:33 +05:30
FileUtils.rm_rf(fake_repo_path)
2018-03-17 18:26:18 +05:30
end
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
it 'does not allow to create a project when path matches existing repository on disk' do
project = create_project(user, opts)
expect(project).not_to be_persisted
expect(project).to respond_to(:errors)
expect(project.errors.messages).to have_key(:base)
expect(project.errors.messages[:base].first).to match('There is already a repository with that name on disk')
end
2017-09-10 17:25:29 +05:30
end
end
2017-08-17 22:00:37 +05:30
end
2015-04-26 12:48:37 +05:30
2018-11-08 19:23:39 +05:30
context 'when readme initialization is requested' do
2020-07-28 23:09:34 +05:30
let(:project) { create_project(user, opts) }
before do
2018-11-08 19:23:39 +05:30
opts[:initialize_with_readme] = '1'
2020-07-28 23:09:34 +05:30
end
2018-11-08 19:23:39 +05:30
2020-07-28 23:09:34 +05:30
shared_examples 'creates README.md' do
it { expect(project.repository.commit_count).to be(1) }
it { expect(project.repository.readme.name).to eql('README.md') }
it { expect(project.repository.readme.data).to include('# GitLab') }
end
2018-11-08 19:23:39 +05:30
2020-07-28 23:09:34 +05:30
it_behaves_like 'creates README.md'
context 'and a default_branch_name is specified' do
before do
allow(Gitlab::CurrentSettings)
.to receive(:default_branch_name)
.and_return('example_branch')
end
it_behaves_like 'creates README.md'
it 'creates README.md within the specified branch rather than master' do
branches = project.repository.branches
expect(branches.size).to eq(1)
expect(branches.collect(&:name)).to contain_exactly('example_branch')
end
2018-11-08 19:23:39 +05:30
end
end
2020-06-23 00:09:42 +05:30
describe 'create service for the project' do
subject(:project) { create_project(user, opts) }
2015-04-26 12:48:37 +05:30
2021-01-03 14:25:43 +05:30
context 'with an active service template' do
2020-06-23 00:09:42 +05:30
let!(:template_integration) { create(:prometheus_service, :template, api_url: 'https://prometheus.template.com/') }
2017-09-10 17:25:29 +05:30
2020-06-23 00:09:42 +05:30
it 'creates a service from the template' do
2021-06-08 01:23:25 +05:30
expect(project.integrations.count).to eq(1)
expect(project.integrations.first.api_url).to eq(template_integration.api_url)
expect(project.integrations.first.inherit_from_id).to be_nil
2020-06-23 00:09:42 +05:30
end
2021-01-03 14:25:43 +05:30
context 'with an active instance-level integration' do
let!(:instance_integration) { create(:prometheus_service, :instance, api_url: 'https://prometheus.instance.com/') }
it 'creates a service from the instance-level integration' do
2021-06-08 01:23:25 +05:30
expect(project.integrations.count).to eq(1)
expect(project.integrations.first.api_url).to eq(instance_integration.api_url)
expect(project.integrations.first.inherit_from_id).to eq(instance_integration.id)
2021-01-03 14:25:43 +05:30
end
context 'with an active group-level integration' do
let!(:group_integration) { create(:prometheus_service, group: group, project: nil, api_url: 'https://prometheus.group.com/') }
let!(:group) do
create(:group).tap do |group|
group.add_owner(user)
end
end
let(:opts) do
{
name: 'GitLab',
namespace_id: group.id
}
end
it 'creates a service from the group-level integration' do
2021-06-08 01:23:25 +05:30
expect(project.integrations.count).to eq(1)
expect(project.integrations.first.api_url).to eq(group_integration.api_url)
expect(project.integrations.first.inherit_from_id).to eq(group_integration.id)
2021-01-03 14:25:43 +05:30
end
context 'with an active subgroup' do
let!(:subgroup_integration) { create(:prometheus_service, group: subgroup, project: nil, api_url: 'https://prometheus.subgroup.com/') }
let!(:subgroup) do
create(:group, parent: group).tap do |subgroup|
subgroup.add_owner(user)
end
end
let(:opts) do
{
name: 'GitLab',
namespace_id: subgroup.id
}
end
it 'creates a service from the subgroup-level integration' do
2021-06-08 01:23:25 +05:30
expect(project.integrations.count).to eq(1)
expect(project.integrations.first.api_url).to eq(subgroup_integration.api_url)
expect(project.integrations.first.inherit_from_id).to eq(subgroup_integration.id)
2021-01-03 14:25:43 +05:30
end
end
end
end
2020-06-23 00:09:42 +05:30
end
2014-09-02 18:07:02 +05:30
end
2018-03-17 18:26:18 +05:30
context 'when skip_disk_validation is used' do
it 'sets the project attribute' do
opts[:skip_disk_validation] = true
project = create_project(user, opts)
expect(project.skip_disk_validation).to be_truthy
end
end
2018-12-13 13:39:08 +05:30
it 'calls the passed block' do
fake_block = double('block')
opts[:relations_block] = fake_block
expect_next_instance_of(Project) do |project|
expect(fake_block).to receive(:call).with(project)
end
create_project(user, opts)
end
2018-03-17 18:26:18 +05:30
it 'writes project full path to .git/config' do
project = create_project(user, opts)
2018-12-05 23:21:45 +05:30
rugged = rugged_repo(project.repository)
2018-03-17 18:26:18 +05:30
2018-11-08 19:23:39 +05:30
expect(rugged.config['gitlab.fullpath']).to eq project.full_path
2018-03-17 18:26:18 +05:30
end
2021-04-29 21:17:54 +05:30
it 'triggers PostCreationWorker' do
expect(Projects::PostCreationWorker).to receive(:perform_async).with(a_kind_of(Integer))
create_project(user, opts)
end
2020-04-08 14:13:33 +05:30
context 'when project has access to shared service' do
2021-04-29 21:17:54 +05:30
before do
stub_feature_flags(projects_post_creation_worker: false)
end
2020-04-08 14:13:33 +05:30
context 'Prometheus application is shared via group cluster' do
let(:cluster) { create(:cluster, :group, groups: [group]) }
let(:group) do
create(:group).tap do |group|
group.add_owner(user)
end
end
before do
create(:clusters_applications_prometheus, :installed, cluster: cluster)
end
it 'creates PrometheusService record', :aggregate_failures do
project = create_project(user, opts.merge!(namespace_id: group.id))
service = project.prometheus_service
expect(service.active).to be true
expect(service.manual_configuration?).to be false
expect(service.persisted?).to be true
end
end
context 'Prometheus application is shared via instance cluster' do
let(:cluster) { create(:cluster, :instance) }
before do
create(:clusters_applications_prometheus, :installed, cluster: cluster)
end
it 'creates PrometheusService record', :aggregate_failures do
project = create_project(user, opts)
service = project.prometheus_service
expect(service.active).to be true
expect(service.manual_configuration?).to be false
expect(service.persisted?).to be true
end
it 'cleans invalid record and logs warning', :aggregate_failures do
invalid_service_record = build(:prometheus_service, properties: { api_url: nil, manual_configuration: true }.to_json)
2021-04-29 21:17:54 +05:30
allow(PrometheusService).to receive(:new).and_return(invalid_service_record)
2020-04-08 14:13:33 +05:30
expect(Gitlab::ErrorTracking).to receive(:track_exception).with(an_instance_of(ActiveRecord::RecordInvalid), include(extra: { project_id: a_kind_of(Integer) }))
project = create_project(user, opts)
expect(project.prometheus_service).to be_nil
end
end
context 'shared Prometheus application is not available' do
it 'does not persist PrometheusService record', :aggregate_failures do
project = create_project(user, opts)
expect(project.prometheus_service).to be_nil
end
end
end
2019-07-07 11:18:12 +05:30
context 'with external authorization enabled' do
before do
enable_external_authorization_service_check
end
it 'does not save the project with an error if the service denies access' do
expect(::Gitlab::ExternalAuthorization)
.to receive(:access_allowed?).with(user, 'new-label', any_args) { false }
project = create_project(user, opts.merge({ external_authorization_classification_label: 'new-label' }))
expect(project.errors[:external_authorization_classification_label]).to be_present
expect(project).not_to be_persisted
end
it 'saves the project when the user has access to the label' do
expect(::Gitlab::ExternalAuthorization)
.to receive(:access_allowed?).with(user, 'new-label', any_args) { true }
project = create_project(user, opts.merge({ external_authorization_classification_label: 'new-label' }))
expect(project).to be_persisted
expect(project.external_authorization_classification_label).to eq('new-label')
end
it 'does not save the project when the user has no access to the default label and no label is provided' do
expect(::Gitlab::ExternalAuthorization)
.to receive(:access_allowed?).with(user, 'default_label', any_args) { false }
project = create_project(user, opts)
expect(project.errors[:external_authorization_classification_label]).to be_present
expect(project).not_to be_persisted
end
end
2020-05-24 23:13:21 +05:30
it_behaves_like 'measurable service' do
before do
opts.merge!(
current_user: user,
path: 'foo'
)
end
let(:base_log_data) do
{
class: Projects::CreateService.name,
current_user: user.name,
project_full_path: "#{user.namespace.full_path}/#{opts[:path]}"
}
end
after do
create_project(user, opts)
end
end
context 'with specialized_project_authorization_workers' do
let_it_be(:other_user) { create(:user) }
let_it_be(:group) { create(:group) }
let(:opts) do
{
name: 'GitLab',
namespace_id: group.id
}
end
before do
group.add_maintainer(user)
group.add_developer(other_user)
end
it 'updates authorization for current_user' do
project = create_project(user, opts)
expect(
Ability.allowed?(user, :read_project, project)
).to be_truthy
end
it 'schedules authorization update for users with access to group' do
expect(AuthorizedProjectsWorker).not_to(
receive(:bulk_perform_async)
)
expect(AuthorizedProjectUpdate::ProjectCreateWorker).to(
receive(:perform_async).and_call_original
)
expect(AuthorizedProjectUpdate::UserRefreshWithLowUrgencyWorker).to(
receive(:bulk_perform_in)
2020-06-23 00:09:42 +05:30
.with(1.hour,
array_including([user.id], [other_user.id]),
batch_delay: 30.seconds, batch_size: 100)
2020-05-24 23:13:21 +05:30
.and_call_original
)
create_project(user, opts)
end
context 'when feature is disabled' do
before do
stub_feature_flags(specialized_project_authorization_workers: false)
end
it 'updates authorization for current_user' do
project = create_project(user, opts)
expect(
Ability.allowed?(user, :read_project, project)
).to be_truthy
end
it 'uses AuthorizedProjectsWorker' do
expect(AuthorizedProjectsWorker).to(
receive(:bulk_perform_async).with(array_including([user.id], [other_user.id])).and_call_original
)
expect(AuthorizedProjectUpdate::ProjectCreateWorker).not_to(
receive(:perform_async)
)
expect(AuthorizedProjectUpdate::UserRefreshWithLowUrgencyWorker).not_to(
receive(:bulk_perform_in)
)
create_project(user, opts)
end
end
end
2014-09-02 18:07:02 +05:30
def create_project(user, opts)
Projects::CreateService.new(user, opts).execute
end
2021-01-03 14:25:43 +05:30
context 'shared Runners config' do
using RSpec::Parameterized::TableSyntax
let_it_be(:user) { create :user }
context 'when parent group is present' do
let_it_be(:group) do
create(:group) do |group|
group.add_owner(user)
end
end
before do
allow_next_found_instance_of(Group) do |group|
allow(group).to receive(:shared_runners_setting).and_return(shared_runners_setting)
end
user.refresh_authorized_projects # Ensure cache is warm
end
context 'default value based on parent group setting' do
where(:shared_runners_setting, :desired_config_for_new_project, :expected_result_for_project) do
'enabled' | nil | true
'disabled_with_override' | nil | false
'disabled_and_unoverridable' | nil | false
end
with_them do
it 'creates project following the parent config' do
params = opts.merge(namespace_id: group.id)
params = params.merge(shared_runners_enabled: desired_config_for_new_project) unless desired_config_for_new_project.nil?
project = create_project(user, params)
expect(project).to be_valid
expect(project.shared_runners_enabled).to eq(expected_result_for_project)
end
end
end
context 'parent group is present and allows desired config' do
where(:shared_runners_setting, :desired_config_for_new_project, :expected_result_for_project) do
'enabled' | true | true
'enabled' | false | false
'disabled_with_override' | false | false
'disabled_with_override' | true | true
'disabled_and_unoverridable' | false | false
end
with_them do
it 'creates project following the parent config' do
params = opts.merge(namespace_id: group.id, shared_runners_enabled: desired_config_for_new_project)
project = create_project(user, params)
expect(project).to be_valid
expect(project.shared_runners_enabled).to eq(expected_result_for_project)
end
end
end
context 'parent group is present and disallows desired config' do
where(:shared_runners_setting, :desired_config_for_new_project) do
'disabled_and_unoverridable' | true
end
with_them do
it 'does not create project' do
params = opts.merge(namespace_id: group.id, shared_runners_enabled: desired_config_for_new_project)
project = create_project(user, params)
expect(project.persisted?).to eq(false)
expect(project).to be_invalid
expect(project.errors[:shared_runners_enabled]).to include('cannot be enabled because parent group does not allow it')
end
end
end
end
context 'parent group is not present' do
where(:desired_config, :expected_result) do
true | true
false | false
nil | true
end
with_them do
it 'follows desired config' do
opts[:shared_runners_enabled] = desired_config unless desired_config.nil?
project = create_project(user, opts)
expect(project).to be_valid
expect(project.shared_runners_enabled).to eq(expected_result)
end
end
end
end
2014-09-02 18:07:02 +05:30
end