PageRenderTime 123ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/activeresource/test/cases/authorization_test.rb

https://github.com/awscloudtest/rails
Ruby | 244 lines | 194 code | 48 blank | 2 comment | 0 complexity | 099261328ee94785971083e44b2c1f9a MD5 | raw file
  1. require 'abstract_unit'
  2. class AuthorizationTest < Test::Unit::TestCase
  3. Response = Struct.new(:code)
  4. def setup
  5. @conn = ActiveResource::Connection.new('http://localhost')
  6. @matz = { :id => 1, :name => 'Matz' }.to_xml(:root => 'person')
  7. @david = { :id => 2, :name => 'David' }.to_xml(:root => 'person')
  8. @authenticated_conn = ActiveResource::Connection.new("http://david:test123@localhost")
  9. @basic_authorization_request_header = { 'Authorization' => 'Basic ZGF2aWQ6dGVzdDEyMw==' }
  10. @nonce = "MTI0OTUxMzc4NzpjYWI3NDM3NDNmY2JmODU4ZjQ2ZjcwNGZkMTJiMjE0NA=="
  11. ActiveResource::HttpMock.respond_to do |mock|
  12. mock.get "/people/2.xml", @basic_authorization_request_header, @david
  13. mock.get "/people/1.xml", @basic_authorization_request_header, nil, 401, { 'WWW-Authenticate' => 'i_should_be_ignored' }
  14. mock.put "/people/2.xml", @basic_authorization_request_header, nil, 204
  15. mock.delete "/people/2.xml", @basic_authorization_request_header, nil, 200
  16. mock.post "/people/2/addresses.xml", @basic_authorization_request_header, nil, 201, 'Location' => '/people/1/addresses/5'
  17. mock.head "/people/2.xml", @basic_authorization_request_header, nil, 200
  18. mock.get "/people/2.xml", { 'Authorization' => blank_digest_auth_header("/people/2.xml", "a10c9bd131c9d4d7755b8f4706fd04af") }, nil, 401, { 'WWW-Authenticate' => response_digest_auth_header }
  19. mock.get "/people/2.xml", { 'Authorization' => request_digest_auth_header("/people/2.xml", "912c7a643f18cda562b8d9662c47b6f5") }, @david, 200
  20. mock.get "/people/1.xml", { 'Authorization' => request_digest_auth_header("/people/1.xml", "d76e675c0ecfa2bb1abe01491b068a06") }, @matz, 200
  21. mock.put "/people/2.xml", { 'Authorization' => blank_digest_auth_header("/people/2.xml", "7de8a265a5be3c4c2d3a246562ecd6bd") }, nil, 401, { 'WWW-Authenticate' => response_digest_auth_header }
  22. mock.put "/people/2.xml", { 'Authorization' => request_digest_auth_header("/people/2.xml", "3fb3b33d9d0b869cc75815aa11faacd9") }, nil, 204
  23. mock.delete "/people/2.xml", { 'Authorization' => blank_digest_auth_header("/people/2.xml", "07dfc32769a34ea3510d3a77d64ca495") }, nil, 401, { 'WWW-Authenticate' => response_digest_auth_header }
  24. mock.delete "/people/2.xml", { 'Authorization' => request_digest_auth_header("/people/2.xml", "5d438610de7ec163b29096c9afcbb254") }, nil, 200
  25. mock.post "/people/2/addresses.xml", { 'Authorization' => blank_digest_auth_header("/people/2/addresses.xml", "966dab13620421f928d051f2b9d7b9af") }, nil, 401, { 'WWW-Authenticate' => response_digest_auth_header }
  26. mock.post "/people/2/addresses.xml", { 'Authorization' => request_digest_auth_header("/people/2/addresses.xml", "ed540d032c63f8ee34959116c090ec45") }, nil, 201, 'Location' => '/people/1/addresses/5'
  27. mock.head "/people/2.xml", { 'Authorization' => blank_digest_auth_header("/people/2.xml", "2854eeb92cce2aed29350ea0ce7ba1e2") }, nil, 401, { 'WWW-Authenticate' => response_digest_auth_header }
  28. mock.head "/people/2.xml", { 'Authorization' => request_digest_auth_header("/people/2.xml", "07cd4d247e9c130f92ba2501a080b328") }, nil, 200
  29. end
  30. # Make client nonce deterministic
  31. class << @authenticated_conn
  32. private
  33. def client_nonce
  34. 'i-am-a-client-nonce'
  35. end
  36. end
  37. end
  38. def test_authorization_header
  39. authorization_header = @authenticated_conn.__send__(:authorization_header, :get, URI.parse('/people/2.xml'))
  40. assert_equal @basic_authorization_request_header['Authorization'], authorization_header['Authorization']
  41. authorization = authorization_header["Authorization"].to_s.split
  42. assert_equal "Basic", authorization[0]
  43. assert_equal ["david", "test123"], ActiveSupport::Base64.decode64(authorization[1]).split(":")[0..1]
  44. end
  45. def test_authorization_header_with_username_but_no_password
  46. @conn = ActiveResource::Connection.new("http://david:@localhost")
  47. authorization_header = @conn.__send__(:authorization_header, :get, URI.parse('/people/2.xml'))
  48. authorization = authorization_header["Authorization"].to_s.split
  49. assert_equal "Basic", authorization[0]
  50. assert_equal ["david"], ActiveSupport::Base64.decode64(authorization[1]).split(":")[0..1]
  51. end
  52. def test_authorization_header_with_password_but_no_username
  53. @conn = ActiveResource::Connection.new("http://:test123@localhost")
  54. authorization_header = @conn.__send__(:authorization_header, :get, URI.parse('/people/2.xml'))
  55. authorization = authorization_header["Authorization"].to_s.split
  56. assert_equal "Basic", authorization[0]
  57. assert_equal ["", "test123"], ActiveSupport::Base64.decode64(authorization[1]).split(":")[0..1]
  58. end
  59. def test_authorization_header_with_decoded_credentials_from_url
  60. @conn = ActiveResource::Connection.new("http://my%40email.com:%31%32%33@localhost")
  61. authorization_header = @conn.__send__(:authorization_header, :get, URI.parse('/people/2.xml'))
  62. authorization = authorization_header["Authorization"].to_s.split
  63. assert_equal "Basic", authorization[0]
  64. assert_equal ["my@email.com", "123"], ActiveSupport::Base64.decode64(authorization[1]).split(":")[0..1]
  65. end
  66. def test_authorization_header_explicitly_setting_username_and_password
  67. @authenticated_conn = ActiveResource::Connection.new("http://@localhost")
  68. @authenticated_conn.user = 'david'
  69. @authenticated_conn.password = 'test123'
  70. authorization_header = @authenticated_conn.__send__(:authorization_header, :get, URI.parse('/people/2.xml'))
  71. assert_equal @basic_authorization_request_header['Authorization'], authorization_header['Authorization']
  72. authorization = authorization_header["Authorization"].to_s.split
  73. assert_equal "Basic", authorization[0]
  74. assert_equal ["david", "test123"], ActiveSupport::Base64.decode64(authorization[1]).split(":")[0..1]
  75. end
  76. def test_authorization_header_explicitly_setting_username_but_no_password
  77. @conn = ActiveResource::Connection.new("http://@localhost")
  78. @conn.user = "david"
  79. authorization_header = @conn.__send__(:authorization_header, :get, URI.parse('/people/2.xml'))
  80. authorization = authorization_header["Authorization"].to_s.split
  81. assert_equal "Basic", authorization[0]
  82. assert_equal ["david"], ActiveSupport::Base64.decode64(authorization[1]).split(":")[0..1]
  83. end
  84. def test_authorization_header_explicitly_setting_password_but_no_username
  85. @conn = ActiveResource::Connection.new("http://@localhost")
  86. @conn.password = "test123"
  87. authorization_header = @conn.__send__(:authorization_header, :get, URI.parse('/people/2.xml'))
  88. authorization = authorization_header["Authorization"].to_s.split
  89. assert_equal "Basic", authorization[0]
  90. assert_equal ["", "test123"], ActiveSupport::Base64.decode64(authorization[1]).split(":")[0..1]
  91. end
  92. def test_authorization_header_if_credentials_supplied_and_auth_type_is_basic
  93. @authenticated_conn.auth_type = :basic
  94. authorization_header = @authenticated_conn.__send__(:authorization_header, :get, URI.parse('/people/2.xml'))
  95. assert_equal @basic_authorization_request_header['Authorization'], authorization_header['Authorization']
  96. authorization = authorization_header["Authorization"].to_s.split
  97. assert_equal "Basic", authorization[0]
  98. assert_equal ["david", "test123"], ActiveSupport::Base64.decode64(authorization[1]).split(":")[0..1]
  99. end
  100. def test_authorization_header_if_credentials_supplied_and_auth_type_is_digest
  101. @authenticated_conn.auth_type = :digest
  102. authorization_header = @authenticated_conn.__send__(:authorization_header, :get, URI.parse('/people/2.xml'))
  103. assert_equal blank_digest_auth_header("/people/2.xml", "a10c9bd131c9d4d7755b8f4706fd04af"), authorization_header['Authorization']
  104. end
  105. def test_get
  106. david = @authenticated_conn.get("/people/2.xml")
  107. assert_equal "David", david["name"]
  108. end
  109. def test_post
  110. response = @authenticated_conn.post("/people/2/addresses.xml")
  111. assert_equal "/people/1/addresses/5", response["Location"]
  112. end
  113. def test_put
  114. response = @authenticated_conn.put("/people/2.xml")
  115. assert_equal 204, response.code
  116. end
  117. def test_delete
  118. response = @authenticated_conn.delete("/people/2.xml")
  119. assert_equal 200, response.code
  120. end
  121. def test_head
  122. response = @authenticated_conn.head("/people/2.xml")
  123. assert_equal 200, response.code
  124. end
  125. def test_get_with_digest_auth_handles_initial_401_response_and_retries
  126. @authenticated_conn.auth_type = :digest
  127. response = @authenticated_conn.get("/people/2.xml")
  128. assert_equal "David", response["name"]
  129. end
  130. def test_post_with_digest_auth_handles_initial_401_response_and_retries
  131. @authenticated_conn.auth_type = :digest
  132. response = @authenticated_conn.post("/people/2/addresses.xml")
  133. assert_equal "/people/1/addresses/5", response["Location"]
  134. assert_equal 201, response.code
  135. end
  136. def test_put_with_digest_auth_handles_initial_401_response_and_retries
  137. @authenticated_conn.auth_type = :digest
  138. response = @authenticated_conn.put("/people/2.xml")
  139. assert_equal 204, response.code
  140. end
  141. def test_delete_with_digest_auth_handles_initial_401_response_and_retries
  142. @authenticated_conn.auth_type = :digest
  143. response = @authenticated_conn.delete("/people/2.xml")
  144. assert_equal 200, response.code
  145. end
  146. def test_head_with_digest_auth_handles_initial_401_response_and_retries
  147. @authenticated_conn.auth_type = :digest
  148. response = @authenticated_conn.head("/people/2.xml")
  149. assert_equal 200, response.code
  150. end
  151. def test_get_with_digest_auth_caches_nonce
  152. @authenticated_conn.auth_type = :digest
  153. response = @authenticated_conn.get("/people/2.xml")
  154. assert_equal "David", response["name"]
  155. # There is no mock for this request with a non-cached nonce.
  156. response = @authenticated_conn.get("/people/1.xml")
  157. assert_equal "Matz", response["name"]
  158. end
  159. def test_retry_on_401_only_happens_with_digest_auth
  160. assert_raise(ActiveResource::UnauthorizedAccess) { @authenticated_conn.get("/people/1.xml") }
  161. assert_equal "", @authenticated_conn.send(:response_auth_header)
  162. end
  163. def test_raises_invalid_request_on_unauthorized_requests
  164. assert_raise(ActiveResource::InvalidRequestError) { @conn.get("/people/2.xml") }
  165. assert_raise(ActiveResource::InvalidRequestError) { @conn.post("/people/2/addresses.xml") }
  166. assert_raise(ActiveResource::InvalidRequestError) { @conn.put("/people/2.xml") }
  167. assert_raise(ActiveResource::InvalidRequestError) { @conn.delete("/people/2.xml") }
  168. assert_raise(ActiveResource::InvalidRequestError) { @conn.head("/people/2.xml") }
  169. end
  170. def test_raises_invalid_request_on_unauthorized_requests_with_digest_auth
  171. @conn.auth_type = :digest
  172. assert_raise(ActiveResource::InvalidRequestError) { @conn.get("/people/2.xml") }
  173. assert_raise(ActiveResource::InvalidRequestError) { @conn.post("/people/2/addresses.xml") }
  174. assert_raise(ActiveResource::InvalidRequestError) { @conn.put("/people/2.xml") }
  175. assert_raise(ActiveResource::InvalidRequestError) { @conn.delete("/people/2.xml") }
  176. assert_raise(ActiveResource::InvalidRequestError) { @conn.head("/people/2.xml") }
  177. end
  178. def test_client_nonce_is_not_nil
  179. assert_not_nil ActiveResource::Connection.new("http://david:test123@localhost").send(:client_nonce)
  180. end
  181. protected
  182. def assert_response_raises(klass, code)
  183. assert_raise(klass, "Expected response code #{code} to raise #{klass}") do
  184. @conn.__send__(:handle_response, Response.new(code))
  185. end
  186. end
  187. def blank_digest_auth_header(uri, response)
  188. %Q(Digest username="david", realm="", qop="", uri="#{uri}", nonce="", nc="0", cnonce="i-am-a-client-nonce", opaque="", response="#{response}")
  189. end
  190. def request_digest_auth_header(uri, response)
  191. %Q(Digest username="david", realm="RailsTestApp", qop="auth", uri="#{uri}", nonce="#{@nonce}", nc="0", cnonce="i-am-a-client-nonce", opaque="ef6dfb078ba22298d366f99567814ffb", response="#{response}")
  192. end
  193. def response_digest_auth_header
  194. %Q(Digest realm="RailsTestApp", qop="auth", algorithm=MD5, nonce="#{@nonce}", opaque="ef6dfb078ba22298d366f99567814ffb")
  195. end
  196. end