File: error_spec.rb

package info (click to toggle)
ruby-rack-oauth2 2.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 560 kB
  • sloc: ruby: 4,013; makefile: 4
file content (77 lines) | stat: -rw-r--r-- 2,766 bytes parent folder | download | duplicates (2)
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
require 'spec_helper.rb'

describe Rack::OAuth2::Server::Token::BadRequest do
  let(:error) { Rack::OAuth2::Server::Token::BadRequest.new(:invalid_request) }

  it { should be_a Rack::OAuth2::Server::Abstract::BadRequest }

  describe '#finish' do
    it 'should respond in JSON' do
      status, headers, response = error.finish
      status.should == 400
      headers['Content-Type'].should == 'application/json'
      response.should == ['{"error":"invalid_request"}']
    end
  end
end

describe Rack::OAuth2::Server::Token::Unauthorized do
  let(:error) { Rack::OAuth2::Server::Token::Unauthorized.new(:invalid_request) }

  it { should be_a Rack::OAuth2::Server::Abstract::Unauthorized }

  describe '#finish' do
    it 'should respond in JSON' do
      status, headers, response = error.finish
      status.should == 401
      headers['Content-Type'].should == 'application/json'
      headers['WWW-Authenticate'].should == 'Basic realm="OAuth2 Token Endpoint"'
      response.should == ['{"error":"invalid_request"}']
    end
  end
end

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

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

  describe 'unauthorized!' do
    it do
      expect { request.unauthorized! :invalid_client }.to raise_error unauthorized
    end
  end

  Rack::OAuth2::Server::Token::ErrorMethods::DEFAULT_DESCRIPTION.keys.each do |error_code|
    method = "#{error_code}!"
    case error_code
    when :invalid_client
      describe method do
        it "should raise Rack::OAuth2::Server::Token::Unauthorized with error = :#{error_code}" do
          expect { request.send method }.to raise_error(unauthorized) { |error|
            error.error.should       == error_code
            error.description.should == default_description[error_code]
          }
        end
      end
    else
      describe method do
        it "should raise Rack::OAuth2::Server::Token::BadRequest with error = :#{error_code}" do
          expect { request.send method }.to raise_error(bad_request) { |error|
            error.error.should       == error_code
            error.description.should == default_description[error_code]
          }
        end
      end
    end
  end
end