您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

account_controller_openid_test.rb 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 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 AccountControllerOpenidTest < Redmine::ControllerTest
  19. tests AccountController
  20. fixtures :users, :roles
  21. def setup
  22. User.current = nil
  23. Setting.openid = '1'
  24. end
  25. def teardown
  26. Setting.openid = '0'
  27. end
  28. if Object.const_defined?(:OpenID)
  29. def test_login_with_openid_for_existing_user
  30. Setting.self_registration = '3'
  31. existing_user = User.new(:firstname => 'Cool',
  32. :lastname => 'User',
  33. :mail => 'user@somedomain.com',
  34. :identity_url => 'http://openid.example.com/good_user')
  35. existing_user.login = 'cool_user'
  36. assert existing_user.save!
  37. post :login, :params => {
  38. :openid_url => existing_user.identity_url
  39. }
  40. assert_redirected_to '/my/page'
  41. end
  42. def test_login_with_invalid_openid_provider
  43. Setting.self_registration = '0'
  44. post :login, :params => {
  45. :openid_url => 'http;//openid.example.com/good_user'
  46. }
  47. assert_redirected_to home_url
  48. end
  49. def test_login_with_openid_for_existing_non_active_user
  50. Setting.self_registration = '2'
  51. existing_user = User.new(:firstname => 'Cool',
  52. :lastname => 'User',
  53. :mail => 'user@somedomain.com',
  54. :identity_url => 'http://openid.example.com/good_user',
  55. :status => User::STATUS_REGISTERED)
  56. existing_user.login = 'cool_user'
  57. assert existing_user.save!
  58. post :login, :params => {
  59. :openid_url => existing_user.identity_url
  60. }
  61. assert_redirected_to '/login'
  62. end
  63. def test_login_with_openid_with_new_user_created
  64. Setting.self_registration = '3'
  65. post :login, :params => {
  66. :openid_url => 'http://openid.example.com/good_user'
  67. }
  68. assert_redirected_to '/my/account'
  69. user = User.find_by_login('cool_user')
  70. assert user
  71. assert_equal 'Cool', user.firstname
  72. assert_equal 'User', user.lastname
  73. end
  74. def test_login_with_openid_with_new_user_and_self_registration_off
  75. Setting.self_registration = '0'
  76. post :login, :params => {
  77. :openid_url => 'http://openid.example.com/good_user'
  78. }
  79. assert_redirected_to home_url
  80. user = User.find_by_login('cool_user')
  81. assert_nil user
  82. end
  83. def test_login_with_openid_with_new_user_created_with_email_activation_should_have_a_token
  84. Setting.self_registration = '1'
  85. post :login, :params => {
  86. :openid_url => 'http://openid.example.com/good_user'
  87. }
  88. assert_redirected_to '/login'
  89. user = User.find_by_login('cool_user')
  90. assert user
  91. token = Token.find_by_user_id_and_action(user.id, 'register')
  92. assert token
  93. end
  94. def test_login_with_openid_with_new_user_created_with_manual_activation
  95. Setting.self_registration = '2'
  96. post :login, :params => {
  97. :openid_url => 'http://openid.example.com/good_user'
  98. }
  99. assert_redirected_to '/login'
  100. user = User.find_by_login('cool_user')
  101. assert user
  102. assert_equal User::STATUS_REGISTERED, user.status
  103. end
  104. def test_login_with_openid_with_new_user_with_conflict_should_register
  105. Setting.self_registration = '3'
  106. existing_user = User.new(:firstname => 'Cool', :lastname => 'User', :mail => 'user@somedomain.com')
  107. existing_user.login = 'cool_user'
  108. assert existing_user.save!
  109. post :login, :params => {
  110. :openid_url => 'http://openid.example.com/good_user'
  111. }
  112. assert_response :success
  113. assert_select 'input[name=?][value=?]', 'user[identity_url]', 'http://openid.example.com/good_user'
  114. end
  115. def test_login_with_openid_with_new_user_with_missing_information_should_register
  116. Setting.self_registration = '3'
  117. post :login, :params => {
  118. :openid_url => 'http://openid.example.com/good_blank_user'
  119. }
  120. assert_response :success
  121. assert_select 'input[name=?]', 'user[login]'
  122. assert_select 'input[name=?]', 'user[password]'
  123. assert_select 'input[name=?]', 'user[password_confirmation]'
  124. assert_select 'input[name=?][value=?]', 'user[identity_url]', 'http://openid.example.com/good_blank_user'
  125. end
  126. def test_post_login_should_not_verify_token_when_using_open_id
  127. ActionController::Base.allow_forgery_protection = true
  128. AccountController.any_instance.stubs(:using_open_id?).returns(true)
  129. AccountController.any_instance.stubs(:authenticate_with_open_id).returns(true)
  130. post :login
  131. assert_response 200
  132. ensure
  133. ActionController::Base.allow_forgery_protection = false
  134. end
  135. def test_register_after_login_failure_should_not_require_user_to_enter_a_password
  136. Setting.self_registration = '3'
  137. assert_difference 'User.count' do
  138. post :register, :params => {
  139. :user => {
  140. :login => 'good_blank_user',
  141. :password => '',
  142. :password_confirmation => '',
  143. :firstname => 'Cool',
  144. :lastname => 'User',
  145. :mail => 'user@somedomain.com',
  146. :identity_url => 'http://openid.example.com/good_blank_user'
  147. }
  148. }
  149. assert_response 302
  150. end
  151. user = User.order('id DESC').first
  152. assert_equal 'http://openid.example.com/good_blank_user', user.identity_url
  153. assert user.hashed_password.blank?, "Hashed password was #{user.hashed_password}"
  154. end
  155. def test_setting_openid_should_return_true_when_set_to_true
  156. assert_equal true, Setting.openid?
  157. end
  158. else
  159. puts "Skipping openid tests."
  160. def test_dummy
  161. end
  162. end
  163. end