annotate test/functional/account_controller_test.rb @ 1519:afce8026aaeb redmine-2.4-integration

Merge from branch "live"
author Chris Cannam
date Tue, 09 Sep 2014 09:34:53 +0100
parents b450a9d58aed
children dffacf8a6908
rev   line source
Chris@909 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 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@1516 46 back_urls = [
Chris@1516 47 'http://test.host/issues/show/1',
Chris@1516 48 '/'
Chris@1516 49 ]
Chris@1516 50 back_urls.each do |back_url|
Chris@1516 51 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => back_url
Chris@1516 52 assert_redirected_to back_url
Chris@1516 53 end
Chris@1516 54 end
Chris@1516 55
Chris@1516 56 def test_login_with_suburi_should_redirect_to_back_url_param
Chris@1516 57 @relative_url_root = ApplicationController.relative_url_root
Chris@1516 58 ApplicationController.relative_url_root = '/redmine'
Chris@1516 59
Chris@1516 60 back_urls = [
Chris@1516 61 'http://test.host/redmine/issues/show/1',
Chris@1516 62 '/redmine'
Chris@1516 63 ]
Chris@1516 64 back_urls.each do |back_url|
Chris@1516 65 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => back_url
Chris@1516 66 assert_redirected_to back_url
Chris@1516 67 end
Chris@1516 68 ensure
Chris@1516 69 ApplicationController.relative_url_root = @relative_url_root
Chris@0 70 end
Chris@909 71
Chris@0 72 def test_login_should_not_redirect_to_another_host
Chris@1516 73 back_urls = [
Chris@1516 74 'http://test.foo/fake',
Chris@1516 75 '//test.foo/fake'
Chris@1516 76 ]
Chris@1516 77 back_urls.each do |back_url|
Chris@1516 78 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => back_url
Chris@1516 79 assert_redirected_to '/my/page'
Chris@1516 80 end
Chris@1516 81 end
Chris@1516 82
Chris@1516 83 def test_login_with_suburi_should_not_redirect_to_another_suburi
Chris@1516 84 @relative_url_root = ApplicationController.relative_url_root
Chris@1516 85 ApplicationController.relative_url_root = '/redmine'
Chris@1516 86
Chris@1516 87 back_urls = [
Chris@1516 88 'http://test.host/',
Chris@1516 89 'http://test.host/fake',
Chris@1516 90 'http://test.host/fake/issues',
Chris@1516 91 'http://test.host/redmine/../fake',
Chris@1516 92 'http://test.host/redmine/../fake/issues',
Chris@1516 93 'http://test.host/redmine/%2e%2e/fake'
Chris@1516 94 ]
Chris@1516 95 back_urls.each do |back_url|
Chris@1516 96 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => back_url
Chris@1516 97 assert_redirected_to '/my/page'
Chris@1516 98 end
Chris@1516 99 ensure
Chris@1516 100 ApplicationController.relative_url_root = @relative_url_root
Chris@0 101 end
Chris@0 102
Chris@0 103 def test_login_with_wrong_password
Chris@0 104 post :login, :username => 'admin', :password => 'bad'
Chris@0 105 assert_response :success
Chris@0 106 assert_template 'login'
Chris@1115 107
Chris@1115 108 assert_select 'div.flash.error', :text => /Invalid user or password/
Chris@1115 109 assert_select 'input[name=username][value=admin]'
Chris@1115 110 assert_select 'input[name=password]'
Chris@1115 111 assert_select 'input[name=password][value]', 0
Chris@0 112 end
Chris@909 113
Chris@1464 114 def test_login_with_locked_account_should_fail
Chris@1464 115 User.find(2).update_attribute :status, User::STATUS_LOCKED
Chris@1464 116
Chris@1464 117 post :login, :username => 'jsmith', :password => 'jsmith'
Chris@1464 118 assert_redirected_to '/login'
Chris@1464 119 assert_include 'locked', flash[:error]
Chris@1464 120 assert_nil @request.session[:user_id]
Chris@1464 121 end
Chris@1464 122
Chris@1464 123 def test_login_as_registered_user_with_manual_activation_should_inform_user
Chris@1464 124 User.find(2).update_attribute :status, User::STATUS_REGISTERED
Chris@1464 125
Chris@1464 126 with_settings :self_registration => '2', :default_language => 'en' do
Chris@1464 127 post :login, :username => 'jsmith', :password => 'jsmith'
Chris@1464 128 assert_redirected_to '/login'
Chris@1464 129 assert_include 'pending administrator approval', flash[:error]
Chris@1464 130 end
Chris@1464 131 end
Chris@1464 132
Chris@1464 133 def test_login_as_registered_user_with_email_activation_should_propose_new_activation_email
Chris@1464 134 User.find(2).update_attribute :status, User::STATUS_REGISTERED
Chris@1464 135
Chris@1464 136 with_settings :self_registration => '1', :default_language => 'en' do
Chris@1464 137 post :login, :username => 'jsmith', :password => 'jsmith'
Chris@1464 138 assert_redirected_to '/login'
Chris@1464 139 assert_equal 2, @request.session[:registered_user_id]
Chris@1464 140 assert_include 'new activation email', flash[:error]
Chris@1464 141 end
Chris@1464 142 end
Chris@1464 143
Chris@1115 144 def test_login_should_rescue_auth_source_exception
Chris@1115 145 source = AuthSource.create!(:name => 'Test')
Chris@1115 146 User.find(2).update_attribute :auth_source_id, source.id
Chris@1115 147 AuthSource.any_instance.stubs(:authenticate).raises(AuthSourceException.new("Something wrong"))
Chris@909 148
Chris@1115 149 post :login, :username => 'jsmith', :password => 'jsmith'
Chris@1115 150 assert_response 500
Chris@1115 151 assert_error_tag :content => /Something wrong/
Chris@0 152 end
Chris@0 153
Chris@1115 154 def test_login_should_reset_session
Chris@1115 155 @controller.expects(:reset_session).once
Chris@909 156
Chris@1115 157 post :login, :username => 'jsmith', :password => 'jsmith'
Chris@1115 158 assert_response 302
Chris@0 159 end
Chris@909 160
Chris@1464 161 def test_get_logout_should_not_logout
Chris@1464 162 @request.session[:user_id] = 2
Chris@1464 163 get :logout
Chris@1464 164 assert_response :success
Chris@1464 165 assert_template 'logout'
Chris@1464 166
Chris@1464 167 assert_equal 2, @request.session[:user_id]
Chris@1464 168 end
Chris@1464 169
Chris@1464 170 def test_get_logout_with_anonymous_should_redirect
Chris@1464 171 get :logout
Chris@1464 172 assert_redirected_to '/'
Chris@1464 173 end
Chris@1464 174
Chris@0 175 def test_logout
Chris@0 176 @request.session[:user_id] = 2
Chris@1464 177 post :logout
chris@37 178 assert_redirected_to '/'
Chris@0 179 assert_nil @request.session[:user_id]
Chris@0 180 end
Chris@14 181
Chris@1115 182 def test_logout_should_reset_session
Chris@1115 183 @controller.expects(:reset_session).once
Chris@909 184
Chris@1115 185 @request.session[:user_id] = 2
Chris@1464 186 post :logout
Chris@1115 187 assert_response 302
Chris@1115 188 end
Chris@1115 189
Chris@1115 190 def test_get_register_with_registration_on
Chris@1115 191 with_settings :self_registration => '3' do
Chris@1115 192 get :register
Chris@1115 193 assert_response :success
Chris@1115 194 assert_template 'register'
Chris@1115 195 assert_not_nil assigns(:user)
Chris@1115 196
Chris@1464 197 assert_select 'input[name=?]', 'user[password]'
Chris@1464 198 assert_select 'input[name=?]', 'user[password_confirmation]'
Chris@1464 199 end
Chris@1464 200 end
Chris@1464 201
Chris@1464 202 def test_get_register_should_detect_user_language
Chris@1464 203 with_settings :self_registration => '3' do
Chris@1464 204 @request.env['HTTP_ACCEPT_LANGUAGE'] = 'fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3'
Chris@1464 205 get :register
Chris@1464 206 assert_response :success
Chris@1464 207 assert_not_nil assigns(:user)
Chris@1464 208 assert_equal 'fr', assigns(:user).language
Chris@1464 209 assert_select 'select[name=?]', 'user[language]' do
Chris@1464 210 assert_select 'option[value=fr][selected=selected]'
Chris@1464 211 end
Chris@14 212 end
Chris@1115 213 end
Chris@909 214
Chris@1115 215 def test_get_register_with_registration_off_should_redirect
Chris@1115 216 with_settings :self_registration => '0' do
Chris@1115 217 get :register
Chris@1115 218 assert_redirected_to '/'
Chris@14 219 end
Chris@14 220 end
Chris@14 221
Chris@14 222 # See integration/account_test.rb for the full test
Chris@1115 223 def test_post_register_with_registration_on
Chris@1115 224 with_settings :self_registration => '3' do
Chris@1115 225 assert_difference 'User.count' do
Chris@1115 226 post :register, :user => {
Chris@1115 227 :login => 'register',
Chris@1115 228 :password => 'secret123',
Chris@1115 229 :password_confirmation => 'secret123',
Chris@1115 230 :firstname => 'John',
Chris@1115 231 :lastname => 'Doe',
Chris@1115 232 :mail => 'register@example.com'
Chris@1115 233 }
Chris@1115 234 assert_redirected_to '/my/account'
Chris@1115 235 end
Chris@1115 236 user = User.first(:order => 'id DESC')
Chris@1115 237 assert_equal 'register', user.login
Chris@1115 238 assert_equal 'John', user.firstname
Chris@1115 239 assert_equal 'Doe', user.lastname
Chris@1115 240 assert_equal 'register@example.com', user.mail
Chris@1115 241 assert user.check_password?('secret123')
Chris@1115 242 assert user.active?
Chris@1115 243 end
Chris@1115 244 end
Chris@1115 245
Chris@1115 246 def test_post_register_with_registration_off_should_redirect
Chris@1115 247 with_settings :self_registration => '0' do
Chris@1115 248 assert_no_difference 'User.count' do
Chris@14 249 post :register, :user => {
Chris@14 250 :login => 'register',
Chris@14 251 :password => 'test',
Chris@14 252 :password_confirmation => 'test',
Chris@14 253 :firstname => 'John',
Chris@14 254 :lastname => 'Doe',
Chris@14 255 :mail => 'register@example.com'
Chris@14 256 }
Chris@1115 257 assert_redirected_to '/'
Chris@14 258 end
Chris@1115 259 end
Chris@1115 260 end
Chris@909 261
Chris@1115 262 def test_get_lost_password_should_display_lost_password_form
Chris@1115 263 get :lost_password
Chris@1115 264 assert_response :success
Chris@1115 265 assert_select 'input[name=mail]'
Chris@1115 266 end
Chris@14 267
Chris@1115 268 def test_lost_password_for_active_user_should_create_a_token
Chris@1115 269 Token.delete_all
Chris@1115 270 ActionMailer::Base.deliveries.clear
Chris@1115 271 assert_difference 'ActionMailer::Base.deliveries.size' do
Chris@1115 272 assert_difference 'Token.count' do
Chris@1115 273 with_settings :host_name => 'mydomain.foo', :protocol => 'http' do
Chris@1115 274 post :lost_password, :mail => 'JSmith@somenet.foo'
Chris@1115 275 assert_redirected_to '/login'
Chris@1115 276 end
Chris@14 277 end
Chris@14 278 end
Chris@909 279
Chris@1115 280 token = Token.order('id DESC').first
Chris@1115 281 assert_equal User.find(2), token.user
Chris@1115 282 assert_equal 'recovery', token.action
Chris@14 283
Chris@1115 284 assert_select_email do
Chris@1115 285 assert_select "a[href=?]", "http://mydomain.foo/account/lost_password?token=#{token.value}"
Chris@14 286 end
Chris@14 287 end
Chris@1115 288
Chris@1115 289 def test_lost_password_for_unknown_user_should_fail
Chris@1115 290 Token.delete_all
Chris@1115 291 assert_no_difference 'Token.count' do
Chris@1115 292 post :lost_password, :mail => 'invalid@somenet.foo'
Chris@1115 293 assert_response :success
Chris@1115 294 end
Chris@1115 295 end
Chris@1115 296
Chris@1115 297 def test_lost_password_for_non_active_user_should_fail
Chris@1115 298 Token.delete_all
Chris@1115 299 assert User.find(2).lock!
Chris@1115 300
Chris@1115 301 assert_no_difference 'Token.count' do
Chris@1115 302 post :lost_password, :mail => 'JSmith@somenet.foo'
Chris@1464 303 assert_redirected_to '/account/lost_password'
Chris@1464 304 end
Chris@1464 305 end
Chris@1464 306
Chris@1464 307 def test_lost_password_for_user_who_cannot_change_password_should_fail
Chris@1464 308 User.any_instance.stubs(:change_password_allowed?).returns(false)
Chris@1464 309
Chris@1464 310 assert_no_difference 'Token.count' do
Chris@1464 311 post :lost_password, :mail => 'JSmith@somenet.foo'
Chris@1115 312 assert_response :success
Chris@1115 313 end
Chris@1115 314 end
Chris@1115 315
Chris@1115 316 def test_get_lost_password_with_token_should_display_the_password_recovery_form
Chris@1115 317 user = User.find(2)
Chris@1115 318 token = Token.create!(:action => 'recovery', :user => user)
Chris@1115 319
Chris@1115 320 get :lost_password, :token => token.value
Chris@1115 321 assert_response :success
Chris@1115 322 assert_template 'password_recovery'
Chris@1115 323
Chris@1115 324 assert_select 'input[type=hidden][name=token][value=?]', token.value
Chris@1115 325 end
Chris@1115 326
Chris@1115 327 def test_get_lost_password_with_invalid_token_should_redirect
Chris@1115 328 get :lost_password, :token => "abcdef"
Chris@1115 329 assert_redirected_to '/'
Chris@1115 330 end
Chris@1115 331
Chris@1115 332 def test_post_lost_password_with_token_should_change_the_user_password
Chris@1115 333 user = User.find(2)
Chris@1115 334 token = Token.create!(:action => 'recovery', :user => user)
Chris@1115 335
Chris@1115 336 post :lost_password, :token => token.value, :new_password => 'newpass123', :new_password_confirmation => 'newpass123'
Chris@1115 337 assert_redirected_to '/login'
Chris@1115 338 user.reload
Chris@1115 339 assert user.check_password?('newpass123')
Chris@1115 340 assert_nil Token.find_by_id(token.id), "Token was not deleted"
Chris@1115 341 end
Chris@1115 342
Chris@1115 343 def test_post_lost_password_with_token_for_non_active_user_should_fail
Chris@1115 344 user = User.find(2)
Chris@1115 345 token = Token.create!(:action => 'recovery', :user => user)
Chris@1115 346 user.lock!
Chris@1115 347
Chris@1115 348 post :lost_password, :token => token.value, :new_password => 'newpass123', :new_password_confirmation => 'newpass123'
Chris@1115 349 assert_redirected_to '/'
Chris@1115 350 assert ! user.check_password?('newpass123')
Chris@1115 351 end
Chris@1115 352
Chris@1115 353 def test_post_lost_password_with_token_and_password_confirmation_failure_should_redisplay_the_form
Chris@1115 354 user = User.find(2)
Chris@1115 355 token = Token.create!(:action => 'recovery', :user => user)
Chris@1115 356
Chris@1115 357 post :lost_password, :token => token.value, :new_password => 'newpass', :new_password_confirmation => 'wrongpass'
Chris@1115 358 assert_response :success
Chris@1115 359 assert_template 'password_recovery'
Chris@1115 360 assert_not_nil Token.find_by_id(token.id), "Token was deleted"
Chris@1115 361
Chris@1115 362 assert_select 'input[type=hidden][name=token][value=?]', token.value
Chris@1115 363 end
Chris@1115 364
Chris@1115 365 def test_post_lost_password_with_invalid_token_should_redirect
Chris@1115 366 post :lost_password, :token => "abcdef", :new_password => 'newpass', :new_password_confirmation => 'newpass'
Chris@1115 367 assert_redirected_to '/'
Chris@1115 368 end
Chris@1464 369
Chris@1464 370 def test_activation_email_should_send_an_activation_email
Chris@1464 371 User.find(2).update_attribute :status, User::STATUS_REGISTERED
Chris@1464 372 @request.session[:registered_user_id] = 2
Chris@1464 373
Chris@1464 374 with_settings :self_registration => '1' do
Chris@1464 375 assert_difference 'ActionMailer::Base.deliveries.size' do
Chris@1464 376 get :activation_email
Chris@1464 377 assert_redirected_to '/login'
Chris@1464 378 end
Chris@1464 379 end
Chris@1464 380 end
Chris@1464 381
Chris@1464 382 def test_activation_email_without_session_data_should_fail
Chris@1464 383 User.find(2).update_attribute :status, User::STATUS_REGISTERED
Chris@1464 384
Chris@1464 385 with_settings :self_registration => '1' do
Chris@1464 386 assert_no_difference 'ActionMailer::Base.deliveries.size' do
Chris@1464 387 get :activation_email
Chris@1464 388 assert_redirected_to '/'
Chris@1464 389 end
Chris@1464 390 end
Chris@1464 391 end
Chris@0 392 end