annotate test/functional/account_controller_test.rb @ 1465:ab8bd24eeb65 bug_635

Close obsolete branch bug_635
author Chris Cannam
date Fri, 19 Jul 2013 12:13:20 +0100
parents 433d4f72a19b
children 622f24f53b42 261b3d9a4903
rev   line source
Chris@909 1 # Redmine - project management software
Chris@1115 2 # Copyright (C) 2006-2012 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 require 'account_controller'
Chris@0 20
Chris@0 21 # Re-raise errors caught by the controller.
Chris@0 22 class AccountController; def rescue_action(e) raise e end; end
Chris@0 23
Chris@0 24 class AccountControllerTest < ActionController::TestCase
Chris@0 25 fixtures :users, :roles
Chris@909 26
Chris@0 27 def setup
Chris@0 28 @controller = AccountController.new
Chris@0 29 @request = ActionController::TestRequest.new
Chris@0 30 @response = ActionController::TestResponse.new
Chris@0 31 User.current = nil
Chris@0 32 end
Chris@909 33
Chris@1115 34 def test_get_login
Chris@1115 35 get :login
Chris@1115 36 assert_response :success
Chris@1115 37 assert_template 'login'
Chris@1115 38
Chris@1115 39 assert_select 'input[name=username]'
Chris@1115 40 assert_select 'input[name=password]'
Chris@1115 41 end
Chris@1115 42
Chris@0 43 def test_login_should_redirect_to_back_url_param
Chris@0 44 # request.uri is "test.host" in test environment
Chris@1115 45 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http://test.host/issues/show/1'
Chris@0 46 assert_redirected_to '/issues/show/1'
Chris@0 47 end
Chris@909 48
Chris@0 49 def test_login_should_not_redirect_to_another_host
Chris@1115 50 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http://test.foo/fake'
Chris@0 51 assert_redirected_to '/my/page'
Chris@0 52 end
Chris@0 53
Chris@0 54 def test_login_with_wrong_password
Chris@0 55 post :login, :username => 'admin', :password => 'bad'
Chris@0 56 assert_response :success
Chris@0 57 assert_template 'login'
Chris@1115 58
Chris@1115 59 assert_select 'div.flash.error', :text => /Invalid user or password/
Chris@1115 60 assert_select 'input[name=username][value=admin]'
Chris@1115 61 assert_select 'input[name=password]'
Chris@1115 62 assert_select 'input[name=password][value]', 0
Chris@0 63 end
Chris@909 64
Chris@1115 65 def test_login_should_rescue_auth_source_exception
Chris@1115 66 source = AuthSource.create!(:name => 'Test')
Chris@1115 67 User.find(2).update_attribute :auth_source_id, source.id
Chris@1115 68 AuthSource.any_instance.stubs(:authenticate).raises(AuthSourceException.new("Something wrong"))
Chris@909 69
Chris@1115 70 post :login, :username => 'jsmith', :password => 'jsmith'
Chris@1115 71 assert_response 500
Chris@1115 72 assert_error_tag :content => /Something wrong/
Chris@0 73 end
Chris@0 74
Chris@1115 75 def test_login_should_reset_session
Chris@1115 76 @controller.expects(:reset_session).once
Chris@909 77
Chris@1115 78 post :login, :username => 'jsmith', :password => 'jsmith'
Chris@1115 79 assert_response 302
Chris@0 80 end
Chris@909 81
Chris@0 82 def test_logout
Chris@0 83 @request.session[:user_id] = 2
Chris@0 84 get :logout
chris@37 85 assert_redirected_to '/'
Chris@0 86 assert_nil @request.session[:user_id]
Chris@0 87 end
Chris@14 88
Chris@1115 89 def test_logout_should_reset_session
Chris@1115 90 @controller.expects(:reset_session).once
Chris@909 91
Chris@1115 92 @request.session[:user_id] = 2
Chris@1115 93 get :logout
Chris@1115 94 assert_response 302
Chris@1115 95 end
Chris@1115 96
Chris@1115 97 def test_get_register_with_registration_on
Chris@1115 98 with_settings :self_registration => '3' do
Chris@1115 99 get :register
Chris@1115 100 assert_response :success
Chris@1115 101 assert_template 'register'
Chris@1115 102 assert_not_nil assigns(:user)
Chris@1115 103
Chris@1115 104 assert_tag 'input', :attributes => {:name => 'user[password]'}
Chris@1115 105 assert_tag 'input', :attributes => {:name => 'user[password_confirmation]'}
Chris@14 106 end
Chris@1115 107 end
Chris@909 108
Chris@1115 109 def test_get_register_with_registration_off_should_redirect
Chris@1115 110 with_settings :self_registration => '0' do
Chris@1115 111 get :register
Chris@1115 112 assert_redirected_to '/'
Chris@14 113 end
Chris@14 114 end
Chris@14 115
Chris@14 116 # See integration/account_test.rb for the full test
Chris@1115 117 def test_post_register_with_registration_on
Chris@1115 118 with_settings :self_registration => '3' do
Chris@1115 119 assert_difference 'User.count' do
Chris@1115 120 post :register, :user => {
Chris@1115 121 :login => 'register',
Chris@1115 122 :password => 'secret123',
Chris@1115 123 :password_confirmation => 'secret123',
Chris@1115 124 :firstname => 'John',
Chris@1115 125 :lastname => 'Doe',
Chris@1115 126 :mail => 'register@example.com'
Chris@1115 127 }
Chris@1115 128 assert_redirected_to '/my/account'
Chris@1115 129 end
Chris@1115 130 user = User.first(:order => 'id DESC')
Chris@1115 131 assert_equal 'register', user.login
Chris@1115 132 assert_equal 'John', user.firstname
Chris@1115 133 assert_equal 'Doe', user.lastname
Chris@1115 134 assert_equal 'register@example.com', user.mail
Chris@1115 135 assert user.check_password?('secret123')
Chris@1115 136 assert user.active?
Chris@1115 137 end
Chris@1115 138 end
Chris@1115 139
Chris@1115 140 def test_post_register_with_registration_off_should_redirect
Chris@1115 141 with_settings :self_registration => '0' do
Chris@1115 142 assert_no_difference 'User.count' do
Chris@14 143 post :register, :user => {
Chris@14 144 :login => 'register',
Chris@14 145 :password => 'test',
Chris@14 146 :password_confirmation => 'test',
Chris@14 147 :firstname => 'John',
Chris@14 148 :lastname => 'Doe',
Chris@14 149 :mail => 'register@example.com'
Chris@14 150 }
Chris@1115 151 assert_redirected_to '/'
Chris@14 152 end
Chris@1115 153 end
Chris@1115 154 end
Chris@909 155
Chris@1115 156 def test_get_lost_password_should_display_lost_password_form
Chris@1115 157 get :lost_password
Chris@1115 158 assert_response :success
Chris@1115 159 assert_select 'input[name=mail]'
Chris@1115 160 end
Chris@14 161
Chris@1115 162 def test_lost_password_for_active_user_should_create_a_token
Chris@1115 163 Token.delete_all
Chris@1115 164 ActionMailer::Base.deliveries.clear
Chris@1115 165 assert_difference 'ActionMailer::Base.deliveries.size' do
Chris@1115 166 assert_difference 'Token.count' do
Chris@1115 167 with_settings :host_name => 'mydomain.foo', :protocol => 'http' do
Chris@1115 168 post :lost_password, :mail => 'JSmith@somenet.foo'
Chris@1115 169 assert_redirected_to '/login'
Chris@1115 170 end
Chris@14 171 end
Chris@14 172 end
Chris@909 173
Chris@1115 174 token = Token.order('id DESC').first
Chris@1115 175 assert_equal User.find(2), token.user
Chris@1115 176 assert_equal 'recovery', token.action
Chris@14 177
Chris@1115 178 assert_select_email do
Chris@1115 179 assert_select "a[href=?]", "http://mydomain.foo/account/lost_password?token=#{token.value}"
Chris@14 180 end
Chris@14 181 end
Chris@1115 182
Chris@1115 183 def test_lost_password_for_unknown_user_should_fail
Chris@1115 184 Token.delete_all
Chris@1115 185 assert_no_difference 'Token.count' do
Chris@1115 186 post :lost_password, :mail => 'invalid@somenet.foo'
Chris@1115 187 assert_response :success
Chris@1115 188 end
Chris@1115 189 end
Chris@1115 190
Chris@1115 191 def test_lost_password_for_non_active_user_should_fail
Chris@1115 192 Token.delete_all
Chris@1115 193 assert User.find(2).lock!
Chris@1115 194
Chris@1115 195 assert_no_difference 'Token.count' do
Chris@1115 196 post :lost_password, :mail => 'JSmith@somenet.foo'
Chris@1115 197 assert_response :success
Chris@1115 198 end
Chris@1115 199 end
Chris@1115 200
Chris@1115 201 def test_get_lost_password_with_token_should_display_the_password_recovery_form
Chris@1115 202 user = User.find(2)
Chris@1115 203 token = Token.create!(:action => 'recovery', :user => user)
Chris@1115 204
Chris@1115 205 get :lost_password, :token => token.value
Chris@1115 206 assert_response :success
Chris@1115 207 assert_template 'password_recovery'
Chris@1115 208
Chris@1115 209 assert_select 'input[type=hidden][name=token][value=?]', token.value
Chris@1115 210 end
Chris@1115 211
Chris@1115 212 def test_get_lost_password_with_invalid_token_should_redirect
Chris@1115 213 get :lost_password, :token => "abcdef"
Chris@1115 214 assert_redirected_to '/'
Chris@1115 215 end
Chris@1115 216
Chris@1115 217 def test_post_lost_password_with_token_should_change_the_user_password
Chris@1115 218 user = User.find(2)
Chris@1115 219 token = Token.create!(:action => 'recovery', :user => user)
Chris@1115 220
Chris@1115 221 post :lost_password, :token => token.value, :new_password => 'newpass123', :new_password_confirmation => 'newpass123'
Chris@1115 222 assert_redirected_to '/login'
Chris@1115 223 user.reload
Chris@1115 224 assert user.check_password?('newpass123')
Chris@1115 225 assert_nil Token.find_by_id(token.id), "Token was not deleted"
Chris@1115 226 end
Chris@1115 227
Chris@1115 228 def test_post_lost_password_with_token_for_non_active_user_should_fail
Chris@1115 229 user = User.find(2)
Chris@1115 230 token = Token.create!(:action => 'recovery', :user => user)
Chris@1115 231 user.lock!
Chris@1115 232
Chris@1115 233 post :lost_password, :token => token.value, :new_password => 'newpass123', :new_password_confirmation => 'newpass123'
Chris@1115 234 assert_redirected_to '/'
Chris@1115 235 assert ! user.check_password?('newpass123')
Chris@1115 236 end
Chris@1115 237
Chris@1115 238 def test_post_lost_password_with_token_and_password_confirmation_failure_should_redisplay_the_form
Chris@1115 239 user = User.find(2)
Chris@1115 240 token = Token.create!(:action => 'recovery', :user => user)
Chris@1115 241
Chris@1115 242 post :lost_password, :token => token.value, :new_password => 'newpass', :new_password_confirmation => 'wrongpass'
Chris@1115 243 assert_response :success
Chris@1115 244 assert_template 'password_recovery'
Chris@1115 245 assert_not_nil Token.find_by_id(token.id), "Token was deleted"
Chris@1115 246
Chris@1115 247 assert_select 'input[type=hidden][name=token][value=?]', token.value
Chris@1115 248 end
Chris@1115 249
Chris@1115 250 def test_post_lost_password_with_invalid_token_should_redirect
Chris@1115 251 post :lost_password, :token => "abcdef", :new_password => 'newpass', :new_password_confirmation => 'newpass'
Chris@1115 252 assert_redirected_to '/'
Chris@1115 253 end
Chris@0 254 end