2013-03-19 11:37:50 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe Projects::ForkService do
|
2017-09-29 04:04:50 -04:00
|
|
|
include ProjectForksHelper
|
2017-08-22 19:19:35 -04:00
|
|
|
let(:gitlab_shell) { Gitlab::Shell.new }
|
2017-12-07 03:44:55 -05:00
|
|
|
context 'when forking a new project' do
|
|
|
|
describe 'fork by user' do
|
|
|
|
before do
|
|
|
|
@from_user = create(:user)
|
|
|
|
@from_namespace = @from_user.namespace
|
|
|
|
avatar = fixture_file_upload(Rails.root + "spec/fixtures/dk.png", "image/png")
|
|
|
|
@from_project = create(:project,
|
|
|
|
:repository,
|
|
|
|
creator_id: @from_user.id,
|
|
|
|
namespace: @from_namespace,
|
|
|
|
star_count: 107,
|
|
|
|
avatar: avatar,
|
|
|
|
description: 'wow such project')
|
|
|
|
@to_user = create(:user)
|
|
|
|
@to_namespace = @to_user.namespace
|
|
|
|
@from_project.add_user(@to_user, :developer)
|
|
|
|
end
|
2017-08-22 19:19:35 -04:00
|
|
|
|
2017-12-07 03:44:55 -05:00
|
|
|
context 'fork project' do
|
|
|
|
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) }
|
2013-03-19 11:37:50 -04:00
|
|
|
|
2017-12-07 03:44:55 -05:00
|
|
|
it { is_expected.not_to be_persisted }
|
|
|
|
it { expect(subject.errors[:forked_from_project_id]).to eq(['is forbidden']) }
|
2016-09-19 14:28:41 -04:00
|
|
|
end
|
|
|
|
|
2017-12-07 03:44:55 -05:00
|
|
|
describe "successfully creates project in the user namespace" do
|
|
|
|
let(:to_project) { fork_project(@from_project, @to_user, namespace: @to_user.namespace) }
|
2016-09-19 14:28:41 -04:00
|
|
|
|
2017-12-07 03:44:55 -05:00
|
|
|
it { expect(to_project).to be_persisted }
|
|
|
|
it { expect(to_project.errors).to be_empty }
|
|
|
|
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) }
|
|
|
|
it { expect(to_project.avatar.file).to be_exists }
|
2017-08-14 09:22:09 -04:00
|
|
|
|
2017-12-07 03:44:55 -05:00
|
|
|
# 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
|
2017-08-14 09:22:09 -04:00
|
|
|
|
2017-12-07 03:44:55 -05:00
|
|
|
expect(@from_project.avatar.file).to be_exists
|
|
|
|
end
|
2017-08-14 09:22:09 -04:00
|
|
|
|
2017-12-07 03:44:55 -05:00
|
|
|
it 'flushes the forks count cache of the source project' do
|
|
|
|
expect(@from_project.forks_count).to be_zero
|
2017-09-28 10:38:12 -04:00
|
|
|
|
2017-12-07 03:44:55 -05:00
|
|
|
fork_project(@from_project, @to_user)
|
2017-09-28 10:38:12 -04:00
|
|
|
|
2017-12-07 03:44:55 -05:00
|
|
|
expect(@from_project.forks_count).to eq(1)
|
|
|
|
end
|
2017-09-28 10:38:12 -04:00
|
|
|
|
2017-12-07 03:44:55 -05:00
|
|
|
it 'creates a fork network with the new project and the root project set' do
|
|
|
|
to_project
|
|
|
|
fork_network = @from_project.reload.fork_network
|
2017-09-28 10:38:12 -04:00
|
|
|
|
2017-12-07 03:44:55 -05:00
|
|
|
expect(fork_network).not_to be_nil
|
|
|
|
expect(fork_network.root_project).to eq(@from_project)
|
|
|
|
expect(fork_network.projects).to contain_exactly(@from_project, to_project)
|
|
|
|
end
|
2017-09-28 10:38:12 -04:00
|
|
|
end
|
|
|
|
|
2017-12-07 03:44:55 -05:00
|
|
|
context 'creating a fork of a fork' do
|
|
|
|
let(:from_forked_project) { fork_project(@from_project, @to_user) }
|
|
|
|
let(:other_namespace) do
|
|
|
|
group = create(:group)
|
|
|
|
group.add_owner(@to_user)
|
|
|
|
group
|
|
|
|
end
|
|
|
|
let(:to_project) { fork_project(from_forked_project, @to_user, namespace: other_namespace) }
|
|
|
|
|
|
|
|
it 'sets the root of the network to the root project' do
|
|
|
|
expect(to_project.fork_network.root_project).to eq(@from_project)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the forked_from_project on the membership' do
|
|
|
|
expect(to_project.fork_network_member.forked_from_project).to eq(from_forked_project)
|
|
|
|
end
|
2017-09-28 10:38:12 -04:00
|
|
|
end
|
2013-05-02 15:30:13 -04:00
|
|
|
end
|
2013-03-19 11:37:50 -04:00
|
|
|
|
2017-12-07 03:44:55 -05:00
|
|
|
context 'project already exists' do
|
|
|
|
it "fails due to validation, not transaction failure" do
|
|
|
|
@existing_project = create(:project, :repository, creator_id: @to_user.id, name: @from_project.name, namespace: @to_namespace)
|
|
|
|
@to_project = fork_project(@from_project, @to_user, namespace: @to_namespace)
|
|
|
|
expect(@existing_project).to be_persisted
|
2016-09-19 14:28:41 -04:00
|
|
|
|
2017-12-07 03:44:55 -05:00
|
|
|
expect(@to_project).not_to be_persisted
|
|
|
|
expect(@to_project.errors[:name]).to eq(['has already been taken'])
|
|
|
|
expect(@to_project.errors[:path]).to eq(['has already been taken'])
|
|
|
|
end
|
2013-05-02 15:30:13 -04:00
|
|
|
end
|
2015-04-06 09:36:56 -04:00
|
|
|
|
2017-12-07 03:44:55 -05:00
|
|
|
context 'repository already exists' do
|
|
|
|
let(:repository_storage) { 'default' }
|
2018-03-14 09:42:49 -04:00
|
|
|
let(:repository_storage_path) { Gitlab.config.repositories.storages[repository_storage].legacy_disk_path }
|
2017-08-22 19:19:35 -04:00
|
|
|
|
2017-12-07 03:44:55 -05:00
|
|
|
before do
|
2018-03-14 04:56:22 -04:00
|
|
|
gitlab_shell.create_repository(repository_storage, "#{@to_user.namespace.full_path}/#{@from_project.path}")
|
2017-12-07 03:44:55 -05:00
|
|
|
end
|
2017-08-22 19:19:35 -04:00
|
|
|
|
2017-12-07 03:44:55 -05:00
|
|
|
after do
|
|
|
|
gitlab_shell.remove_repository(repository_storage_path, "#{@to_user.namespace.full_path}/#{@from_project.path}")
|
|
|
|
end
|
2017-08-22 19:19:35 -04:00
|
|
|
|
2017-12-07 03:44:55 -05:00
|
|
|
it 'does not allow creation' do
|
|
|
|
to_project = fork_project(@from_project, @to_user, namespace: @to_user.namespace)
|
2017-08-22 19:19:35 -04:00
|
|
|
|
2017-12-07 03:44:55 -05:00
|
|
|
expect(to_project).not_to be_persisted
|
|
|
|
expect(to_project.errors.messages).to have_key(:base)
|
|
|
|
expect(to_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-12-07 03:44:55 -05:00
|
|
|
context 'GitLab CI is enabled' do
|
|
|
|
it "forks and enables CI for fork" do
|
|
|
|
@from_project.enable_ci
|
|
|
|
@to_project = fork_project(@from_project, @to_user)
|
|
|
|
expect(@to_project.builds_enabled?).to be_truthy
|
|
|
|
end
|
2015-04-06 09:36:56 -04:00
|
|
|
end
|
2016-05-24 17:53:53 -04:00
|
|
|
|
2017-12-07 03:44:55 -05: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
|
|
|
|
|
2017-12-20 10:19:54 -05:00
|
|
|
it "creates fork with lowest level" do
|
2017-12-07 03:44:55 -05:00
|
|
|
forked_project = fork_project(@from_project, @to_user)
|
|
|
|
|
2017-12-20 10:19:54 -05:00
|
|
|
expect(forked_project.visibility_level).to eq(Gitlab::VisibilityLevel::PRIVATE)
|
2017-12-07 03:44:55 -05:00
|
|
|
end
|
2016-05-24 17:53:53 -04:00
|
|
|
end
|
|
|
|
|
2017-12-07 03:44:55 -05:00
|
|
|
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)
|
2016-05-24 17:53:53 -04:00
|
|
|
|
2017-12-07 03:44:55 -05:00
|
|
|
expect(forked_project.visibility_level).to eq(Gitlab::VisibilityLevel::PRIVATE)
|
|
|
|
end
|
2016-05-24 17:53:53 -04:00
|
|
|
end
|
|
|
|
end
|
2017-12-07 03:44:55 -05:00
|
|
|
end
|
2016-05-24 17:53:53 -04:00
|
|
|
|
2017-12-07 03:44:55 -05:00
|
|
|
describe 'fork to namespace' do
|
|
|
|
before do
|
|
|
|
@group_owner = create(:user)
|
|
|
|
@developer = create(:user)
|
|
|
|
@project = create(:project, :repository,
|
|
|
|
creator_id: @group_owner.id,
|
|
|
|
star_count: 777,
|
|
|
|
description: 'Wow, such a cool project!')
|
|
|
|
@group = create(:group)
|
|
|
|
@group.add_user(@group_owner, GroupMember::OWNER)
|
|
|
|
@group.add_user(@developer, GroupMember::DEVELOPER)
|
|
|
|
@project.add_user(@developer, :developer)
|
|
|
|
@project.add_user(@group_owner, :developer)
|
|
|
|
@opts = { namespace: @group }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'fork project for group' do
|
|
|
|
it 'group owner successfully forks project into the group' do
|
|
|
|
to_project = fork_project(@project, @group_owner, @opts)
|
|
|
|
|
|
|
|
expect(to_project).to be_persisted
|
|
|
|
expect(to_project.errors).to be_empty
|
|
|
|
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)
|
|
|
|
expect(to_project.star_count).to be_zero
|
2016-05-24 17:53:53 -04:00
|
|
|
end
|
2017-12-07 03:44:55 -05:00
|
|
|
end
|
2016-05-24 17:53:53 -04:00
|
|
|
|
2017-12-07 03:44:55 -05:00
|
|
|
context 'fork project for group when user not owner' do
|
|
|
|
it 'group developer fails to fork project into the group' do
|
|
|
|
to_project = fork_project(@project, @developer, @opts)
|
|
|
|
expect(to_project.errors[:namespace]).to eq(['is not valid'])
|
|
|
|
end
|
|
|
|
end
|
2016-05-24 17:53:53 -04:00
|
|
|
|
2017-12-07 03:44:55 -05:00
|
|
|
context 'project already exists in group' do
|
|
|
|
it 'fails due to validation, not transaction failure' do
|
|
|
|
existing_project = create(:project, :repository,
|
|
|
|
name: @project.name,
|
|
|
|
namespace: @group)
|
|
|
|
to_project = fork_project(@project, @group_owner, @opts)
|
|
|
|
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'])
|
2016-05-24 17:53:53 -04:00
|
|
|
end
|
|
|
|
end
|
2017-12-20 10:19:54 -05:00
|
|
|
|
|
|
|
context 'when the namespace has a lower visibility level than the project' do
|
|
|
|
it 'creates the project with the lower visibility level' do
|
|
|
|
public_project = create(:project, :public)
|
|
|
|
private_group = create(:group, :private)
|
|
|
|
group_owner = create(:user)
|
|
|
|
private_group.add_owner(group_owner)
|
|
|
|
|
|
|
|
forked_project = fork_project(public_project, group_owner, namespace: private_group)
|
|
|
|
|
|
|
|
expect(forked_project.visibility_level).to eq(Gitlab::VisibilityLevel::PRIVATE)
|
|
|
|
end
|
|
|
|
end
|
2016-05-24 17:53:53 -04:00
|
|
|
end
|
2013-03-19 11:37:50 -04:00
|
|
|
end
|
|
|
|
|
2017-12-07 03:44:55 -05:00
|
|
|
context 'when linking fork to an existing project' do
|
|
|
|
let(:fork_from_project) { create(:project, :public) }
|
|
|
|
let(:fork_to_project) { create(:project, :public) }
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
subject { described_class.new(fork_from_project, user) }
|
|
|
|
|
|
|
|
def forked_from_project(project)
|
|
|
|
project.fork_network_member&.forked_from_project
|
2014-10-03 04:12:44 -04:00
|
|
|
end
|
|
|
|
|
2017-12-07 03:44:55 -05:00
|
|
|
context 'if project is already forked' do
|
|
|
|
it 'does not create fork relation' do
|
|
|
|
allow(fork_to_project).to receive(:forked?).and_return(true)
|
|
|
|
expect(forked_from_project(fork_to_project)).to be_nil
|
|
|
|
expect(subject.execute(fork_to_project)).to be_nil
|
|
|
|
expect(forked_from_project(fork_to_project)).to be_nil
|
2014-10-03 04:12:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-07 03:44:55 -05:00
|
|
|
context 'if project is not forked' do
|
|
|
|
it 'creates fork relation' do
|
|
|
|
expect(fork_to_project.forked?).to be false
|
|
|
|
expect(forked_from_project(fork_to_project)).to be_nil
|
|
|
|
|
|
|
|
subject.execute(fork_to_project)
|
|
|
|
|
|
|
|
expect(fork_to_project.forked?).to be true
|
|
|
|
expect(forked_from_project(fork_to_project)).to eq fork_from_project
|
|
|
|
expect(fork_to_project.forked_from_project).to eq fork_from_project
|
2014-10-03 04:12:44 -04:00
|
|
|
end
|
|
|
|
|
2017-12-07 03:44:55 -05:00
|
|
|
it 'flushes the forks count cache of the source project' do
|
|
|
|
expect(fork_from_project.forks_count).to be_zero
|
|
|
|
|
|
|
|
subject.execute(fork_to_project)
|
|
|
|
|
|
|
|
expect(fork_from_project.forks_count).to eq(1)
|
2014-10-03 04:12:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-03-19 11:37:50 -04:00
|
|
|
end
|