2014-09-02 18:07:02 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
describe Projects::CreateService, '#execute' do
|
|
|
|
let(:gitlab_shell) { Gitlab::Shell.new }
|
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
|
|
|
|
|
|
|
|
it 'creates labels on Project creation if there are templates' do
|
|
|
|
Label.create(title: "bug", template: true)
|
|
|
|
project = create_project(user, opts)
|
|
|
|
|
|
|
|
expect(project.labels).not_to be_empty
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
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)
|
|
|
|
expect(project.team.masters).to include(user)
|
|
|
|
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
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
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)
|
2015-09-25 12:07:36 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(project).to be_persisted
|
|
|
|
expect(project.owner).to eq(user)
|
|
|
|
expect(project.team.masters).to include(user, admin)
|
|
|
|
expect(project.namespace).to eq(user.namespace)
|
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)
|
|
|
|
expect(user.authorized_projects).to include(project)
|
|
|
|
end
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
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)
|
|
|
|
path = ProjectWiki.new(project, user).send(:path_to_repo)
|
|
|
|
|
|
|
|
expect(File.exist?(path)).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)
|
|
|
|
path = ProjectWiki.new(project, user).send(:path_to_repo)
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(File.exist?(path)).to be_falsey
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
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
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'restricted visibility level' do
|
|
|
|
before do
|
|
|
|
stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::PUBLIC])
|
2015-11-26 14:37:03 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
opts.merge!(
|
2017-09-10 17:25:29 +05:30
|
|
|
visibility_level: Gitlab::VisibilityLevel::PUBLIC
|
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
|
|
|
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-11-26 14:37:03 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it 'allows a restricted visibility level for admins' do
|
|
|
|
admin = create(:admin)
|
|
|
|
project = create_project(admin, opts)
|
2015-11-26 14:37:03 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(project.errors.any?).to be(false)
|
|
|
|
expect(project.saved?).to be(true)
|
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
|
|
|
|
expect_any_instance_of(Project).to receive(:create_repository)
|
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
|
|
|
|
{
|
|
|
|
name: 'Existing',
|
|
|
|
namespace_id: user.namespace.id
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:repository_storage_path) { Gitlab.config.repositories.storages['default']['path'] }
|
|
|
|
|
|
|
|
before do
|
|
|
|
gitlab_shell.add_repository(repository_storage_path, "#{user.namespace.full_path}/existing")
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
gitlab_shell.remove_repository(repository_storage_path, "#{user.namespace.full_path}/existing")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not allow to create project with same path' do
|
|
|
|
project = create_project(user, opts)
|
|
|
|
|
|
|
|
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 a project with the same path' do
|
|
|
|
project = create_project(user, opts.merge({ import_url: 'https://gitlab.com/gitlab-org/gitlab-test.git' }))
|
|
|
|
|
|
|
|
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
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'when there is an active service template' do
|
|
|
|
before do
|
|
|
|
create(:service, project: nil, template: true, active: true)
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it 'creates a service from this template' do
|
|
|
|
project = create_project(user, opts)
|
2015-09-11 14:41:01 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(project.services.count).to eq 1
|
2014-09-02 18:07:02 +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 'when a bad service template is created' do
|
|
|
|
it 'reports an error in the imported project' do
|
|
|
|
opts[:import_url] = 'http://www.gitlab.com/gitlab-org/gitlab-ce'
|
2017-09-10 17:25:29 +05:30
|
|
|
create(:service, type: 'DroneCiService', project: nil, template: true, active: true)
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
project = create_project(user, opts)
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(project.errors.full_messages_for(:base).first).to match(/Unable to save project. Error: Unable to save DroneCiService/)
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(project.services.count).to eq 0
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def create_project(user, opts)
|
|
|
|
Projects::CreateService.new(user, opts).execute
|
|
|
|
end
|
|
|
|
end
|