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_test.rb 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2012 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. require 'account_controller'
  19. # Re-raise errors caught by the controller.
  20. class AccountController; def rescue_action(e) raise e end; end
  21. class AccountControllerTest < ActionController::TestCase
  22. fixtures :users, :roles
  23. def setup
  24. @controller = AccountController.new
  25. @request = ActionController::TestRequest.new
  26. @response = ActionController::TestResponse.new
  27. User.current = nil
  28. end
  29. def test_login_should_redirect_to_back_url_param
  30. # request.uri is "test.host" in test environment
  31. post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http%3A%2F%2Ftest.host%2Fissues%2Fshow%2F1'
  32. assert_redirected_to '/issues/show/1'
  33. end
  34. def test_login_should_not_redirect_to_another_host
  35. post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http%3A%2F%2Ftest.foo%2Ffake'
  36. assert_redirected_to '/my/page'
  37. end
  38. def test_login_with_wrong_password
  39. post :login, :username => 'admin', :password => 'bad'
  40. assert_response :success
  41. assert_template 'login'
  42. assert_tag 'div',
  43. :attributes => { :class => "flash error" },
  44. :content => /Invalid user or password/
  45. end
  46. def test_login_should_rescue_auth_source_exception
  47. source = AuthSource.create!(:name => 'Test')
  48. User.find(2).update_attribute :auth_source_id, source.id
  49. AuthSource.any_instance.stubs(:authenticate).raises(AuthSourceException.new("Something wrong"))
  50. post :login, :username => 'jsmith', :password => 'jsmith'
  51. assert_response 500
  52. assert_error_tag :content => /Something wrong/
  53. end
  54. def test_login_should_reset_session
  55. @controller.expects(:reset_session).once
  56. post :login, :username => 'jsmith', :password => 'jsmith'
  57. assert_response 302
  58. end
  59. if Object.const_defined?(:OpenID)
  60. def test_login_with_openid_for_existing_user
  61. Setting.self_registration = '3'
  62. Setting.openid = '1'
  63. existing_user = User.new(:firstname => 'Cool',
  64. :lastname => 'User',
  65. :mail => 'user@somedomain.com',
  66. :identity_url => 'http://openid.example.com/good_user')
  67. existing_user.login = 'cool_user'
  68. assert existing_user.save!
  69. post :login, :openid_url => existing_user.identity_url
  70. assert_redirected_to '/my/page'
  71. end
  72. def test_login_with_invalid_openid_provider
  73. Setting.self_registration = '0'
  74. Setting.openid = '1'
  75. post :login, :openid_url => 'http;//openid.example.com/good_user'
  76. assert_redirected_to home_url
  77. end
  78. def test_login_with_openid_for_existing_non_active_user
  79. Setting.self_registration = '2'
  80. Setting.openid = '1'
  81. existing_user = User.new(:firstname => 'Cool',
  82. :lastname => 'User',
  83. :mail => 'user@somedomain.com',
  84. :identity_url => 'http://openid.example.com/good_user',
  85. :status => User::STATUS_REGISTERED)
  86. existing_user.login = 'cool_user'
  87. assert existing_user.save!
  88. post :login, :openid_url => existing_user.identity_url
  89. assert_redirected_to '/login'
  90. end
  91. def test_login_with_openid_with_new_user_created
  92. Setting.self_registration = '3'
  93. Setting.openid = '1'
  94. post :login, :openid_url => 'http://openid.example.com/good_user'
  95. assert_redirected_to '/my/account'
  96. user = User.find_by_login('cool_user')
  97. assert user
  98. assert_equal 'Cool', user.firstname
  99. assert_equal 'User', user.lastname
  100. end
  101. def test_login_with_openid_with_new_user_and_self_registration_off
  102. Setting.self_registration = '0'
  103. Setting.openid = '1'
  104. post :login, :openid_url => 'http://openid.example.com/good_user'
  105. assert_redirected_to home_url
  106. user = User.find_by_login('cool_user')
  107. assert ! user
  108. end
  109. def test_login_with_openid_with_new_user_created_with_email_activation_should_have_a_token
  110. Setting.self_registration = '1'
  111. Setting.openid = '1'
  112. post :login, :openid_url => 'http://openid.example.com/good_user'
  113. assert_redirected_to '/login'
  114. user = User.find_by_login('cool_user')
  115. assert user
  116. token = Token.find_by_user_id_and_action(user.id, 'register')
  117. assert token
  118. end
  119. def test_login_with_openid_with_new_user_created_with_manual_activation
  120. Setting.self_registration = '2'
  121. Setting.openid = '1'
  122. post :login, :openid_url => 'http://openid.example.com/good_user'
  123. assert_redirected_to '/login'
  124. user = User.find_by_login('cool_user')
  125. assert user
  126. assert_equal User::STATUS_REGISTERED, user.status
  127. end
  128. def test_login_with_openid_with_new_user_with_conflict_should_register
  129. Setting.self_registration = '3'
  130. Setting.openid = '1'
  131. existing_user = User.new(:firstname => 'Cool', :lastname => 'User', :mail => 'user@somedomain.com')
  132. existing_user.login = 'cool_user'
  133. assert existing_user.save!
  134. post :login, :openid_url => 'http://openid.example.com/good_user'
  135. assert_response :success
  136. assert_template 'register'
  137. assert assigns(:user)
  138. assert_equal 'http://openid.example.com/good_user', assigns(:user)[:identity_url]
  139. end
  140. def test_setting_openid_should_return_true_when_set_to_true
  141. Setting.openid = '1'
  142. assert_equal true, Setting.openid?
  143. end
  144. else
  145. puts "Skipping openid tests."
  146. end
  147. def test_logout
  148. @request.session[:user_id] = 2
  149. get :logout
  150. assert_redirected_to '/'
  151. assert_nil @request.session[:user_id]
  152. end
  153. def test_logout_should_reset_session
  154. @controller.expects(:reset_session).once
  155. @request.session[:user_id] = 2
  156. get :logout
  157. assert_response 302
  158. end
  159. def test_get_register_with_registration_on
  160. with_settings :self_registration => '3' do
  161. get :register
  162. assert_response :success
  163. assert_template 'register'
  164. assert_not_nil assigns(:user)
  165. assert_tag 'input', :attributes => {:name => 'user[password]'}
  166. assert_tag 'input', :attributes => {:name => 'user[password_confirmation]'}
  167. end
  168. end
  169. def test_get_register_with_registration_off_should_redirect
  170. with_settings :self_registration => '0' do
  171. get :register
  172. assert_redirected_to '/'
  173. end
  174. end
  175. # See integration/account_test.rb for the full test
  176. def test_post_register_with_registration_on
  177. with_settings :self_registration => '3' do
  178. assert_difference 'User.count' do
  179. post :register, :user => {
  180. :login => 'register',
  181. :password => 'test',
  182. :password_confirmation => 'test',
  183. :firstname => 'John',
  184. :lastname => 'Doe',
  185. :mail => 'register@example.com'
  186. }
  187. assert_redirected_to '/my/account'
  188. end
  189. user = User.first(:order => 'id DESC')
  190. assert_equal 'register', user.login
  191. assert_equal 'John', user.firstname
  192. assert_equal 'Doe', user.lastname
  193. assert_equal 'register@example.com', user.mail
  194. assert user.check_password?('test')
  195. assert user.active?
  196. end
  197. end
  198. def test_post_register_with_registration_off_should_redirect
  199. with_settings :self_registration => '0' do
  200. assert_no_difference 'User.count' do
  201. post :register, :user => {
  202. :login => 'register',
  203. :password => 'test',
  204. :password_confirmation => 'test',
  205. :firstname => 'John',
  206. :lastname => 'Doe',
  207. :mail => 'register@example.com'
  208. }
  209. assert_redirected_to '/'
  210. end
  211. end
  212. end
  213. end