annotate .svn/pristine/32/3298e1c1ddf5d23eff798c7cd5979f9e2dc8ea9f.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
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 begin
Chris@1295 21 require 'mocha'
Chris@1295 22 rescue
Chris@1295 23 # Won't run some tests
Chris@1295 24 end
Chris@1295 25
Chris@1295 26 class AccountTest < ActionController::IntegrationTest
Chris@1295 27 fixtures :users, :roles
Chris@1295 28
Chris@1295 29 # Replace this with your real tests.
Chris@1295 30 def test_login
Chris@1295 31 get "my/page"
Chris@1295 32 assert_redirected_to "/login?back_url=http%3A%2F%2Fwww.example.com%2Fmy%2Fpage"
Chris@1295 33 log_user('jsmith', 'jsmith')
Chris@1295 34
Chris@1295 35 get "my/account"
Chris@1295 36 assert_response :success
Chris@1295 37 assert_template "my/account"
Chris@1295 38 end
Chris@1295 39
Chris@1295 40 def test_autologin
Chris@1295 41 user = User.find(1)
Chris@1295 42 Setting.autologin = "7"
Chris@1295 43 Token.delete_all
Chris@1295 44
Chris@1295 45 # User logs in with 'autologin' checked
Chris@1295 46 post '/login', :username => user.login, :password => 'admin', :autologin => 1
Chris@1295 47 assert_redirected_to '/my/page'
Chris@1295 48 token = Token.first
Chris@1295 49 assert_not_nil token
Chris@1295 50 assert_equal user, token.user
Chris@1295 51 assert_equal 'autologin', token.action
Chris@1295 52 assert_equal user.id, session[:user_id]
Chris@1295 53 assert_equal token.value, cookies['autologin']
Chris@1295 54
Chris@1295 55 # Session is cleared
Chris@1295 56 reset!
Chris@1295 57 User.current = nil
Chris@1295 58 # Clears user's last login timestamp
Chris@1295 59 user.update_attribute :last_login_on, nil
Chris@1295 60 assert_nil user.reload.last_login_on
Chris@1295 61
Chris@1295 62 # User comes back with his autologin cookie
Chris@1295 63 cookies[:autologin] = token.value
Chris@1295 64 get '/my/page'
Chris@1295 65 assert_response :success
Chris@1295 66 assert_template 'my/page'
Chris@1295 67 assert_equal user.id, session[:user_id]
Chris@1295 68 assert_not_nil user.reload.last_login_on
Chris@1295 69 end
Chris@1295 70
Chris@1295 71 def test_autologin_should_use_autologin_cookie_name
Chris@1295 72 Token.delete_all
Chris@1295 73 Redmine::Configuration.stubs(:[]).with('autologin_cookie_name').returns('custom_autologin')
Chris@1295 74 Redmine::Configuration.stubs(:[]).with('autologin_cookie_path').returns('/')
Chris@1295 75 Redmine::Configuration.stubs(:[]).with('autologin_cookie_secure').returns(false)
Chris@1295 76
Chris@1295 77 with_settings :autologin => '7' do
Chris@1295 78 assert_difference 'Token.count' do
Chris@1295 79 post '/login', :username => 'admin', :password => 'admin', :autologin => 1
Chris@1295 80 end
Chris@1295 81 assert_response 302
Chris@1295 82 assert cookies['custom_autologin'].present?
Chris@1295 83 token = cookies['custom_autologin']
Chris@1295 84
Chris@1295 85 # Session is cleared
Chris@1295 86 reset!
Chris@1295 87 cookies['custom_autologin'] = token
Chris@1295 88 get '/my/page'
Chris@1295 89 assert_response :success
Chris@1295 90
Chris@1295 91 assert_difference 'Token.count', -1 do
Chris@1295 92 post '/logout'
Chris@1295 93 end
Chris@1295 94 assert cookies['custom_autologin'].blank?
Chris@1295 95 end
Chris@1295 96 end
Chris@1295 97
Chris@1295 98 def test_lost_password
Chris@1295 99 Token.delete_all
Chris@1295 100
Chris@1295 101 get "account/lost_password"
Chris@1295 102 assert_response :success
Chris@1295 103 assert_template "account/lost_password"
Chris@1295 104 assert_select 'input[name=mail]'
Chris@1295 105
Chris@1295 106 post "account/lost_password", :mail => 'jSmith@somenet.foo'
Chris@1295 107 assert_redirected_to "/login"
Chris@1295 108
Chris@1295 109 token = Token.first
Chris@1295 110 assert_equal 'recovery', token.action
Chris@1295 111 assert_equal 'jsmith@somenet.foo', token.user.mail
Chris@1295 112 assert !token.expired?
Chris@1295 113
Chris@1295 114 get "account/lost_password", :token => token.value
Chris@1295 115 assert_response :success
Chris@1295 116 assert_template "account/password_recovery"
Chris@1295 117 assert_select 'input[type=hidden][name=token][value=?]', token.value
Chris@1295 118 assert_select 'input[name=new_password]'
Chris@1295 119 assert_select 'input[name=new_password_confirmation]'
Chris@1295 120
Chris@1295 121 post "account/lost_password", :token => token.value, :new_password => 'newpass123', :new_password_confirmation => 'newpass123'
Chris@1295 122 assert_redirected_to "/login"
Chris@1295 123 assert_equal 'Password was successfully updated.', flash[:notice]
Chris@1295 124
Chris@1295 125 log_user('jsmith', 'newpass123')
Chris@1295 126 assert_equal 0, Token.count
Chris@1295 127 end
Chris@1295 128
Chris@1295 129 def test_register_with_automatic_activation
Chris@1295 130 Setting.self_registration = '3'
Chris@1295 131
Chris@1295 132 get 'account/register'
Chris@1295 133 assert_response :success
Chris@1295 134 assert_template 'account/register'
Chris@1295 135
Chris@1295 136 post 'account/register', :user => {:login => "newuser", :language => "en", :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar",
Chris@1295 137 :password => "newpass123", :password_confirmation => "newpass123"}
Chris@1295 138 assert_redirected_to '/my/account'
Chris@1295 139 follow_redirect!
Chris@1295 140 assert_response :success
Chris@1295 141 assert_template 'my/account'
Chris@1295 142
Chris@1295 143 user = User.find_by_login('newuser')
Chris@1295 144 assert_not_nil user
Chris@1295 145 assert user.active?
Chris@1295 146 assert_not_nil user.last_login_on
Chris@1295 147 end
Chris@1295 148
Chris@1295 149 def test_register_with_manual_activation
Chris@1295 150 Setting.self_registration = '2'
Chris@1295 151
Chris@1295 152 post 'account/register', :user => {:login => "newuser", :language => "en", :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar",
Chris@1295 153 :password => "newpass123", :password_confirmation => "newpass123"}
Chris@1295 154 assert_redirected_to '/login'
Chris@1295 155 assert !User.find_by_login('newuser').active?
Chris@1295 156 end
Chris@1295 157
Chris@1295 158 def test_register_with_email_activation
Chris@1295 159 Setting.self_registration = '1'
Chris@1295 160 Token.delete_all
Chris@1295 161
Chris@1295 162 post 'account/register', :user => {:login => "newuser", :language => "en", :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar",
Chris@1295 163 :password => "newpass123", :password_confirmation => "newpass123"}
Chris@1295 164 assert_redirected_to '/login'
Chris@1295 165 assert !User.find_by_login('newuser').active?
Chris@1295 166
Chris@1295 167 token = Token.first
Chris@1295 168 assert_equal 'register', token.action
Chris@1295 169 assert_equal 'newuser@foo.bar', token.user.mail
Chris@1295 170 assert !token.expired?
Chris@1295 171
Chris@1295 172 get 'account/activate', :token => token.value
Chris@1295 173 assert_redirected_to '/login'
Chris@1295 174 log_user('newuser', 'newpass123')
Chris@1295 175 end
Chris@1295 176
Chris@1295 177 def test_onthefly_registration
Chris@1295 178 # disable registration
Chris@1295 179 Setting.self_registration = '0'
Chris@1295 180 AuthSource.expects(:authenticate).returns({:login => 'foo', :firstname => 'Foo', :lastname => 'Smith', :mail => 'foo@bar.com', :auth_source_id => 66})
Chris@1295 181
Chris@1295 182 post '/login', :username => 'foo', :password => 'bar'
Chris@1295 183 assert_redirected_to '/my/page'
Chris@1295 184
Chris@1295 185 user = User.find_by_login('foo')
Chris@1295 186 assert user.is_a?(User)
Chris@1295 187 assert_equal 66, user.auth_source_id
Chris@1295 188 assert user.hashed_password.blank?
Chris@1295 189 end
Chris@1295 190
Chris@1295 191 def test_onthefly_registration_with_invalid_attributes
Chris@1295 192 # disable registration
Chris@1295 193 Setting.self_registration = '0'
Chris@1295 194 AuthSource.expects(:authenticate).returns({:login => 'foo', :lastname => 'Smith', :auth_source_id => 66})
Chris@1295 195
Chris@1295 196 post '/login', :username => 'foo', :password => 'bar'
Chris@1295 197 assert_response :success
Chris@1295 198 assert_template 'account/register'
Chris@1295 199 assert_tag :input, :attributes => { :name => 'user[firstname]', :value => '' }
Chris@1295 200 assert_tag :input, :attributes => { :name => 'user[lastname]', :value => 'Smith' }
Chris@1295 201 assert_no_tag :input, :attributes => { :name => 'user[login]' }
Chris@1295 202 assert_no_tag :input, :attributes => { :name => 'user[password]' }
Chris@1295 203
Chris@1295 204 post 'account/register', :user => {:firstname => 'Foo', :lastname => 'Smith', :mail => 'foo@bar.com'}
Chris@1295 205 assert_redirected_to '/my/account'
Chris@1295 206
Chris@1295 207 user = User.find_by_login('foo')
Chris@1295 208 assert user.is_a?(User)
Chris@1295 209 assert_equal 66, user.auth_source_id
Chris@1295 210 assert user.hashed_password.blank?
Chris@1295 211 end
Chris@1295 212 end