summaryrefslogtreecommitdiff
path: root/spec/lib/constraints/user_url_constrainer_spec.rb
blob: ed69b8309790dee8a51d822b68b281cd422947cb (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
require 'spec_helper'

describe UserUrlConstrainer, lib: true do
  let!(:user) { create(:user, username: 'dz') }

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

      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
      let(:old_project_path) { 'old_project_path' }
      let!(:redirect_route) { user.namespace.redirect_routes.create!(path: 'foo') }

      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
  end

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