File: error_spec.rb

package info (click to toggle)
ruby-rack-oauth2 2.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 564 kB
  • sloc: ruby: 4,038; makefile: 4
file content (112 lines) | stat: -rw-r--r-- 3,877 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
70
71
72
73
74
75
76
77
78
79
80
81
82
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
require 'spec_helper.rb'

describe Rack::OAuth2::Server::Authorize::BadRequest do
  let(:klass)        { Rack::OAuth2::Server::Authorize::BadRequest }
  let(:error)        { klass.new(:invalid_request) }
  let(:redirect_uri) { 'http://client.example.com/callback' }

  subject { error }
  it { should be_a Rack::OAuth2::Server::Abstract::BadRequest }
  its(:protocol_params) do
    should == {
      error:             :invalid_request,
      error_description: nil,
      error_uri:         nil,
      state:             nil
    }
  end

  describe '#finish' do
    context 'when redirect_uri is given' do
      before { error.redirect_uri = redirect_uri }

      context 'when protocol_params_location = :query' do
        before { error.protocol_params_location = :query }
        it 'should redirect with error in query' do
          state, headers, response = error.finish
          state.should == 302
          headers["Location"].should == "#{redirect_uri}?error=invalid_request"
        end
      end

      context 'when protocol_params_location = :fragment' do
        before { error.protocol_params_location = :fragment }
        it 'should redirect with error in fragment' do
          state, headers, response = error.finish
          state.should == 302
          headers["Location"].should == "#{redirect_uri}#error=invalid_request"
        end
      end

      context 'otherwise' do
        before { error.protocol_params_location = :other }
        it 'should redirect without error' do
          state, headers, response = error.finish
          state.should == 302
          headers["Location"].should == redirect_uri
        end
      end
    end

    context 'otherwise' do
      it 'should raise itself' do
        expect { error.finish }.to raise_error(klass) { |e|
          e.should == error
        }
      end
    end
  end
end

describe Rack::OAuth2::Server::Authorize::ErrorMethods do
  let(:klass)               { Rack::OAuth2::Server::Authorize::BadRequest }
  let(:redirect_uri)        { 'http://client.example.com/callback' }
  let(:default_description) { Rack::OAuth2::Server::Authorize::ErrorMethods::DEFAULT_DESCRIPTION }
  let(:env)                 { Rack::MockRequest.env_for("/authorize?client_id=client_id") }
  let(:request)             { Rack::OAuth2::Server::Authorize::Request.new env }
  let(:request_for_code)    { Rack::OAuth2::Server::Authorize::Code::Request.new env }
  let(:request_for_token)   { Rack::OAuth2::Server::Authorize::Token::Request.new env }

  describe 'bad_request!' do
    it do
      expect { request.bad_request! }.to raise_error klass
    end

    context 'when response_type = :code' do
      it 'should set protocol_params_location = :query' do
        expect { request_for_code.bad_request! }.to raise_error(klass) { |e|
          e.protocol_params_location.should == :query
        }
      end
    end

    context 'when response_type = :token' do
      it 'should set protocol_params_location = :fragment' do
        expect { request_for_token.bad_request! }.to raise_error(klass) { |e|
          e.protocol_params_location.should == :fragment
        }
      end
    end
  end

  Rack::OAuth2::Server::Authorize::ErrorMethods::DEFAULT_DESCRIPTION.keys.each do |error_code|
    method = "#{error_code}!"
    klass = case error_code
    when :server_error
      Rack::OAuth2::Server::Authorize::ServerError
    when :temporarily_unavailable
      Rack::OAuth2::Server::Authorize::TemporarilyUnavailable
    else
      Rack::OAuth2::Server::Authorize::BadRequest
    end
    describe method do
      it "should raise #{klass} with error = :#{error_code}" do
        klass =
        expect { request.send method }.to raise_error(klass) { |error|
          error.error.should       == error_code
          error.description.should == default_description[error_code]
        }
      end
    end
  end
end