annotate test/unit/user_test.rb @ 1516:b450a9d58aed redmine-2.4

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