File: failure_endpoint_spec.rb

package info (click to toggle)
ruby-omniauth 2.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 400 kB
  • sloc: ruby: 2,483; makefile: 7
file content (69 lines) | stat: -rw-r--r-- 2,361 bytes parent folder | download | duplicates (3)
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
66
67
68
69
require 'helper'

describe OmniAuth::FailureEndpoint do
  subject { OmniAuth::FailureEndpoint }

  context 'raise-out environment' do
    before do
      @rack_env = ENV['RACK_ENV']
      ENV['RACK_ENV'] = 'test'

      @default = OmniAuth.config.failure_raise_out_environments
      OmniAuth.config.failure_raise_out_environments = ['test']
    end

    it 'raises out the error' do
      expect do
        subject.call('omniauth.error' => StandardError.new('Blah'))
      end.to raise_error(StandardError, 'Blah')
    end

    it 'raises out an OmniAuth::Error if no omniauth.error is set' do
      expect { subject.call('omniauth.error.type' => 'example') }.to raise_error(OmniAuth::Error, 'example')
    end

    after do
      ENV['RACK_ENV'] = @rack_env
      OmniAuth.config.failure_raise_out_environments = @default
    end
  end

  context 'non-raise-out environment' do
    let(:env) do
      {'omniauth.error.type' => 'invalid_request', 'omniauth.error.strategy' => ExampleStrategy.new({})}
    end

    it 'is a redirect' do
      status, = *subject.call(env)
      expect(status).to eq(302)
    end

    it 'includes the SCRIPT_NAME' do
      _, head, = *subject.call(env.merge('SCRIPT_NAME' => '/random'))
      expect(head['Location']).to eq('/random/auth/failure?message=invalid_request&strategy=test')
    end

    it 'respects the globally configured path prefix' do
      allow(OmniAuth.config).to receive(:path_prefix).and_return('/boo')
      _, head, = *subject.call(env)
      expect(head['Location']).to eq('/boo/failure?message=invalid_request&strategy=test')
    end

    it 'respects the custom path prefix configured on the strategy' do
      env['omniauth.error.strategy'] = ExampleStrategy.new({}, path_prefix: "/some/custom/path")
      _, head, = *subject.call(env)
      expect(head['Location']).to eq('/some/custom/path/failure?message=invalid_request&strategy=test')
    end

    it 'includes the origin (escaped) if one is provided' do
      env['omniauth.origin'] = '/origin-example'
      _, head, = *subject.call(env)
      expect(head['Location']).to be_include('&origin=%2Forigin-example')
    end

    it 'escapes the message key' do
      _, head = *subject.call(env.merge('omniauth.error.type' => 'Connection refused!'))
      expect(head['Location']).to be_include('message=Connection+refused%21')
    end
  end
end