namespace_spec.rb 4.23 KB
Newer Older
1 2
require 'spec_helper'

Douwe Maan's avatar
Douwe Maan committed
3
describe Namespace, models: true do
4 5
  let!(:namespace) { create(:namespace) }

6 7 8 9 10 11
  it { is_expected.to have_many :projects }
  it { is_expected.to validate_presence_of :name }
  it { is_expected.to validate_uniqueness_of(:name) }
  it { is_expected.to validate_presence_of :path }
  it { is_expected.to validate_uniqueness_of(:path) }
  it { is_expected.to validate_presence_of :owner }
12 13 14 15 16

  describe "Mass assignment" do
  end

  describe "Respond to" do
17 18
    it { is_expected.to respond_to(:human_name) }
    it { is_expected.to respond_to(:to_param) }
19
  end
20

21
  describe '#to_param' do
22
    it { expect(namespace.to_param).to eq(namespace.path) }
23 24
  end

25
  describe '#human_name' do
26
    it { expect(namespace.human_name).to eq(namespace.owner_name) }
27 28
  end

29
  describe '.search' do
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    let(:namespace) { create(:namespace) }

    it 'returns namespaces with a matching name' do
      expect(described_class.search(namespace.name)).to eq([namespace])
    end

    it 'returns namespaces with a partially matching name' do
      expect(described_class.search(namespace.name[0..2])).to eq([namespace])
    end

    it 'returns namespaces with a matching name regardless of the casing' do
      expect(described_class.search(namespace.name.upcase)).to eq([namespace])
    end

    it 'returns namespaces with a matching path' do
      expect(described_class.search(namespace.path)).to eq([namespace])
46 47
    end

48 49 50 51 52 53 54
    it 'returns namespaces with a partially matching path' do
      expect(described_class.search(namespace.path[0..2])).to eq([namespace])
    end

    it 'returns namespaces with a matching path regardless of the casing' do
      expect(described_class.search(namespace.path.upcase)).to eq([namespace])
    end
55 56
  end

57
  describe '#move_dir' do
58 59
    before do
      @namespace = create :namespace
60
      @project = create :project, namespace: @namespace
61
      allow(@namespace).to receive(:path_changed?).and_return(true)
62 63
    end

64
    it "raises error when directory exists" do
65
      expect { @namespace.move_dir }.to raise_error("namespace directory cannot be moved")
66 67
    end

68
    it "moves dir if path changed" do
69
      new_path = @namespace.path + "_new"
70 71
      allow(@namespace).to receive(:path_was).and_return(@namespace.path)
      allow(@namespace).to receive(:path).and_return(new_path)
72
      expect(@namespace.move_dir).to be_truthy
73
    end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87

    context "when any project has container tags" do
      before do
        stub_container_registry_config(enabled: true)
        stub_container_registry_tags('tag')

        create(:empty_project, namespace: @namespace)

        allow(@namespace).to receive(:path_was).and_return(@namespace.path)
        allow(@namespace).to receive(:path).and_return('new_path')
      end

      it { expect { @namespace.move_dir }.to raise_error('Namespace cannot be moved, because at least one project has tags in container registry') }
    end
88 89
  end

90 91 92 93 94 95 96 97 98 99 100 101
  describe '#actual_size_limit' do
    let(:namespace) { build(:namespace) }

    before do
      allow_any_instance_of(ApplicationSetting).to receive(:repository_size_limit).and_return(50)
    end

    it 'returns the correct size limit' do
      expect(namespace.actual_size_limit).to eq(50)
    end
  end

102
  describe :rm_dir do
103 104 105 106 107
    let!(:project) { create(:project, namespace: namespace) }
    let!(:path) { File.join(Gitlab.config.repositories.storages.default, namespace.path) }

    before { namespace.destroy }

108
    it "removes its dirs when deleted" do
109
      expect(File.exist?(path)).to be(false)
110 111
    end
  end
112

113
  describe '.find_by_path_or_name' do
114 115 116 117 118 119 120 121
    before do
      @namespace = create(:namespace, name: 'WoW', path: 'woW')
    end

    it { expect(Namespace.find_by_path_or_name('wow')).to eq(@namespace) }
    it { expect(Namespace.find_by_path_or_name('WOW')).to eq(@namespace) }
    it { expect(Namespace.find_by_path_or_name('unknown')).to eq(nil) }
  end
122 123 124 125 126 127 128

  describe ".clean_path" do
    let!(:user)       { create(:user, username: "johngitlab-etc") }
    let!(:namespace)  { create(:namespace, path: "JohnGitLab-etc1") }

    it "cleans the path and makes sure it's available" do
      expect(Namespace.clean_path("-john+gitlab-ETC%.git@gmail.com")).to eq("johngitlab-ETC2")
129
      expect(Namespace.clean_path("--%+--valid_*&%name=.git.%.atom.atom.@email.com")).to eq("valid_name")
130 131
    end
  end
132
end