annotate test/unit/.svn/text-base/user_test.rb.svn-base @ 0:513646585e45

* Import Redmine trunk SVN rev 3859
author Chris Cannam
date Fri, 23 Jul 2010 15:52:44 +0100
parents
children cca12e1c1fd4
rev   line source
Chris@0 1 # redMine - project management software
Chris@0 2 # Copyright (C) 2006 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@0 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@0 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@0 18 require File.dirname(__FILE__) + '/../test_helper'
Chris@0 19
Chris@0 20 class UserTest < ActiveSupport::TestCase
Chris@0 21 fixtures :users, :members, :projects, :roles, :member_roles, :auth_sources
Chris@0 22
Chris@0 23 def setup
Chris@0 24 @admin = User.find(1)
Chris@0 25 @jsmith = User.find(2)
Chris@0 26 @dlopper = User.find(3)
Chris@0 27 end
Chris@0 28
Chris@0 29 test 'object_daddy creation' do
Chris@0 30 User.generate_with_protected!(:firstname => 'Testing connection')
Chris@0 31 User.generate_with_protected!(:firstname => 'Testing connection')
Chris@0 32 assert_equal 2, User.count(:all, :conditions => {:firstname => 'Testing connection'})
Chris@0 33 end
Chris@0 34
Chris@0 35 def test_truth
Chris@0 36 assert_kind_of User, @jsmith
Chris@0 37 end
Chris@0 38
Chris@0 39 def test_create
Chris@0 40 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
Chris@0 41
Chris@0 42 user.login = "jsmith"
Chris@0 43 user.password, user.password_confirmation = "password", "password"
Chris@0 44 # login uniqueness
Chris@0 45 assert !user.save
Chris@0 46 assert_equal 1, user.errors.count
Chris@0 47
Chris@0 48 user.login = "newuser"
Chris@0 49 user.password, user.password_confirmation = "passwd", "password"
Chris@0 50 # password confirmation
Chris@0 51 assert !user.save
Chris@0 52 assert_equal 1, user.errors.count
Chris@0 53
Chris@0 54 user.password, user.password_confirmation = "password", "password"
Chris@0 55 assert user.save
Chris@0 56 end
Chris@0 57
Chris@0 58 context "User.login" do
Chris@0 59 should "be case-insensitive." do
Chris@0 60 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
Chris@0 61 u.login = 'newuser'
Chris@0 62 u.password, u.password_confirmation = "password", "password"
Chris@0 63 assert u.save
Chris@0 64
Chris@0 65 u = User.new(:firstname => "Similar", :lastname => "User", :mail => "similaruser@somenet.foo")
Chris@0 66 u.login = 'NewUser'
Chris@0 67 u.password, u.password_confirmation = "password", "password"
Chris@0 68 assert !u.save
Chris@0 69 assert_equal I18n.translate('activerecord.errors.messages.taken'), u.errors.on(:login)
Chris@0 70 end
Chris@0 71 end
Chris@0 72
Chris@0 73 def test_mail_uniqueness_should_not_be_case_sensitive
Chris@0 74 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
Chris@0 75 u.login = 'newuser1'
Chris@0 76 u.password, u.password_confirmation = "password", "password"
Chris@0 77 assert u.save
Chris@0 78
Chris@0 79 u = User.new(:firstname => "new", :lastname => "user", :mail => "newUser@Somenet.foo")
Chris@0 80 u.login = 'newuser2'
Chris@0 81 u.password, u.password_confirmation = "password", "password"
Chris@0 82 assert !u.save
Chris@0 83 assert_equal I18n.translate('activerecord.errors.messages.taken'), u.errors.on(:mail)
Chris@0 84 end
Chris@0 85
Chris@0 86 def test_update
Chris@0 87 assert_equal "admin", @admin.login
Chris@0 88 @admin.login = "john"
Chris@0 89 assert @admin.save, @admin.errors.full_messages.join("; ")
Chris@0 90 @admin.reload
Chris@0 91 assert_equal "john", @admin.login
Chris@0 92 end
Chris@0 93
Chris@0 94 def test_destroy
Chris@0 95 User.find(2).destroy
Chris@0 96 assert_nil User.find_by_id(2)
Chris@0 97 assert Member.find_all_by_user_id(2).empty?
Chris@0 98 end
Chris@0 99
Chris@0 100 def test_validate
Chris@0 101 @admin.login = ""
Chris@0 102 assert !@admin.save
Chris@0 103 assert_equal 1, @admin.errors.count
Chris@0 104 end
Chris@0 105
Chris@0 106 context "User#try_to_login" do
Chris@0 107 should "fall-back to case-insensitive if user login is not found as-typed." do
Chris@0 108 user = User.try_to_login("AdMin", "admin")
Chris@0 109 assert_kind_of User, user
Chris@0 110 assert_equal "admin", user.login
Chris@0 111 end
Chris@0 112
Chris@0 113 should "select the exact matching user first" do
Chris@0 114 case_sensitive_user = User.generate_with_protected!(:login => 'changed', :password => 'admin', :password_confirmation => 'admin')
Chris@0 115 # bypass validations to make it appear like existing data
Chris@0 116 case_sensitive_user.update_attribute(:login, 'ADMIN')
Chris@0 117
Chris@0 118 user = User.try_to_login("ADMIN", "admin")
Chris@0 119 assert_kind_of User, user
Chris@0 120 assert_equal "ADMIN", user.login
Chris@0 121
Chris@0 122 end
Chris@0 123 end
Chris@0 124
Chris@0 125 def test_password
Chris@0 126 user = User.try_to_login("admin", "admin")
Chris@0 127 assert_kind_of User, user
Chris@0 128 assert_equal "admin", user.login
Chris@0 129 user.password = "hello"
Chris@0 130 assert user.save
Chris@0 131
Chris@0 132 user = User.try_to_login("admin", "hello")
Chris@0 133 assert_kind_of User, user
Chris@0 134 assert_equal "admin", user.login
Chris@0 135 assert_equal User.hash_password("hello"), user.hashed_password
Chris@0 136 end
Chris@0 137
Chris@0 138 def test_name_format
Chris@0 139 assert_equal 'Smith, John', @jsmith.name(:lastname_coma_firstname)
Chris@0 140 Setting.user_format = :firstname_lastname
Chris@0 141 assert_equal 'John Smith', @jsmith.reload.name
Chris@0 142 Setting.user_format = :username
Chris@0 143 assert_equal 'jsmith', @jsmith.reload.name
Chris@0 144 end
Chris@0 145
Chris@0 146 def test_lock
Chris@0 147 user = User.try_to_login("jsmith", "jsmith")
Chris@0 148 assert_equal @jsmith, user
Chris@0 149
Chris@0 150 @jsmith.status = User::STATUS_LOCKED
Chris@0 151 assert @jsmith.save
Chris@0 152
Chris@0 153 user = User.try_to_login("jsmith", "jsmith")
Chris@0 154 assert_equal nil, user
Chris@0 155 end
Chris@0 156
Chris@0 157 if ldap_configured?
Chris@0 158 context "#try_to_login using LDAP" do
Chris@0 159 context "with failed connection to the LDAP server" do
Chris@0 160 should "return nil" do
Chris@0 161 @auth_source = AuthSourceLdap.find(1)
Chris@0 162 AuthSource.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::LdapError, 'Cannot connect')
Chris@0 163
Chris@0 164 assert_equal nil, User.try_to_login('edavis', 'wrong')
Chris@0 165 end
Chris@0 166 end
Chris@0 167
Chris@0 168 context "with an unsuccessful authentication" do
Chris@0 169 should "return nil" do
Chris@0 170 assert_equal nil, User.try_to_login('edavis', 'wrong')
Chris@0 171 end
Chris@0 172 end
Chris@0 173
Chris@0 174 context "on the fly registration" do
Chris@0 175 setup do
Chris@0 176 @auth_source = AuthSourceLdap.find(1)
Chris@0 177 end
Chris@0 178
Chris@0 179 context "with a successful authentication" do
Chris@0 180 should "create a new user account if it doesn't exist" do
Chris@0 181 assert_difference('User.count') do
Chris@0 182 user = User.try_to_login('edavis', '123456')
Chris@0 183 assert !user.admin?
Chris@0 184 end
Chris@0 185 end
Chris@0 186
Chris@0 187 should "retrieve existing user" do
Chris@0 188 user = User.try_to_login('edavis', '123456')
Chris@0 189 user.admin = true
Chris@0 190 user.save!
Chris@0 191
Chris@0 192 assert_no_difference('User.count') do
Chris@0 193 user = User.try_to_login('edavis', '123456')
Chris@0 194 assert user.admin?
Chris@0 195 end
Chris@0 196 end
Chris@0 197 end
Chris@0 198 end
Chris@0 199 end
Chris@0 200
Chris@0 201 else
Chris@0 202 puts "Skipping LDAP tests."
Chris@0 203 end
Chris@0 204
Chris@0 205 def test_create_anonymous
Chris@0 206 AnonymousUser.delete_all
Chris@0 207 anon = User.anonymous
Chris@0 208 assert !anon.new_record?
Chris@0 209 assert_kind_of AnonymousUser, anon
Chris@0 210 end
Chris@0 211
Chris@0 212 should_have_one :rss_token
Chris@0 213
Chris@0 214 def test_rss_key
Chris@0 215 assert_nil @jsmith.rss_token
Chris@0 216 key = @jsmith.rss_key
Chris@0 217 assert_equal 40, key.length
Chris@0 218
Chris@0 219 @jsmith.reload
Chris@0 220 assert_equal key, @jsmith.rss_key
Chris@0 221 end
Chris@0 222
Chris@0 223
Chris@0 224 should_have_one :api_token
Chris@0 225
Chris@0 226 context "User#api_key" do
Chris@0 227 should "generate a new one if the user doesn't have one" do
Chris@0 228 user = User.generate_with_protected!(:api_token => nil)
Chris@0 229 assert_nil user.api_token
Chris@0 230
Chris@0 231 key = user.api_key
Chris@0 232 assert_equal 40, key.length
Chris@0 233 user.reload
Chris@0 234 assert_equal key, user.api_key
Chris@0 235 end
Chris@0 236
Chris@0 237 should "return the existing api token value" do
Chris@0 238 user = User.generate_with_protected!
Chris@0 239 token = Token.generate!(:action => 'api')
Chris@0 240 user.api_token = token
Chris@0 241 assert user.save
Chris@0 242
Chris@0 243 assert_equal token.value, user.api_key
Chris@0 244 end
Chris@0 245 end
Chris@0 246
Chris@0 247 context "User#find_by_api_key" do
Chris@0 248 should "return nil if no matching key is found" do
Chris@0 249 assert_nil User.find_by_api_key('zzzzzzzzz')
Chris@0 250 end
Chris@0 251
Chris@0 252 should "return nil if the key is found for an inactive user" do
Chris@0 253 user = User.generate_with_protected!(:status => User::STATUS_LOCKED)
Chris@0 254 token = Token.generate!(:action => 'api')
Chris@0 255 user.api_token = token
Chris@0 256 user.save
Chris@0 257
Chris@0 258 assert_nil User.find_by_api_key(token.value)
Chris@0 259 end
Chris@0 260
Chris@0 261 should "return the user if the key is found for an active user" do
Chris@0 262 user = User.generate_with_protected!(:status => User::STATUS_ACTIVE)
Chris@0 263 token = Token.generate!(:action => 'api')
Chris@0 264 user.api_token = token
Chris@0 265 user.save
Chris@0 266
Chris@0 267 assert_equal user, User.find_by_api_key(token.value)
Chris@0 268 end
Chris@0 269 end
Chris@0 270
Chris@0 271 def test_roles_for_project
Chris@0 272 # user with a role
Chris@0 273 roles = @jsmith.roles_for_project(Project.find(1))
Chris@0 274 assert_kind_of Role, roles.first
Chris@0 275 assert_equal "Manager", roles.first.name
Chris@0 276
Chris@0 277 # user with no role
Chris@0 278 assert_nil @dlopper.roles_for_project(Project.find(2)).detect {|role| role.member?}
Chris@0 279 end
Chris@0 280
Chris@0 281 def test_mail_notification_all
Chris@0 282 @jsmith.mail_notification = true
Chris@0 283 @jsmith.notified_project_ids = []
Chris@0 284 @jsmith.save
Chris@0 285 @jsmith.reload
Chris@0 286 assert @jsmith.projects.first.recipients.include?(@jsmith.mail)
Chris@0 287 end
Chris@0 288
Chris@0 289 def test_mail_notification_selected
Chris@0 290 @jsmith.mail_notification = false
Chris@0 291 @jsmith.notified_project_ids = [1]
Chris@0 292 @jsmith.save
Chris@0 293 @jsmith.reload
Chris@0 294 assert Project.find(1).recipients.include?(@jsmith.mail)
Chris@0 295 end
Chris@0 296
Chris@0 297 def test_mail_notification_none
Chris@0 298 @jsmith.mail_notification = false
Chris@0 299 @jsmith.notified_project_ids = []
Chris@0 300 @jsmith.save
Chris@0 301 @jsmith.reload
Chris@0 302 assert !@jsmith.projects.first.recipients.include?(@jsmith.mail)
Chris@0 303 end
Chris@0 304
Chris@0 305 def test_comments_sorting_preference
Chris@0 306 assert !@jsmith.wants_comments_in_reverse_order?
Chris@0 307 @jsmith.pref.comments_sorting = 'asc'
Chris@0 308 assert !@jsmith.wants_comments_in_reverse_order?
Chris@0 309 @jsmith.pref.comments_sorting = 'desc'
Chris@0 310 assert @jsmith.wants_comments_in_reverse_order?
Chris@0 311 end
Chris@0 312
Chris@0 313 def test_find_by_mail_should_be_case_insensitive
Chris@0 314 u = User.find_by_mail('JSmith@somenet.foo')
Chris@0 315 assert_not_nil u
Chris@0 316 assert_equal 'jsmith@somenet.foo', u.mail
Chris@0 317 end
Chris@0 318
Chris@0 319 def test_random_password
Chris@0 320 u = User.new
Chris@0 321 u.random_password
Chris@0 322 assert !u.password.blank?
Chris@0 323 assert !u.password_confirmation.blank?
Chris@0 324 end
Chris@0 325
Chris@0 326 context "#change_password_allowed?" do
Chris@0 327 should "be allowed if no auth source is set" do
Chris@0 328 user = User.generate_with_protected!
Chris@0 329 assert user.change_password_allowed?
Chris@0 330 end
Chris@0 331
Chris@0 332 should "delegate to the auth source" do
Chris@0 333 user = User.generate_with_protected!
Chris@0 334
Chris@0 335 allowed_auth_source = AuthSource.generate!
Chris@0 336 def allowed_auth_source.allow_password_changes?; true; end
Chris@0 337
Chris@0 338 denied_auth_source = AuthSource.generate!
Chris@0 339 def denied_auth_source.allow_password_changes?; false; end
Chris@0 340
Chris@0 341 assert user.change_password_allowed?
Chris@0 342
Chris@0 343 user.auth_source = allowed_auth_source
Chris@0 344 assert user.change_password_allowed?, "User not allowed to change password, though auth source does"
Chris@0 345
Chris@0 346 user.auth_source = denied_auth_source
Chris@0 347 assert !user.change_password_allowed?, "User allowed to change password, though auth source does not"
Chris@0 348 end
Chris@0 349
Chris@0 350 end
Chris@0 351
Chris@0 352 if Object.const_defined?(:OpenID)
Chris@0 353
Chris@0 354 def test_setting_identity_url
Chris@0 355 normalized_open_id_url = 'http://example.com/'
Chris@0 356 u = User.new( :identity_url => 'http://example.com/' )
Chris@0 357 assert_equal normalized_open_id_url, u.identity_url
Chris@0 358 end
Chris@0 359
Chris@0 360 def test_setting_identity_url_without_trailing_slash
Chris@0 361 normalized_open_id_url = 'http://example.com/'
Chris@0 362 u = User.new( :identity_url => 'http://example.com' )
Chris@0 363 assert_equal normalized_open_id_url, u.identity_url
Chris@0 364 end
Chris@0 365
Chris@0 366 def test_setting_identity_url_without_protocol
Chris@0 367 normalized_open_id_url = 'http://example.com/'
Chris@0 368 u = User.new( :identity_url => 'example.com' )
Chris@0 369 assert_equal normalized_open_id_url, u.identity_url
Chris@0 370 end
Chris@0 371
Chris@0 372 def test_setting_blank_identity_url
Chris@0 373 u = User.new( :identity_url => 'example.com' )
Chris@0 374 u.identity_url = ''
Chris@0 375 assert u.identity_url.blank?
Chris@0 376 end
Chris@0 377
Chris@0 378 def test_setting_invalid_identity_url
Chris@0 379 u = User.new( :identity_url => 'this is not an openid url' )
Chris@0 380 assert u.identity_url.blank?
Chris@0 381 end
Chris@0 382
Chris@0 383 else
Chris@0 384 puts "Skipping openid tests."
Chris@0 385 end
Chris@0 386
Chris@0 387 end