transfer_service_spec.rb 8.9 KB
Newer Older
1
2
require 'spec_helper'

3
describe Projects::TransferService do
4
  let(:gitlab_shell) { Gitlab::Shell.new }
5
6
  let(:user) { create(:user) }
  let(:group) { create(:group) }
Nick Thomas's avatar
Nick Thomas committed
7
  let(:project) { create(:project, :repository, :legacy_storage, namespace: user.namespace) }
8

9
10
  context 'namespace -> namespace' do
    before do
11
12
13
14
      allow_any_instance_of(Gitlab::UploadsTransfer)
        .to receive(:move_project).and_return(true)
      allow_any_instance_of(Gitlab::PagesTransfer)
        .to receive(:move_project).and_return(true)
15
      group.add_owner(user)
Douwe Maan's avatar
Douwe Maan committed
16
      @result = transfer_project(project, user, group)
17
18
    end

19
20
    it { expect(@result).to be_truthy }
    it { expect(project.namespace).to eq(group) }
21
22
  end

23
24
25
26
27
28
29
30
31
32
33
  context 'when transfer succeeds' do
    before do
      group.add_owner(user)
    end

    it 'sends notifications' do
      expect_any_instance_of(NotificationService).to receive(:project_was_moved)

      transfer_project(project, user, group)
    end

34
35
36
37
38
39
    it 'invalidates the user\'s personal_project_count cache' do
      expect(user).to receive(:invalidate_personal_projects_count)

      transfer_project(project, user, group)
    end

40
    it 'executes system hooks' do
41
42
43
      transfer_project(project, user, group) do |service|
        expect(service).to receive(:execute_system_hooks)
      end
44
    end
45
46
47
48
49
50
51
52
53
54
55
56

    it 'disk path has moved' do
      old_path = project.repository.disk_path
      old_full_path = project.repository.full_path

      transfer_project(project, user, group)

      expect(project.repository.disk_path).not_to eq(old_path)
      expect(project.repository.full_path).not_to eq(old_full_path)
      expect(project.disk_path).not_to eq(old_path)
      expect(project.disk_path).to start_with(group.path)
    end
57
58
59
60

    it 'updates project full path in .git/config' do
      transfer_project(project, user, group)

61
      expect(rugged_config['gitlab.fullpath']).to eq "#{group.full_path}/#{project.path}"
62
    end
63
64
65
66
67
  end

  context 'when transfer fails' do
    let!(:original_path) { project_path(project) }

68
    def attempt_project_transfer(&block)
69
      expect do
70
        transfer_project(project, user, group, &block)
71
72
73
74
75
76
77
78
79
80
      end.to raise_error(ActiveRecord::ActiveRecordError)
    end

    before do
      group.add_owner(user)

      expect_any_instance_of(Labels::TransferService).to receive(:execute).and_raise(ActiveRecord::StatementInvalid, "PG ERROR")
    end

    def project_path(project)
81
82
83
      Gitlab::GitalyClient::StorageSettings.allow_disk_access do
        project.repository.path_to_repo
      end
84
85
86
87
88
89
90
91
92
    end

    def current_path
      project_path(project)
    end

    it 'rolls back repo location' do
      attempt_project_transfer

93
      expect(gitlab_shell.exists?(project.repository_storage, "#{project.disk_path}.git")).to be(true)
94
95
96
      expect(original_path).to eq current_path
    end

97
98
99
    it 'rolls back project full path in .git/config' do
      attempt_project_transfer

100
      expect(rugged_config['gitlab.fullpath']).to eq project.full_path
101
102
    end

103
104
105
106
107
108
109
    it "doesn't send move notifications" do
      expect_any_instance_of(NotificationService).not_to receive(:project_was_moved)

      attempt_project_transfer
    end

    it "doesn't run system hooks" do
110
111
112
      attempt_project_transfer do |service|
        expect(service).not_to receive(:execute_system_hooks)
      end
113
114
115
    end
  end

116
  context 'namespace -> no namespace' do
117
    before do
Douwe Maan's avatar
Douwe Maan committed
118
      @result = transfer_project(project, user, nil)
119
    end
120

121
    it { expect(@result).to eq false }
122
    it { expect(project.namespace).to eq(user.namespace) }
123
    it { expect(project.errors.messages[:new_namespace].first).to eq 'Please select a new namespace for your project.' }
124
125
  end

James Lopez's avatar
James Lopez committed
126
  context 'disallow transfering of project with tags' do
127
    let(:container_repository) { create(:container_repository) }
Andre Guedes's avatar
Andre Guedes committed
128

James Lopez's avatar
James Lopez committed
129
130
    before do
      stub_container_registry_config(enabled: true)
131
      stub_container_registry_tags(repository: :any, tags: ['tag'])
132
      project.container_repositories << container_repository
James Lopez's avatar
James Lopez committed
133
134
135
136
137
138
139
    end

    subject { transfer_project(project, user, group) }

    it { is_expected.to be_falsey }
  end

140
141
  context 'namespace -> not allowed namespace' do
    before do
Douwe Maan's avatar
Douwe Maan committed
142
      @result = transfer_project(project, user, group)
143
144
    end

145
    it { expect(@result).to eq false }
146
    it { expect(project.namespace).to eq(user.namespace) }
147
  end
