|
| 1 | +require 'openid/extensions/oauth' |
| 2 | +require 'openid/message' |
| 3 | +require 'openid/server' |
| 4 | +require 'openid/consumer/responses' |
| 5 | +require 'openid/consumer/discovery' |
| 6 | + |
| 7 | +module OpenID |
| 8 | + module OAuthTest |
| 9 | + class OAuthRequestTestCase < Test::Unit::TestCase |
| 10 | + def setup |
| 11 | + @req = OAuth::Request.new |
| 12 | + end |
| 13 | + |
| 14 | + def test_construct |
| 15 | + assert_nil(@req.consumer) |
| 16 | + assert_nil(@req.scope) |
| 17 | + assert_equal('oauth', @req.ns_alias) |
| 18 | + |
| 19 | + req2 = OAuth::Request.new("CONSUMER","http://sample.com/some_scope") |
| 20 | + assert_equal("CONSUMER",req2.consumer) |
| 21 | + assert_equal("http://sample.com/some_scope",req2.scope) |
| 22 | + end |
| 23 | + |
| 24 | + def test_add_consumer |
| 25 | + @req.consumer="CONSUMER" |
| 26 | + assert_equal("CONSUMER",@req.consumer) |
| 27 | + end |
| 28 | + |
| 29 | + def test_add_scope |
| 30 | + @req.scope="http://sample.com/some_scope" |
| 31 | + assert_equal("http://sample.com/some_scope",@req.scope) |
| 32 | + end |
| 33 | + |
| 34 | + def test_get_extension_args |
| 35 | + assert_equal({}, @req.get_extension_args) |
| 36 | + @req.consumer="CONSUMER" |
| 37 | + assert_equal({'consumer' => 'CONSUMER'}, @req.get_extension_args) |
| 38 | + @req.scope="http://sample.com/some_scope" |
| 39 | + assert_equal({'consumer' => 'CONSUMER', 'scope' => 'http://sample.com/some_scope'}, @req.get_extension_args) |
| 40 | + end |
| 41 | + |
| 42 | + def test_parse_extension_args |
| 43 | + args = {'consumer' => 'CONSUMER', 'scope' => 'http://sample.com/some_scope'} |
| 44 | + @req.parse_extension_args(args) |
| 45 | + assert_equal("CONSUMER",@req.consumer) |
| 46 | + assert_equal("http://sample.com/some_scope",@req.scope) |
| 47 | + end |
| 48 | + |
| 49 | + def test_parse_extension_args_empty |
| 50 | + @req.parse_extension_args({}) |
| 51 | + assert_nil( @req.consumer ) |
| 52 | + assert_nil( @req.scope ) |
| 53 | + end |
| 54 | + |
| 55 | + def test_from_openid_request |
| 56 | + openid_req_msg = Message.from_openid_args({ |
| 57 | + 'mode' => 'checkid_setup', |
| 58 | + 'ns' => OPENID2_NS, |
| 59 | + 'ns.oauth' => OAuth::NS_URI, |
| 60 | + 'oauth.consumer' => 'CONSUMER', |
| 61 | + 'oauth.scope' => "http://sample.com/some_scope" |
| 62 | + }) |
| 63 | + oid_req = Server::OpenIDRequest.new |
| 64 | + oid_req.message = openid_req_msg |
| 65 | + req = OAuth::Request.from_openid_request(oid_req) |
| 66 | + assert_equal("CONSUMER",req.consumer) |
| 67 | + assert_equal("http://sample.com/some_scope",req.scope) |
| 68 | + end |
| 69 | + |
| 70 | + def test_from_openid_request_no_oauth |
| 71 | + message = Message.new |
| 72 | + openid_req = Server::OpenIDRequest.new |
| 73 | + openid_req.message = message |
| 74 | + oauth_req = OAuth::Request.from_openid_request(openid_req) |
| 75 | + assert(oauth_req.nil?) |
| 76 | + end |
| 77 | + |
| 78 | + end |
| 79 | + |
| 80 | + class DummySuccessResponse |
| 81 | + attr_accessor :message |
| 82 | + |
| 83 | + def initialize(message, signed_stuff) |
| 84 | + @message = message |
| 85 | + @signed_stuff = signed_stuff |
| 86 | + end |
| 87 | + |
| 88 | + def get_signed_ns(ns_uri) |
| 89 | + return @signed_stuff |
| 90 | + end |
| 91 | + |
| 92 | + end |
| 93 | + |
| 94 | + class OAuthResponseTestCase < Test::Unit::TestCase |
| 95 | + def setup |
| 96 | + @req = OAuth::Response.new |
| 97 | + end |
| 98 | + |
| 99 | + def test_construct |
| 100 | + assert_nil(@req.request_token) |
| 101 | + assert_nil(@req.scope) |
| 102 | + |
| 103 | + req2 = OAuth::Response.new("REQUESTTOKEN","http://sample.com/some_scope") |
| 104 | + assert_equal("REQUESTTOKEN",req2.request_token) |
| 105 | + assert_equal("http://sample.com/some_scope",req2.scope) |
| 106 | + end |
| 107 | + |
| 108 | + def test_add_request_token |
| 109 | + @req.request_token="REQUESTTOKEN" |
| 110 | + assert_equal("REQUESTTOKEN",@req.request_token) |
| 111 | + end |
| 112 | + |
| 113 | + def test_add_scope |
| 114 | + @req.scope="http://sample.com/some_scope" |
| 115 | + assert_equal("http://sample.com/some_scope",@req.scope) |
| 116 | + end |
| 117 | + |
| 118 | + def test_get_extension_args |
| 119 | + assert_equal({}, @req.get_extension_args) |
| 120 | + @req.request_token="REQUESTTOKEN" |
| 121 | + assert_equal({'request_token' => 'REQUESTTOKEN'}, @req.get_extension_args) |
| 122 | + @req.scope="http://sample.com/some_scope" |
| 123 | + assert_equal({'request_token' => 'REQUESTTOKEN', 'scope' => 'http://sample.com/some_scope'}, @req.get_extension_args) |
| 124 | + end |
| 125 | + |
| 126 | + def test_parse_extension_args |
| 127 | + args = {'request_token' => 'REQUESTTOKEN', 'scope' => 'http://sample.com/some_scope'} |
| 128 | + @req.parse_extension_args(args) |
| 129 | + assert_equal("REQUESTTOKEN",@req.request_token) |
| 130 | + assert_equal("http://sample.com/some_scope",@req.scope) |
| 131 | + end |
| 132 | + |
| 133 | + def test_parse_extension_args_empty |
| 134 | + @req.parse_extension_args({}) |
| 135 | + assert_nil( @req.request_token ) |
| 136 | + assert_nil( @req.scope ) |
| 137 | + end |
| 138 | + |
| 139 | + def test_from_success_response |
| 140 | + |
| 141 | + openid_req_msg = Message.from_openid_args({ |
| 142 | + 'mode' => 'id_res', |
| 143 | + 'ns' => OPENID2_NS, |
| 144 | + 'ns.oauth' => OAuth::NS_URI, |
| 145 | + 'ns.oauth' => OAuth::NS_URI, |
| 146 | + 'oauth.request_token' => 'REQUESTTOKEN', |
| 147 | + 'oauth.scope' => "http://sample.com/some_scope" |
| 148 | + }) |
| 149 | + signed_stuff = { |
| 150 | + 'request_token' => 'REQUESTTOKEN', |
| 151 | + 'scope' => "http://sample.com/some_scope" |
| 152 | + } |
| 153 | + oid_req = DummySuccessResponse.new(openid_req_msg, signed_stuff) |
| 154 | + req = OAuth::Response.from_success_response(oid_req) |
| 155 | + assert_equal("REQUESTTOKEN",req.request_token) |
| 156 | + assert_equal("http://sample.com/some_scope",req.scope) |
| 157 | + end |
| 158 | + |
| 159 | + def test_from_success_response_unsigned |
| 160 | + openid_req_msg = Message.from_openid_args({ |
| 161 | + 'mode' => 'id_res', |
| 162 | + 'ns' => OPENID2_NS, |
| 163 | + 'ns.oauth' => OAuth::NS_URI, |
| 164 | + 'oauth.request_token' => 'REQUESTTOKEN', |
| 165 | + 'oauth.scope' => "http://sample.com/some_scope" |
| 166 | + }) |
| 167 | + signed_stuff = {} |
| 168 | + endpoint = OpenIDServiceEndpoint.new |
| 169 | + oid_req = Consumer::SuccessResponse.new(endpoint, openid_req_msg, signed_stuff) |
| 170 | + req = OAuth::Response.from_success_response(oid_req) |
| 171 | + assert(req.nil?, req.inspect) |
| 172 | + end |
| 173 | + end |
| 174 | + end |
| 175 | +end |
0 commit comments