summaryrefslogtreecommitdiff
path: root/spec/lib/constraints/group_url_constrainer_spec.rb
blob: db680489a8daa8b6a2cf7d7be865cc7760f908ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
require 'spec_helper'

describe GroupUrlConstrainer, lib: true do
  let!(:group) { create(:group, path: 'gitlab') }

  describe '#matches?' do
    context 'valid request' do
      let(:request) { build_request(group.path) }

      it { expect(subject.matches?(request)).to be_truthy }
    end

    context 'valid request for nested group' do
      let!(:nested_group) { create(:group, path: 'nested', parent: group) }
      let!(:request) { build_request('gitlab/nested') }

      it { expect(subject.matches?(request)).to be_truthy }
    end

    context 'valid request for nested group with reserved top level name' do
      let!(:nested_group) { create(:group, path: 'api', parent: group) }
      let!(:request) { build_request('gitlab/api') }

      it { expect(subject.matches?(request)).to be_truthy }
    end

    context 'invalid request' do
      let(:request) { build_request('foo') }

      it { expect(subject.matches?(request)).to be_falsey }
    end

    context 'when the request matches a redirect route' do
      context 'for a root group' do
        let!(:redirect_route) { group.redirect_routes.create!(path: 'gitlabb') }

        context 'and is a GET request' do
          let(:request) { build_request(redirect_route.path) }

          it { expect(subject.matches?(request)).to be_truthy }
        end

        context 'and is NOT a GET request' do
          let(:request) { build_request(redirect_route.path, 'POST') }

          it { expect(subject.matches?(request)).to be_falsey }
        end
      end

      context 'for a nested group' do
        let!(:nested_group) { create(:group, path: 'nested', parent: group) }
        let!(:redirect_route) { nested_group.redirect_routes.create!(path: 'gitlabb/nested') }
        let(:request) { build_request(redirect_route.path) }

        it { expect(subject.matches?(request)).to be_truthy }
      end
    end
  end

  def build_request(path, method = 'GET')
    double(:request,
      'get?': (method == 'GET'),
      params: { id: path })
  end
end