2013-03-19 11:37:50 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2015-12-09 05:55:49 -05:00
|
|
|
describe Projects::ForkService, services: true do
|
2017-03-24 19:04:03 -04:00
|
|
|
describe 'fork by user' do
|
2013-03-19 11:37:50 -04:00
|
|
|
before do
|
2013-05-02 15:30:13 -04:00
|
|
|
@from_namespace = create(:namespace)
|
|
|
|
@from_user = create(:user, namespace: @from_namespace )
|
2017-01-02 06:35:15 -05:00
|
|
|
avatar = fixture_file_upload(Rails.root + "spec/fixtures/dk.png", "image/png")
|
2015-06-23 01:24:39 -04:00
|
|
|
@from_project = create(:project,
|
2017-03-27 17:14:01 -04:00
|
|
|
:repository,
|
2015-06-23 01:24:39 -04:00
|
|
|
creator_id: @from_user.id,
|
|
|
|
namespace: @from_namespace,
|
|
|
|
star_count: 107,
|
2017-01-02 06:35:15 -05:00
|
|
|
avatar: avatar,
|
2014-08-25 05:19:52 -04:00
|
|
|
description: 'wow such project')
|
2013-05-02 15:30:13 -04:00
|
|
|
@to_namespace = create(:namespace)
|
|
|
|
@to_user = create(:user, namespace: @to_namespace)
|
2016-09-19 14:28:41 -04:00
|
|
|
@from_project.add_user(@to_user, :developer)
|
2013-03-19 11:37:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'fork project' do
|
2016-09-19 14:28:41 -04:00
|
|
|
context 'when forker is a guest' do
|
|
|
|
before do
|
|
|
|
@guest = create(:user)
|
|
|
|
@from_project.add_user(@guest, :guest)
|
|
|
|
end
|
|
|
|
subject { fork_project(@from_project, @guest) }
|
|
|
|
|
|
|
|
it { is_expected.not_to be_persisted }
|
|
|
|
it { expect(subject.errors[:forked_from_project_id]).to eq(['is forbidden']) }
|
|
|
|
end
|
|
|
|
|
2014-08-25 05:19:52 -04:00
|
|
|
describe "successfully creates project in the user namespace" do
|
|
|
|
let(:to_project) { fork_project(@from_project, @to_user) }
|
2013-05-02 15:30:13 -04:00
|
|
|
|
2016-09-19 14:28:41 -04:00
|
|
|
it { expect(to_project).to be_persisted }
|
|
|
|
it { expect(to_project.errors).to be_empty }
|
2015-02-12 13:17:35 -05:00
|
|
|
it { expect(to_project.owner).to eq(@to_user) }
|
|
|
|
it { expect(to_project.namespace).to eq(@to_user.namespace) }
|
|
|
|
it { expect(to_project.star_count).to be_zero }
|
|
|
|
it { expect(to_project.description).to eq(@from_project.description) }
|
2017-01-02 06:35:15 -05:00
|
|
|
it { expect(to_project.avatar.file).to be_exists }
|
|
|
|
|
|
|
|
# This test is here because we had a bug where the from-project lost its
|
|
|
|
# avatar after being forked.
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab-ce/issues/26158
|
|
|
|
it "after forking the from-project still has its avatar" do
|
|
|
|
# If we do not fork the project first we cannot detect the bug.
|
|
|
|
expect(to_project).to be_persisted
|
|
|
|
|
|
|
|
expect(@from_project.avatar.file).to be_exists
|
|
|
|
end
|
2013-05-02 15:30:13 -04:00
|
|
|
end
|
2013-03-19 11:37:50 -04:00
|
|
|
end
|
|
|
|
|
2013-05-02 15:30:13 -04:00
|
|
|
context 'project already exists' do
|
2016-08-01 11:00:44 -04:00
|
|
|
it "fails due to validation, not transaction failure" do
|
2017-03-27 17:14:01 -04:00
|
|
|
@existing_project = create(:project, :repository, creator_id: @to_user.id, name: @from_project.name, namespace: @to_namespace)
|
2013-05-02 15:30:13 -04:00
|
|
|
@to_project = fork_project(@from_project, @to_user)
|
2016-09-19 14:28:41 -04:00
|
|
|
expect(@existing_project).to be_persisted
|
|
|
|
|
|
|
|
expect(@to_project).not_to be_persisted
|
2015-04-24 15:37:12 -04:00
|
|
|
expect(@to_project.errors[:name]).to eq(['has already been taken'])
|
|
|
|
expect(@to_project.errors[:path]).to eq(['has already been taken'])
|
2013-05-02 15:30:13 -04:00
|
|
|
end
|
2013-03-19 11:37:50 -04:00
|
|
|
end
|
2015-04-06 09:36:56 -04:00
|
|
|
|
|
|
|
context 'GitLab CI is enabled' do
|
2016-08-01 11:00:44 -04:00
|
|
|
it "forks and enables CI for fork" do
|
2015-10-02 05:02:05 -04:00
|
|
|
@from_project.enable_ci
|
|
|
|
@to_project = fork_project(@from_project, @to_user)
|
2015-11-09 10:48:03 -05:00
|
|
|
expect(@to_project.builds_enabled?).to be_truthy
|
2015-04-06 09:36:56 -04:00
|
|
|
end
|
|
|
|
end
|
2016-05-24 17:53:53 -04:00
|
|
|
|
|
|
|
context "when project has restricted visibility level" do
|
|
|
|
context "and only one visibility level is restricted" do
|
|
|
|
before do
|
|
|
|
@from_project.update_attributes(visibility_level: Gitlab::VisibilityLevel::INTERNAL)
|
|
|
|
stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::INTERNAL])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates fork with highest allowed level" do
|
|
|
|
forked_project = fork_project(@from_project, @to_user)
|
|
|
|
|
|
|
|
expect(forked_project.visibility_level).to eq(Gitlab::VisibilityLevel::PUBLIC)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "and all visibility levels are restricted" do
|
|
|
|
before do
|
|
|
|
stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::PUBLIC, Gitlab::VisibilityLevel::INTERNAL, Gitlab::VisibilityLevel::PRIVATE])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates fork with private visibility levels" do
|
|
|
|
forked_project = fork_project(@from_project, @to_user)
|
|
|
|
|
|
|
|
expect(forked_project.visibility_level).to eq(Gitlab::VisibilityLevel::PRIVATE)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-03-19 11:37:50 -04:00
|
|
|
end
|
|
|
|
|
2017-03-24 19:04:03 -04:00
|
|
|
describe 'fork to namespace' do
|
2014-10-03 04:12:44 -04:00
|
|
|
before do
|
|
|
|
@group_owner = create(:user)
|
|
|
|
@developer = create(:user)
|
2017-03-27 17:14:01 -04:00
|
|
|
@project = create(:project, :repository,
|
|
|
|
creator_id: @group_owner.id,
|
|
|
|
star_count: 777,
|
|
|
|
description: 'Wow, such a cool project!')
|
2014-10-03 04:12:44 -04:00
|
|
|
@group = create(:group)
|
|
|
|
@group.add_user(@group_owner, GroupMember::OWNER)
|
|
|
|
@group.add_user(@developer, GroupMember::DEVELOPER)
|
2016-09-19 14:28:41 -04:00
|
|
|
@project.add_user(@developer, :developer)
|
|
|
|
@project.add_user(@group_owner, :developer)
|
2014-10-03 04:12:44 -04:00
|
|
|
@opts = { namespace: @group }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'fork project for group' do
|
|
|
|
it 'group owner successfully forks project into the group' do
|
2015-11-12 07:46:50 -05:00
|
|
|
to_project = fork_project(@project, @group_owner, @opts)
|
2016-09-19 14:28:41 -04:00
|
|
|
|
|
|
|
expect(to_project).to be_persisted
|
|
|
|
expect(to_project.errors).to be_empty
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(to_project.owner).to eq(@group)
|
|
|
|
expect(to_project.namespace).to eq(@group)
|
|
|
|
expect(to_project.name).to eq(@project.name)
|
|
|
|
expect(to_project.path).to eq(@project.path)
|
|
|
|
expect(to_project.description).to eq(@project.description)
|
2016-09-19 14:28:41 -04:00
|
|
|
expect(to_project.star_count).to be_zero
|
2014-10-03 04:12:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'fork project for group when user not owner' do
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'group developer fails to fork project into the group' do
|
2015-11-12 07:46:50 -05:00
|
|
|
to_project = fork_project(@project, @developer, @opts)
|
2015-04-24 15:37:12 -04:00
|
|
|
expect(to_project.errors[:namespace]).to eq(['is not valid'])
|
2014-10-03 04:12:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'project already exists in group' do
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'fails due to validation, not transaction failure' do
|
2017-03-27 17:14:01 -04:00
|
|
|
existing_project = create(:project, :repository,
|
|
|
|
name: @project.name,
|
|
|
|
namespace: @group)
|
2015-11-12 07:46:50 -05:00
|
|
|
to_project = fork_project(@project, @group_owner, @opts)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(existing_project.persisted?).to be_truthy
|
|
|
|
expect(to_project.errors[:name]).to eq(['has already been taken'])
|
|
|
|
expect(to_project.errors[:path]).to eq(['has already been taken'])
|
2014-10-03 04:12:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-12 07:46:50 -05:00
|
|
|
def fork_project(from_project, user, params = {})
|
|
|
|
allow(RepositoryForkWorker).to receive(:perform_async).and_return(true)
|
2015-04-24 15:37:12 -04:00
|
|
|
Projects::ForkService.new(from_project, user, params).execute
|
2013-03-19 11:37:50 -04:00
|
|
|
end
|
|
|
|
end
|