annotate .svn/pristine/2d/2d6ad8e6f87f6d379c227b9a81e69b5a26399856.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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