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.5KB

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