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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 test_active_scope_should_return_groups_and_active_users
  24. result = Principal.active.to_a
  25. assert_include Group.first, result
  26. assert_not_nil result.detect {|p| p.is_a?(User)}
  27. assert_nil result.detect {|p| p.is_a?(User) && !p.active?}
  28. assert_nil result.detect {|p| p.is_a?(AnonymousUser)}
  29. end
  30. def test_visible_scope_for_admin_should_return_all_principals
  31. admin = User.generate! {|u| u.admin = true}
  32. assert_equal Principal.count, Principal.visible(admin).count
  33. end
  34. def test_visible_scope_for_user_with_members_of_visible_projects_visibility_should_return_active_principals
  35. Role.non_member.update! :users_visibility => 'all'
  36. user = User.generate!
  37. expected = Principal.active
  38. assert_equal expected.map(&:id).sort, Principal.visible(user).pluck(:id).sort
  39. end
  40. def test_visible_scope_for_user_with_members_of_visible_projects_visibility_should_return_members_of_visible_projects_and_self
  41. Role.non_member.update! :users_visibility => 'members_of_visible_projects'
  42. user = User.generate!
  43. expected = Project.visible(user).map {|p| p.memberships.active}.flatten.map(&:principal).uniq << user
  44. assert_equal expected.map(&:id).sort, Principal.visible(user).pluck(:id).sort
  45. end
  46. def test_member_of_scope_should_return_the_union_of_all_active_and_locked_members
  47. projects = Project.find([1])
  48. assert_equal [3, 5, 2], Principal.member_of(projects).sort.map(&:id)
  49. projects = Project.find([1, 2])
  50. assert_equal [3, 5, 2, 8, 11], Principal.member_of(projects).sort.map(&:id)
  51. end
  52. def test_member_of_scope_should_be_empty_for_no_projects
  53. assert_equal [], Principal.member_of([]).sort
  54. end
  55. def test_not_member_of_scope_should_return_users_that_have_no_memberships
  56. [[1], [1, 2]].each do |ids|
  57. projects = Project.find(ids)
  58. assert_equal ids.size, projects.count
  59. expected = (Principal.all - projects.map(&:memberships).flatten.map(&:principal)).sort
  60. assert_equal expected, Principal.not_member_of(projects).sort
  61. end
  62. end
  63. def test_not_member_of_scope_should_be_empty_for_no_projects
  64. assert_equal [], Principal.not_member_of([]).sort
  65. end
  66. def test_sorted_scope_should_sort_users_before_groups
  67. scope = Principal.where(:type => ['User', 'Group'])
  68. users = scope.select {|p| p.is_a?(User)}.sort
  69. groups = scope.select {|p| p.is_a?(Group)}.sort
  70. assert_equal (users + groups).map(&:name).map(&:downcase),
  71. scope.sorted.map(&:name).map(&:downcase)
  72. end
  73. test "like scope should search login" do
  74. results = Principal.like('jsmi')
  75. assert results.any?
  76. assert results.all? {|u| u.login.match(/jsmi/i) }
  77. end
  78. test "like scope should search firstname" do
  79. results = Principal.like('john')
  80. assert results.any?
  81. assert results.all? {|u| u.firstname.match(/john/i) }
  82. end
  83. test "like scope should search lastname" do
  84. results = Principal.like('smi')
  85. assert results.any?
  86. assert results.all? {|u| u.lastname.match(/smi/i) }
  87. end
  88. test "like scope should search mail" do
  89. results = Principal.like('somenet')
  90. assert results.any?
  91. assert results.all? {|u| u.mail.match(/somenet/i) }
  92. end
  93. test "like scope should search firstname and lastname" do
  94. results = Principal.like('john smi')
  95. assert_equal 1, results.count
  96. assert_equal User.find(2), results.first
  97. end
  98. test "like scope should search lastname and firstname" do
  99. results = Principal.like('smith joh')
  100. assert_equal 1, results.count
  101. assert_equal User.find(2), results.first
  102. end
  103. test "like scope should find lastname with spaces" do
  104. user = User.find(1)
  105. user.update_columns(:firstname => 'Leonardo', :lastname => 'da Vinci')
  106. results = Principal.like('Leonardo da Vinci')
  107. assert_equal 1, results.count
  108. assert_equal user, results.first
  109. end
  110. def test_like_scope_with_cyrillic_name
  111. user = User.generate!(:firstname => 'Соболев', :lastname => 'Денис')
  112. results = Principal.like('Собо')
  113. assert_equal 1, results.count
  114. assert_equal user, results.first
  115. end
  116. end