148

149
  context 'namespace which contains orphan repository with same projects path name' do
Jacob Vosmaer's avatar
Jacob Vosmaer committed
150
    let(:repository_storage) { 'default' }
151
    let(:repository_storage_path) { Gitlab.config.repositories.storages[repository_storage].legacy_disk_path }
152
153
154

    before do
      group.add_owner(user)
155

156
      unless gitlab_shell.create_repository(repository_storage, "#{group.full_path}/#{project.path}")
Jacob Vosmaer's avatar
Jacob Vosmaer committed
157
158
        raise 'failed to add repository'
      end
159
160
161
162
163

      @result = transfer_project(project, user, group)
    end

    after do
164
      gitlab_shell.remove_repository(repository_storage, "#{group.full_path}/#{project.path}")
165
166
167
168
169
170
171
    end

    it { expect(@result).to eq false }
    it { expect(project.namespace).to eq(user.namespace) }
    it { expect(project.errors[:new_namespace]).to include('Cannot move project') }
  end

172
173
174
  context 'target namespace containing the same project name' do
    before do
      group.add_owner(user)
James Lopez's avatar
James Lopez committed
175
      project.update(name: 'new_name')
176

James Lopez's avatar
James Lopez committed
177
      create(:project, name: 'new_name', group: group, path: 'other')
178
179
180
181
182
183
184
185
186
187
188
189
190

      @result = transfer_project(project, user, group)
    end

    it { expect(@result).to eq false }
    it { expect(project.namespace).to eq(user.namespace) }
    it { expect(project.errors[:new_namespace]).to include('Project with same name or path in target namespace already exists') }
  end

  context 'target namespace containing the same project path' do
    before do
      group.add_owner(user)

James Lopez's avatar
James Lopez committed
191
      create(:project, name: 'other-name', path: project.path, group: group)
192
193
194
195
196
197
198
199
200

      @result = transfer_project(project, user, group)
    end

    it { expect(@result).to eq false }
    it { expect(project.namespace).to eq(user.namespace) }
    it { expect(project.errors[:new_namespace]).to include('Project with same name or path in target namespace already exists') }
  end

Douwe Maan's avatar
Douwe Maan committed
201
  def transfer_project(project, user, new_namespace)
202
203
204
205
206
    service = Projects::TransferService.new(project, user)

    yield(service) if block_given?

    service.execute(new_namespace)
207
  end
208
209
210
211

  context 'visibility level' do
    let(:internal_group) { create(:group, :internal) }

212
213
214
    before do
      internal_group.add_owner(user)
    end
215
216

    context 'when namespace visibility level < project visibility level' do
217
      let(:public_project) { create(:project, :public, :repository, namespace: user.namespace) }
218

219
220
221
      before do
        transfer_project(public_project, user, internal_group)
      end
222
223
224
225
226

      it { expect(public_project.visibility_level).to eq(internal_group.visibility_level) }
    end

    context 'when namespace visibility level > project visibility level' do
227
      let(:private_project) { create(:project, :private, :repository, namespace: user.namespace) }
228

229
230
231
      before do
        transfer_project(private_project, user, internal_group)
      end
232
233
234
235

      it { expect(private_project.visibility_level).to eq(Gitlab::VisibilityLevel::PRIVATE) }
    end
  end
236
237
238
239
240
241
242
243
244
245

  context 'missing group labels applied to issues or merge requests' do
    it 'delegates tranfer to Labels::TransferService' do
      group.add_owner(user)

      expect_any_instance_of(Labels::TransferService).to receive(:execute).once.and_call_original

      transfer_project(project, user, group)
    end
  end
246

247
  context 'when hashed storage in use' do
Nick Thomas's avatar
Nick Thomas committed
248
    let(:hashed_project) { create(:project, :repository, namespace: user.namespace) }
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266

    before do
      group.add_owner(user)
    end

    it 'does not move the directory' do
      old_path = hashed_project.repository.disk_path
      old_full_path = hashed_project.repository.full_path

      transfer_project(hashed_project, user, group)
      project.reload

      expect(hashed_project.repository.disk_path).to eq(old_path)
      expect(hashed_project.repository.full_path).to eq(old_full_path)
      expect(hashed_project.disk_path).to eq(old_path)
    end
  end

267
268
269
270
271
272
  describe 'refreshing project authorizations' do
    let(:group) { create(:group) }
    let(:owner) { project.namespace.owner }
    let(:group_member) { create(:user) }

    before do
273
      group.add_user(owner, GroupMember::MAINTAINER)
274
275
276
277
278
279
280
281
282
283
284
      group.add_user(group_member, GroupMember::DEVELOPER)
    end

    it 'refreshes the permissions of the old and new namespace' do
      transfer_project(project, owner, group)

      expect(group_member.authorized_projects).to include(project)
      expect(owner.authorized_projects).to include(project)
    end

    it 'only schedules a single job for every user' do
285
286
287
      expect(UserProjectAccessChangedService).to receive(:new)
        .with([owner.id, group_member.id])
        .and_call_original
288
289
290
291

      transfer_project(project, owner, group)
    end
  end
292
293
294
295
296
297

  def rugged_config
    Gitlab::GitalyClient::StorageSettings.allow_disk_access do
      project.repository.rugged.config
    end
  end
298
end