rename_projects_spec.rb 5.3 KB
Newer Older
1 2
require 'spec_helper'

3
describe Gitlab::Database::RenameReservedPathsMigration::V1::RenameProjects, :truncate do
4
  let(:migration) { FakeRenameReservedPathMigrationV1.new }
5
  let(:subject) { described_class.new(['the-path'], migration) }
6 7 8 9 10 11
  let(:project) do
    create(:empty_project,
           path: 'the-path',
           namespace: create(:namespace, path: 'known-parent' ))
  end

12
  before do
13
    allow(migration).to receive(:say)
14
    TestEnv.clean_test_path
15 16 17
  end

  describe '#projects_for_paths' do
18 19 20 21
    it 'searches using nested paths' do
      namespace = create(:namespace, path: 'hello')
      project = create(:empty_project, path: 'THE-path', namespace: namespace)

22 23
      result_ids = described_class.new(['Hello/the-path'], migration)
                     .projects_for_paths.map(&:id)
24 25 26 27

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

28 29 30 31
    it 'includes the correct projects' do
      project = create(:empty_project, path: 'THE-path')
      _other_project = create(:empty_project)

32
      result_ids = subject.projects_for_paths.map(&:id)
33 34 35 36 37

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

38 39 40 41 42 43 44 45 46 47
  describe '#rename_projects' do
    let!(:projects) { create_list(:empty_project, 2, 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
48 49
      expect(subject).to receive(:remove_cached_html_for_projects)
                           .with(projects.map(&:id))
50 51 52 53 54

      subject.rename_projects
    end
  end

55 56
  describe '#rename_project' do
    it 'renames path & route for the project' do
57 58 59
      expect(subject).to receive(:rename_path_for_routable)
                           .with(project)
                           .and_call_original
60 61

      subject.rename_project(project)
62 63

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

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    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
81
    it 'moves the wiki & the repo' do
82 83 84 85
      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')
86

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

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

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

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

101
      subject.move_project_folders(project, 'known-parent/the-path', 'known-parent/the-path0')
102
    end
103 104 105 106 107 108 109 110 111 112 113 114 115 116
  end

  describe '#move_repository' do
    let(:known_parent) { create(:namespace, path: 'known-parent') }
    let(:project) { create(:project, 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
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

  describe '#revert_renames', redis: true 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
152 153 154 155 156 157 158

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

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