summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/database/rename_reserved_paths_migration/v1/rename_projects_spec.rb
blob: 1ccdb1d9447da192fba35582a971012610cafaac (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
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::Database::RenameReservedPathsMigration::V1::RenameProjects, :delete do
  let(:migration) { FakeRenameReservedPathMigrationV1.new }
  let(:subject) { described_class.new(['the-path'], migration) }
  let(:project) do
    create(:project,
           :legacy_storage,
           path: 'the-path',
           namespace: create(:namespace, path: 'known-parent' ))
  end

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

  describe '#projects_for_paths' do
    it 'searches using nested paths' do
      namespace = create(:namespace, path: 'hello')
      project = create(:project, :legacy_storage, path: 'THE-path', namespace: namespace)

      result_ids = described_class.new(['Hello/the-path'], migration)
                     .projects_for_paths.map(&:id)

      expect(result_ids).to contain_exactly(project.id)
    end

    it 'includes the correct projects' do
      project = create(:project, :legacy_storage, path: 'THE-path')
      _other_project = create(:project, :legacy_storage)

      result_ids = subject.projects_for_paths.map(&:id)

      expect(result_ids).to contain_exactly(project.id)
    end
  end

  describe '#rename_projects' do
    let!(:projects) { create_list(:project, 2, :legacy_storage, path: 'the-path') }

    it 'renames each project' do
      expect(subject).to receive(:rename_project).twice

      subject.rename_projects
    end

    it 'invalidates the markdown cache of related projects' do
      expect(subject).to receive(:remove_cached_html_for_projects)
                           .with(projects.map(&:id))

      subject.rename_projects
    end
  end

  describe '#rename_project' do
    it 'renames path & route for the project' do
      expect(subject).to receive(:rename_path_for_routable)
                           .with(project)
                           .and_call_original

      subject.rename_project(project)

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

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

      subject.rename_project(project)
    end

    it 'renames the folders for the project' do
      expect(subject).to receive(:move_project_folders).with(project, 'known-parent/the-path', 'known-parent/the-path0')

      subject.rename_project(project)
    end
  end

  describe '#move_project_folders' do
    it 'moves the wiki & the repo' do
      expect(subject).to receive(:move_repository)
                           .with(project, 'known-parent/the-path.wiki', 'known-parent/the-path0.wiki')
      expect(subject).to receive(:move_repository)
                           .with(project, 'known-parent/the-path', 'known-parent/the-path0')

      subject.move_project_folders(project, 'known-parent/the-path', 'known-parent/the-path0')
    end

    it 'does not move the repositories when hashed storage is enabled' do
      project.update!(storage_version: Project::HASHED_STORAGE_FEATURES[:repository])

      expect(subject).not_to receive(:move_repository)

      subject.move_project_folders(project, 'known-parent/the-path', 'known-parent/the-path0')
    end

    it 'moves uploads' do
      expect(subject).to receive(:move_uploads)
                           .with('known-parent/the-path', 'known-parent/the-path0')

      subject.move_project_folders(project, 'known-parent/the-path', 'known-parent/the-path0')
    end

    it 'does not move uploads when hashed storage is enabled for attachments' do
      project.update!(storage_version: Project::HASHED_STORAGE_FEATURES[:attachments])

      expect(subject).not_to receive(:move_uploads)

      subject.move_project_folders(project, 'known-parent/the-path', 'known-parent/the-path0')
    end

    it 'moves pages' do
      expect(subject).to receive(:move_pages)
                           .with('known-parent/the-path', 'known-parent/the-path0')

      subject.move_project_folders(project, 'known-parent/the-path', 'known-parent/the-path0')
    end
  end

  describe '#move_repository' do
    let(:known_parent) { create(:namespace, path: 'known-parent') }
    let(:project) { create(:project, :repository, :legacy_storage, path: 'the-path', namespace: known_parent) }

    it 'moves the repository for a project' do
      expected_path = File.join(TestEnv.repos_path, 'known-parent', 'new-repo.git')

      subject.move_repository(project, 'known-parent/the-path', 'known-parent/new-repo')

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

  describe '#revert_renames', :redis do
    it 'renames the routes back to the previous values' do
      subject.rename_project(project)

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

      subject.revert_renames

      expect(project.reload.path).to eq('the-path')
      expect(project.route.path).to eq('known-parent/the-path')
    end

    it 'moves the repositories back to their original place' do
      project.create_repository
      subject.rename_project(project)

      expected_path = File.join(TestEnv.repos_path, 'known-parent', 'the-path.git')

      expect(subject).to receive(:move_project_folders)
                           .with(
                             kind_of(Gitlab::Database::RenameReservedPathsMigration::V1::MigrationClasses::Project),
                             'known-parent/the-path0',
                             'known-parent/the-path'
                           ).and_call_original

      subject.revert_renames

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

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

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