summaryrefslogtreecommitdiff
path: root/spec/migrations/remove_dot_git_from_usernames_spec.rb
blob: f11880a83e9c3521a242f7b36dede983e47e7781 (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
# encoding: utf-8

require 'spec_helper'
require Rails.root.join('db', 'migrate', '20161226122833_remove_dot_git_from_usernames.rb')

describe RemoveDotGitFromUsernames do
  let(:user) { create(:user) } # rubocop:disable RSpec/FactoriesInMigrationSpecs
  let(:migration) { described_class.new }

  describe '#up' do
    before do
      update_namespace(user, 'test.git')
    end

    it 'renames user with .git in username' do
      migration.up

      expect(user.reload.username).to eq('test_git')
      expect(user.namespace.reload.path).to eq('test_git')
      expect(user.namespace.route.path).to eq('test_git')
    end
  end

  context 'when new path exists already' do
    describe '#up' do
      let(:user2) { create(:user) } # rubocop:disable RSpec/FactoriesInMigrationSpecs

      before do
        update_namespace(user, 'test.git')
        update_namespace(user2, 'test_git')

        default_hash = Gitlab.config.repositories.storages.default.to_h
        default_hash['path'] = 'tmp/tests/custom_repositories'
        storages = { 'default' => Gitlab::GitalyClient::StorageSettings.new(default_hash) }

        allow(Gitlab.config.repositories).to receive(:storages).and_return(storages)
        allow(migration).to receive(:route_exists?).with('test_git').and_return(true)
        allow(migration).to receive(:route_exists?).with('test_git1').and_return(false)
      end

      it 'renames user with .git in username' do
        migration.up

        expect(user.reload.username).to eq('test_git1')
        expect(user.namespace.reload.path).to eq('test_git1')
        expect(user.namespace.route.path).to eq('test_git1')
      end
    end
  end

  def update_namespace(user, path)
    namespace = user.namespace
    namespace.path = path
    namespace.save!(validate: false)

    user.update_column(:username, path)
  end
end