Mercurial > hg > soundsoftware-site
comparison test/unit/user_test.rb @ 1115:433d4f72a19b redmine-2.2
Update to Redmine SVN revision 11137 on 2.2-stable branch
author | Chris Cannam |
---|---|
date | Mon, 07 Jan 2013 12:01:42 +0000 |
parents | 5f33065ddc4b |
children | 622f24f53b42 261b3d9a4903 |
comparison
equal
deleted
inserted
replaced
929:5f33065ddc4b | 1115:433d4f72a19b |
---|---|
1 # Redmine - project management software | 1 # Redmine - project management software |
2 # Copyright (C) 2006-2011 Jean-Philippe Lang | 2 # Copyright (C) 2006-2012 Jean-Philippe Lang |
3 # | 3 # |
4 # This program is free software; you can redistribute it and/or | 4 # This program is free software; you can redistribute it and/or |
5 # modify it under the terms of the GNU General Public License | 5 # modify it under the terms of the GNU General Public License |
6 # as published by the Free Software Foundation; either version 2 | 6 # as published by the Free Software Foundation; either version 2 |
7 # of the License, or (at your option) any later version. | 7 # of the License, or (at your option) any later version. |
32 @admin = User.find(1) | 32 @admin = User.find(1) |
33 @jsmith = User.find(2) | 33 @jsmith = User.find(2) |
34 @dlopper = User.find(3) | 34 @dlopper = User.find(3) |
35 end | 35 end |
36 | 36 |
37 test 'object_daddy creation' do | 37 def test_generate |
38 User.generate_with_protected!(:firstname => 'Testing connection') | 38 User.generate!(:firstname => 'Testing connection') |
39 User.generate_with_protected!(:firstname => 'Testing connection') | 39 User.generate!(:firstname => 'Testing connection') |
40 assert_equal 2, User.count(:all, :conditions => {:firstname => 'Testing connection'}) | 40 assert_equal 2, User.count(:all, :conditions => {:firstname => 'Testing connection'}) |
41 end | 41 end |
42 | 42 |
43 def test_truth | 43 def test_truth |
44 assert_kind_of User, @jsmith | 44 assert_kind_of User, @jsmith |
52 | 52 |
53 def test_mail_validation | 53 def test_mail_validation |
54 u = User.new | 54 u = User.new |
55 u.mail = '' | 55 u.mail = '' |
56 assert !u.valid? | 56 assert !u.valid? |
57 assert_equal I18n.translate('activerecord.errors.messages.blank'), u.errors.on(:mail) | 57 assert_include I18n.translate('activerecord.errors.messages.blank'), u.errors[:mail] |
58 end | |
59 | |
60 def test_login_length_validation | |
61 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo") | |
62 user.login = "x" * (User::LOGIN_LENGTH_LIMIT+1) | |
63 assert !user.valid? | |
64 | |
65 user.login = "x" * (User::LOGIN_LENGTH_LIMIT) | |
66 assert user.valid? | |
67 assert user.save | |
58 end | 68 end |
59 | 69 |
60 def test_create | 70 def test_create |
61 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo") | 71 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo") |
62 | 72 |
65 # login uniqueness | 75 # login uniqueness |
66 assert !user.save | 76 assert !user.save |
67 assert_equal 1, user.errors.count | 77 assert_equal 1, user.errors.count |
68 | 78 |
69 user.login = "newuser" | 79 user.login = "newuser" |
70 user.password, user.password_confirmation = "passwd", "password" | 80 user.password, user.password_confirmation = "password", "pass" |
71 # password confirmation | 81 # password confirmation |
72 assert !user.save | 82 assert !user.save |
73 assert_equal 1, user.errors.count | 83 assert_equal 1, user.errors.count |
74 | 84 |
75 user.password, user.password_confirmation = "password", "password" | 85 user.password, user.password_confirmation = "password", "password" |
76 assert user.save | 86 assert user.save |
77 end | 87 end |
78 | 88 |
79 context "User#before_create" do | 89 def test_user_before_create_should_set_the_mail_notification_to_the_default_setting |
80 should "set the mail_notification to the default Setting" do | 90 @user1 = User.generate! |
81 @user1 = User.generate_with_protected! | 91 assert_equal 'only_my_events', @user1.mail_notification |
82 assert_equal 'only_my_events', @user1.mail_notification | 92 with_settings :default_notification_option => 'all' do |
83 | 93 @user2 = User.generate! |
84 with_settings :default_notification_option => 'all' do | 94 assert_equal 'all', @user2.mail_notification |
85 @user2 = User.generate_with_protected! | 95 end |
86 assert_equal 'all', @user2.mail_notification | 96 end |
87 end | 97 |
88 end | 98 def test_user_login_should_be_case_insensitive |
89 end | 99 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo") |
90 | 100 u.login = 'newuser' |
91 context "User.login" do | 101 u.password, u.password_confirmation = "password", "password" |
92 should "be case-insensitive." do | 102 assert u.save |
93 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo") | 103 u = User.new(:firstname => "Similar", :lastname => "User", :mail => "similaruser@somenet.foo") |
94 u.login = 'newuser' | 104 u.login = 'NewUser' |
95 u.password, u.password_confirmation = "password", "password" | 105 u.password, u.password_confirmation = "password", "password" |
96 assert u.save | 106 assert !u.save |
97 | 107 assert_include I18n.translate('activerecord.errors.messages.taken'), u.errors[:login] |
98 u = User.new(:firstname => "Similar", :lastname => "User", :mail => "similaruser@somenet.foo") | |
99 u.login = 'NewUser' | |
100 u.password, u.password_confirmation = "password", "password" | |
101 assert !u.save | |
102 assert_equal I18n.translate('activerecord.errors.messages.taken'), u.errors.on(:login) | |
103 end | |
104 end | 108 end |
105 | 109 |
106 def test_mail_uniqueness_should_not_be_case_sensitive | 110 def test_mail_uniqueness_should_not_be_case_sensitive |
107 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo") | 111 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo") |
108 u.login = 'newuser1' | 112 u.login = 'newuser1' |
111 | 115 |
112 u = User.new(:firstname => "new", :lastname => "user", :mail => "newUser@Somenet.foo") | 116 u = User.new(:firstname => "new", :lastname => "user", :mail => "newUser@Somenet.foo") |
113 u.login = 'newuser2' | 117 u.login = 'newuser2' |
114 u.password, u.password_confirmation = "password", "password" | 118 u.password, u.password_confirmation = "password", "password" |
115 assert !u.save | 119 assert !u.save |
116 assert_equal I18n.translate('activerecord.errors.messages.taken'), u.errors.on(:mail) | 120 assert_include I18n.translate('activerecord.errors.messages.taken'), u.errors[:mail] |
117 end | 121 end |
118 | 122 |
119 def test_update | 123 def test_update |
120 assert_equal "admin", @admin.login | 124 assert_equal "admin", @admin.login |
121 @admin.login = "john" | 125 @admin.login = "john" |
122 assert @admin.save, @admin.errors.full_messages.join("; ") | 126 assert @admin.save, @admin.errors.full_messages.join("; ") |
123 @admin.reload | 127 @admin.reload |
124 assert_equal "john", @admin.login | 128 assert_equal "john", @admin.login |
129 end | |
130 | |
131 def test_update_should_not_fail_for_legacy_user_with_different_case_logins | |
132 u1 = User.new(:firstname => "new", :lastname => "user", :mail => "newuser1@somenet.foo") | |
133 u1.login = 'newuser1' | |
134 assert u1.save | |
135 | |
136 u2 = User.new(:firstname => "new", :lastname => "user", :mail => "newuser2@somenet.foo") | |
137 u2.login = 'newuser1' | |
138 assert u2.save(:validate => false) | |
139 | |
140 user = User.find(u2.id) | |
141 user.firstname = "firstname" | |
142 assert user.save, "Save failed" | |
125 end | 143 end |
126 | 144 |
127 def test_destroy_should_delete_members_and_roles | 145 def test_destroy_should_delete_members_and_roles |
128 members = Member.find_all_by_user_id(2) | 146 members = Member.find_all_by_user_id(2) |
129 ms = members.size | 147 ms = members.size |
313 | 331 |
314 def test_destroy_should_nullify_changesets | 332 def test_destroy_should_nullify_changesets |
315 changeset = Changeset.create!( | 333 changeset = Changeset.create!( |
316 :repository => Repository::Subversion.create!( | 334 :repository => Repository::Subversion.create!( |
317 :project_id => 1, | 335 :project_id => 1, |
318 :url => 'file:///var/svn' | 336 :url => 'file:///tmp', |
337 :identifier => 'tmp' | |
319 ), | 338 ), |
320 :revision => '12', | 339 :revision => '12', |
321 :committed_on => Time.now, | 340 :committed_on => Time.now, |
322 :committer => 'jsmith' | 341 :committer => 'jsmith' |
323 ) | 342 ) |
353 assert_kind_of User, user | 372 assert_kind_of User, user |
354 assert_equal "admin", user.login | 373 assert_equal "admin", user.login |
355 end | 374 end |
356 | 375 |
357 should "select the exact matching user first" do | 376 should "select the exact matching user first" do |
358 case_sensitive_user = User.generate_with_protected!( | 377 case_sensitive_user = User.generate! do |user| |
359 :login => 'changed', :password => 'admin', | 378 user.password = "admin123" |
360 :password_confirmation => 'admin') | 379 end |
361 # bypass validations to make it appear like existing data | 380 # bypass validations to make it appear like existing data |
362 case_sensitive_user.update_attribute(:login, 'ADMIN') | 381 case_sensitive_user.update_attribute(:login, 'ADMIN') |
363 | 382 |
364 user = User.try_to_login("ADMIN", "admin") | 383 user = User.try_to_login("ADMIN", "admin123") |
365 assert_kind_of User, user | 384 assert_kind_of User, user |
366 assert_equal "ADMIN", user.login | 385 assert_equal "ADMIN", user.login |
367 | 386 |
368 end | 387 end |
369 end | 388 end |
370 | 389 |
371 def test_password | 390 def test_password |
372 user = User.try_to_login("admin", "admin") | 391 user = User.try_to_login("admin", "admin") |
373 assert_kind_of User, user | 392 assert_kind_of User, user |
374 assert_equal "admin", user.login | 393 assert_equal "admin", user.login |
375 user.password = "hello" | 394 user.password = "hello123" |
376 assert user.save | 395 assert user.save |
377 | 396 |
378 user = User.try_to_login("admin", "hello") | 397 user = User.try_to_login("admin", "hello123") |
379 assert_kind_of User, user | 398 assert_kind_of User, user |
380 assert_equal "admin", user.login | 399 assert_equal "admin", user.login |
381 end | 400 end |
382 | 401 |
383 def test_validate_password_length | 402 def test_validate_password_length |
389 assert_equal 1, user.errors.count | 408 assert_equal 1, user.errors.count |
390 end | 409 end |
391 end | 410 end |
392 | 411 |
393 def test_name_format | 412 def test_name_format |
413 assert_equal 'John S.', @jsmith.name(:firstname_lastinitial) | |
394 assert_equal 'Smith, John', @jsmith.name(:lastname_coma_firstname) | 414 assert_equal 'Smith, John', @jsmith.name(:lastname_coma_firstname) |
395 Setting.user_format = :firstname_lastname | 415 with_settings :user_format => :firstname_lastname do |
396 assert_equal 'John Smith', @jsmith.reload.name | 416 assert_equal 'John Smith', @jsmith.reload.name |
397 Setting.user_format = :username | 417 end |
398 assert_equal 'jsmith', @jsmith.reload.name | 418 with_settings :user_format => :username do |
399 end | 419 assert_equal 'jsmith', @jsmith.reload.name |
400 | 420 end |
421 with_settings :user_format => :lastname do | |
422 assert_equal 'Smith', @jsmith.reload.name | |
423 end | |
424 end | |
425 | |
426 def test_today_should_return_the_day_according_to_user_time_zone | |
427 preference = User.find(1).pref | |
428 date = Date.new(2012, 05, 15) | |
429 time = Time.gm(2012, 05, 15, 23, 30).utc # 2012-05-15 23:30 UTC | |
430 Date.stubs(:today).returns(date) | |
431 Time.stubs(:now).returns(time) | |
432 | |
433 preference.update_attribute :time_zone, 'Baku' # UTC+4 | |
434 assert_equal '2012-05-16', User.find(1).today.to_s | |
435 | |
436 preference.update_attribute :time_zone, 'La Paz' # UTC-4 | |
437 assert_equal '2012-05-15', User.find(1).today.to_s | |
438 | |
439 preference.update_attribute :time_zone, '' | |
440 assert_equal '2012-05-15', User.find(1).today.to_s | |
441 end | |
442 | |
443 def test_time_to_date_should_return_the_date_according_to_user_time_zone | |
444 preference = User.find(1).pref | |
445 time = Time.gm(2012, 05, 15, 23, 30).utc # 2012-05-15 23:30 UTC | |
446 | |
447 preference.update_attribute :time_zone, 'Baku' # UTC+4 | |
448 assert_equal '2012-05-16', User.find(1).time_to_date(time).to_s | |
449 | |
450 preference.update_attribute :time_zone, 'La Paz' # UTC-4 | |
451 assert_equal '2012-05-15', User.find(1).time_to_date(time).to_s | |
452 | |
453 preference.update_attribute :time_zone, '' | |
454 assert_equal '2012-05-15', User.find(1).time_to_date(time).to_s | |
455 end | |
456 | |
401 def test_fields_for_order_statement_should_return_fields_according_user_format_setting | 457 def test_fields_for_order_statement_should_return_fields_according_user_format_setting |
402 with_settings :user_format => 'lastname_coma_firstname' do | 458 with_settings :user_format => 'lastname_coma_firstname' do |
403 assert_equal ['users.lastname', 'users.firstname', 'users.id'], User.fields_for_order_statement | 459 assert_equal ['users.lastname', 'users.firstname', 'users.id'], User.fields_for_order_statement |
404 end | 460 end |
405 end | 461 end |
464 should "return nil" do | 520 should "return nil" do |
465 assert_equal nil, User.try_to_login('edavis', 'wrong') | 521 assert_equal nil, User.try_to_login('edavis', 'wrong') |
466 end | 522 end |
467 end | 523 end |
468 | 524 |
525 context "binding with user's account" do | |
526 setup do | |
527 @auth_source = AuthSourceLdap.find(1) | |
528 @auth_source.account = "uid=$login,ou=Person,dc=redmine,dc=org" | |
529 @auth_source.account_password = '' | |
530 @auth_source.save! | |
531 | |
532 @ldap_user = User.new(:mail => 'example1@redmine.org', :firstname => 'LDAP', :lastname => 'user', :auth_source_id => 1) | |
533 @ldap_user.login = 'example1' | |
534 @ldap_user.save! | |
535 end | |
536 | |
537 context "with a successful authentication" do | |
538 should "return the user" do | |
539 assert_equal @ldap_user, User.try_to_login('example1', '123456') | |
540 end | |
541 end | |
542 | |
543 context "with an unsuccessful authentication" do | |
544 should "return nil" do | |
545 assert_nil User.try_to_login('example1', '11111') | |
546 end | |
547 end | |
548 end | |
549 | |
469 context "on the fly registration" do | 550 context "on the fly registration" do |
470 setup do | 551 setup do |
471 @auth_source = AuthSourceLdap.find(1) | 552 @auth_source = AuthSourceLdap.find(1) |
553 @auth_source.update_attribute :onthefly_register, true | |
472 end | 554 end |
473 | 555 |
474 context "with a successful authentication" do | 556 context "with a successful authentication" do |
475 should "create a new user account if it doesn't exist" do | 557 should "create a new user account if it doesn't exist" do |
476 assert_difference('User.count') do | 558 assert_difference('User.count') do |
485 user.save! | 567 user.save! |
486 | 568 |
487 assert_no_difference('User.count') do | 569 assert_no_difference('User.count') do |
488 user = User.try_to_login('edavis', '123456') | 570 user = User.try_to_login('edavis', '123456') |
489 assert user.admin? | 571 assert user.admin? |
572 end | |
573 end | |
574 end | |
575 | |
576 context "binding with user's account" do | |
577 setup do | |
578 @auth_source = AuthSourceLdap.find(1) | |
579 @auth_source.account = "uid=$login,ou=Person,dc=redmine,dc=org" | |
580 @auth_source.account_password = '' | |
581 @auth_source.save! | |
582 end | |
583 | |
584 context "with a successful authentication" do | |
585 should "create a new user account if it doesn't exist" do | |
586 assert_difference('User.count') do | |
587 user = User.try_to_login('example1', '123456') | |
588 assert_kind_of User, user | |
589 end | |
590 end | |
591 end | |
592 | |
593 context "with an unsuccessful authentication" do | |
594 should "return nil" do | |
595 assert_nil User.try_to_login('example1', '11111') | |
490 end | 596 end |
491 end | 597 end |
492 end | 598 end |
493 end | 599 end |
494 end | 600 end |
513 :lastname => 'Anonymous', :firstname => '', | 619 :lastname => 'Anonymous', :firstname => '', |
514 :mail => '', :login => '', :status => 0) | 620 :mail => '', :login => '', :status => 0) |
515 assert_equal 1, anon2.errors.count | 621 assert_equal 1, anon2.errors.count |
516 end | 622 end |
517 | 623 |
518 should_have_one :rss_token | |
519 | |
520 def test_rss_key | 624 def test_rss_key |
521 assert_nil @jsmith.rss_token | 625 assert_nil @jsmith.rss_token |
522 key = @jsmith.rss_key | 626 key = @jsmith.rss_key |
523 assert_equal 40, key.length | 627 assert_equal 40, key.length |
524 | 628 |
525 @jsmith.reload | 629 @jsmith.reload |
526 assert_equal key, @jsmith.rss_key | 630 assert_equal key, @jsmith.rss_key |
527 end | 631 end |
528 | 632 |
529 | 633 def test_rss_key_should_not_be_generated_twice |
530 should_have_one :api_token | 634 assert_difference 'Token.count', 1 do |
635 key1 = @jsmith.rss_key | |
636 key2 = @jsmith.rss_key | |
637 assert_equal key1, key2 | |
638 end | |
639 end | |
640 | |
641 def test_api_key_should_not_be_generated_twice | |
642 assert_difference 'Token.count', 1 do | |
643 key1 = @jsmith.api_key | |
644 key2 = @jsmith.api_key | |
645 assert_equal key1, key2 | |
646 end | |
647 end | |
531 | 648 |
532 context "User#api_key" do | 649 context "User#api_key" do |
533 should "generate a new one if the user doesn't have one" do | 650 should "generate a new one if the user doesn't have one" do |
534 user = User.generate_with_protected!(:api_token => nil) | 651 user = User.generate!(:api_token => nil) |
535 assert_nil user.api_token | 652 assert_nil user.api_token |
536 | 653 |
537 key = user.api_key | 654 key = user.api_key |
538 assert_equal 40, key.length | 655 assert_equal 40, key.length |
539 user.reload | 656 user.reload |
540 assert_equal key, user.api_key | 657 assert_equal key, user.api_key |
541 end | 658 end |
542 | 659 |
543 should "return the existing api token value" do | 660 should "return the existing api token value" do |
544 user = User.generate_with_protected! | 661 user = User.generate! |
545 token = Token.generate!(:action => 'api') | 662 token = Token.create!(:action => 'api') |
546 user.api_token = token | 663 user.api_token = token |
547 assert user.save | 664 assert user.save |
548 | 665 |
549 assert_equal token.value, user.api_key | 666 assert_equal token.value, user.api_key |
550 end | 667 end |
554 should "return nil if no matching key is found" do | 671 should "return nil if no matching key is found" do |
555 assert_nil User.find_by_api_key('zzzzzzzzz') | 672 assert_nil User.find_by_api_key('zzzzzzzzz') |
556 end | 673 end |
557 | 674 |
558 should "return nil if the key is found for an inactive user" do | 675 should "return nil if the key is found for an inactive user" do |
559 user = User.generate_with_protected!(:status => User::STATUS_LOCKED) | 676 user = User.generate! |
560 token = Token.generate!(:action => 'api') | 677 user.status = User::STATUS_LOCKED |
678 token = Token.create!(:action => 'api') | |
561 user.api_token = token | 679 user.api_token = token |
562 user.save | 680 user.save |
563 | 681 |
564 assert_nil User.find_by_api_key(token.value) | 682 assert_nil User.find_by_api_key(token.value) |
565 end | 683 end |
566 | 684 |
567 should "return the user if the key is found for an active user" do | 685 should "return the user if the key is found for an active user" do |
568 user = User.generate_with_protected!(:status => User::STATUS_ACTIVE) | 686 user = User.generate! |
569 token = Token.generate!(:action => 'api') | 687 token = Token.create!(:action => 'api') |
570 user.api_token = token | 688 user.api_token = token |
571 user.save | 689 user.save |
572 | 690 |
573 assert_equal user, User.find_by_api_key(token.value) | 691 assert_equal user, User.find_by_api_key(token.value) |
574 end | 692 end |
575 end | 693 end |
576 | 694 |
577 def test_default_admin_account_changed_should_return_false_if_account_was_not_changed | 695 def test_default_admin_account_changed_should_return_false_if_account_was_not_changed |
578 user = User.find_by_login("admin") | 696 user = User.find_by_login("admin") |
579 user.password = "admin" | 697 user.password = "admin" |
580 user.save! | 698 assert user.save(:validate => false) |
581 | 699 |
582 assert_equal false, User.default_admin_account_changed? | 700 assert_equal false, User.default_admin_account_changed? |
583 end | 701 end |
584 | 702 |
585 def test_default_admin_account_changed_should_return_true_if_password_was_changed | 703 def test_default_admin_account_changed_should_return_true_if_password_was_changed |
592 | 710 |
593 def test_default_admin_account_changed_should_return_true_if_account_is_disabled | 711 def test_default_admin_account_changed_should_return_true_if_account_is_disabled |
594 user = User.find_by_login("admin") | 712 user = User.find_by_login("admin") |
595 user.password = "admin" | 713 user.password = "admin" |
596 user.status = User::STATUS_LOCKED | 714 user.status = User::STATUS_LOCKED |
597 user.save! | 715 assert user.save(:validate => false) |
598 | 716 |
599 assert_equal true, User.default_admin_account_changed? | 717 assert_equal true, User.default_admin_account_changed? |
600 end | 718 end |
601 | 719 |
602 def test_default_admin_account_changed_should_return_true_if_account_does_not_exist | 720 def test_default_admin_account_changed_should_return_true_if_account_does_not_exist |
622 assert_equal 2, user.projects_by_role.size | 740 assert_equal 2, user.projects_by_role.size |
623 assert_equal [1,5], user.projects_by_role[Role.find(1)].collect(&:id).sort | 741 assert_equal [1,5], user.projects_by_role[Role.find(1)].collect(&:id).sort |
624 assert_equal [2], user.projects_by_role[Role.find(2)].collect(&:id).sort | 742 assert_equal [2], user.projects_by_role[Role.find(2)].collect(&:id).sort |
625 end | 743 end |
626 | 744 |
745 def test_accessing_projects_by_role_with_no_projects_should_return_an_empty_array | |
746 user = User.find(2) | |
747 assert_equal [], user.projects_by_role[Role.find(3)] | |
748 # should not update the hash | |
749 assert_nil user.projects_by_role.values.detect(&:blank?) | |
750 end | |
751 | |
627 def test_projects_by_role_for_user_with_no_role | 752 def test_projects_by_role_for_user_with_no_role |
628 user = User.generate! | 753 user = User.generate! |
629 assert_equal({}, user.projects_by_role) | 754 assert_equal({}, user.projects_by_role) |
630 end | 755 end |
631 | 756 |
691 assert !u.password_confirmation.blank? | 816 assert !u.password_confirmation.blank? |
692 end | 817 end |
693 | 818 |
694 context "#change_password_allowed?" do | 819 context "#change_password_allowed?" do |
695 should "be allowed if no auth source is set" do | 820 should "be allowed if no auth source is set" do |
696 user = User.generate_with_protected! | 821 user = User.generate! |
697 assert user.change_password_allowed? | 822 assert user.change_password_allowed? |
698 end | 823 end |
699 | 824 |
700 should "delegate to the auth source" do | 825 should "delegate to the auth source" do |
701 user = User.generate_with_protected! | 826 user = User.generate! |
702 | 827 |
703 allowed_auth_source = AuthSource.generate! | 828 allowed_auth_source = AuthSource.generate! |
704 def allowed_auth_source.allow_password_changes?; true; end | 829 def allowed_auth_source.allow_password_changes?; true; end |
705 | 830 |
706 denied_auth_source = AuthSource.generate! | 831 denied_auth_source = AuthSource.generate! |
712 assert user.change_password_allowed?, "User not allowed to change password, though auth source does" | 837 assert user.change_password_allowed?, "User not allowed to change password, though auth source does" |
713 | 838 |
714 user.auth_source = denied_auth_source | 839 user.auth_source = denied_auth_source |
715 assert !user.change_password_allowed?, "User allowed to change password, though auth source does not" | 840 assert !user.change_password_allowed?, "User allowed to change password, though auth source does not" |
716 end | 841 end |
717 | 842 end |
843 | |
844 def test_own_account_deletable_should_be_true_with_unsubscrive_enabled | |
845 with_settings :unsubscribe => '1' do | |
846 assert_equal true, User.find(2).own_account_deletable? | |
847 end | |
848 end | |
849 | |
850 def test_own_account_deletable_should_be_false_with_unsubscrive_disabled | |
851 with_settings :unsubscribe => '0' do | |
852 assert_equal false, User.find(2).own_account_deletable? | |
853 end | |
854 end | |
855 | |
856 def test_own_account_deletable_should_be_false_for_a_single_admin | |
857 User.delete_all(["admin = ? AND id <> ?", true, 1]) | |
858 | |
859 with_settings :unsubscribe => '1' do | |
860 assert_equal false, User.find(1).own_account_deletable? | |
861 end | |
862 end | |
863 | |
864 def test_own_account_deletable_should_be_true_for_an_admin_if_other_admin_exists | |
865 User.generate! do |user| | |
866 user.admin = true | |
867 end | |
868 | |
869 with_settings :unsubscribe => '1' do | |
870 assert_equal true, User.find(1).own_account_deletable? | |
871 end | |
718 end | 872 end |
719 | 873 |
720 context "#allowed_to?" do | 874 context "#allowed_to?" do |
721 context "with a unique project" do | 875 context "with a unique project" do |
722 should "return false if project is archived" do | 876 should "return false if project is archived" do |
723 project = Project.find(1) | 877 project = Project.find(1) |
724 Project.any_instance.stubs(:status).returns(Project::STATUS_ARCHIVED) | 878 Project.any_instance.stubs(:status).returns(Project::STATUS_ARCHIVED) |
725 assert ! @admin.allowed_to?(:view_issues, Project.find(1)) | 879 assert_equal false, @admin.allowed_to?(:view_issues, Project.find(1)) |
880 end | |
881 | |
882 should "return false for write action if project is closed" do | |
883 project = Project.find(1) | |
884 Project.any_instance.stubs(:status).returns(Project::STATUS_CLOSED) | |
885 assert_equal false, @admin.allowed_to?(:edit_project, Project.find(1)) | |
886 end | |
887 | |
888 should "return true for read action if project is closed" do | |
889 project = Project.find(1) | |
890 Project.any_instance.stubs(:status).returns(Project::STATUS_CLOSED) | |
891 assert_equal true, @admin.allowed_to?(:view_project, Project.find(1)) | |
726 end | 892 end |
727 | 893 |
728 should "return false if related module is disabled" do | 894 should "return false if related module is disabled" do |
729 project = Project.find(1) | 895 project = Project.find(1) |
730 project.enabled_module_names = ["issue_tracking"] | 896 project.enabled_module_names = ["issue_tracking"] |
731 assert @admin.allowed_to?(:add_issues, project) | 897 assert_equal true, @admin.allowed_to?(:add_issues, project) |
732 assert ! @admin.allowed_to?(:view_wiki_pages, project) | 898 assert_equal false, @admin.allowed_to?(:view_wiki_pages, project) |
733 end | 899 end |
734 | 900 |
735 should "authorize nearly everything for admin users" do | 901 should "authorize nearly everything for admin users" do |
736 project = Project.find(1) | 902 project = Project.find(1) |
737 assert ! @admin.member_of?(project) | 903 assert ! @admin.member_of?(project) |
738 %w(edit_issues delete_issues manage_news manage_documents manage_wiki).each do |p| | 904 %w(edit_issues delete_issues manage_news manage_documents manage_wiki).each do |p| |
739 assert @admin.allowed_to?(p.to_sym, project) | 905 assert_equal true, @admin.allowed_to?(p.to_sym, project) |
740 end | 906 end |
741 end | 907 end |
742 | 908 |
743 should "authorize normal users depending on their roles" do | 909 should "authorize normal users depending on their roles" do |
744 project = Project.find(1) | 910 project = Project.find(1) |
745 assert @jsmith.allowed_to?(:delete_messages, project) #Manager | 911 assert_equal true, @jsmith.allowed_to?(:delete_messages, project) #Manager |
746 assert ! @dlopper.allowed_to?(:delete_messages, project) #Developper | 912 assert_equal false, @dlopper.allowed_to?(:delete_messages, project) #Developper |
747 end | 913 end |
748 end | 914 end |
749 | 915 |
750 context "with multiple projects" do | 916 context "with multiple projects" do |
751 should "return false if array is empty" do | 917 should "return false if array is empty" do |
752 assert ! @admin.allowed_to?(:view_project, []) | 918 assert_equal false, @admin.allowed_to?(:view_project, []) |
753 end | 919 end |
754 | 920 |
755 should "return true only if user has permission on all these projects" do | 921 should "return true only if user has permission on all these projects" do |
756 assert @admin.allowed_to?(:view_project, Project.all) | 922 assert_equal true, @admin.allowed_to?(:view_project, Project.all) |
757 assert ! @dlopper.allowed_to?(:view_project, Project.all) #cannot see Project(2) | 923 assert_equal false, @dlopper.allowed_to?(:view_project, Project.all) #cannot see Project(2) |
758 assert @jsmith.allowed_to?(:edit_issues, @jsmith.projects) #Manager or Developer everywhere | 924 assert_equal true, @jsmith.allowed_to?(:edit_issues, @jsmith.projects) #Manager or Developer everywhere |
759 assert ! @jsmith.allowed_to?(:delete_issue_watchers, @jsmith.projects) #Dev cannot delete_issue_watchers | 925 assert_equal false, @jsmith.allowed_to?(:delete_issue_watchers, @jsmith.projects) #Dev cannot delete_issue_watchers |
760 end | 926 end |
761 | 927 |
762 should "behave correctly with arrays of 1 project" do | 928 should "behave correctly with arrays of 1 project" do |
763 assert ! User.anonymous.allowed_to?(:delete_issues, [Project.first]) | 929 assert_equal false, User.anonymous.allowed_to?(:delete_issues, [Project.first]) |
764 end | 930 end |
765 end | 931 end |
766 | 932 |
767 context "with options[:global]" do | 933 context "with options[:global]" do |
768 should "authorize if user has at least one role that has this permission" do | 934 should "authorize if user has at least one role that has this permission" do |
769 @dlopper2 = User.find(5) #only Developper on a project, not Manager anywhere | 935 @dlopper2 = User.find(5) #only Developper on a project, not Manager anywhere |
770 @anonymous = User.find(6) | 936 @anonymous = User.find(6) |
771 assert @jsmith.allowed_to?(:delete_issue_watchers, nil, :global => true) | 937 assert_equal true, @jsmith.allowed_to?(:delete_issue_watchers, nil, :global => true) |
772 assert ! @dlopper2.allowed_to?(:delete_issue_watchers, nil, :global => true) | 938 assert_equal false, @dlopper2.allowed_to?(:delete_issue_watchers, nil, :global => true) |
773 assert @dlopper2.allowed_to?(:add_issues, nil, :global => true) | 939 assert_equal true, @dlopper2.allowed_to?(:add_issues, nil, :global => true) |
774 assert ! @anonymous.allowed_to?(:add_issues, nil, :global => true) | 940 assert_equal false, @anonymous.allowed_to?(:add_issues, nil, :global => true) |
775 assert @anonymous.allowed_to?(:view_issues, nil, :global => true) | 941 assert_equal true, @anonymous.allowed_to?(:view_issues, nil, :global => true) |
776 end | 942 end |
777 end | 943 end |
778 end | 944 end |
779 | 945 |
780 context "User#notify_about?" do | 946 context "User#notify_about?" do |
781 context "Issues" do | 947 context "Issues" do |
782 setup do | 948 setup do |
783 @project = Project.find(1) | 949 @project = Project.find(1) |
784 @author = User.generate_with_protected! | 950 @author = User.generate! |
785 @assignee = User.generate_with_protected! | 951 @assignee = User.generate! |
786 @issue = Issue.generate_for_project!(@project, :assigned_to => @assignee, :author => @author) | 952 @issue = Issue.generate!(:project => @project, :assigned_to => @assignee, :author => @author) |
787 end | 953 end |
788 | 954 |
789 should "be true for a user with :all" do | 955 should "be true for a user with :all" do |
790 @author.update_attribute(:mail_notification, 'all') | 956 @author.update_attribute(:mail_notification, 'all') |
791 assert @author.notify_about?(@issue) | 957 assert @author.notify_about?(@issue) |
795 @author.update_attribute(:mail_notification, 'none') | 961 @author.update_attribute(:mail_notification, 'none') |
796 assert ! @author.notify_about?(@issue) | 962 assert ! @author.notify_about?(@issue) |
797 end | 963 end |
798 | 964 |
799 should "be false for a user with :only_my_events and isn't an author, creator, or assignee" do | 965 should "be false for a user with :only_my_events and isn't an author, creator, or assignee" do |
800 @user = User.generate_with_protected!(:mail_notification => 'only_my_events') | 966 @user = User.generate!(:mail_notification => 'only_my_events') |
801 Member.create!(:user => @user, :project => @project, :role_ids => [1]) | 967 Member.create!(:user => @user, :project => @project, :role_ids => [1]) |
802 assert ! @user.notify_about?(@issue) | 968 assert ! @user.notify_about?(@issue) |
803 end | 969 end |
804 | 970 |
805 should "be true for a user with :only_my_events and is the author" do | 971 should "be true for a user with :only_my_events and is the author" do |
841 @assignee.update_attribute(:mail_notification, 'selected') | 1007 @assignee.update_attribute(:mail_notification, 'selected') |
842 assert @assignee.notify_about?(@issue) | 1008 assert @assignee.notify_about?(@issue) |
843 end | 1009 end |
844 | 1010 |
845 should "be false for a user with :selected and is not the author or assignee" do | 1011 should "be false for a user with :selected and is not the author or assignee" do |
846 @user = User.generate_with_protected!(:mail_notification => 'selected') | 1012 @user = User.generate!(:mail_notification => 'selected') |
847 Member.create!(:user => @user, :project => @project, :role_ids => [1]) | 1013 Member.create!(:user => @user, :project => @project, :role_ids => [1]) |
848 assert ! @user.notify_about?(@issue) | 1014 assert ! @user.notify_about?(@issue) |
849 end | 1015 end |
850 end | 1016 end |
851 | 1017 |