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.

account_controller_openid_test.rb 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 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, :openid_url => existing_user.identity_url
  38. assert_redirected_to '/my/page'
  39. end
  40. def test_login_with_invalid_openid_provider
  41. Setting.self_registration = '0'
  42. post :login, :openid_url => 'http;//openid.example.com/good_user'
  43. assert_redirected_to home_url
  44. end
  45. def test_login_with_openid_for_existing_non_active_user
  46. Setting.self_registration = '2'
  47. existing_user = User.new(:firstname => 'Cool',
  48. :lastname => 'User',
  49. :mail => 'user@somedomain.com',
  50. :identity_url => 'http://openid.example.com/good_user',
  51. :status => User::STATUS_REGISTERED)
  52. existing_user.login = 'cool_user'
  53. assert existing_user.save!
  54. post :login, :openid_url => existing_user.identity_url
  55. assert_redirected_to '/login'
  56. end
  57. def test_login_with_openid_with_new_user_created
  58. Setting.self_registration = '3'
  59. post :login, :openid_url => 'http://openid.example.com/good_user'
  60. assert_redirected_to '/my/account'
  61. user = User.find_by_login('cool_user')
  62. assert user
  63. assert_equal 'Cool', user.firstname
  64. assert_equal 'User', user.lastname
  65. end
  66. def test_login_with_openid_with_new_user_and_self_registration_off
  67. Setting.self_registration = '0'
  68. post :login, :openid_url => 'http://openid.example.com/good_user'
  69. assert_redirected_to home_url
  70. user = User.find_by_login('cool_user')
  71. assert_nil user
  72. end
  73. def test_login_with_openid_with_new_user_created_with_email_activation_should_have_a_token
  74. Setting.self_registration = '1'
  75. post :login, :openid_url => 'http://openid.example.com/good_user'
  76. assert_redirected_to '/login'
  77. user = User.find_by_login('cool_user')
  78. assert user
  79. token = Token.find_by_user_id_and_action(user.id, 'register')
  80. assert token
  81. end
  82. def test_login_with_openid_with_new_user_created_with_manual_activation
  83. Setting.self_registration = '2'
  84. post :login, :openid_url => 'http://openid.example.com/good_user'
  85. assert_redirected_to '/login'
  86. user = User.find_by_login('cool_user')
  87. assert user
  88. assert_equal User::STATUS_REGISTERED, user.status
  89. end
  90. def test_login_with_openid_with_new_user_with_conflict_should_register
  91. Setting.self_registration = '3'
  92. existing_user = User.new(:firstname => 'Cool', :lastname => 'User', :mail => 'user@somedomain.com')
  93. existing_user.login = 'cool_user'
  94. assert existing_user.save!
  95. post :login, :openid_url => 'http://openid.example.com/good_user'
  96. assert_response :success
  97. assert_select 'input[name=?][value=?]', 'user[identity_url]', 'http://openid.example.com/good_user'
  98. end
  99. def test_login_with_openid_with_new_user_with_missing_information_should_register
  100. Setting.self_registration = '3'
  101. post :login, :openid_url => 'http://openid.example.com/good_blank_user'
  102. assert_response :success
  103. assert_select 'input[name=?]', 'user[login]'
  104. assert_select 'input[name=?]', 'user[password]'
  105. assert_select 'input[name=?]', 'user[password_confirmation]'
  106. assert_select 'input[name=?][value=?]', 'user[identity_url]', 'http://openid.example.com/good_blank_user'
  107. end
  108. def test_post_login_should_not_verify_token_when_using_open_id
  109. ActionController::Base.allow_forgery_protection = true
  110. AccountController.any_instance.stubs(:using_open_id?).returns(true)
  111. AccountController.any_instance.stubs(:authenticate_with_open_id).returns(true)
  112. post :login
  113. assert_response 200
  114. ensure
  115. ActionController::Base.allow_forgery_protection = false
  116. end
  117. def test_register_after_login_failure_should_not_require_user_to_enter_a_password
  118. Setting.self_registration = '3'
  119. assert_difference 'User.count' do
  120. post :register, :user => {
  121. :login => 'good_blank_user',
  122. :password => '',
  123. :password_confirmation => '',
  124. :firstname => 'Cool',
  125. :lastname => 'User',
  126. :mail => 'user@somedomain.com',
  127. :identity_url => 'http://openid.example.com/good_blank_user'
  128. }
  129. assert_response 302
  130. end
  131. user = User.order('id DESC').first
  132. assert_equal 'http://openid.example.com/good_blank_user', user.identity_url
  133. assert user.hashed_password.blank?, "Hashed password was #{user.hashed_password}"
  134. end
  135. def test_setting_openid_should_return_true_when_set_to_true
  136. assert_equal true, Setting.openid?
  137. end
  138. else
  139. puts "Skipping openid tests."
  140. def test_dummy
  141. end
  142. end
  143. end