2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-01-18 13:21:13 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe Projects::CreateService, '#execute' do
|
2019-04-09 11:38:58 -04:00
|
|
|
include ExternalAuthorizationServiceHelpers
|
2018-10-01 23:21:46 -04:00
|
|
|
include GitHelpers
|
|
|
|
|
2017-08-22 19:19:35 -04:00
|
|
|
let(:gitlab_shell) { Gitlab::Shell.new }
|
2016-11-20 03:42:58 -05:00
|
|
|
let(:user) { create :user }
|
|
|
|
let(:opts) do
|
|
|
|
{
|
2017-08-22 19:19:35 -04:00
|
|
|
name: 'GitLab',
|
|
|
|
namespace_id: user.namespace.id
|
2016-11-20 03:42:58 -05:00
|
|
|
}
|
|
|
|
end
|
2013-01-18 13:21:13 -05:00
|
|
|
|
2016-11-20 03:42:58 -05:00
|
|
|
it 'creates labels on Project creation if there are templates' do
|
|
|
|
Label.create(title: "bug", template: true)
|
|
|
|
project = create_project(user, opts)
|
2015-09-03 11:44:58 -04:00
|
|
|
|
2019-02-04 05:52:19 -05:00
|
|
|
created_label = project.reload.labels.last
|
|
|
|
|
|
|
|
expect(created_label.type).to eq('ProjectLabel')
|
|
|
|
expect(created_label.project_id).to eq(project.id)
|
|
|
|
expect(created_label.title).to eq('bug')
|
2016-11-20 03:42:58 -05:00
|
|
|
end
|
2015-09-03 11:44:58 -04:00
|
|
|
|
2016-11-20 03:42:58 -05:00
|
|
|
context 'user namespace' do
|
|
|
|
it do
|
|
|
|
project = create_project(user, opts)
|
2013-01-18 13:21:13 -05:00
|
|
|
|
2016-11-20 03:42:58 -05:00
|
|
|
expect(project).to be_valid
|
|
|
|
expect(project.owner).to eq(user)
|
2018-07-11 10:36:08 -04:00
|
|
|
expect(project.team.maintainers).to include(user)
|
2016-11-20 03:42:58 -05:00
|
|
|
expect(project.namespace).to eq(user.namespace)
|
2013-01-18 13:21:13 -05:00
|
|
|
end
|
2016-11-20 03:42:58 -05:00
|
|
|
end
|
2013-01-18 13:21:13 -05:00
|
|
|
|
2018-04-04 11:14:19 -04:00
|
|
|
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
|
|
|
|
end
|
|
|
|
|
2017-04-25 08:44:40 -04:00
|
|
|
context "admin creates project with other user's namespace_id" do
|
|
|
|
it 'sets the correct permissions' do
|
|
|
|
admin = create(:admin)
|
|
|
|
opts = {
|
|
|
|
name: 'GitLab',
|
|
|
|
namespace_id: user.namespace.id
|
|
|
|
}
|
|
|
|
project = create_project(admin, opts)
|
|
|
|
|
|
|
|
expect(project).to be_persisted
|
|
|
|
expect(project.owner).to eq(user)
|
2018-07-11 10:36:08 -04:00
|
|
|
expect(project.team.maintainers).to contain_exactly(user)
|
2017-04-25 08:44:40 -04:00
|
|
|
expect(project.namespace).to eq(user.namespace)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-20 03:42:58 -05:00
|
|
|
context 'group namespace' do
|
|
|
|
let(:group) do
|
|
|
|
create(:group).tap do |group|
|
|
|
|
group.add_owner(user)
|
|
|
|
end
|
|
|
|
end
|
2013-09-26 07:52:17 -04:00
|
|
|
|
2016-11-20 03:42:58 -05:00
|
|
|
before do
|
|
|
|
user.refresh_authorized_projects # Ensure cache is warm
|
|
|
|
end
|
2016-10-11 08:25:17 -04:00
|
|
|
|
2016-11-20 03:42:58 -05:00
|
|
|
it do
|
|
|
|
project = create_project(user, opts.merge!(namespace_id: group.id))
|
2013-01-18 13:21:13 -05:00
|
|
|
|
2016-11-20 03:42:58 -05:00
|
|
|
expect(project).to be_valid
|
|
|
|
expect(project.owner).to eq(group)
|
|
|
|
expect(project.namespace).to eq(group)
|
|
|
|
expect(user.authorized_projects).to include(project)
|
2013-01-18 13:21:13 -05:00
|
|
|
end
|
2016-11-20 03:42:58 -05:00
|
|
|
end
|
2013-09-09 01:45:30 -04:00
|
|
|
|
2016-11-20 03:42:58 -05:00
|
|
|
context 'error handling' do
|
|
|
|
it 'handles invalid options' do
|
2017-02-21 18:50:22 -05:00
|
|
|
opts[:default_branch] = 'master'
|
2016-11-20 03:42:58 -05:00
|
|
|
expect(create_project(user, opts)).to eq(nil)
|
2015-12-09 00:49:24 -05:00
|
|
|
end
|
2018-03-27 05:28:04 -04:00
|
|
|
|
2018-03-27 11:33:29 -04:00
|
|
|
it 'sets invalid service as inactive' do
|
2018-03-27 05:28:04 -04:00
|
|
|
create(:service, type: 'JiraService', project: nil, template: true, active: true)
|
|
|
|
|
|
|
|
project = create_project(user, opts)
|
2018-03-27 11:33:29 -04:00
|
|
|
service = project.services.first
|
2018-03-27 05:28:04 -04:00
|
|
|
|
2018-03-27 11:33:29 -04:00
|
|
|
expect(project).to be_persisted
|
|
|
|
expect(service.active).to be false
|
2018-03-27 05:28:04 -04:00
|
|
|
end
|
2016-11-20 03:42:58 -05:00
|
|
|
end
|
2015-12-09 00:49:24 -05:00
|
|
|
|
2016-11-20 03:42:58 -05:00
|
|
|
context 'wiki_enabled creates repository directory' do
|
|
|
|
context 'wiki_enabled true creates wiki repository directory' do
|
|
|
|
it do
|
|
|
|
project = create_project(user, opts)
|
2014-03-14 06:53:05 -04:00
|
|
|
|
2017-10-03 12:58:33 -04:00
|
|
|
expect(wiki_repo(project).exists?).to be_truthy
|
2014-03-14 06:53:05 -04:00
|
|
|
end
|
2016-11-20 03:42:58 -05:00
|
|
|
end
|
2014-03-14 06:53:05 -04:00
|
|
|
|
2016-11-20 03:42:58 -05:00
|
|
|
context 'wiki_enabled false does not create wiki repository directory' do
|
|
|
|
it do
|
2017-02-21 18:50:22 -05:00
|
|
|
opts[:wiki_enabled] = false
|
2016-11-20 03:42:58 -05:00
|
|
|
project = create_project(user, opts)
|
2014-03-14 06:53:05 -04:00
|
|
|
|
2017-10-03 12:58:33 -04:00
|
|
|
expect(wiki_repo(project).exists?).to be_falsey
|
2014-03-14 06:53:05 -04:00
|
|
|
end
|
|
|
|
end
|
2017-10-03 12:58:33 -04:00
|
|
|
|
|
|
|
def wiki_repo(project)
|
|
|
|
relative_path = ProjectWiki.new(project).disk_path + '.git'
|
2018-12-23 02:34:35 -05:00
|
|
|
Gitlab::Git::Repository.new(project.repository_storage, relative_path, 'foobar', project.full_path)
|
2017-10-03 12:58:33 -04:00
|
|
|
end
|
2016-11-20 03:42:58 -05:00
|
|
|
end
|
2015-03-07 13:23:43 -05:00
|
|
|
|
2018-07-15 17:28:25 -04:00
|
|
|
context 'import data' do
|
|
|
|
it 'stores import data and URL' do
|
|
|
|
import_data = { data: { 'test' => 'some data' } }
|
|
|
|
project = create_project(user, { name: 'test', import_url: 'http://import-url', import_data: import_data })
|
|
|
|
|
|
|
|
expect(project.import_data).to be_persisted
|
|
|
|
expect(project.import_data.data).to eq(import_data[:data])
|
|
|
|
expect(project.import_url).to eq('http://import-url')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-20 03:42:58 -05:00
|
|
|
context 'builds_enabled global setting' do
|
|
|
|
let(:project) { create_project(user, opts) }
|
2015-11-09 10:48:03 -05:00
|
|
|
|
2016-11-20 03:42:58 -05:00
|
|
|
subject { project.builds_enabled? }
|
2015-11-09 10:48:03 -05:00
|
|
|
|
2016-11-20 03:42:58 -05:00
|
|
|
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-11-09 10:48:03 -05:00
|
|
|
end
|
|
|
|
|
2016-11-20 03:42:58 -05:00
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
2015-11-09 10:48:03 -05:00
|
|
|
|
2016-11-20 03:42:58 -05:00
|
|
|
context 'global builds_enabled true does enable CI by default' do
|
|
|
|
it { is_expected.to be_truthy }
|
2015-11-09 10:48:03 -05:00
|
|
|
end
|
2016-11-20 03:42:58 -05:00
|
|
|
end
|
2015-11-09 10:48:03 -05:00
|
|
|
|
2019-06-12 19:03:46 -04:00
|
|
|
context 'default visibility level' do
|
|
|
|
let(:group) { create(:group, :private) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_application_setting(default_project_visibility: Gitlab::VisibilityLevel::INTERNAL)
|
|
|
|
group.add_developer(user)
|
|
|
|
|
|
|
|
opts.merge!(
|
|
|
|
visibility: 'private',
|
|
|
|
name: 'test',
|
|
|
|
namespace: group,
|
|
|
|
path: 'foo'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a private project' do
|
|
|
|
project = create_project(user, opts)
|
|
|
|
|
|
|
|
expect(project).to respond_to(:errors)
|
|
|
|
|
|
|
|
expect(project.errors.any?).to be(false)
|
|
|
|
expect(project.visibility_level).to eq(Gitlab::VisibilityLevel::PRIVATE)
|
|
|
|
expect(project.saved?).to be(true)
|
|
|
|
expect(project.valid?).to be(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-20 03:42:58 -05:00
|
|
|
context 'restricted visibility level' do
|
|
|
|
before do
|
|
|
|
stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::PUBLIC])
|
2015-03-07 13:23:43 -05:00
|
|
|
|
2016-11-20 03:42:58 -05:00
|
|
|
opts.merge!(
|
2017-06-07 16:13:44 -04:00
|
|
|
visibility_level: Gitlab::VisibilityLevel::PUBLIC
|
2016-11-20 03:42:58 -05:00
|
|
|
)
|
|
|
|
end
|
2015-03-07 13:23:43 -05:00
|
|
|
|
2016-11-20 03:42:58 -05:00
|
|
|
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
|
2015-03-07 13:23:43 -05:00
|
|
|
|
2016-11-20 03:42:58 -05:00
|
|
|
it 'allows a restricted visibility level for admins' do
|
|
|
|
admin = create(:admin)
|
|
|
|
project = create_project(admin, opts)
|
2015-07-25 16:42:28 -04:00
|
|
|
|
2016-11-20 03:42:58 -05:00
|
|
|
expect(project.errors.any?).to be(false)
|
|
|
|
expect(project.saved?).to be(true)
|
2015-03-07 13:23:43 -05:00
|
|
|
end
|
2016-11-20 03:42:58 -05:00
|
|
|
end
|
2015-09-01 03:56:40 -04:00
|
|
|
|
2016-11-20 03:42:58 -05:00
|
|
|
context 'repository creation' do
|
|
|
|
it 'synchronously creates the repository' do
|
|
|
|
expect_any_instance_of(Project).to receive(:create_repository)
|
2015-09-01 03:56:40 -04:00
|
|
|
|
2016-11-20 03:42:58 -05:00
|
|
|
project = create_project(user, opts)
|
|
|
|
expect(project).to be_valid
|
|
|
|
expect(project.owner).to eq(user)
|
|
|
|
expect(project.namespace).to eq(user.namespace)
|
2015-09-01 03:56:40 -04:00
|
|
|
end
|
2017-08-22 19:19:35 -04:00
|
|
|
|
|
|
|
context 'when another repository already exists on disk' do
|
2017-09-21 17:04:09 -04:00
|
|
|
let(:repository_storage) { 'default' }
|
|
|
|
|
2017-08-22 19:19:35 -04:00
|
|
|
let(:opts) do
|
|
|
|
{
|
|
|
|
name: 'Existing',
|
|
|
|
namespace_id: user.namespace.id
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-09-21 17:04:09 -04:00
|
|
|
context 'with legacy storage' do
|
|
|
|
before do
|
2019-06-12 23:18:05 -04:00
|
|
|
stub_application_setting(hashed_storage_enabled: false)
|
2018-12-23 02:34:35 -05:00
|
|
|
gitlab_shell.create_repository(repository_storage, "#{user.namespace.full_path}/existing", 'group/project')
|
2017-09-21 17:04:09 -04:00
|
|
|
end
|
2017-08-22 19:19:35 -04:00
|
|
|
|
2017-09-21 17:04:09 -04:00
|
|
|
after do
|
2018-04-13 06:57:19 -04:00
|
|
|
gitlab_shell.remove_repository(repository_storage, "#{user.namespace.full_path}/existing")
|
2017-09-21 17:04:09 -04:00
|
|
|
end
|
2017-08-22 19:19:35 -04:00
|
|
|
|
2017-09-21 17:04:09 -04:00
|
|
|
it 'does not allow to create a project when path matches existing repository on disk' do
|
|
|
|
project = create_project(user, opts)
|
2017-08-22 19:19:35 -04:00
|
|
|
|
2017-09-21 17:04:09 -04:00
|
|
|
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-08-22 19:19:35 -04:00
|
|
|
|
2017-09-21 17:04:09 -04:00
|
|
|
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' }))
|
|
|
|
|
|
|
|
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-08-22 19:19:35 -04:00
|
|
|
end
|
|
|
|
|
2017-09-21 17:04:09 -04:00
|
|
|
context 'with hashed storage' do
|
|
|
|
let(:hash) { '6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b' }
|
|
|
|
let(:hashed_path) { '@hashed/6b/86/6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b' }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Digest::SHA2).to receive(:hexdigest) { hash }
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2018-12-23 02:34:35 -05:00
|
|
|
gitlab_shell.create_repository(repository_storage, hashed_path, 'group/project')
|
2017-09-21 17:04:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
2018-04-13 06:57:19 -04:00
|
|
|
gitlab_shell.remove_repository(repository_storage, hashed_path)
|
2017-09-21 17:04:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not allow to create a project when path matches existing repository on disk' do
|
|
|
|
project = create_project(user, opts)
|
2017-08-22 19:19:35 -04:00
|
|
|
|
2017-09-21 17:04:09 -04:00
|
|
|
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-08-22 19:19:35 -04:00
|
|
|
end
|
|
|
|
end
|
2016-11-20 03:42:58 -05:00
|
|
|
end
|
2016-11-16 06:46:07 -05:00
|
|
|
|
2018-07-03 09:09:58 -04:00
|
|
|
context 'when readme initialization is requested' do
|
|
|
|
it 'creates README.md' do
|
|
|
|
opts[:initialize_with_readme] = '1'
|
|
|
|
|
|
|
|
project = create_project(user, opts)
|
|
|
|
|
|
|
|
expect(project.repository.commit_count).to be(1)
|
|
|
|
expect(project.repository.readme.name).to eql('README.md')
|
|
|
|
expect(project.repository.readme.data).to include('# GitLab')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-20 03:42:58 -05:00
|
|
|
context 'when there is an active service template' do
|
|
|
|
before do
|
|
|
|
create(:service, project: nil, template: true, active: true)
|
|
|
|
end
|
2016-11-16 06:46:07 -05:00
|
|
|
|
2016-11-20 03:42:58 -05:00
|
|
|
it 'creates a service from this template' do
|
|
|
|
project = create_project(user, opts)
|
2016-11-16 06:46:07 -05:00
|
|
|
|
2016-11-20 03:42:58 -05:00
|
|
|
expect(project.services.count).to eq 1
|
2016-11-16 06:46:07 -05:00
|
|
|
end
|
2013-01-18 13:21:13 -05:00
|
|
|
end
|
|
|
|
|
2017-04-13 19:09:17 -04:00
|
|
|
context 'when a bad service template is created' do
|
2018-03-27 11:33:29 -04:00
|
|
|
it 'sets service to be inactive' do
|
2017-04-13 19:09:17 -04:00
|
|
|
opts[:import_url] = 'http://www.gitlab.com/gitlab-org/gitlab-ce'
|
2017-06-01 10:27:35 -04:00
|
|
|
create(:service, type: 'DroneCiService', project: nil, template: true, active: true)
|
|
|
|
|
2017-04-13 19:09:17 -04:00
|
|
|
project = create_project(user, opts)
|
2018-03-27 11:33:29 -04:00
|
|
|
service = project.services.first
|
2017-04-13 19:09:17 -04:00
|
|
|
|
2018-03-27 11:33:29 -04:00
|
|
|
expect(project).to be_persisted
|
|
|
|
expect(service.active).to be false
|
2017-04-13 19:09:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-29 20:20:56 -04:00
|
|
|
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-08-31 13:16:34 -04:00
|
|
|
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
|
|
|
|
|
2017-12-19 11:36:33 -05:00
|
|
|
it 'writes project full path to .git/config' do
|
|
|
|
project = create_project(user, opts)
|
2018-10-01 23:21:46 -04:00
|
|
|
rugged = rugged_repo(project.repository)
|
2017-12-19 11:36:33 -05:00
|
|
|
|
2018-06-14 07:18:25 -04:00
|
|
|
expect(rugged.config['gitlab.fullpath']).to eq project.full_path
|
2017-12-19 11:36:33 -05:00
|
|
|
end
|
|
|
|
|
2019-04-09 11:38:58 -04:00
|
|
|
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
|
|
|
|
|
2013-01-18 13:21:13 -05:00
|
|
|
def create_project(user, opts)
|
2014-01-16 13:29:18 -05:00
|
|
|
Projects::CreateService.new(user, opts).execute
|
2013-01-18 13:21:13 -05:00
|
|
|
end
|
|
|
|
end
|