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.7KB

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