annotate test/functional/account_controller_test.rb @ 1485:c8d3ad483bea redmine-2.4-integration

Fix stray merge markers
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Wed, 15 Jan 2014 13:34:12 +0000
parents 261b3d9a4903
children e248c7af89ec
rev   line source
Chris@909 1 # Redmine - project management software
Chris@1464 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@119 18 require File.expand_path('../../test_helper', __FILE__)
Chris@0 19
Chris@0 20 class AccountControllerTest < ActionController::TestCase
Chris@0 21 fixtures :users, :roles
Chris@909 22
Chris@0 23 def setup
Chris@0 24 User.current = nil
Chris@0 25 end
Chris@909 26
Chris@1115 27 def test_get_login
Chris@1115 28 get :login
Chris@1115 29 assert_response :success
Chris@1115 30 assert_template 'login'
Chris@1115 31
Chris@1115 32 assert_select 'input[name=username]'
Chris@1115 33 assert_select 'input[name=password]'
Chris@1115 34 end
Chris@1115 35
Chris@1464 36 def test_get_login_while_logged_in_should_redirect_to_home
Chris@1464 37 @request.session[:user_id] = 2
Chris@1464 38
Chris@1464 39 get :login
Chris@1464 40 assert_redirected_to '/'
Chris@1464 41 assert_equal 2, @request.session[:user_id]
Chris@1464 42 end
Chris@1464 43
Chris@0 44 def test_login_should_redirect_to_back_url_param
Chris@0 45 # request.uri is "test.host" in test environment
Chris@1115 46 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http://test.host/issues/show/1'
Chris@0 47 assert_redirected_to '/issues/show/1'
Chris@0 48 end
Chris@909 49
Chris@0 50 def test_login_should_not_redirect_to_another_host
Chris@1115 51 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http://test.foo/fake'
Chris@0 52 assert_redirected_to '/my/page'
Chris@0 53 end
Chris@0 54
Chris@0 55 def test_login_with_wrong_password
Chris@0 56 post :login, :username => 'admin', :password => 'bad'
Chris@0 57 assert_response :success
Chris@0 58 assert_template 'login'
Chris@1115 59
Chris@1115 60 assert_select 'div.flash.error', :text => /Invalid user or password/
Chris@1115 61 assert_select 'input[name=username][value=admin]'
Chris@1115 62 assert_select 'input[name=password]'
Chris@1115 63 assert_select 'input[name=password][value]', 0
Chris@0 64 end
Chris@909 65
Chris@1464 66 def test_login_with_locked_account_should_fail
Chris@1464 67 User.find(2).update_attribute :status, User::STATUS_LOCKED
Chris@1464 68
Chris@1464 69 post :login, :username => 'jsmith', :password => 'jsmith'
Chris@1464 70 assert_redirected_to '/login'
Chris@1464 71 assert_include 'locked', flash[:error]
Chris@1464 72 assert_nil @request.session[:user_id]
Chris@1464 73 end
Chris@1464 74
Chris@1464 75 def test_login_as_registered_user_with_manual_activation_should_inform_user
Chris@1464 76 User.find(2).update_attribute :status, User::STATUS_REGISTERED
Chris@1464 77
Chris@1464 78 with_settings :self_registration => '2', :default_language => 'en' do
Chris@1464 79 post :login, :username => 'jsmith', :password => 'jsmith'
Chris@1464 80 assert_redirected_to '/login'
Chris@1464 81 assert_include 'pending administrator approval', flash[:error]
Chris@1464 82 end
Chris@1464 83 end
Chris@1464 84
Chris@1464 85 def test_login_as_registered_user_with_email_activation_should_propose_new_activation_email
Chris@1464 86 User.find(2).update_attribute :status, User::STATUS_REGISTERED
Chris@1464 87
Chris@1464 88 with_settings :self_registration => '1', :default_language => 'en' do
Chris@1464 89 post :login, :username => 'jsmith', :password => 'jsmith'
Chris@1464 90 assert_redirected_to '/login'
Chris@1464 91 assert_equal 2, @request.session[:registered_user_id]
Chris@1464 92 assert_include 'new activation email', flash[:error]
Chris@1464 93 end
Chris@1464 94 end
Chris@1464 95
Chris@1115 96 def test_login_should_rescue_auth_source_exception
Chris@1115 97 source = AuthSource.create!(:name => 'Test')
Chris@1115 98 User.find(2).update_attribute :auth_source_id, source.id
Chris@1115 99 AuthSource.any_instance.stubs(:authenticate).raises(AuthSourceException.new("Something wrong"))
Chris@909 100
Chris@1115 101 post :login, :username => 'jsmith', :password => 'jsmith'
Chris@1115 102 assert_response 500
Chris@1115 103 assert_error_tag :content => /Something wrong/
Chris@0 104 end
Chris@0 105
Chris@1115 106 def test_login_should_reset_session
Chris@1115 107 @controller.expects(:reset_session).once
Chris@909 108
Chris@1115 109 post :login, :username => 'jsmith', :password => 'jsmith'
Chris@1115 110 assert_response 302
Chris@0 111 end
Chris@909 112
Chris@1464 113 def test_get_logout_should_not_logout
Chris@1464 114 @request.session[:user_id] = 2
Chris@1464 115 get :logout
Chris@1464 116 assert_response :success
Chris@1464 117 assert_template 'logout'
Chris@1464 118
Chris@1464 119 assert_equal 2, @request.session[:user_id]
Chris@1464 120 end
Chris@1464 121
Chris@1464 122 def test_get_logout_with_anonymous_should_redirect
Chris@1464 123 get :logout
Chris@1464 124 assert_redirected_to '/'
Chris@1464 125 end
Chris@1464 126
Chris@0 127 def test_logout
Chris@0 128 @request.session[:user_id] = 2
Chris@1464 129 post :logout
chris@37 130 assert_redirected_to '/'
Chris@0 131 assert_nil @request.session[:user_id]
Chris@0 132 end
Chris@14 133
Chris@1115 134 def test_logout_should_reset_session
Chris@1115 135 @controller.expects(:reset_session).once
Chris@909 136
Chris@1115 137 @request.session[:user_id] = 2
Chris@1464 138 post :logout
Chris@1115 139 assert_response 302
Chris@1115 140 end
Chris@1115 141
Chris@1115 142 def test_get_register_with_registration_on
Chris@1115 143 with_settings :self_registration => '3' do
Chris@1115 144 get :register
Chris@1115 145 assert_response :success
Chris@1115 146 assert_template 'register'
Chris@1115 147 assert_not_nil assigns(:user)
Chris@1115 148
Chris@1464 149 assert_select 'input[name=?]', 'user[password]'
Chris@1464 150 assert_select 'input[name=?]', 'user[password_confirmation]'
Chris@1464 151 end
Chris@1464 152 end
Chris@1464 153
Chris@1464 154 def test_get_register_should_detect_user_language
Chris@1464 155 with_settings :self_registration => '3' do
Chris@1464 156 @request.env['HTTP_ACCEPT_LANGUAGE'] = 'fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3'
Chris@1464 157 get :register
Chris@1464 158 assert_response :success
Chris@1464 159 assert_not_nil assigns(:user)
Chris@1464 160 assert_equal 'fr', assigns(:user).language
Chris@1464 161 assert_select 'select[name=?]', 'user[language]' do
Chris@1464 162 assert_select 'option[value=fr][selected=selected]'
Chris@1464 163 end
Chris@14 164 end
Chris@1115 165 end
Chris@909 166
Chris@1115 167 def test_get_register_with_registration_off_should_redirect
Chris@1115 168 with_settings :self_registration => '0' do
Chris@1115 169 get :register
Chris@1115 170 assert_redirected_to '/'
Chris@14 171 end
Chris@14 172 end
Chris@14 173
Chris@14 174 # See integration/account_test.rb for the full test
Chris@1115 175 def test_post_register_with_registration_on
Chris@1115 176 with_settings :self_registration => '3' do
Chris@1115 177 assert_difference 'User.count' do
Chris@1115 178 post :register, :user => {
Chris@1115 179 :login => 'register',
Chris@1115 180 :password => 'secret123',
Chris@1115 181 :password_confirmation => 'secret123',
Chris@1115 182 :firstname => 'John',
Chris@1115 183 :lastname => 'Doe',
Chris@1115 184 :mail => 'register@example.com'
Chris@1115 185 }
Chris@1115 186 assert_redirected_to '/my/account'
Chris@1115 187 end
Chris@1115 188 user = User.first(:order => 'id DESC')
Chris@1115 189 assert_equal 'register', user.login
Chris@1115 190 assert_equal 'John', user.firstname
Chris@1115 191 assert_equal 'Doe', user.lastname
Chris@1115 192 assert_equal 'register@example.com', user.mail
Chris@1115 193 assert user.check_password?('secret123')
Chris@1115 194 assert user.active?
Chris@1115 195 end
Chris@1115 196 end
Chris@1115 197
Chris@1115 198 def test_post_register_with_registration_off_should_redirect
Chris@1115 199 with_settings :self_registration => '0' do
Chris@1115 200 assert_no_difference 'User.count' do
Chris@14 201 post :register, :user => {
Chris@14 202 :login => 'register',
Chris@14 203 :password => 'test',
Chris@14 204 :password_confirmation => 'test',
Chris@14 205 :firstname => 'John',
Chris@14 206 :lastname => 'Doe',
Chris@14 207 :mail => 'register@example.com'
Chris@14 208 }
Chris@1115 209 assert_redirected_to '/'
Chris@14 210 end
Chris@1115 211 end
Chris@1115 212 end
Chris@909 213
Chris@1115 214 def test_get_lost_password_should_display_lost_password_form
Chris@1115 215 get :lost_password
Chris@1115 216 assert_response :success
Chris@1115 217 assert_select 'input[name=mail]'
Chris@1115 218 end
Chris@14 219
Chris@1115 220 def test_lost_password_for_active_user_should_create_a_token
Chris@1115 221 Token.delete_all
Chris@1115 222 ActionMailer::Base.deliveries.clear
Chris@1115 223 assert_difference 'ActionMailer::Base.deliveries.size' do
Chris@1115 224 assert_difference 'Token.count' do
Chris@1115 225 with_settings :host_name => 'mydomain.foo', :protocol => 'http' do
Chris@1115 226 post :lost_password, :mail => 'JSmith@somenet.foo'
Chris@1115 227 assert_redirected_to '/login'
Chris@1115 228 end
Chris@14 229 end
Chris@14 230 end
Chris@909 231
Chris@1115 232 token = Token.order('id DESC').first
Chris@1115 233 assert_equal User.find(2), token.user
Chris@1115 234 assert_equal 'recovery', token.action
Chris@14 235
Chris@1115 236 assert_select_email do
Chris@1115 237 assert_select "a[href=?]", "http://mydomain.foo/account/lost_password?token=#{token.value}"
Chris@14 238 end
Chris@14 239 end
Chris@1115 240
Chris@1115 241 def test_lost_password_for_unknown_user_should_fail
Chris@1115 242 Token.delete_all
Chris@1115 243 assert_no_difference 'Token.count' do
Chris@1115 244 post :lost_password, :mail => 'invalid@somenet.foo'
Chris@1115 245 assert_response :success
Chris@1115 246 end
Chris@1115 247 end
Chris@1115 248
Chris@1115 249 def test_lost_password_for_non_active_user_should_fail
Chris@1115 250 Token.delete_all
Chris@1115 251 assert User.find(2).lock!
Chris@1115 252
Chris@1115 253 assert_no_difference 'Token.count' do
Chris@1115 254 post :lost_password, :mail => 'JSmith@somenet.foo'
Chris@1464 255 assert_redirected_to '/account/lost_password'
Chris@1464 256 end
Chris@1464 257 end
Chris@1464 258
Chris@1464 259 def test_lost_password_for_user_who_cannot_change_password_should_fail
Chris@1464 260 User.any_instance.stubs(:change_password_allowed?).returns(false)
Chris@1464 261
Chris@1464 262 assert_no_difference 'Token.count' do
Chris@1464 263 post :lost_password, :mail => 'JSmith@somenet.foo'
Chris@1115 264 assert_response :success
Chris@1115 265 end
Chris@1115 266 end
Chris@1115 267
Chris@1115 268 def test_get_lost_password_with_token_should_display_the_password_recovery_form
Chris@1115 269 user = User.find(2)
Chris@1115 270 token = Token.create!(:action => 'recovery', :user => user)
Chris@1115 271
Chris@1115 272 get :lost_password, :token => token.value
Chris@1115 273 assert_response :success
Chris@1115 274 assert_template 'password_recovery'
Chris@1115 275
Chris@1115 276 assert_select 'input[type=hidden][name=token][value=?]', token.value
Chris@1115 277 end
Chris@1115 278
Chris@1115 279 def test_get_lost_password_with_invalid_token_should_redirect
Chris@1115 280 get :lost_password, :token => "abcdef"
Chris@1115 281 assert_redirected_to '/'
Chris@1115 282 end
Chris@1115 283
Chris@1115 284 def test_post_lost_password_with_token_should_change_the_user_password
Chris@1115 285 user = User.find(2)
Chris@1115 286 token = Token.create!(:action => 'recovery', :user => user)
Chris@1115 287
Chris@1115 288 post :lost_password, :token => token.value, :new_password => 'newpass123', :new_password_confirmation => 'newpass123'
Chris@1115 289 assert_redirected_to '/login'
Chris@1115 290 user.reload
Chris@1115 291 assert user.check_password?('newpass123')
Chris@1115 292 assert_nil Token.find_by_id(token.id), "Token was not deleted"
Chris@1115 293 end
Chris@1115 294
Chris@1115 295 def test_post_lost_password_with_token_for_non_active_user_should_fail
Chris@1115 296 user = User.find(2)
Chris@1115 297 token = Token.create!(:action => 'recovery', :user => user)
Chris@1115 298 user.lock!
Chris@1115 299
Chris@1115 300 post :lost_password, :token => token.value, :new_password => 'newpass123', :new_password_confirmation => 'newpass123'
Chris@1115 301 assert_redirected_to '/'
Chris@1115 302 assert ! user.check_password?('newpass123')
Chris@1115 303 end
Chris@1115 304
Chris@1115 305 def test_post_lost_password_with_token_and_password_confirmation_failure_should_redisplay_the_form
Chris@1115 306 user = User.find(2)
Chris@1115 307 token = Token.create!(:action => 'recovery', :user => user)
Chris@1115 308
Chris@1115 309 post :lost_password, :token => token.value, :new_password => 'newpass', :new_password_confirmation => 'wrongpass'
Chris@1115 310 assert_response :success
Chris@1115 311 assert_template 'password_recovery'
Chris@1115 312 assert_not_nil Token.find_by_id(token.id), "Token was deleted"
Chris@1115 313
Chris@1115 314 assert_select 'input[type=hidden][name=token][value=?]', token.value
Chris@1115 315 end
Chris@1115 316
Chris@1115 317 def test_post_lost_password_with_invalid_token_should_redirect
Chris@1115 318 post :lost_password, :token => "abcdef", :new_password => 'newpass', :new_password_confirmation => 'newpass'
Chris@1115 319 assert_redirected_to '/'
Chris@1115 320 end
Chris@1464 321
Chris@1464 322 def test_activation_email_should_send_an_activation_email
Chris@1464 323 User.find(2).update_attribute :status, User::STATUS_REGISTERED
Chris@1464 324 @request.session[:registered_user_id] = 2
Chris@1464 325
Chris@1464 326 with_settings :self_registration => '1' do
Chris@1464 327 assert_difference 'ActionMailer::Base.deliveries.size' do
Chris@1464 328 get :activation_email
Chris@1464 329 assert_redirected_to '/login'
Chris@1464 330 end
Chris@1464 331 end
Chris@1464 332 end
Chris@1464 333
Chris@1464 334 def test_activation_email_without_session_data_should_fail
Chris@1464 335 User.find(2).update_attribute :status, User::STATUS_REGISTERED
Chris@1464 336
Chris@1464 337 with_settings :self_registration => '1' do
Chris@1464 338 assert_no_difference 'ActionMailer::Base.deliveries.size' do
Chris@1464 339 get :activation_email
Chris@1464 340 assert_redirected_to '/'
Chris@1464 341 end
Chris@1464 342 end
Chris@1464 343 end
Chris@0 344 end