annotate .svn/pristine/ec/ec7c0d826b7228b3557fac4fa00ebff6a515cfda.svn-base @ 1485:c8d3ad483bea redmine-2.4-integration

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