BigW Consortium Gitlab

route_spec.rb 7.85 KB
Newer Older
1 2
require 'spec_helper'

3
describe Route do
4 5
  let(:group) { create(:group, path: 'git_lab', name: 'git_lab') }
  let(:route) { group.route }
6 7 8 9 10 11

  describe 'relationships' do
    it { is_expected.to belong_to(:source) }
  end

  describe 'validations' do
12 13 14 15
    before do
      expect(route).to be_persisted
    end

16 17
    it { is_expected.to validate_presence_of(:source) }
    it { is_expected.to validate_presence_of(:path) }
18
    it { is_expected.to validate_uniqueness_of(:path).case_insensitive }
19 20
  end

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
  describe 'callbacks' do
    context 'after update' do
      it 'calls #create_redirect_for_old_path' do
        expect(route).to receive(:create_redirect_for_old_path)
        route.update_attributes(path: 'foo')
      end

      it 'calls #delete_conflicting_redirects' do
        expect(route).to receive(:delete_conflicting_redirects)
        route.update_attributes(path: 'foo')
      end
    end

    context 'after create' do
      it 'calls #delete_conflicting_redirects' do
        route.destroy
37
        new_route = described_class.new(source: group, path: group.path)
38 39 40 41 42 43
        expect(new_route).to receive(:delete_conflicting_redirects)
        new_route.save!
      end
    end
  end

44 45 46 47 48 49 50 51
  describe '.inside_path' do
    let!(:nested_group) { create(:group, path: 'test', name: 'test', parent: group) }
    let!(:deep_nested_group) { create(:group, path: 'foo', name: 'foo', parent: nested_group) }
    let!(:another_group) { create(:group, path: 'other') }
    let!(:similar_group) { create(:group, path: 'gitllab') }
    let!(:another_group_nested) { create(:group, path: 'another', name: 'another', parent: similar_group) }

    it 'returns correct routes' do
52
      expect(described_class.inside_path('git_lab')).to match_array([nested_group.route, deep_nested_group.route])
53 54 55
    end
  end

56
  describe '#rename_descendants' do
57 58 59
    let!(:nested_group) { create(:group, path: 'test', name: 'test', parent: group) }
    let!(:deep_nested_group) { create(:group, path: 'foo', name: 'foo', parent: nested_group) }
    let!(:similar_group) { create(:group, path: 'gitlab-org', name: 'gitlab-org') }
60 61
    let!(:another_group) { create(:group, path: 'gittlab', name: 'gitllab') }
    let!(:another_group_nested) { create(:group, path: 'git_lab', name: 'git_lab', parent: another_group) }
62

63
    context 'path update' do
64
      context 'when route name is set' do
65 66 67
        before do
          route.update_attributes(path: 'bar')
        end
68

69
        it 'updates children routes with new path' do
70 71 72 73
          expect(described_class.exists?(path: 'bar')).to be_truthy
          expect(described_class.exists?(path: 'bar/test')).to be_truthy
          expect(described_class.exists?(path: 'bar/test/foo')).to be_truthy
          expect(described_class.exists?(path: 'gitlab-org')).to be_truthy
74 75
          expect(described_class.exists?(path: 'gittlab')).to be_truthy
          expect(described_class.exists?(path: 'gittlab/git_lab')).to be_truthy
76 77
        end
      end
78

79 80 81 82 83 84 85 86
      context 'when route name is nil' do
        before do
          route.update_column(:name, nil)
        end

        it "does not fail" do
          expect(route.update_attributes(path: 'bar')).to be_truthy
        end
87
      end
88 89 90 91 92 93 94 95 96 97 98 99 100 101

      context 'when conflicting redirects exist' do
        let!(:conflicting_redirect1) { route.create_redirect('bar/test') }
        let!(:conflicting_redirect2) { route.create_redirect('bar/test/foo') }
        let!(:conflicting_redirect3) { route.create_redirect('gitlab-org') }

        it 'deletes the conflicting redirects' do
          route.update_attributes(path: 'bar')

          expect(RedirectRoute.exists?(path: 'bar/test')).to be_falsey
          expect(RedirectRoute.exists?(path: 'bar/test/foo')).to be_falsey
          expect(RedirectRoute.exists?(path: 'gitlab-org')).to be_truthy
        end
      end
102 103 104
    end

    context 'name update' do
105
      it 'updates children routes with new path' do
106
        route.update_attributes(name: 'bar')
