fork_service_spec.rb 4.12 KB
Newer Older
1
2
require 'spec_helper'

3
describe Projects::ForkService do
4
5
  describe :fork_by_user do
    before do
6
7
      @from_namespace = create(:namespace)
      @from_user = create(:user, namespace: @from_namespace )
Stan Hu's avatar
Stan Hu committed
8
9
10
11
      @from_project = create(:project,
                             creator_id: @from_user.id,
                             namespace: @from_namespace,
                             star_count: 107,
12
                             description: 'wow such project')
13
14
      @to_namespace = create(:namespace)
      @to_user = create(:user, namespace: @to_namespace)
15
16
17
    end

    context 'fork project' do
18
19
      describe "successfully creates project in the user namespace" do
        let(:to_project) { fork_project(@from_project, @to_user) }
20

21
22
23
24
        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) }
25
      end
26
27
28
    end

    context 'fork project failure' do
29
      it "fails due to transaction failure" do
30
        @to_project = fork_project(@from_project, @to_user, false)
31
        expect(@to_project.import_failed?)
32
      end
33
34
35
36
37
38
    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)
39
        expect(@existing_project.persisted?).to be_truthy
40
41
        expect(@to_project.errors[:name]).to eq(['has already been taken'])
        expect(@to_project.errors[:path]).to eq(['has already been taken'])
42
      end
43
    end
Valery Sizov's avatar
Valery Sizov committed
44
45
46
47
48
49
50
51
52
53
54

    context 'GitLab CI is enabled' do
      it "calls fork registrator for CI" do
        @from_project.build_missing_services
        @from_project.gitlab_ci_service.update_attributes(active: true)

        expect(ForkRegistrationWorker).to receive(:perform_async)

        fork_project(@from_project, @to_user)
      end
    end
55
56
  end

57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  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)
73
74
75
76
77
78
        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
79
80
81
82
83
84
      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)
85
        expect(to_project.errors[:namespace]).to eq(['is not valid'])
86
87
88
89
90
91
92
93
      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)
94
95
96
        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'])
97
98
99
100
101
      end
    end
  end

  def fork_project(from_project, user, fork_success = true, params = {})
102
    allow(RepositoryForkWorker).to receive(:perform_async).and_return(fork_success)
103
    Projects::ForkService.new(from_project, user, params).execute
104
105
  end
end