namespace_spec.rb 4.19 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
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
20

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

25
  describe '#human_name' do
26
    it { expect(namespace.human_name).to eq(namespace.owner_name) }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
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])
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
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
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
55
56
  end

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

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

68
    it "moves dir if path changed" do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
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
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
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
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
88
89
90
  end

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

    before { namespace.destroy }

96
    it "removes its dirs when deleted" do
97
      expect(File.exist?(path)).to be(false)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
98
99
    end
  end
100

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

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

  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
128
end