annotate .svn/pristine/70/70ab74ca7311c5ad448162e1787a3cb96a72a2f0.svn-base @ 1621:3a510bf6a9bc

Merge from live branch
author Chris Cannam
date Fri, 13 Jul 2018 10:44:33 +0100
parents e248c7af89ec
children
rev   line source
Chris@1494 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@1494 3 #
Chris@1494 4 # This program is free software; you can redistribute it and/or
Chris@1494 5 # modify it under the terms of the GNU General Public License
Chris@1494 6 # as published by the Free Software Foundation; either version 2
Chris@1494 7 # of the License, or (at your option) any later version.
Chris@1494 8 #
Chris@1494 9 # This program is distributed in the hope that it will be useful,
Chris@1494 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1494 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1494 12 # GNU General Public License for more details.
Chris@1494 13 #
Chris@1494 14 # You should have received a copy of the GNU General Public License
Chris@1494 15 # along with this program; if not, write to the Free Software
Chris@1494 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1494 17
Chris@1494 18 require File.expand_path('../../test_helper', __FILE__)
Chris@1494 19
Chris@1494 20 begin
Chris@1494 21 require 'mocha/setup'
Chris@1494 22 rescue
Chris@1494 23 # Won't run some tests
Chris@1494 24 end
Chris@1494 25
Chris@1494 26 class AccountTest < ActionController::IntegrationTest
Chris@1494 27 fixtures :users, :roles
Chris@1494 28
Chris@1494 29 # Replace this with your real tests.
Chris@1494 30 def test_login
Chris@1494 31 get "my/page"
Chris@1494 32 assert_redirected_to "/login?back_url=http%3A%2F%2Fwww.example.com%2Fmy%2Fpage"
Chris@1494 33 log_user('jsmith', 'jsmith')
Chris@1494 34
Chris@1494 35 get "my/account"
Chris@1494 36 assert_response :success
Chris@1494 37 assert_template "my/account"
Chris@1494 38 end
Chris@1494 39
Chris@1494 40 def test_autologin
Chris@1494 41 user = User.find(1)
Chris@1494 42 Setting.autologin = "7"
Chris@1494 43 Token.delete_all
Chris@1494 44
Chris@1494 45 # User logs in with 'autologin' checked
Chris@1494 46 post '/login', :username => user.login, :password => 'admin', :autologin => 1
Chris@1494 47 assert_redirected_to '/my/page'
Chris@1494 48 token = Token.first
Chris@1494 49 assert_not_nil token
Chris@1494 50 assert_equal user, token.user
Chris@1494 51 assert_equal 'autologin', token.action
Chris@1494 52 assert_equal user.id, session[:user_id]
Chris@1494 53 assert_equal token.value, cookies['autologin']
Chris@1494 54
Chris@1494 55 # Session is cleared
Chris@1494 56 reset!
Chris@1494 57 User.current = nil
Chris@1494 58 # Clears user's last login timestamp
Chris@1494 59 user.update_attribute :last_login_on, nil
Chris@1494 60 assert_nil user.reload.last_login_on
Chris@1494 61
Chris@1494 62 # User comes back with user's autologin cookie
Chris@1494 63 cookies[:autologin] = token.value
Chris@1494 64 get '/my/page'
Chris@1494 65 assert_response :success
Chris@1494 66 assert_template 'my/page'
Chris@1494 67 assert_equal user.id, session[:user_id]
Chris@1494 68 assert_not_nil user.reload.last_login_on
Chris@1494 69 end
Chris@1494 70
Chris@1494 71 def test_autologin_should_use_autologin_cookie_name
Chris@1494 72 Token.delete_all
Chris@1494 73 Redmine::Configuration.stubs(:[]).with('autologin_cookie_name').returns('custom_autologin')
Chris@1494 74 Redmine::Configuration.stubs(:[]).with('autologin_cookie_path').returns('/')
Chris@1494 75 Redmine::Configuration.stubs(:[]).with('autologin_cookie_secure').returns(false)
Chris@1494 76
Chris@1494 77 with_settings :autologin => '7' do
Chris@1494 78 assert_difference 'Token.count' do
Chris@1494 79 post '/login', :username => 'admin', :password => 'admin', :autologin => 1
Chris@1494 80 end
Chris@1494 81 assert_response 302
Chris@1494 82 assert cookies['custom_autologin'].present?
Chris@1494 83 token = cookies['custom_autologin']
Chris@1494 84
Chris@1494 85 # Session is cleared
Chris@1494 86 reset!
Chris@1494 87 cookies['custom_autologin'] = token
Chris@1494 88 get '/my/page'
Chris@1494 89 assert_response :success
Chris@1494 90
Chris@1494 91 assert_difference 'Token.count', -1 do
Chris@1494 92 post '/logout'
Chris@1494 93 end
Chris@1494 94 assert cookies['custom_autologin'].blank?
Chris@1494 95 end
Chris@1494 96 end
Chris@1494 97
Chris@1494 98 def test_lost_password
Chris@1494 99 Token.delete_all
Chris@1494 100
Chris@1494 101 get "account/lost_password"
Chris@1494 102 assert_response :success
Chris@1494 103 assert_template "account/lost_password"
Chris@1494 104 assert_select 'input[name=mail]'
Chris@1494 105
Chris@1494 106 post "account/lost_password", :mail => 'jSmith@somenet.foo'
Chris@1494 107 assert_redirected_to "/login"
Chris@1494 108
Chris@1494 109 token = Token.first
Chris@1494 110 assert_equal 'recovery', token.action
Chris@1494 111 assert_equal 'jsmith@somenet.foo', token.user.mail
Chris@1494 112 assert !token.expired?
Chris@1494 113
Chris@1494 114 get "account/lost_password", :token => token.value
Chris@1494 115 assert_response :success
Chris@1494 116 assert_template "account/password_recovery"
Chris@1494 117 assert_select 'input[type=hidden][name=token][value=?]', token.value
Chris@1494 118 assert_select 'input[name=new_password]'
Chris@1494 119 assert_select 'input[name=new_password_confirmation]'
Chris@1494 120
Chris@1494 121 post "account/lost_password",
Chris@1494 122 :token => token.value, :new_password => 'newpass123',
Chris@1494 123 :new_password_confirmation => 'newpass123'
Chris@1494 124 assert_redirected_to "/login"
Chris@1494 125 assert_equal 'Password was successfully updated.', flash[:notice]
Chris@1494 126
Chris@1494 127 log_user('jsmith', 'newpass123')
Chris@1494 128 assert_equal 0, Token.count
Chris@1494 129 end
Chris@1494 130
Chris@1494 131 def test_user_with_must_change_passwd_should_be_forced_to_change_its_password
Chris@1494 132 User.find_by_login('jsmith').update_attribute :must_change_passwd, true
Chris@1494 133
Chris@1494 134 post '/login', :username => 'jsmith', :password => 'jsmith'
Chris@1494 135 assert_redirected_to '/my/page'
Chris@1494 136 follow_redirect!
Chris@1494 137 assert_redirected_to '/my/password'
Chris@1494 138
Chris@1494 139 get '/issues'
Chris@1494 140 assert_redirected_to '/my/password'
Chris@1494 141 end
Chris@1494 142
Chris@1494 143 def test_user_with_must_change_passwd_should_be_able_to_change_its_password
Chris@1494 144 User.find_by_login('jsmith').update_attribute :must_change_passwd, true
Chris@1494 145
Chris@1494 146 post '/login', :username => 'jsmith', :password => 'jsmith'
Chris@1494 147 assert_redirected_to '/my/page'
Chris@1494 148 follow_redirect!
Chris@1494 149 assert_redirected_to '/my/password'
Chris@1494 150 follow_redirect!
Chris@1494 151 assert_response :success
Chris@1494 152 post '/my/password', :password => 'jsmith', :new_password => 'newpassword', :new_password_confirmation => 'newpassword'
Chris@1494 153 assert_redirected_to '/my/account'
Chris@1494 154 follow_redirect!
Chris@1494 155 assert_response :success
Chris@1494 156
Chris@1494 157 assert_equal false, User.find_by_login('jsmith').must_change_passwd?
Chris@1494 158 end
Chris@1494 159
Chris@1494 160 def test_register_with_automatic_activation
Chris@1494 161 Setting.self_registration = '3'
Chris@1494 162
Chris@1494 163 get 'account/register'
Chris@1494 164 assert_response :success
Chris@1494 165 assert_template 'account/register'
Chris@1494 166
Chris@1494 167 post 'account/register',
Chris@1494 168 :user => {:login => "newuser", :language => "en",
Chris@1494 169 :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar",
Chris@1494 170 :password => "newpass123", :password_confirmation => "newpass123"}
Chris@1494 171 assert_redirected_to '/my/account'
Chris@1494 172 follow_redirect!
Chris@1494 173 assert_response :success
Chris@1494 174 assert_template 'my/account'
Chris@1494 175
Chris@1494 176 user = User.find_by_login('newuser')
Chris@1494 177 assert_not_nil user
Chris@1494 178 assert user.active?
Chris@1494 179 assert_not_nil user.last_login_on
Chris@1494 180 end
Chris@1494 181
Chris@1494 182 def test_register_with_manual_activation
Chris@1494 183 Setting.self_registration = '2'
Chris@1494 184
Chris@1494 185 post 'account/register',
Chris@1494 186 :user => {:login => "newuser", :language => "en",
Chris@1494 187 :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar",
Chris@1494 188 :password => "newpass123", :password_confirmation => "newpass123"}
Chris@1494 189 assert_redirected_to '/login'
Chris@1494 190 assert !User.find_by_login('newuser').active?
Chris@1494 191 end
Chris@1494 192
Chris@1494 193 def test_register_with_email_activation
Chris@1494 194 Setting.self_registration = '1'
Chris@1494 195 Token.delete_all
Chris@1494 196
Chris@1494 197 post 'account/register',
Chris@1494 198 :user => {:login => "newuser", :language => "en",
Chris@1494 199 :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar",
Chris@1494 200 :password => "newpass123", :password_confirmation => "newpass123"}
Chris@1494 201 assert_redirected_to '/login'
Chris@1494 202 assert !User.find_by_login('newuser').active?
Chris@1494 203
Chris@1494 204 token = Token.first
Chris@1494 205 assert_equal 'register', token.action
Chris@1494 206 assert_equal 'newuser@foo.bar', token.user.mail
Chris@1494 207 assert !token.expired?
Chris@1494 208
Chris@1494 209 get 'account/activate', :token => token.value
Chris@1494 210 assert_redirected_to '/login'
Chris@1494 211 log_user('newuser', 'newpass123')
Chris@1494 212 end
Chris@1494 213
Chris@1494 214 def test_onthefly_registration
Chris@1494 215 # disable registration
Chris@1494 216 Setting.self_registration = '0'
Chris@1494 217 AuthSource.expects(:authenticate).returns(
Chris@1494 218 {:login => 'foo', :firstname => 'Foo', :lastname => 'Smith',
Chris@1494 219 :mail => 'foo@bar.com', :auth_source_id => 66})
Chris@1494 220
Chris@1494 221 post '/login', :username => 'foo', :password => 'bar'
Chris@1494 222 assert_redirected_to '/my/page'
Chris@1494 223
Chris@1494 224 user = User.find_by_login('foo')
Chris@1494 225 assert user.is_a?(User)
Chris@1494 226 assert_equal 66, user.auth_source_id
Chris@1494 227 assert user.hashed_password.blank?
Chris@1494 228 end
Chris@1494 229
Chris@1494 230 def test_onthefly_registration_with_invalid_attributes
Chris@1494 231 # disable registration
Chris@1494 232 Setting.self_registration = '0'
Chris@1494 233 AuthSource.expects(:authenticate).returns(
Chris@1494 234 {:login => 'foo', :lastname => 'Smith', :auth_source_id => 66})
Chris@1494 235
Chris@1494 236 post '/login', :username => 'foo', :password => 'bar'
Chris@1494 237 assert_response :success
Chris@1494 238 assert_template 'account/register'
Chris@1494 239 assert_tag :input, :attributes => { :name => 'user[firstname]', :value => '' }
Chris@1494 240 assert_tag :input, :attributes => { :name => 'user[lastname]', :value => 'Smith' }
Chris@1494 241 assert_no_tag :input, :attributes => { :name => 'user[login]' }
Chris@1494 242 assert_no_tag :input, :attributes => { :name => 'user[password]' }
Chris@1494 243
Chris@1494 244 post 'account/register',
Chris@1494 245 :user => {:firstname => 'Foo', :lastname => 'Smith', :mail => 'foo@bar.com'}
Chris@1494 246 assert_redirected_to '/my/account'
Chris@1494 247
Chris@1494 248 user = User.find_by_login('foo')
Chris@1494 249 assert user.is_a?(User)
Chris@1494 250 assert_equal 66, user.auth_source_id
Chris@1494 251 assert user.hashed_password.blank?
Chris@1494 252 end
Chris@1494 253
Chris@1494 254 def test_registered_user_should_be_able_to_get_a_new_activation_email
Chris@1494 255 Token.delete_all
Chris@1494 256
Chris@1494 257 with_settings :self_registration => '1', :default_language => 'en' do
Chris@1494 258 # register a new account
Chris@1494 259 assert_difference 'User.count' do
Chris@1494 260 assert_difference 'Token.count' do
Chris@1494 261 post 'account/register',
Chris@1494 262 :user => {:login => "newuser", :language => "en",
Chris@1494 263 :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar",
Chris@1494 264 :password => "newpass123", :password_confirmation => "newpass123"}
Chris@1494 265 end
Chris@1494 266 end
Chris@1494 267 user = User.order('id desc').first
Chris@1494 268 assert_equal User::STATUS_REGISTERED, user.status
Chris@1494 269 reset!
Chris@1494 270
Chris@1494 271 # try to use "lost password"
Chris@1494 272 assert_no_difference 'ActionMailer::Base.deliveries.size' do
Chris@1494 273 post '/account/lost_password', :mail => 'newuser@foo.bar'
Chris@1494 274 end
Chris@1494 275 assert_redirected_to '/account/lost_password'
Chris@1494 276 follow_redirect!
Chris@1494 277 assert_response :success
Chris@1494 278 assert_select 'div.flash', :text => /new activation email/
Chris@1494 279 assert_select 'div.flash a[href=/account/activation_email]'
Chris@1494 280
Chris@1494 281 # request a new action activation email
Chris@1494 282 assert_difference 'ActionMailer::Base.deliveries.size' do
Chris@1494 283 get '/account/activation_email'
Chris@1494 284 end
Chris@1494 285 assert_redirected_to '/login'
Chris@1494 286 token = Token.order('id desc').first
Chris@1494 287 activation_path = "/account/activate?token=#{token.value}"
Chris@1494 288 assert_include activation_path, mail_body(ActionMailer::Base.deliveries.last)
Chris@1494 289
Chris@1494 290 # activate the account
Chris@1494 291 get activation_path
Chris@1494 292 assert_redirected_to '/login'
Chris@1494 293
Chris@1494 294 post '/login', :username => 'newuser', :password => 'newpass123'
Chris@1494 295 assert_redirected_to '/my/page'
Chris@1494 296 end
Chris@1494 297 end
Chris@1494 298 end