You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

authentication_test.rb 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2016 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require File.expand_path('../../../test_helper', __FILE__)
  18. class Redmine::ApiTest::AuthenticationTest < Redmine::ApiTest::Base
  19. fixtures :users
  20. def test_api_should_deny_without_credentials
  21. get '/users/current.xml', {}
  22. assert_response 401
  23. assert_equal User.anonymous, User.current
  24. assert response.headers.has_key?('WWW-Authenticate')
  25. end
  26. def test_api_should_accept_http_basic_auth_using_username_and_password
  27. user = User.generate! do |user|
  28. user.password = 'my_password'
  29. end
  30. get '/users/current.xml', {}, credentials(user.login, 'my_password')
  31. assert_response 200
  32. assert_equal user, User.current
  33. end
  34. def test_api_should_deny_http_basic_auth_using_username_and_wrong_password
  35. user = User.generate! do |user|
  36. user.password = 'my_password'
  37. end
  38. get '/users/current.xml', {}, credentials(user.login, 'wrong_password')
  39. assert_response 401
  40. assert_equal User.anonymous, User.current
  41. end
  42. def test_api_should_accept_http_basic_auth_using_api_key
  43. user = User.generate!
  44. token = Token.create!(:user => user, :action => 'api')
  45. get '/users/current.xml', {}, credentials(token.value, 'X')
  46. assert_response 200
  47. assert_equal user, User.current
  48. end
  49. def test_api_should_deny_http_basic_auth_using_wrong_api_key
  50. user = User.generate!
  51. token = Token.create!(:user => user, :action => 'feeds') # not the API key
  52. get '/users/current.xml', {}, credentials(token.value, 'X')
  53. assert_response 401
  54. assert_equal User.anonymous, User.current
  55. end
  56. def test_api_should_accept_auth_using_api_key_as_parameter
  57. user = User.generate!
  58. token = Token.create!(:user => user, :action => 'api')
  59. get "/users/current.xml?key=#{token.value}", {}
  60. assert_response 200
  61. assert_equal user, User.current
  62. end
  63. def test_api_should_deny_auth_using_wrong_api_key_as_parameter
  64. user = User.generate!
  65. token = Token.create!(:user => user, :action => 'feeds') # not the API key
  66. get "/users/current.xml?key=#{token.value}", {}
  67. assert_response 401
  68. assert_equal User.anonymous, User.current
  69. end
  70. def test_api_should_accept_auth_using_api_key_as_request_header
  71. user = User.generate!
  72. token = Token.create!(:user => user, :action => 'api')
  73. get "/users/current.xml", {}, {'X-Redmine-API-Key' => token.value.to_s}
  74. assert_response 200
  75. assert_equal user, User.current
  76. end
  77. def test_api_should_deny_auth_using_wrong_api_key_as_request_header
  78. user = User.generate!
  79. token = Token.create!(:user => user, :action => 'feeds') # not the API key
  80. get "/users/current.xml", {}, {'X-Redmine-API-Key' => token.value.to_s}
  81. assert_response 401
  82. assert_equal User.anonymous, User.current
  83. end
  84. def test_api_should_trigger_basic_http_auth_with_basic_authorization_header
  85. ApplicationController.any_instance.expects(:authenticate_with_http_basic).once
  86. get '/users/current.xml', {}, credentials('jsmith')
  87. assert_response 401
  88. end
  89. def test_api_should_not_trigger_basic_http_auth_with_non_basic_authorization_header
  90. ApplicationController.any_instance.expects(:authenticate_with_http_basic).never
  91. get '/users/current.xml', {}, 'HTTP_AUTHORIZATION' => 'Digest foo bar'
  92. assert_response 401
  93. end
  94. def test_invalid_utf8_credentials_should_not_trigger_an_error
  95. invalid_utf8 = "\x82".force_encoding('UTF-8')
  96. assert !invalid_utf8.valid_encoding?
  97. assert_nothing_raised do
  98. get '/users/current.xml', {}, credentials(invalid_utf8, "foo")
  99. end
  100. end
  101. def test_api_request_should_not_use_user_session
  102. log_user('jsmith', 'jsmith')
  103. get '/users/current'
  104. assert_response :success
  105. get '/users/current.json'
  106. assert_response 401
  107. end
  108. def test_api_should_accept_switch_user_header_for_admin_user
  109. user = User.find(1)
  110. su = User.find(4)
  111. get '/users/current', {}, {'X-Redmine-API-Key' => user.api_key, 'X-Redmine-Switch-User' => su.login}
  112. assert_response :success
  113. assert_equal su, assigns(:user)
  114. assert_equal su, User.current
  115. end
  116. def test_api_should_respond_with_412_when_trying_to_switch_to_a_invalid_user
  117. get '/users/current', {}, {'X-Redmine-API-Key' => User.find(1).api_key, 'X-Redmine-Switch-User' => 'foobar'}
  118. assert_response 412
  119. end
  120. def test_api_should_respond_with_412_when_trying_to_switch_to_a_locked_user
  121. user = User.find(5)
  122. assert user.locked?
  123. get '/users/current', {}, {'X-Redmine-API-Key' => User.find(1).api_key, 'X-Redmine-Switch-User' => user.login}
  124. assert_response 412
  125. end
  126. def test_api_should_not_accept_switch_user_header_for_non_admin_user
  127. user = User.find(2)
  128. su = User.find(4)
  129. get '/users/current', {}, {'X-Redmine-API-Key' => user.api_key, 'X-Redmine-Switch-User' => su.login}
  130. assert_response :success
  131. assert_equal user, assigns(:user)
  132. assert_equal user, User.current
  133. end
  134. end