namespace_spec.rb 5.15 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
  it { is_expected.to have_many :projects }
7 8

  it { is_expected.to validate_presence_of(:name) }
9
  it { is_expected.to validate_uniqueness_of(:name).scoped_to(:parent_id) }
10 11 12 13 14 15 16 17
  it { is_expected.to validate_length_of(:name).is_at_most(255) }

  it { is_expected.to validate_length_of(:description).is_at_most(255) }

  it { is_expected.to validate_presence_of(:path) }
  it { is_expected.to validate_length_of(:path).is_at_most(255) }

  it { is_expected.to validate_presence_of(:owner) }
18 19

  describe "Respond to" do
20 21
    it { is_expected.to respond_to(:human_name) }
    it { is_expected.to respond_to(:to_param) }
22
  end
23

24
  describe '#to_param' do
25
    it { expect(namespace.to_param).to eq(namespace.path) }
26 27
  end

28
  describe '#human_name' do
29
    it { expect(namespace.human_name).to eq(namespace.owner_name) }
30 31
  end

32
  describe '.search' do
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    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])
49 50
    end

51 52 53 54 55 56 57
    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
58 59
  end

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

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

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

    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
91 92 93
  end

  describe :rm_dir do
94 95 96 97 98
    let!(:project) { create(:project, namespace: namespace) }
    let!(:path) { File.join(Gitlab.config.repositories.storages.default, namespace.path) }

    before { namespace.destroy }

99
    it "removes its dirs when deleted" do
100
      expect(File.exist?(path)).to be(false)
101 102
    end
  end
103

104
  describe '.find_by_path_or_name' do
105 106 107 108 109 110 111 112
    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
113 114 115 116 117 118 119

  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")
120
      expect(Namespace.clean_path("--%+--valid_*&%name=.git.%.atom.atom.@email.com")).to eq("valid_name")
121 122
    end
  end
123 124 125 126 127 128 129 130

  describe '#full_path' do
    let(:group) { create(:group) }
    let(:nested_group) { create(:group, parent: group) }

    it { expect(group.full_path).to eq(group.path) }
    it { expect(nested_group.full_path).to eq("#{group.path}/#{nested_group.path}") }
  end
131

132 133 134 135 136 137 138 139
  describe '#full_name' do
    let(:group) { create(:group) }
    let(:nested_group) { create(:group, parent: group) }

    it { expect(group.full_name).to eq(group.name) }
    it { expect(nested_group.full_name).to eq("#{group.name} / #{nested_group.name}") }
  end

140 141 142 143
  describe '#parents' do
    let(:group) { create(:group) }
    let(:nested_group) { create(:group, parent: group) }
    let(:deep_nested_group) { create(:group, parent: nested_group) }
144
    let(:very_deep_nested_group) { create(:group, parent: deep_nested_group) }
145

146 147 148 149 150 151
    it 'returns the correct parents' do
      expect(very_deep_nested_group.parents).to eq([group, nested_group, deep_nested_group])
      expect(deep_nested_group.parents).to eq([group, nested_group])
      expect(nested_group.parents).to eq([group])
      expect(group.parents).to eq([])
    end
152
  end
153
end