Read about our upcoming Code of Conduct on this issue

This instance will be upgraded to Heptapod 0.28.1 on 2022-01-26 at 16:00 UTC+1 (a few minutes of down time)

routable_spec.rb 3.52 KB
Newer Older
1
2
3
4
5
require 'spec_helper'

describe Group, 'Routable' do
  let!(:group) { create(:group) }

6
7
8
9
  describe 'Validations' do
    it { is_expected.to validate_presence_of(:route) }
  end

10
11
12
13
14
15
16
  describe 'Associations' do
    it { is_expected.to have_one(:route).dependent(:destroy) }
  end

  describe 'Callbacks' do
    it 'creates route record on create' do
      expect(group.route.path).to eq(group.path)
17
      expect(group.route.name).to eq(group.name)
18
19
20
    end

    it 'updates route record on path change' do
21
      group.update_attributes(path: 'wow', name: 'much')
22
23

      expect(group.route.path).to eq('wow')
24
      expect(group.route.name).to eq('much')
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
    end

    it 'ensure route path uniqueness across different objects' do
      create(:group, parent: group, path: 'xyz')
      duplicate = build(:project, namespace: group, path: 'xyz')

      expect { duplicate.save! }.to raise_error(ActiveRecord::RecordInvalid, 'Validation failed: Route path has already been taken, Route is invalid')
    end
  end

  describe '.find_by_full_path' do
    let!(:nested_group) { create(:group, parent: group) }

    it { expect(described_class.find_by_full_path(group.to_param)).to eq(group) }
    it { expect(described_class.find_by_full_path(group.to_param.upcase)).to eq(group) }
    it { expect(described_class.find_by_full_path(nested_group.to_param)).to eq(nested_group) }
    it { expect(described_class.find_by_full_path('unknown')).to eq(nil) }
  end

44
  describe '.where_full_path_in' do
45
46
    context 'without any paths' do
      it 'returns an empty relation' do
47
        expect(described_class.where_full_path_in([])).to eq([])
48
49
50
51
52
      end
    end

    context 'without any valid paths' do
      it 'returns an empty relation' do
53
        expect(described_class.where_full_path_in(%w[unknown])).to eq([])
54
55
56
57
58
59
60
      end
    end

    context 'with valid paths' do
      let!(:nested_group) { create(:group, parent: group) }

      it 'returns the projects matching the paths' do
61
        result = described_class.where_full_path_in([group.to_param, nested_group.to_param])
62
63
64
65
66

        expect(result).to contain_exactly(group, nested_group)
      end

      it 'returns projects regardless of the casing of paths' do
67
        result = described_class.where_full_path_in([group.to_param.upcase, nested_group.to_param.upcase])
68
69
70
71
72

        expect(result).to contain_exactly(group, nested_group)
      end
    end
  end
73
74
75
76
77
78
79
80
81
82

  describe '.member_descendants' do
    let!(:user) { create(:user) }
    let!(:nested_group) { create(:group, parent: group) }

    before { group.add_owner(user) }
    subject { described_class.member_descendants(user.id) }

    it { is_expected.to eq([nested_group]) }
  end
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

  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

  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
end

describe Project, 'Routable' do
  describe '#full_path' do
    let(:project) { build_stubbed(:empty_project) }

    it { expect(project.full_path).to eq "#{project.namespace.path}/#{project.path}" }
  end

  describe '#full_name' do
    let(:project) { build_stubbed(:empty_project) }

    it { expect(project.full_name).to eq "#{project.namespace.human_name} / #{project.name}" }
  end
113
end