annotate test/unit/user_test.rb @ 1628:9c5f8e24dadc live tip

Quieten this cron script
author Chris Cannam
date Tue, 25 Aug 2020 11:38:49 +0100
parents dffacf8a6908
children
rev   line source
Chris@128 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 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
Chris@0 20 class UserTest < ActiveSupport::TestCase
Chris@909 21 fixtures :users, :members, :projects, :roles, :member_roles, :auth_sources,
Chris@909 22 :trackers, :issue_statuses,
Chris@909 23 :projects_trackers,
Chris@909 24 :watchers,
Chris@909 25 :issue_categories, :enumerations, :issues,
Chris@909 26 :journals, :journal_details,
Chris@909 27 :groups_users,
Chris@1464 28 :enabled_modules
Chris@0 29
Chris@0 30 def setup
Chris@0 31 @admin = User.find(1)
Chris@0 32 @jsmith = User.find(2)
Chris@0 33 @dlopper = User.find(3)
Chris@0 34 end
Chris@0 35
Chris@1464 36 def test_sorted_scope_should_sort_user_by_display_name
Chris@1517 37 assert_equal User.all.map(&:name).map(&:downcase).sort,
Chris@1517 38 User.sorted.map(&:name).map(&:downcase)
Chris@1464 39 end
Chris@1464 40
Chris@1115 41 def test_generate
Chris@1115 42 User.generate!(:firstname => 'Testing connection')
Chris@1115 43 User.generate!(:firstname => 'Testing connection')
Chris@1464 44 assert_equal 2, User.where(:firstname => 'Testing connection').count
Chris@0 45 end
Chris@909 46
Chris@0 47 def test_truth
Chris@0 48 assert_kind_of User, @jsmith
Chris@0 49 end
Chris@909 50
Chris@1 51 def test_mail_should_be_stripped
Chris@1 52 u = User.new
Chris@1 53 u.mail = " foo@bar.com "
Chris@1 54 assert_equal "foo@bar.com", u.mail
Chris@1 55 end
Chris@0 56
Chris@909 57 def test_mail_validation
Chris@909 58 u = User.new
Chris@909 59 u.mail = ''
Chris@909 60 assert !u.valid?
Chris@1115 61 assert_include I18n.translate('activerecord.errors.messages.blank'), u.errors[:mail]
Chris@1115 62 end
Chris@1115 63
Chris@1115 64 def test_login_length_validation
Chris@1115 65 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
Chris@1115 66 user.login = "x" * (User::LOGIN_LENGTH_LIMIT+1)
Chris@1115 67 assert !user.valid?
Chris@1115 68
Chris@1115 69 user.login = "x" * (User::LOGIN_LENGTH_LIMIT)
Chris@1115 70 assert user.valid?
Chris@1115 71 assert user.save
Chris@909 72 end
Chris@909 73
Chris@1464 74 def test_generate_password_should_respect_minimum_password_length
Chris@1464 75 with_settings :password_min_length => 15 do
Chris@1464 76 user = User.generate!(:generate_password => true)
Chris@1464 77 assert user.password.length >= 15
Chris@1464 78 end
Chris@1464 79 end
Chris@1464 80
Chris@1464 81 def test_generate_password_should_not_generate_password_with_less_than_10_characters
Chris@1464 82 with_settings :password_min_length => 4 do
Chris@1464 83 user = User.generate!(:generate_password => true)
Chris@1464 84 assert user.password.length >= 10
Chris@1464 85 end
Chris@1464 86 end
Chris@1464 87
Chris@1464 88 def test_generate_password_on_create_should_set_password
Chris@1464 89 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
Chris@1464 90 user.login = "newuser"
Chris@1464 91 user.generate_password = true
Chris@1464 92 assert user.save
Chris@1464 93
Chris@1464 94 password = user.password
Chris@1464 95 assert user.check_password?(password)
Chris@1464 96 end
Chris@1464 97
Chris@1464 98 def test_generate_password_on_update_should_update_password
Chris@1464 99 user = User.find(2)
Chris@1464 100 hash = user.hashed_password
Chris@1464 101 user.generate_password = true
Chris@1464 102 assert user.save
Chris@1464 103
Chris@1464 104 password = user.password
Chris@1464 105 assert user.check_password?(password)
Chris@1464 106 assert_not_equal hash, user.reload.hashed_password
Chris@1464 107 end
Chris@1464 108
Chris@0 109 def test_create
Chris@0 110 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
Chris@909 111
Chris@0 112 user.login = "jsmith"
Chris@0 113 user.password, user.password_confirmation = "password", "password"
Chris@0 114 # login uniqueness
Chris@0 115 assert !user.save
Chris@0 116 assert_equal 1, user.errors.count
Chris@909 117
Chris@0 118 user.login = "newuser"
Chris@1115 119 user.password, user.password_confirmation = "password", "pass"
Chris@0 120 # password confirmation
Chris@0 121 assert !user.save
Chris@0 122 assert_equal 1, user.errors.count
Chris@0 123
Chris@0 124 user.password, user.password_confirmation = "password", "password"
Chris@0 125 assert user.save
Chris@0 126 end
chris@37 127
Chris@1115 128 def test_user_before_create_should_set_the_mail_notification_to_the_default_setting
Chris@1115 129 @user1 = User.generate!
Chris@1115 130 assert_equal 'only_my_events', @user1.mail_notification
Chris@1115 131 with_settings :default_notification_option => 'all' do
Chris@1115 132 @user2 = User.generate!
Chris@1115 133 assert_equal 'all', @user2.mail_notification
chris@37 134 end
chris@37 135 end
Chris@909 136
Chris@1115 137 def test_user_login_should_be_case_insensitive
Chris@1115 138 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
Chris@1115 139 u.login = 'newuser'
Chris@1115 140 u.password, u.password_confirmation = "password", "password"
Chris@1115 141 assert u.save
Chris@1517 142 u = User.new(:firstname => "Similar", :lastname => "User",
Chris@1517 143 :mail => "similaruser@somenet.foo")
Chris@1115 144 u.login = 'NewUser'
Chris@1115 145 u.password, u.password_confirmation = "password", "password"
Chris@1115 146 assert !u.save
Chris@1115 147 assert_include I18n.translate('activerecord.errors.messages.taken'), u.errors[:login]
Chris@0 148 end
Chris@0 149
Chris@0 150 def test_mail_uniqueness_should_not_be_case_sensitive
Chris@0 151 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
Chris@0 152 u.login = 'newuser1'
Chris@0 153 u.password, u.password_confirmation = "password", "password"
Chris@0 154 assert u.save
Chris@909 155
Chris@0 156 u = User.new(:firstname => "new", :lastname => "user", :mail => "newUser@Somenet.foo")
Chris@0 157 u.login = 'newuser2'
Chris@0 158 u.password, u.password_confirmation = "password", "password"
Chris@0 159 assert !u.save
Chris@1115 160 assert_include I18n.translate('activerecord.errors.messages.taken'), u.errors[:mail]
Chris@0 161 end
Chris@0 162
Chris@0 163 def test_update
Chris@0 164 assert_equal "admin", @admin.login
Chris@0 165 @admin.login = "john"
Chris@0 166 assert @admin.save, @admin.errors.full_messages.join("; ")
Chris@0 167 @admin.reload
Chris@0 168 assert_equal "john", @admin.login
Chris@0 169 end
Chris@909 170
Chris@1115 171 def test_update_should_not_fail_for_legacy_user_with_different_case_logins
Chris@1115 172 u1 = User.new(:firstname => "new", :lastname => "user", :mail => "newuser1@somenet.foo")
Chris@1115 173 u1.login = 'newuser1'
Chris@1115 174 assert u1.save
Chris@1115 175
Chris@1115 176 u2 = User.new(:firstname => "new", :lastname => "user", :mail => "newuser2@somenet.foo")
Chris@1115 177 u2.login = 'newuser1'
Chris@1115 178 assert u2.save(:validate => false)
Chris@1115 179
Chris@1115 180 user = User.find(u2.id)
Chris@1115 181 user.firstname = "firstname"
Chris@1115 182 assert user.save, "Save failed"
Chris@1115 183 end
Chris@1115 184
Chris@128 185 def test_destroy_should_delete_members_and_roles
Chris@1517 186 members = Member.where(:user_id => 2)
Chris@1517 187 ms = members.count
Chris@128 188 rs = members.collect(&:roles).flatten.size
Chris@1517 189 assert ms > 0
Chris@1517 190 assert rs > 0
Chris@128 191 assert_difference 'Member.count', - ms do
Chris@128 192 assert_difference 'MemberRole.count', - rs do
Chris@128 193 User.find(2).destroy
Chris@128 194 end
Chris@128 195 end
Chris@128 196 assert_nil User.find_by_id(2)
Chris@1517 197 assert_equal 0, Member.where(:user_id => 2).count
Chris@128 198 end
Chris@909 199
Chris@128 200 def test_destroy_should_update_attachments
Chris@128 201 attachment = Attachment.create!(:container => Project.find(1),
Chris@128 202 :file => uploaded_test_file("testfile.txt", "text/plain"),
Chris@128 203 :author_id => 2)
Chris@909 204
Chris@0 205 User.find(2).destroy
Chris@0 206 assert_nil User.find_by_id(2)
Chris@128 207 assert_equal User.anonymous, attachment.reload.author
Chris@128 208 end
Chris@909 209
Chris@128 210 def test_destroy_should_update_comments
Chris@128 211 comment = Comment.create!(
Chris@1517 212 :commented => News.create!(:project_id => 1,
Chris@1517 213 :author_id => 1, :title => 'foo', :description => 'foo'),
Chris@128 214 :author => User.find(2),
Chris@128 215 :comments => 'foo'
Chris@128 216 )
Chris@909 217
Chris@128 218 User.find(2).destroy
Chris@128 219 assert_nil User.find_by_id(2)
Chris@128 220 assert_equal User.anonymous, comment.reload.author
Chris@128 221 end
Chris@909 222
Chris@128 223 def test_destroy_should_update_issues
Chris@1517 224 issue = Issue.create!(:project_id => 1, :author_id => 2,
Chris@1517 225 :tracker_id => 1, :subject => 'foo')
Chris@909 226
Chris@128 227 User.find(2).destroy
Chris@128 228 assert_nil User.find_by_id(2)
Chris@128 229 assert_equal User.anonymous, issue.reload.author
Chris@128 230 end
Chris@909 231
Chris@128 232 def test_destroy_should_unassign_issues
Chris@1517 233 issue = Issue.create!(:project_id => 1, :author_id => 1,
Chris@1517 234 :tracker_id => 1, :subject => 'foo', :assigned_to_id => 2)
Chris@909 235
Chris@128 236 User.find(2).destroy
Chris@128 237 assert_nil User.find_by_id(2)
Chris@128 238 assert_nil issue.reload.assigned_to
Chris@128 239 end
Chris@909 240
Chris@128 241 def test_destroy_should_update_journals
Chris@1517 242 issue = Issue.create!(:project_id => 1, :author_id => 2,
Chris@1517 243 :tracker_id => 1, :subject => 'foo')
Chris@128 244 issue.init_journal(User.find(2), "update")
Chris@128 245 issue.save!
Chris@909 246
Chris@128 247 User.find(2).destroy
Chris@128 248 assert_nil User.find_by_id(2)
Chris@128 249 assert_equal User.anonymous, issue.journals.first.reload.user
Chris@128 250 end
Chris@909 251
Chris@128 252 def test_destroy_should_update_journal_details_old_value
Chris@1517 253 issue = Issue.create!(:project_id => 1, :author_id => 1,
Chris@1517 254 :tracker_id => 1, :subject => 'foo', :assigned_to_id => 2)
Chris@128 255 issue.init_journal(User.find(1), "update")
Chris@128 256 issue.assigned_to_id = nil
Chris@128 257 assert_difference 'JournalDetail.count' do
Chris@128 258 issue.save!
Chris@128 259 end
Chris@1517 260 journal_detail = JournalDetail.order('id DESC').first
Chris@128 261 assert_equal '2', journal_detail.old_value
Chris@909 262
Chris@128 263 User.find(2).destroy
Chris@128 264 assert_nil User.find_by_id(2)
Chris@128 265 assert_equal User.anonymous.id.to_s, journal_detail.reload.old_value
Chris@128 266 end
Chris@909 267
Chris@128 268 def test_destroy_should_update_journal_details_value
Chris@1517 269 issue = Issue.create!(:project_id => 1, :author_id => 1,
Chris@1517 270 :tracker_id => 1, :subject => 'foo')
Chris@128 271 issue.init_journal(User.find(1), "update")
Chris@128 272 issue.assigned_to_id = 2
Chris@128 273 assert_difference 'JournalDetail.count' do
Chris@128 274 issue.save!
Chris@128 275 end
Chris@1517 276 journal_detail = JournalDetail.order('id DESC').first
Chris@128 277 assert_equal '2', journal_detail.value
Chris@909 278
Chris@128 279 User.find(2).destroy
Chris@128 280 assert_nil User.find_by_id(2)
Chris@128 281 assert_equal User.anonymous.id.to_s, journal_detail.reload.value
Chris@128 282 end
Chris@909 283
Chris@128 284 def test_destroy_should_update_messages
Chris@128 285 board = Board.create!(:project_id => 1, :name => 'Board', :description => 'Board')
Chris@1517 286 message = Message.create!(:board_id => board.id, :author_id => 2,
Chris@1517 287 :subject => 'foo', :content => 'foo')
Chris@128 288 User.find(2).destroy
Chris@128 289 assert_nil User.find_by_id(2)
Chris@128 290 assert_equal User.anonymous, message.reload.author
Chris@128 291 end
Chris@909 292
Chris@128 293 def test_destroy_should_update_news
Chris@1517 294 news = News.create!(:project_id => 1, :author_id => 2,
Chris@1517 295 :title => 'foo', :description => 'foo')
Chris@128 296 User.find(2).destroy
Chris@128 297 assert_nil User.find_by_id(2)
Chris@128 298 assert_equal User.anonymous, news.reload.author
Chris@128 299 end
Chris@909 300
Chris@128 301 def test_destroy_should_delete_private_queries
Chris@1464 302 query = Query.new(:name => 'foo', :visibility => Query::VISIBILITY_PRIVATE)
Chris@128 303 query.project_id = 1
Chris@128 304 query.user_id = 2
Chris@128 305 query.save!
Chris@909 306
Chris@128 307 User.find(2).destroy
Chris@128 308 assert_nil User.find_by_id(2)
Chris@128 309 assert_nil Query.find_by_id(query.id)
Chris@128 310 end
Chris@909 311
Chris@128 312 def test_destroy_should_update_public_queries
Chris@1464 313 query = Query.new(:name => 'foo', :visibility => Query::VISIBILITY_PUBLIC)
Chris@128 314 query.project_id = 1
Chris@128 315 query.user_id = 2
Chris@128 316 query.save!
Chris@909 317
Chris@128 318 User.find(2).destroy
Chris@128 319 assert_nil User.find_by_id(2)
Chris@128 320 assert_equal User.anonymous, query.reload.user
Chris@128 321 end
Chris@909 322
Chris@128 323 def test_destroy_should_update_time_entries
Chris@1517 324 entry = TimeEntry.new(:hours => '2', :spent_on => Date.today,
Chris@1517 325 :activity => TimeEntryActivity.create!(:name => 'foo'))
Chris@128 326 entry.project_id = 1
Chris@128 327 entry.user_id = 2
Chris@128 328 entry.save!
Chris@909 329
Chris@128 330 User.find(2).destroy
Chris@128 331 assert_nil User.find_by_id(2)
Chris@128 332 assert_equal User.anonymous, entry.reload.user
Chris@128 333 end
Chris@909 334
Chris@128 335 def test_destroy_should_delete_tokens
Chris@128 336 token = Token.create!(:user_id => 2, :value => 'foo')
Chris@909 337
Chris@128 338 User.find(2).destroy
Chris@128 339 assert_nil User.find_by_id(2)
Chris@128 340 assert_nil Token.find_by_id(token.id)
Chris@128 341 end
Chris@909 342
Chris@128 343 def test_destroy_should_delete_watchers
Chris@1517 344 issue = Issue.create!(:project_id => 1, :author_id => 1,
Chris@1517 345 :tracker_id => 1, :subject => 'foo')
Chris@128 346 watcher = Watcher.create!(:user_id => 2, :watchable => issue)
Chris@909 347
Chris@128 348 User.find(2).destroy
Chris@128 349 assert_nil User.find_by_id(2)
Chris@128 350 assert_nil Watcher.find_by_id(watcher.id)
Chris@128 351 end
Chris@909 352
Chris@128 353 def test_destroy_should_update_wiki_contents
Chris@128 354 wiki_content = WikiContent.create!(
Chris@128 355 :text => 'foo',
Chris@128 356 :author_id => 2,
Chris@1517 357 :page => WikiPage.create!(:title => 'Foo',
Chris@1517 358 :wiki => Wiki.create!(:project_id => 3,
Chris@1517 359 :start_page => 'Start'))
Chris@128 360 )
Chris@128 361 wiki_content.text = 'bar'
Chris@128 362 assert_difference 'WikiContent::Version.count' do
Chris@128 363 wiki_content.save!
Chris@128 364 end
Chris@909 365
Chris@128 366 User.find(2).destroy
Chris@128 367 assert_nil User.find_by_id(2)
Chris@128 368 assert_equal User.anonymous, wiki_content.reload.author
Chris@128 369 wiki_content.versions.each do |version|
Chris@128 370 assert_equal User.anonymous, version.reload.author
Chris@128 371 end
Chris@128 372 end
Chris@909 373
Chris@128 374 def test_destroy_should_nullify_issue_categories
Chris@128 375 category = IssueCategory.create!(:project_id => 1, :assigned_to_id => 2, :name => 'foo')
Chris@909 376
Chris@128 377 User.find(2).destroy
Chris@128 378 assert_nil User.find_by_id(2)
Chris@128 379 assert_nil category.reload.assigned_to_id
Chris@128 380 end
Chris@909 381
Chris@128 382 def test_destroy_should_nullify_changesets
Chris@128 383 changeset = Changeset.create!(
Chris@128 384 :repository => Repository::Subversion.create!(
Chris@128 385 :project_id => 1,
Chris@1115 386 :url => 'file:///tmp',
Chris@1115 387 :identifier => 'tmp'
Chris@128 388 ),
Chris@128 389 :revision => '12',
Chris@128 390 :committed_on => Time.now,
Chris@128 391 :committer => 'jsmith'
Chris@128 392 )
Chris@128 393 assert_equal 2, changeset.user_id
Chris@909 394
Chris@128 395 User.find(2).destroy
Chris@128 396 assert_nil User.find_by_id(2)
Chris@128 397 assert_nil changeset.reload.user_id
Chris@128 398 end
Chris@909 399
Chris@128 400 def test_anonymous_user_should_not_be_destroyable
Chris@128 401 assert_no_difference 'User.count' do
Chris@128 402 assert_equal false, User.anonymous.destroy
Chris@128 403 end
Chris@0 404 end
Chris@909 405
Chris@119 406 def test_validate_login_presence
Chris@0 407 @admin.login = ""
Chris@0 408 assert !@admin.save
Chris@0 409 assert_equal 1, @admin.errors.count
Chris@0 410 end
Chris@909 411
Chris@119 412 def test_validate_mail_notification_inclusion
Chris@119 413 u = User.new
Chris@119 414 u.mail_notification = 'foo'
Chris@119 415 u.save
Chris@1464 416 assert_not_equal [], u.errors[:mail_notification]
Chris@0 417 end
Chris@0 418
Chris@0 419 def test_password
Chris@0 420 user = User.try_to_login("admin", "admin")
Chris@0 421 assert_kind_of User, user
Chris@0 422 assert_equal "admin", user.login
Chris@1115 423 user.password = "hello123"
Chris@0 424 assert user.save
Chris@909 425
Chris@1115 426 user = User.try_to_login("admin", "hello123")
Chris@0 427 assert_kind_of User, user
Chris@0 428 assert_equal "admin", user.login
Chris@0 429 end
Chris@909 430
Chris@909 431 def test_validate_password_length
Chris@909 432 with_settings :password_min_length => '100' do
Chris@1517 433 user = User.new(:firstname => "new100",
Chris@1517 434 :lastname => "user100", :mail => "newuser100@somenet.foo")
Chris@909 435 user.login = "newuser100"
Chris@909 436 user.password, user.password_confirmation = "password100", "password100"
Chris@909 437 assert !user.save
Chris@909 438 assert_equal 1, user.errors.count
Chris@909 439 end
Chris@909 440 end
Chris@909 441
Chris@0 442 def test_name_format
Chris@1115 443 assert_equal 'John S.', @jsmith.name(:firstname_lastinitial)
Chris@0 444 assert_equal 'Smith, John', @jsmith.name(:lastname_coma_firstname)
Chris@1517 445 assert_equal 'J. Smith', @jsmith.name(:firstinitial_lastname)
Chris@1517 446 assert_equal 'J.-P. Lang', User.new(:firstname => 'Jean-Philippe', :lastname => 'Lang').name(:firstinitial_lastname)
Chris@1517 447 end
Chris@1517 448
Chris@1517 449 def test_name_should_use_setting_as_default_format
Chris@1115 450 with_settings :user_format => :firstname_lastname do
Chris@1115 451 assert_equal 'John Smith', @jsmith.reload.name
Chris@1115 452 end
Chris@1115 453 with_settings :user_format => :username do
Chris@1115 454 assert_equal 'jsmith', @jsmith.reload.name
Chris@1115 455 end
Chris@1115 456 with_settings :user_format => :lastname do
Chris@1115 457 assert_equal 'Smith', @jsmith.reload.name
Chris@1115 458 end
Chris@0 459 end
Chris@1115 460
Chris@1115 461 def test_today_should_return_the_day_according_to_user_time_zone
Chris@1115 462 preference = User.find(1).pref
Chris@1115 463 date = Date.new(2012, 05, 15)
Chris@1115 464 time = Time.gm(2012, 05, 15, 23, 30).utc # 2012-05-15 23:30 UTC
Chris@1115 465 Date.stubs(:today).returns(date)
Chris@1115 466 Time.stubs(:now).returns(time)
Chris@1115 467
Chris@1115 468 preference.update_attribute :time_zone, 'Baku' # UTC+4
Chris@1115 469 assert_equal '2012-05-16', User.find(1).today.to_s
Chris@1115 470
Chris@1115 471 preference.update_attribute :time_zone, 'La Paz' # UTC-4
Chris@1115 472 assert_equal '2012-05-15', User.find(1).today.to_s
Chris@1115 473
Chris@1115 474 preference.update_attribute :time_zone, ''
Chris@1115 475 assert_equal '2012-05-15', User.find(1).today.to_s
Chris@1115 476 end
Chris@1115 477
Chris@1115 478 def test_time_to_date_should_return_the_date_according_to_user_time_zone
Chris@1115 479 preference = User.find(1).pref
Chris@1115 480 time = Time.gm(2012, 05, 15, 23, 30).utc # 2012-05-15 23:30 UTC
Chris@1115 481
Chris@1115 482 preference.update_attribute :time_zone, 'Baku' # UTC+4
Chris@1115 483 assert_equal '2012-05-16', User.find(1).time_to_date(time).to_s
Chris@1115 484
Chris@1115 485 preference.update_attribute :time_zone, 'La Paz' # UTC-4
Chris@1115 486 assert_equal '2012-05-15', User.find(1).time_to_date(time).to_s
Chris@1115 487
Chris@1115 488 preference.update_attribute :time_zone, ''
Chris@1115 489 assert_equal '2012-05-15', User.find(1).time_to_date(time).to_s
Chris@1115 490 end
Chris@1115 491
Chris@909 492 def test_fields_for_order_statement_should_return_fields_according_user_format_setting
Chris@909 493 with_settings :user_format => 'lastname_coma_firstname' do
Chris@1517 494 assert_equal ['users.lastname', 'users.firstname', 'users.id'],
Chris@1517 495 User.fields_for_order_statement
Chris@909 496 end
Chris@909 497 end
Chris@1464 498
Chris@909 499 def test_fields_for_order_statement_width_table_name_should_prepend_table_name
Chris@909 500 with_settings :user_format => 'lastname_firstname' do
Chris@1517 501 assert_equal ['authors.lastname', 'authors.firstname', 'authors.id'],
Chris@1517 502 User.fields_for_order_statement('authors')
Chris@909 503 end
Chris@909 504 end
Chris@1464 505
Chris@909 506 def test_fields_for_order_statement_with_blank_format_should_return_default
Chris@909 507 with_settings :user_format => '' do
Chris@1517 508 assert_equal ['users.firstname', 'users.lastname', 'users.id'],
Chris@1517 509 User.fields_for_order_statement
Chris@909 510 end
Chris@909 511 end
Chris@1464 512
Chris@909 513 def test_fields_for_order_statement_with_invalid_format_should_return_default
Chris@909 514 with_settings :user_format => 'foo' do
Chris@1517 515 assert_equal ['users.firstname', 'users.lastname', 'users.id'],
Chris@1517 516 User.fields_for_order_statement
Chris@909 517 end
Chris@909 518 end
Chris@909 519
Chris@1464 520 test ".try_to_login with good credentials should return the user" do
Chris@1464 521 user = User.try_to_login("admin", "admin")
Chris@1464 522 assert_kind_of User, user
Chris@1464 523 assert_equal "admin", user.login
Chris@1464 524 end
Chris@909 525
Chris@1464 526 test ".try_to_login with wrong credentials should return nil" do
Chris@1464 527 assert_nil User.try_to_login("admin", "foo")
Chris@1464 528 end
Chris@1464 529
Chris@1464 530 def test_try_to_login_with_locked_user_should_return_nil
Chris@0 531 @jsmith.status = User::STATUS_LOCKED
Chris@1464 532 @jsmith.save!
Chris@909 533
Chris@0 534 user = User.try_to_login("jsmith", "jsmith")
Chris@909 535 assert_equal nil, user
Chris@0 536 end
Chris@909 537
Chris@1464 538 def test_try_to_login_with_locked_user_and_not_active_only_should_return_user
Chris@1464 539 @jsmith.status = User::STATUS_LOCKED
Chris@1464 540 @jsmith.save!
Chris@1464 541
Chris@1464 542 user = User.try_to_login("jsmith", "jsmith", false)
Chris@1464 543 assert_equal @jsmith, user
Chris@1464 544 end
Chris@1464 545
Chris@1464 546 test ".try_to_login should fall-back to case-insensitive if user login is not found as-typed" do
Chris@1464 547 user = User.try_to_login("AdMin", "admin")
Chris@1464 548 assert_kind_of User, user
Chris@1464 549 assert_equal "admin", user.login
Chris@1464 550 end
Chris@1464 551
Chris@1464 552 test ".try_to_login should select the exact matching user first" do
Chris@1464 553 case_sensitive_user = User.generate! do |user|
Chris@1464 554 user.password = "admin123"
Chris@245 555 end
Chris@1464 556 # bypass validations to make it appear like existing data
Chris@1464 557 case_sensitive_user.update_attribute(:login, 'ADMIN')
Chris@909 558
Chris@1464 559 user = User.try_to_login("ADMIN", "admin123")
Chris@1464 560 assert_kind_of User, user
Chris@1464 561 assert_equal "ADMIN", user.login
Chris@245 562 end
Chris@909 563
Chris@0 564 if ldap_configured?
Chris@0 565 context "#try_to_login using LDAP" do
Chris@0 566 context "with failed connection to the LDAP server" do
Chris@0 567 should "return nil" do
Chris@0 568 @auth_source = AuthSourceLdap.find(1)
Chris@0 569 AuthSource.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::LdapError, 'Cannot connect')
Chris@909 570
Chris@0 571 assert_equal nil, User.try_to_login('edavis', 'wrong')
Chris@0 572 end
Chris@0 573 end
Chris@0 574
Chris@0 575 context "with an unsuccessful authentication" do
Chris@0 576 should "return nil" do
Chris@0 577 assert_equal nil, User.try_to_login('edavis', 'wrong')
Chris@0 578 end
Chris@0 579 end
Chris@909 580
Chris@1115 581 context "binding with user's account" do
Chris@1115 582 setup do
Chris@1115 583 @auth_source = AuthSourceLdap.find(1)
Chris@1115 584 @auth_source.account = "uid=$login,ou=Person,dc=redmine,dc=org"
Chris@1115 585 @auth_source.account_password = ''
Chris@1115 586 @auth_source.save!
Chris@1115 587
Chris@1115 588 @ldap_user = User.new(:mail => 'example1@redmine.org', :firstname => 'LDAP', :lastname => 'user', :auth_source_id => 1)
Chris@1115 589 @ldap_user.login = 'example1'
Chris@1115 590 @ldap_user.save!
Chris@1115 591 end
Chris@1115 592
Chris@1115 593 context "with a successful authentication" do
Chris@1115 594 should "return the user" do
Chris@1115 595 assert_equal @ldap_user, User.try_to_login('example1', '123456')
Chris@1115 596 end
Chris@1115 597 end
Chris@1115 598
Chris@1115 599 context "with an unsuccessful authentication" do
Chris@1115 600 should "return nil" do
Chris@1115 601 assert_nil User.try_to_login('example1', '11111')
Chris@1115 602 end
Chris@1115 603 end
Chris@1115 604 end
Chris@1115 605
Chris@0 606 context "on the fly registration" do
Chris@0 607 setup do
Chris@0 608 @auth_source = AuthSourceLdap.find(1)
Chris@1115 609 @auth_source.update_attribute :onthefly_register, true
Chris@0 610 end
Chris@0 611
Chris@0 612 context "with a successful authentication" do
Chris@0 613 should "create a new user account if it doesn't exist" do
Chris@0 614 assert_difference('User.count') do
Chris@0 615 user = User.try_to_login('edavis', '123456')
Chris@0 616 assert !user.admin?
Chris@0 617 end
Chris@0 618 end
Chris@909 619
Chris@0 620 should "retrieve existing user" do
Chris@0 621 user = User.try_to_login('edavis', '123456')
Chris@0 622 user.admin = true
Chris@0 623 user.save!
Chris@909 624
Chris@0 625 assert_no_difference('User.count') do
Chris@0 626 user = User.try_to_login('edavis', '123456')
Chris@0 627 assert user.admin?
Chris@0 628 end
Chris@0 629 end
Chris@0 630 end
Chris@1115 631
Chris@1115 632 context "binding with user's account" do
Chris@1115 633 setup do
Chris@1115 634 @auth_source = AuthSourceLdap.find(1)
Chris@1115 635 @auth_source.account = "uid=$login,ou=Person,dc=redmine,dc=org"
Chris@1115 636 @auth_source.account_password = ''
Chris@1115 637 @auth_source.save!
Chris@1115 638 end
Chris@1464 639
Chris@1115 640 context "with a successful authentication" do
Chris@1115 641 should "create a new user account if it doesn't exist" do
Chris@1115 642 assert_difference('User.count') do
Chris@1115 643 user = User.try_to_login('example1', '123456')
Chris@1115 644 assert_kind_of User, user
Chris@1115 645 end
Chris@1115 646 end
Chris@1115 647 end
Chris@1464 648
Chris@1115 649 context "with an unsuccessful authentication" do
Chris@1115 650 should "return nil" do
Chris@1115 651 assert_nil User.try_to_login('example1', '11111')
Chris@1115 652 end
Chris@1115 653 end
Chris@1115 654 end
Chris@0 655 end
Chris@0 656 end
Chris@0 657
Chris@0 658 else
Chris@0 659 puts "Skipping LDAP tests."
Chris@0 660 end
Chris@909 661
Chris@0 662 def test_create_anonymous
Chris@0 663 AnonymousUser.delete_all
Chris@0 664 anon = User.anonymous
Chris@0 665 assert !anon.new_record?
Chris@0 666 assert_kind_of AnonymousUser, anon
Chris@0 667 end
Chris@0 668
Chris@909 669 def test_ensure_single_anonymous_user
Chris@909 670 AnonymousUser.delete_all
Chris@909 671 anon1 = User.anonymous
Chris@909 672 assert !anon1.new_record?
Chris@909 673 assert_kind_of AnonymousUser, anon1
Chris@909 674 anon2 = AnonymousUser.create(
Chris@909 675 :lastname => 'Anonymous', :firstname => '',
Chris@909 676 :mail => '', :login => '', :status => 0)
Chris@909 677 assert_equal 1, anon2.errors.count
Chris@909 678 end
Chris@909 679
Chris@0 680 def test_rss_key
Chris@0 681 assert_nil @jsmith.rss_token
Chris@0 682 key = @jsmith.rss_key
Chris@0 683 assert_equal 40, key.length
Chris@909 684
Chris@0 685 @jsmith.reload
Chris@0 686 assert_equal key, @jsmith.rss_key
Chris@0 687 end
Chris@0 688
Chris@1115 689 def test_rss_key_should_not_be_generated_twice
Chris@1115 690 assert_difference 'Token.count', 1 do
Chris@1115 691 key1 = @jsmith.rss_key
Chris@1115 692 key2 = @jsmith.rss_key
Chris@1115 693 assert_equal key1, key2
Chris@1115 694 end
Chris@1115 695 end
Chris@909 696
Chris@1115 697 def test_api_key_should_not_be_generated_twice
Chris@1115 698 assert_difference 'Token.count', 1 do
Chris@1115 699 key1 = @jsmith.api_key
Chris@1115 700 key2 = @jsmith.api_key
Chris@1115 701 assert_equal key1, key2
Chris@1115 702 end
Chris@1115 703 end
Chris@0 704
Chris@1464 705 test "#api_key should generate a new one if the user doesn't have one" do
Chris@1464 706 user = User.generate!(:api_token => nil)
Chris@1464 707 assert_nil user.api_token
Chris@0 708
Chris@1464 709 key = user.api_key
Chris@1464 710 assert_equal 40, key.length
Chris@1464 711 user.reload
Chris@1464 712 assert_equal key, user.api_key
Chris@0 713 end
Chris@0 714
Chris@1464 715 test "#api_key should return the existing api token value" do
Chris@1464 716 user = User.generate!
Chris@1464 717 token = Token.create!(:action => 'api')
Chris@1464 718 user.api_token = token
Chris@1464 719 assert user.save
Chris@0 720
Chris@1464 721 assert_equal token.value, user.api_key
Chris@1464 722 end
Chris@0 723
Chris@1464 724 test "#find_by_api_key should return nil if no matching key is found" do
Chris@1464 725 assert_nil User.find_by_api_key('zzzzzzzzz')
Chris@1464 726 end
Chris@0 727
Chris@1464 728 test "#find_by_api_key should return nil if the key is found for an inactive user" do
Chris@1464 729 user = User.generate!
Chris@1464 730 user.status = User::STATUS_LOCKED
Chris@1464 731 token = Token.create!(:action => 'api')
Chris@1464 732 user.api_token = token
Chris@1464 733 user.save
Chris@909 734
Chris@1464 735 assert_nil User.find_by_api_key(token.value)
Chris@1464 736 end
Chris@1464 737
Chris@1464 738 test "#find_by_api_key should return the user if the key is found for an active user" do
Chris@1464 739 user = User.generate!
Chris@1464 740 token = Token.create!(:action => 'api')
Chris@1464 741 user.api_token = token
Chris@1464 742 user.save
Chris@1464 743
Chris@1464 744 assert_equal user, User.find_by_api_key(token.value)
Chris@0 745 end
Chris@0 746
Chris@929 747 def test_default_admin_account_changed_should_return_false_if_account_was_not_changed
Chris@929 748 user = User.find_by_login("admin")
Chris@929 749 user.password = "admin"
Chris@1115 750 assert user.save(:validate => false)
Chris@929 751
Chris@929 752 assert_equal false, User.default_admin_account_changed?
Chris@929 753 end
Chris@929 754
Chris@929 755 def test_default_admin_account_changed_should_return_true_if_password_was_changed
Chris@929 756 user = User.find_by_login("admin")
Chris@929 757 user.password = "newpassword"
Chris@929 758 user.save!
Chris@929 759
Chris@929 760 assert_equal true, User.default_admin_account_changed?
Chris@929 761 end
Chris@929 762
Chris@929 763 def test_default_admin_account_changed_should_return_true_if_account_is_disabled
Chris@929 764 user = User.find_by_login("admin")
Chris@929 765 user.password = "admin"
Chris@929 766 user.status = User::STATUS_LOCKED
Chris@1115 767 assert user.save(:validate => false)
Chris@929 768
Chris@929 769 assert_equal true, User.default_admin_account_changed?
Chris@929 770 end
Chris@929 771
Chris@929 772 def test_default_admin_account_changed_should_return_true_if_account_does_not_exist
Chris@929 773 user = User.find_by_login("admin")
Chris@929 774 user.destroy
Chris@929 775
Chris@929 776 assert_equal true, User.default_admin_account_changed?
Chris@929 777 end
Chris@929 778
Chris@1464 779 def test_membership_with_project_should_return_membership
Chris@1464 780 project = Project.find(1)
Chris@1464 781
Chris@1464 782 membership = @jsmith.membership(project)
Chris@1464 783 assert_kind_of Member, membership
Chris@1464 784 assert_equal @jsmith, membership.user
Chris@1464 785 assert_equal project, membership.project
Chris@1464 786 end
Chris@1464 787
Chris@1464 788 def test_membership_with_project_id_should_return_membership
Chris@1464 789 project = Project.find(1)
Chris@1464 790
Chris@1464 791 membership = @jsmith.membership(1)
Chris@1464 792 assert_kind_of Member, membership
Chris@1464 793 assert_equal @jsmith, membership.user
Chris@1464 794 assert_equal project, membership.project
Chris@1464 795 end
Chris@1464 796
Chris@1464 797 def test_membership_for_non_member_should_return_nil
Chris@1464 798 project = Project.find(1)
Chris@1464 799
Chris@1464 800 user = User.generate!
Chris@1464 801 membership = user.membership(1)
Chris@1464 802 assert_nil membership
Chris@1464 803 end
Chris@1464 804
Chris@0 805 def test_roles_for_project
Chris@0 806 # user with a role
Chris@0 807 roles = @jsmith.roles_for_project(Project.find(1))
Chris@0 808 assert_kind_of Role, roles.first
Chris@0 809 assert_equal "Manager", roles.first.name
Chris@909 810
Chris@0 811 # user with no role
Chris@0 812 assert_nil @dlopper.roles_for_project(Project.find(2)).detect {|role| role.member?}
Chris@0 813 end
Chris@909 814
Chris@441 815 def test_projects_by_role_for_user_with_role
Chris@441 816 user = User.find(2)
Chris@441 817 assert_kind_of Hash, user.projects_by_role
Chris@441 818 assert_equal 2, user.projects_by_role.size
Chris@441 819 assert_equal [1,5], user.projects_by_role[Role.find(1)].collect(&:id).sort
Chris@441 820 assert_equal [2], user.projects_by_role[Role.find(2)].collect(&:id).sort
Chris@441 821 end
Chris@909 822
Chris@1115 823 def test_accessing_projects_by_role_with_no_projects_should_return_an_empty_array
Chris@1115 824 user = User.find(2)
Chris@1115 825 assert_equal [], user.projects_by_role[Role.find(3)]
Chris@1115 826 # should not update the hash
Chris@1115 827 assert_nil user.projects_by_role.values.detect(&:blank?)
Chris@1115 828 end
Chris@1115 829
Chris@441 830 def test_projects_by_role_for_user_with_no_role
Chris@441 831 user = User.generate!
Chris@441 832 assert_equal({}, user.projects_by_role)
Chris@441 833 end
Chris@909 834
Chris@441 835 def test_projects_by_role_for_anonymous
Chris@441 836 assert_equal({}, User.anonymous.projects_by_role)
Chris@441 837 end
Chris@441 838
Chris@128 839 def test_valid_notification_options
Chris@128 840 # without memberships
Chris@128 841 assert_equal 5, User.find(7).valid_notification_options.size
Chris@128 842 # with memberships
Chris@128 843 assert_equal 6, User.find(2).valid_notification_options.size
Chris@128 844 end
Chris@909 845
Chris@128 846 def test_valid_notification_options_class_method
Chris@128 847 assert_equal 5, User.valid_notification_options.size
Chris@128 848 assert_equal 5, User.valid_notification_options(User.find(7)).size
Chris@128 849 assert_equal 6, User.valid_notification_options(User.find(2)).size
Chris@128 850 end
Chris@909 851
Chris@0 852 def test_mail_notification_all
chris@37 853 @jsmith.mail_notification = 'all'
Chris@0 854 @jsmith.notified_project_ids = []
Chris@0 855 @jsmith.save
Chris@0 856 @jsmith.reload
Chris@0 857 assert @jsmith.projects.first.recipients.include?(@jsmith.mail)
Chris@0 858 end
Chris@909 859
Chris@0 860 def test_mail_notification_selected
chris@37 861 @jsmith.mail_notification = 'selected'
Chris@0 862 @jsmith.notified_project_ids = [1]
Chris@0 863 @jsmith.save
Chris@0 864 @jsmith.reload
Chris@0 865 assert Project.find(1).recipients.include?(@jsmith.mail)
Chris@0 866 end
Chris@909 867
chris@37 868 def test_mail_notification_only_my_events
chris@37 869 @jsmith.mail_notification = 'only_my_events'
Chris@0 870 @jsmith.notified_project_ids = []
Chris@0 871 @jsmith.save
Chris@0 872 @jsmith.reload
Chris@0 873 assert !@jsmith.projects.first.recipients.include?(@jsmith.mail)
Chris@0 874 end
Chris@909 875
Chris@0 876 def test_comments_sorting_preference
Chris@0 877 assert !@jsmith.wants_comments_in_reverse_order?
Chris@0 878 @jsmith.pref.comments_sorting = 'asc'
Chris@0 879 assert !@jsmith.wants_comments_in_reverse_order?
Chris@0 880 @jsmith.pref.comments_sorting = 'desc'
Chris@0 881 assert @jsmith.wants_comments_in_reverse_order?
Chris@0 882 end
Chris@909 883
Chris@0 884 def test_find_by_mail_should_be_case_insensitive
Chris@0 885 u = User.find_by_mail('JSmith@somenet.foo')
Chris@0 886 assert_not_nil u
Chris@0 887 assert_equal 'jsmith@somenet.foo', u.mail
Chris@0 888 end
Chris@909 889
Chris@0 890 def test_random_password
Chris@0 891 u = User.new
Chris@0 892 u.random_password
Chris@0 893 assert !u.password.blank?
Chris@0 894 assert !u.password_confirmation.blank?
Chris@0 895 end
Chris@0 896
Chris@1464 897 test "#change_password_allowed? should be allowed if no auth source is set" do
Chris@1464 898 user = User.generate!
Chris@1464 899 assert user.change_password_allowed?
Chris@1464 900 end
Chris@0 901
Chris@1464 902 test "#change_password_allowed? should delegate to the auth source" do
Chris@1464 903 user = User.generate!
Chris@909 904
Chris@1464 905 allowed_auth_source = AuthSource.generate!
Chris@1464 906 def allowed_auth_source.allow_password_changes?; true; end
Chris@0 907
Chris@1464 908 denied_auth_source = AuthSource.generate!
Chris@1464 909 def denied_auth_source.allow_password_changes?; false; end
Chris@0 910
Chris@1464 911 assert user.change_password_allowed?
Chris@0 912
Chris@1464 913 user.auth_source = allowed_auth_source
Chris@1464 914 assert user.change_password_allowed?, "User not allowed to change password, though auth source does"
Chris@0 915
Chris@1464 916 user.auth_source = denied_auth_source
Chris@1464 917 assert !user.change_password_allowed?, "User allowed to change password, though auth source does not"
Chris@1115 918 end
Chris@0 919
Chris@1115 920 def test_own_account_deletable_should_be_true_with_unsubscrive_enabled
Chris@1115 921 with_settings :unsubscribe => '1' do
Chris@1115 922 assert_equal true, User.find(2).own_account_deletable?
Chris@1115 923 end
Chris@1115 924 end
Chris@1115 925
Chris@1115 926 def test_own_account_deletable_should_be_false_with_unsubscrive_disabled
Chris@1115 927 with_settings :unsubscribe => '0' do
Chris@1115 928 assert_equal false, User.find(2).own_account_deletable?
Chris@1115 929 end
Chris@1115 930 end
Chris@1115 931
Chris@1115 932 def test_own_account_deletable_should_be_false_for_a_single_admin
Chris@1115 933 User.delete_all(["admin = ? AND id <> ?", true, 1])
Chris@1115 934
Chris@1115 935 with_settings :unsubscribe => '1' do
Chris@1115 936 assert_equal false, User.find(1).own_account_deletable?
Chris@1115 937 end
Chris@1115 938 end
Chris@1115 939
Chris@1115 940 def test_own_account_deletable_should_be_true_for_an_admin_if_other_admin_exists
Chris@1115 941 User.generate! do |user|
Chris@1115 942 user.admin = true
Chris@1115 943 end
Chris@1115 944
Chris@1115 945 with_settings :unsubscribe => '1' do
Chris@1115 946 assert_equal true, User.find(1).own_account_deletable?
Chris@1115 947 end
Chris@0 948 end
Chris@909 949
chris@22 950 context "#allowed_to?" do
chris@22 951 context "with a unique project" do
chris@22 952 should "return false if project is archived" do
chris@22 953 project = Project.find(1)
chris@22 954 Project.any_instance.stubs(:status).returns(Project::STATUS_ARCHIVED)
Chris@1115 955 assert_equal false, @admin.allowed_to?(:view_issues, Project.find(1))
Chris@1115 956 end
Chris@1115 957
Chris@1115 958 should "return false for write action if project is closed" do
Chris@1115 959 project = Project.find(1)
Chris@1115 960 Project.any_instance.stubs(:status).returns(Project::STATUS_CLOSED)
Chris@1115 961 assert_equal false, @admin.allowed_to?(:edit_project, Project.find(1))
Chris@1115 962 end
Chris@1115 963
Chris@1115 964 should "return true for read action if project is closed" do
Chris@1115 965 project = Project.find(1)
Chris@1115 966 Project.any_instance.stubs(:status).returns(Project::STATUS_CLOSED)
Chris@1115 967 assert_equal true, @admin.allowed_to?(:view_project, Project.find(1))
chris@22 968 end
Chris@909 969
chris@22 970 should "return false if related module is disabled" do
chris@22 971 project = Project.find(1)
chris@22 972 project.enabled_module_names = ["issue_tracking"]
Chris@1115 973 assert_equal true, @admin.allowed_to?(:add_issues, project)
Chris@1115 974 assert_equal false, @admin.allowed_to?(:view_wiki_pages, project)
chris@22 975 end
Chris@909 976
chris@22 977 should "authorize nearly everything for admin users" do
chris@22 978 project = Project.find(1)
chris@22 979 assert ! @admin.member_of?(project)
Chris@1464 980 %w(edit_issues delete_issues manage_news add_documents manage_wiki).each do |p|
Chris@1115 981 assert_equal true, @admin.allowed_to?(p.to_sym, project)
chris@22 982 end
chris@22 983 end
Chris@909 984
chris@22 985 should "authorize normal users depending on their roles" do
chris@22 986 project = Project.find(1)
Chris@1115 987 assert_equal true, @jsmith.allowed_to?(:delete_messages, project) #Manager
Chris@1115 988 assert_equal false, @dlopper.allowed_to?(:delete_messages, project) #Developper
chris@22 989 end
chris@22 990 end
chris@37 991
chris@37 992 context "with multiple projects" do
chris@37 993 should "return false if array is empty" do
Chris@1115 994 assert_equal false, @admin.allowed_to?(:view_project, [])
chris@37 995 end
Chris@909 996
chris@37 997 should "return true only if user has permission on all these projects" do
Chris@1115 998 assert_equal true, @admin.allowed_to?(:view_project, Project.all)
Chris@1115 999 assert_equal false, @dlopper.allowed_to?(:view_project, Project.all) #cannot see Project(2)
Chris@1115 1000 assert_equal true, @jsmith.allowed_to?(:edit_issues, @jsmith.projects) #Manager or Developer everywhere
Chris@1115 1001 assert_equal false, @jsmith.allowed_to?(:delete_issue_watchers, @jsmith.projects) #Dev cannot delete_issue_watchers
chris@37 1002 end
Chris@909 1003
chris@37 1004 should "behave correctly with arrays of 1 project" do
Chris@1115 1005 assert_equal false, User.anonymous.allowed_to?(:delete_issues, [Project.first])
chris@37 1006 end
chris@37 1007 end
Chris@909 1008
chris@22 1009 context "with options[:global]" do
chris@22 1010 should "authorize if user has at least one role that has this permission" do
chris@22 1011 @dlopper2 = User.find(5) #only Developper on a project, not Manager anywhere
chris@22 1012 @anonymous = User.find(6)
Chris@1115 1013 assert_equal true, @jsmith.allowed_to?(:delete_issue_watchers, nil, :global => true)
Chris@1115 1014 assert_equal false, @dlopper2.allowed_to?(:delete_issue_watchers, nil, :global => true)
Chris@1115 1015 assert_equal true, @dlopper2.allowed_to?(:add_issues, nil, :global => true)
Chris@1115 1016 assert_equal false, @anonymous.allowed_to?(:add_issues, nil, :global => true)
Chris@1115 1017 assert_equal true, @anonymous.allowed_to?(:view_issues, nil, :global => true)
chris@22 1018 end
chris@22 1019 end
chris@22 1020 end
Chris@909 1021
chris@37 1022 context "User#notify_about?" do
chris@37 1023 context "Issues" do
chris@37 1024 setup do
chris@37 1025 @project = Project.find(1)
Chris@1115 1026 @author = User.generate!
Chris@1115 1027 @assignee = User.generate!
Chris@1115 1028 @issue = Issue.generate!(:project => @project, :assigned_to => @assignee, :author => @author)
chris@37 1029 end
chris@37 1030
chris@37 1031 should "be true for a user with :all" do
Chris@119 1032 @author.update_attribute(:mail_notification, 'all')
chris@37 1033 assert @author.notify_about?(@issue)
chris@37 1034 end
Chris@909 1035
chris@37 1036 should "be false for a user with :none" do
Chris@119 1037 @author.update_attribute(:mail_notification, 'none')
chris@37 1038 assert ! @author.notify_about?(@issue)
chris@37 1039 end
Chris@909 1040
chris@37 1041 should "be false for a user with :only_my_events and isn't an author, creator, or assignee" do
Chris@1115 1042 @user = User.generate!(:mail_notification => 'only_my_events')
Chris@210 1043 Member.create!(:user => @user, :project => @project, :role_ids => [1])
chris@37 1044 assert ! @user.notify_about?(@issue)
chris@37 1045 end
Chris@909 1046
chris@37 1047 should "be true for a user with :only_my_events and is the author" do
Chris@119 1048 @author.update_attribute(:mail_notification, 'only_my_events')
chris@37 1049 assert @author.notify_about?(@issue)
chris@37 1050 end
Chris@909 1051
chris@37 1052 should "be true for a user with :only_my_events and is the assignee" do
Chris@119 1053 @assignee.update_attribute(:mail_notification, 'only_my_events')
chris@37 1054 assert @assignee.notify_about?(@issue)
chris@37 1055 end
Chris@909 1056
chris@37 1057 should "be true for a user with :only_assigned and is the assignee" do
Chris@119 1058 @assignee.update_attribute(:mail_notification, 'only_assigned')
chris@37 1059 assert @assignee.notify_about?(@issue)
chris@37 1060 end
Chris@909 1061
chris@37 1062 should "be false for a user with :only_assigned and is not the assignee" do
Chris@119 1063 @author.update_attribute(:mail_notification, 'only_assigned')
chris@37 1064 assert ! @author.notify_about?(@issue)
chris@37 1065 end
Chris@909 1066
chris@37 1067 should "be true for a user with :only_owner and is the author" do
Chris@119 1068 @author.update_attribute(:mail_notification, 'only_owner')
chris@37 1069 assert @author.notify_about?(@issue)
chris@37 1070 end
Chris@909 1071
chris@37 1072 should "be false for a user with :only_owner and is not the author" do
Chris@119 1073 @assignee.update_attribute(:mail_notification, 'only_owner')
chris@37 1074 assert ! @assignee.notify_about?(@issue)
chris@37 1075 end
Chris@909 1076
Chris@210 1077 should "be true for a user with :selected and is the author" do
Chris@210 1078 @author.update_attribute(:mail_notification, 'selected')
Chris@210 1079 assert @author.notify_about?(@issue)
Chris@210 1080 end
Chris@909 1081
Chris@210 1082 should "be true for a user with :selected and is the assignee" do
Chris@210 1083 @assignee.update_attribute(:mail_notification, 'selected')
Chris@210 1084 assert @assignee.notify_about?(@issue)
Chris@210 1085 end
Chris@909 1086
Chris@210 1087 should "be false for a user with :selected and is not the author or assignee" do
Chris@1115 1088 @user = User.generate!(:mail_notification => 'selected')
Chris@210 1089 Member.create!(:user => @user, :project => @project, :role_ids => [1])
Chris@210 1090 assert ! @user.notify_about?(@issue)
Chris@210 1091 end
chris@37 1092 end
Chris@1464 1093 end
chris@37 1094
Chris@1464 1095 def test_notify_about_news
Chris@1464 1096 user = User.generate!
Chris@1464 1097 news = News.new
Chris@1464 1098
Chris@1464 1099 User::MAIL_NOTIFICATION_OPTIONS.map(&:first).each do |option|
Chris@1464 1100 user.mail_notification = option
Chris@1464 1101 assert_equal (option != 'none'), user.notify_about?(news)
chris@37 1102 end
chris@37 1103 end
Chris@245 1104
Chris@245 1105 def test_salt_unsalted_passwords
Chris@245 1106 # Restore a user with an unsalted password
Chris@245 1107 user = User.find(1)
Chris@245 1108 user.salt = nil
Chris@245 1109 user.hashed_password = User.hash_password("unsalted")
Chris@245 1110 user.save!
Chris@909 1111
Chris@245 1112 User.salt_unsalted_passwords!
Chris@909 1113
Chris@245 1114 user.reload
Chris@245 1115 # Salt added
Chris@245 1116 assert !user.salt.blank?
Chris@245 1117 # Password still valid
Chris@245 1118 assert user.check_password?("unsalted")
Chris@245 1119 assert_equal user, User.try_to_login(user.login, "unsalted")
Chris@245 1120 end
Chris@909 1121
Chris@0 1122 if Object.const_defined?(:OpenID)
Chris@1517 1123 def test_setting_identity_url
Chris@1517 1124 normalized_open_id_url = 'http://example.com/'
Chris@1517 1125 u = User.new( :identity_url => 'http://example.com/' )
Chris@1517 1126 assert_equal normalized_open_id_url, u.identity_url
Chris@1517 1127 end
Chris@909 1128
Chris@1517 1129 def test_setting_identity_url_without_trailing_slash
Chris@1517 1130 normalized_open_id_url = 'http://example.com/'
Chris@1517 1131 u = User.new( :identity_url => 'http://example.com' )
Chris@1517 1132 assert_equal normalized_open_id_url, u.identity_url
Chris@1517 1133 end
Chris@0 1134
Chris@1517 1135 def test_setting_identity_url_without_protocol
Chris@1517 1136 normalized_open_id_url = 'http://example.com/'
Chris@1517 1137 u = User.new( :identity_url => 'example.com' )
Chris@1517 1138 assert_equal normalized_open_id_url, u.identity_url
Chris@1517 1139 end
Chris@0 1140
Chris@1517 1141 def test_setting_blank_identity_url
Chris@1517 1142 u = User.new( :identity_url => 'example.com' )
Chris@1517 1143 u.identity_url = ''
Chris@1517 1144 assert u.identity_url.blank?
Chris@1517 1145 end
Chris@909 1146
Chris@1517 1147 def test_setting_invalid_identity_url
Chris@1517 1148 u = User.new( :identity_url => 'this is not an openid url' )
Chris@1517 1149 assert u.identity_url.blank?
Chris@1517 1150 end
Chris@0 1151 else
Chris@0 1152 puts "Skipping openid tests."
Chris@0 1153 end
Chris@0 1154 end