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

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