You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

principal_test.rb 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # encoding: utf-8
  2. #
  3. # Redmine - project management software
  4. # Copyright (C) 2006-2017 Jean-Philippe Lang
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License
  8. # as published by the Free Software Foundation; either version 2
  9. # of the License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. require File.expand_path('../../test_helper', __FILE__)
  20. class PrincipalTest < ActiveSupport::TestCase
  21. fixtures :users, :projects, :members, :member_roles, :roles,
  22. :email_addresses
  23. def setup
  24. User.current = nil
  25. end
  26. def test_active_scope_should_return_groups_and_active_users
  27. result = Principal.active.to_a
  28. assert_include Group.first, result
  29. assert_not_nil result.detect {|p| p.is_a?(User)}
  30. assert_nil result.detect {|p| p.is_a?(User) && !p.active?}
  31. assert_nil result.detect {|p| p.is_a?(AnonymousUser)}
  32. end
  33. def test_visible_scope_for_admin_should_return_all_principals
  34. admin = User.generate! {|u| u.admin = true}
  35. assert_equal Principal.count, Principal.visible(admin).count
  36. end
  37. def test_visible_scope_for_user_with_members_of_visible_projects_visibility_should_return_active_principals
  38. Role.non_member.update! :users_visibility => 'all'
  39. user = User.generate!
  40. expected = Principal.active
  41. assert_equal expected.map(&:id).sort, Principal.visible(user).pluck(:id).sort
  42. end
  43. def test_visible_scope_for_user_with_members_of_visible_projects_visibility_should_return_members_of_visible_projects_and_self
  44. Role.non_member.update! :users_visibility => 'members_of_visible_projects'
  45. user = User.generate!
  46. expected = Project.visible(user).map {|p| p.memberships.active}.flatten.map(&:principal).uniq << user
  47. assert_equal expected.map(&:id).sort, Principal.visible(user).pluck(:id).sort
  48. end
  49. def test_member_of_scope_should_return_the_union_of_all_active_and_locked_members
  50. projects = Project.find([1])
  51. assert_equal [3, 5, 2], Principal.member_of(projects).sort.map(&:id)
  52. projects = Project.find([1, 2])
  53. assert_equal [3, 5, 2, 8, 11], Principal.member_of(projects).sort.map(&:id)
  54. end
  55. def test_member_of_scope_should_be_empty_for_no_projects
  56. assert_equal [], Principal.member_of([]).sort
  57. end
  58. def test_not_member_of_scope_should_return_users_that_have_no_memberships
  59. [[1], [1, 2]].each do |ids|
  60. projects = Project.find(ids)
  61. assert_equal ids.size, projects.count
  62. expected = (Principal.all - projects.map(&:memberships).flatten.map(&:principal)).sort
  63. assert_equal expected, Principal.not_member_of(projects).sort
  64. end
  65. end
  66. def test_not_member_of_scope_should_be_empty_for_no_projects
  67. assert_equal [], Principal.not_member_of([]).sort
  68. end
  69. def test_sorted_scope_should_sort_users_before_groups
  70. scope = Principal.where(:type => ['User', 'Group'])
  71. users = scope.select {|p| p.is_a?(User)}.sort
  72. groups = scope.select {|p| p.is_a?(Group)}.sort
  73. assert_equal (users + groups).map(&:name).map(&:downcase),
  74. scope.sorted.map(&:name).map(&:downcase)
  75. end
  76. test "like scope should search login" do
  77. results = Principal.like('jsmi')
  78. assert results.any?
  79. assert results.all? {|u| u.login.match(/jsmi/i) }
  80. end
  81. test "like scope should search firstname" do
  82. results = Principal.like('john')
  83. assert results.any?
  84. assert results.all? {|u| u.firstname.match(/john/i) }
  85. end
  86. test "like scope should search lastname" do
  87. results = Principal.like('smi')
  88. assert results.any?
  89. assert results.all? {|u| u.lastname.match(/smi/i) }
  90. end
  91. test "like scope should search mail" do
  92. results = Principal.like('somenet')
  93. assert results.any?
  94. assert results.all? {|u| u.mail.match(/somenet/i) }
  95. end
  96. test "like scope should search firstname and lastname" do
  97. results = Principal.like('john smi')
  98. assert_equal 1, results.count
  99. assert_equal User.find(2), results.first
  100. end
  101. test "like scope should search lastname and firstname" do
  102. results = Principal.like('smith joh')
  103. assert_equal 1, results.count
  104. assert_equal User.find(2), results.first
  105. end
  106. test "like scope should find lastname with spaces" do
  107. user = User.find(1)
  108. user.update_columns(:firstname => 'Leonardo', :lastname => 'da Vinci')
  109. results = Principal.like('Leonardo da Vinci')
  110. assert_equal 1, results.count
  111. assert_equal user, results.first
  112. end
  113. def test_like_scope_with_cyrillic_name
  114. user = User.generate!(:firstname => 'Соболев', :lastname => 'Денис')
  115. results = Principal.like('Собо')
  116. assert_equal 1, results.count
  117. assert_equal user, results.first
  118. end
  119. end