2013-03-19 15:37:50 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2014-01-16 18:29:18 +00:00
|
|
|
describe Projects::ForkService do
|
2013-03-19 15:37:50 +00:00
|
|
|
describe :fork_by_user do
|
|
|
|
before do
|
2013-05-02 19:30:13 +00:00
|
|
|
@from_namespace = create(:namespace)
|
|
|
|
@from_user = create(:user, namespace: @from_namespace )
|
2014-08-25 09:19:52 +00:00
|
|
|
@from_project = create(:project, creator_id: @from_user.id,
|
|
|
|
namespace: @from_namespace, star_count: 107,
|
|
|
|
description: 'wow such project')
|
2013-05-02 19:30:13 +00:00
|
|
|
@to_namespace = create(:namespace)
|
|
|
|
@to_user = create(:user, namespace: @to_namespace)
|
2013-03-19 15:37:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'fork project' do
|
2014-08-25 09:19:52 +00:00
|
|
|
describe "successfully creates project in the user namespace" do
|
|
|
|
let(:to_project) { fork_project(@from_project, @to_user) }
|
2013-05-02 19:30:13 +00:00
|
|
|
|
2015-02-12 18:17:35 +00: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) }
|
2013-05-02 19:30:13 +00:00
|
|
|
end
|
2013-03-19 15:37:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'fork project failure' do
|
2013-05-02 19:30:13 +00:00
|
|
|
it "fails due to transaction failure" do
|
2013-03-19 15:37:50 +00:00
|
|
|
@to_project = fork_project(@from_project, @to_user, false)
|
2015-02-12 18:17:35 +00:00
|
|
|
expect(@to_project.errors).not_to be_empty
|
|
|
|
expect(@to_project.errors[:base]).to include("Fork transaction failed.")
|
2013-03-19 15:37:50 +00:00
|
|
|
end
|
2013-05-02 19:30:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'project already exists' do
|
|
|
|
it "should fail due to validation, not transaction failure" do
|
|
|
|
@existing_project = create(:project, creator_id: @to_user.id, name: @from_project.name, namespace: @to_namespace)
|
|
|
|
@to_project = fork_project(@from_project, @to_user)
|
2015-02-12 18:17:35 +00:00
|
|
|
expect(@existing_project.persisted?).to be_truthy
|
|
|
|
expect(@to_project.errors[:base]).to include("Invalid fork destination")
|
|
|
|
expect(@to_project.errors[:base]).not_to include("Fork transaction failed.")
|
2013-05-02 19:30:13 +00:00
|
|
|
end
|
2013-03-19 15:37:50 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-03 08:12:44 +00:00
|
|
|
describe :fork_to_namespace do
|
|
|
|
before do
|
|
|
|
@group_owner = create(:user)
|
|
|
|
@developer = create(:user)
|
|
|
|
@project = create(:project, 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)
|
|
|
|
@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, true, @opts)
|
2015-02-12 18:17:35 +00: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)
|
|
|
|
expect(to_project.star_count).to be_zero
|
2014-10-03 08:12:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'fork project for group when user not owner' do
|
|
|
|
it 'group developer should fail to fork project into the group' do
|
|
|
|
to_project = fork_project(@project, @developer, true, @opts)
|
2015-02-12 18:17:35 +00:00
|
|
|
expect(to_project.errors[:namespace]).to eq(['insufficient access rights'])
|
2014-10-03 08:12:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'project already exists in group' do
|
|
|
|
it 'should fail due to validation, not transaction failure' do
|
|
|
|
existing_project = create(:project, name: @project.name,
|
|
|
|
namespace: @group)
|
|
|
|
to_project = fork_project(@project, @group_owner, true, @opts)
|
2015-02-12 18:17:35 +00:00
|
|
|
expect(existing_project.persisted?).to be_truthy
|
|
|
|
expect(to_project.errors[:base]).to eq(['Invalid fork destination'])
|
|
|
|
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 08:12:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def fork_project(from_project, user, fork_success = true, params = {})
|
|
|
|
context = Projects::ForkService.new(from_project, user, params)
|
|
|
|
shell = double('gitlab_shell').stub(fork_repository: fork_success)
|
2013-03-19 15:37:50 +00:00
|
|
|
context.stub(gitlab_shell: shell)
|
|
|
|
context.execute
|
|
|
|
end
|
|
|
|
end
|