annotate .svn/pristine/91/91db92d5f8c4cd8c1392a3d4def27437c7f826a5.svn-base @ 1295:622f24f53b42 redmine-2.3

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