debian-mirror-gitlab/spec/services/projects/fork_service_spec.rb

123 lines
4.9 KiB
Ruby
Raw Normal View History

2015-04-26 12:48:37 +05:30
require 'spec_helper'
2015-12-23 02:04:40 +05:30
describe Projects::ForkService, services: true do
2015-04-26 12:48:37 +05:30
describe :fork_by_user do
before do
@from_namespace = create(:namespace)
@from_user = create(:user, namespace: @from_namespace )
2015-09-11 14:41:01 +05:30
@from_project = create(:project,
creator_id: @from_user.id,
namespace: @from_namespace,
star_count: 107,
2015-04-26 12:48:37 +05:30
description: 'wow such project')
@to_namespace = create(:namespace)
@to_user = create(:user, namespace: @to_namespace)
end
context 'fork project' do
describe "successfully creates project in the user namespace" do
let(:to_project) { fork_project(@from_project, @to_user) }
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) }
end
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)
expect(@existing_project.persisted?).to be_truthy
2015-09-11 14:41:01 +05:30
expect(@to_project.errors[:name]).to eq(['has already been taken'])
expect(@to_project.errors[:path]).to eq(['has already been taken'])
2015-04-26 12:48:37 +05:30
end
end
context 'GitLab CI is enabled' do
2015-10-24 18:46:33 +05:30
it "fork and enable CI for fork" do
@from_project.enable_ci
@to_project = fork_project(@from_project, @to_user)
2015-11-26 14:37:03 +05:30
expect(@to_project.builds_enabled?).to be_truthy
2015-04-26 12:48:37 +05:30
end
end
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
2015-04-26 12:48:37 +05:30
end
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
2015-11-26 14:37:03 +05:30
to_project = fork_project(@project, @group_owner, @opts)
2015-04-26 12:48:37 +05:30
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
end
end
context 'fork project for group when user not owner' do
it 'group developer should fail to fork project into the group' do
2015-11-26 14:37:03 +05:30
to_project = fork_project(@project, @developer, @opts)
2015-09-11 14:41:01 +05:30
expect(to_project.errors[:namespace]).to eq(['is not valid'])
2015-04-26 12:48:37 +05:30
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)
2015-11-26 14:37:03 +05:30
to_project = fork_project(@project, @group_owner, @opts)
2015-04-26 12:48:37 +05:30
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'])
end
end
end
2015-11-26 14:37:03 +05:30
def fork_project(from_project, user, params = {})
allow(RepositoryForkWorker).to receive(:perform_async).and_return(true)
2015-09-11 14:41:01 +05:30
Projects::ForkService.new(from_project, user, params).execute
2015-04-26 12:48:37 +05:30
end
end