107

108 109 110 111 112
        expect(described_class.exists?(name: 'bar')).to be_truthy
        expect(described_class.exists?(name: 'bar / test')).to be_truthy
        expect(described_class.exists?(name: 'bar / test / foo')).to be_truthy
        expect(described_class.exists?(name: 'gitlab-org')).to be_truthy
      end
113 114 115 116 117 118 119 120

      it 'handles a rename from nil' do
        # Note: using `update_columns` to skip all validation and callbacks
        route.update_columns(name: nil)

        expect { route.update_attributes(name: 'bar') }
          .to change { route.name }.from(nil).to('bar')
      end
121 122
    end
  end
123

124
  describe '#create_redirect_for_old_path' do
125 126
    context 'if the path changed' do
      it 'creates a RedirectRoute for the old path' do
127 128
        redirect_scope = route.source.redirect_routes.where(path: 'git_lab')
        expect(redirect_scope.exists?).to be_falsey
129 130
        route.path = 'new-path'
        route.save!
131
        expect(redirect_scope.exists?).to be_truthy
132 133
      end
    end
134
  end
135

136 137 138 139 140 141 142 143 144 145 146 147
  describe '#create_redirect' do
    it 'creates a RedirectRoute with the same source' do
      redirect_route = route.create_redirect('foo')
      expect(redirect_route).to be_a(RedirectRoute)
      expect(redirect_route).to be_persisted
      expect(redirect_route.source).to eq(route.source)
      expect(redirect_route.path).to eq('foo')
    end
  end

  describe '#delete_conflicting_redirects' do
    context 'when a redirect route with the same path exists' do
148 149
      context 'when the redirect route has matching case' do
        let!(:redirect1) { route.create_redirect(route.path) }
150

151 152 153
        it 'deletes the redirect' do
          expect do
            route.delete_conflicting_redirects
Michael Kozono committed
154
          end.to change { RedirectRoute.count }.by(-1)
155 156 157 158 159 160 161 162 163 164 165
        end

        context 'when redirect routes with paths descending from the route path exists' do
          let!(:redirect2) { route.create_redirect("#{route.path}/foo") }
          let!(:redirect3) { route.create_redirect("#{route.path}/foo/bar") }
          let!(:redirect4) { route.create_redirect("#{route.path}/baz/quz") }
          let!(:other_redirect) { route.create_redirect("other") }

          it 'deletes all redirects with paths that descend from the route path' do
            expect do
              route.delete_conflicting_redirects
Michael Kozono committed
166
            end.to change { RedirectRoute.count }.by(-4)
167 168
          end
        end
169 170
      end

171 172
      context 'when the redirect route is differently cased' do
        let!(:redirect1) { route.create_redirect(route.path.upcase) }
173

174 175 176
        it 'deletes the redirect' do
          expect do
            route.delete_conflicting_redirects
Michael Kozono committed
177
          end.to change { RedirectRoute.count }.by(-1)
178 179 180 181 182 183
        end
      end
    end
  end

  describe '#conflicting_redirects' do
184 185 186 187
    it 'returns an ActiveRecord::Relation' do
      expect(route.conflicting_redirects).to be_an(ActiveRecord::Relation)
    end

188
    context 'when a redirect route with the same path exists' do
189 190
      context 'when the redirect route has matching case' do
        let!(:redirect1) { route.create_redirect(route.path) }
191

192 193 194 195 196 197 198 199 200 201 202 203 204 205
        it 'returns the redirect route' do
          expect(route.conflicting_redirects).to match_array([redirect1])
        end

        context 'when redirect routes with paths descending from the route path exists' do
          let!(:redirect2) { route.create_redirect("#{route.path}/foo") }
          let!(:redirect3) { route.create_redirect("#{route.path}/foo/bar") }
          let!(:redirect4) { route.create_redirect("#{route.path}/baz/quz") }
          let!(:other_redirect) { route.create_redirect("other") }

          it 'returns the redirect routes' do
            expect(route.conflicting_redirects).to match_array([redirect1, redirect2, redirect3, redirect4])
          end
        end
206 207
      end

208 209
      context 'when the redirect route is differently cased' do
        let!(:redirect1) { route.create_redirect(route.path.upcase) }
210

211 212
        it 'returns the redirect route' do
          expect(route.conflicting_redirects).to match_array([redirect1])
213
        end
214 215 216
      end
    end
  end
217
end