summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/database/rename_reserved_paths_migration/v1/rename_namespaces_spec.rb
blob: 8c4d7e323fa962e49660f3226c1401459609a820 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::Database::RenameReservedPathsMigration::V1::RenameNamespaces, :delete do
  let(:migration) { FakeRenameReservedPathMigrationV1.new }
  let(:subject) { described_class.new(['the-path'], migration) }
  let(:namespace) { create(:group, name: 'the-path') }

  before do
    allow(migration).to receive(:say)
    TestEnv.clean_test_path
  end

  def migration_namespace(namespace)
    Gitlab::Database::RenameReservedPathsMigration::V1::MigrationClasses::
      Namespace.find(namespace.id)
  end

  describe '#namespaces_for_paths' do
    context 'nested namespaces' do
      let(:subject) { described_class.new(['parent/the-Path'], migration) }

      it 'includes the namespace' do
        parent = create(:group, path: 'parent')
        child = create(:group, path: 'the-path', parent: parent)

        found_ids = subject.namespaces_for_paths(type: :child)
                      .map(&:id)

        expect(found_ids).to contain_exactly(child.id)
      end
    end

    context 'for child namespaces' do
      it 'only returns child namespaces with the correct path' do
        _root_namespace = create(:group, path: 'THE-path')
        _other_path = create(:group,
                             path: 'other',
                             parent: create(:group))
        namespace = create(:group,
                           path: 'the-path',
                           parent: create(:group))

        found_ids = subject.namespaces_for_paths(type: :child)
                      .map(&:id)

        expect(found_ids).to contain_exactly(namespace.id)
      end

      it 'has no namespaces that look the same' do
        _root_namespace = create(:group, path: 'THE-path')
        _similar_path = create(:group,
                             path: 'not-really-the-path',
                             parent: create(:group))
        namespace = create(:group,
                           path: 'the-path',
                           parent: create(:group))

        found_ids = subject.namespaces_for_paths(type: :child)
                      .map(&:id)

        expect(found_ids).to contain_exactly(namespace.id)
      end
    end

    context 'for top levelnamespaces' do
      it 'only returns child namespaces with the correct path' do
        root_namespace = create(:group, path: 'the-path')
        _other_path = create(:group, path: 'other')
        _child_namespace = create(:group,
                                  path: 'the-path',
                                  parent: create(:group))

        found_ids = subject.namespaces_for_paths(type: :top_level)
                      .map(&:id)

        expect(found_ids).to contain_exactly(root_namespace.id)
      end

      it 'has no namespaces that just look the same' do
        root_namespace = create(:group, path: 'the-path')
        _similar_path = create(:group, path: 'not-really-the-path')
        _child_namespace = create(:group,
                                  path: 'the-path',
                                  parent: create(:group))

        found_ids = subject.namespaces_for_paths(type: :top_level)
                      .map(&:id)

        expect(found_ids).to contain_exactly(root_namespace.id)
      end
    end
  end

  describe '#move_repositories' do
    let(:namespace) { create(:group, name: 'hello-group') }
    it 'moves a project for a namespace' do
      create(:project, :repository, :legacy_storage, namespace: namespace, path: 'hello-project')
      expected_path = File.join(TestEnv.repos_path, 'bye-group', 'hello-project.git')

      subject.move_repositories(namespace, 'hello-group', 'bye-group')

      expect(File.directory?(expected_path)).to be(true)
    end

    it 'moves a namespace in a subdirectory correctly' do
      child_namespace = create(:group, name: 'sub-group', parent: namespace)
      create(:project, :repository, :legacy_storage, namespace: child_namespace, path: 'hello-project')

      expected_path = File.join(TestEnv.repos_path, 'hello-group', 'renamed-sub-group', 'hello-project.git')

      subject.move_repositories(child_namespace, 'hello-group/sub-group', 'hello-group/renamed-sub-group')

      expect(File.directory?(expected_path)).to be(true)
    end

    it 'moves a parent namespace with subdirectories' do
      child_namespace = create(:group, name: 'sub-group', parent: namespace)
      create(:project, :repository, :legacy_storage, namespace: child_namespace, path: 'hello-project')
      expected_path = File.join(TestEnv.repos_path, 'renamed-group', 'sub-group', 'hello-project.git')

      subject.move_repositories(child_namespace, 'hello-group', 'renamed-group')

      expect(File.directory?(expected_path)).to be(true)
    end
  end

  describe "#child_ids_for_parent" do
    it "collects child ids for all levels" do
      parent = create(:group)
      first_child = create(:group, parent: parent)
      second_child = create(:group, parent: parent)
      third_child = create(:group, parent: second_child)
      all_ids = [parent.id, first_child.id, second_child.id, third_child.id]

      collected_ids = subject.child_ids_for_parent(parent, ids: [parent.id])

      expect(collected_ids).to contain_exactly(*all_ids)
    end
  end

  describe "#rename_namespace" do
    it 'renames paths & routes for the namespace' do
      expect(subject).to receive(:rename_path_for_routable)
                           .with(namespace)
                           .and_call_original

      subject.rename_namespace(namespace)

      expect(namespace.reload.path).to eq('the-path0')
    end

    it 'tracks the rename' do
      expect(subject).to receive(:track_rename)
                           .with('namespace', 'the-path', 'the-path0')

      subject.rename_namespace(namespace)
    end

    it 'renames things related to the namespace' do
      expect(subject).to receive(:rename_namespace_dependencies)
                           .with(namespace, 'the-path', 'the-path0')

      subject.rename_namespace(namespace)
    end
  end

  describe '#rename_namespace_dependencies' do
    it "moves the repository for a project in the namespace" do
      create(:project, :repository, :legacy_storage, namespace: namespace, path: "the-path-project")
      expected_repo = File.join(TestEnv.repos_path, "the-path0", "the-path-project.git")

      subject.rename_namespace_dependencies(namespace, 'the-path', 'the-path0')

      expect(File.directory?(expected_repo)).to be(true)
    end

    it "moves the uploads for the namespace" do
      expect(subject).to receive(:move_uploads).with("the-path", "the-path0")

      subject.rename_namespace_dependencies(namespace, 'the-path', 'the-path0')
    end

    it "moves the pages for the namespace" do
      expect(subject).to receive(:move_pages).with("the-path", "the-path0")

      subject.rename_namespace_dependencies(namespace, 'the-path', 'the-path0')
    end

    it 'invalidates the markdown cache of related projects' do
      project = create(:project, :legacy_storage, namespace: namespace, path: "the-path-project")

      expect(subject).to receive(:remove_cached_html_for_projects).with([project.id])

      subject.rename_namespace_dependencies(namespace, 'the-path', 'the-path0')
    end

    it "doesn't rename users for other namespaces" do
      expect(subject).not_to receive(:rename_user)

      subject.rename_namespace_dependencies(namespace, 'the-path', 'the-path0')
    end

    it 'renames the username of a namespace for a user' do
      user = create(:user, username: 'the-path')

      expect(subject).to receive(:rename_user).with('the-path', 'the-path0')

      subject.rename_namespace_dependencies(user.namespace, 'the-path', 'the-path0')
    end
  end

  describe '#rename_user' do
    it 'renames a username' do
      subject = described_class.new([], migration)
      user = create(:user, username: 'broken')

      subject.rename_user('broken', 'broken0')

      expect(user.reload.username).to eq('broken0')
    end
  end

  describe '#rename_namespaces' do
    let!(:top_level_namespace) { create(:group, path: 'the-path') }
    let!(:child_namespace) do
      create(:group, path: 'the-path', parent: create(:group))
    end

    it 'renames top level namespaces the namespace' do
      expect(subject).to receive(:rename_namespace)
                           .with(migration_namespace(top_level_namespace))

      subject.rename_namespaces(type: :top_level)
    end

    it 'renames child namespaces' do
      expect(subject).to receive(:rename_namespace)
                           .with(migration_namespace(child_namespace))

      subject.rename_namespaces(type: :child)
    end
  end

  describe '#revert_renames', :redis do
    it 'renames the routes back to the previous values' do
      project = create(:project, :legacy_storage, :repository, path: 'a-project', namespace: namespace)
      subject.rename_namespace(namespace)

      expect(subject).to receive(:perform_rename)
                           .with(
                             kind_of(Gitlab::Database::RenameReservedPathsMigration::V1::MigrationClasses::Namespace),
                             'the-path0',
                             'the-path'
                           ).and_call_original

      subject.revert_renames

      expect(namespace.reload.path).to eq('the-path')
      expect(namespace.reload.route.path).to eq('the-path')
      expect(project.reload.route.path).to eq('the-path/a-project')
    end

    it 'moves the repositories back to their original place' do
      project = create(:project, :repository, :legacy_storage, path: 'a-project', namespace: namespace)
      project.create_repository
      subject.rename_namespace(namespace)

      expected_path = File.join(TestEnv.repos_path, 'the-path', 'a-project.git')

      expect(subject).to receive(:rename_namespace_dependencies)
                           .with(
                             kind_of(Gitlab::Database::RenameReservedPathsMigration::V1::MigrationClasses::Namespace),
                             'the-path0',
                             'the-path'
                           ).and_call_original

      subject.revert_renames

      expect(File.directory?(expected_path)).to be_truthy
    end

    it "doesn't break when the namespace was renamed" do
      subject.rename_namespace(namespace)
      namespace.update!(path: 'renamed-afterwards')

      expect { subject.revert_renames }.not_to raise_error
    end
  end
end