Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
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.

323 lines
11KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.UserSearchTest do
  5. alias Pleroma.Repo
  6. alias Pleroma.User
  7. use Pleroma.DataCase
  8. import Pleroma.Factory
  9. setup_all do
  10. Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
  11. :ok
  12. end
  13. describe "User.search" do
  14. setup do: clear_config([:instance, :limit_to_local_content])
  15. test "excludes invisible users from results" do
  16. user = insert(:user, %{nickname: "john t1000"})
  17. insert(:user, %{invisible: true, nickname: "john t800"})
  18. [found_user] = User.search("john")
  19. assert found_user.id == user.id
  20. end
  21. test "excludes users when discoverable is false" do
  22. insert(:user, %{nickname: "john 3000", discoverable: false})
  23. insert(:user, %{nickname: "john 3001"})
  24. users = User.search("john")
  25. assert Enum.count(users) == 1
  26. end
  27. test "excludes service actors from results" do
  28. insert(:user, actor_type: "Application", nickname: "user1")
  29. service = insert(:user, actor_type: "Service", nickname: "user2")
  30. person = insert(:user, actor_type: "Person", nickname: "user3")
  31. assert [found_user1, found_user2] = User.search("user")
  32. assert [found_user1.id, found_user2.id] -- [service.id, person.id] == []
  33. end
  34. test "accepts limit parameter" do
  35. Enum.each(0..4, &insert(:user, %{nickname: "john#{&1}"}))
  36. assert length(User.search("john", limit: 3)) == 3
  37. assert length(User.search("john")) == 5
  38. end
  39. test "accepts offset parameter" do
  40. Enum.each(0..4, &insert(:user, %{nickname: "john#{&1}"}))
  41. assert length(User.search("john", limit: 3)) == 3
  42. assert length(User.search("john", limit: 3, offset: 3)) == 2
  43. end
  44. defp clear_virtual_fields(user) do
  45. Map.merge(user, %{search_rank: nil, search_type: nil})
  46. end
  47. test "finds a user by full nickname or its leading fragment" do
  48. user = insert(:user, %{nickname: "john"})
  49. Enum.each(["john", "jo", "j"], fn query ->
  50. assert user ==
  51. User.search(query)
  52. |> List.first()
  53. |> clear_virtual_fields()
  54. end)
  55. end
  56. test "finds a user by full name or leading fragment(s) of its words" do
  57. user = insert(:user, %{name: "John Doe"})
  58. Enum.each(["John Doe", "JOHN", "doe", "j d", "j", "d"], fn query ->
  59. assert user ==
  60. User.search(query)
  61. |> List.first()
  62. |> clear_virtual_fields()
  63. end)
  64. end
  65. test "matches by leading fragment of user domain" do
  66. user = insert(:user, %{nickname: "arandom@dude.com"})
  67. insert(:user, %{nickname: "iamthedude"})
  68. assert [user.id] == User.search("dud") |> Enum.map(& &1.id)
  69. end
  70. test "ranks full nickname match higher than full name match" do
  71. nicknamed_user = insert(:user, %{nickname: "hj@shigusegubu.club"})
  72. named_user = insert(:user, %{nickname: "xyz@sample.com", name: "HJ"})
  73. results = User.search("hj")
  74. assert [nicknamed_user.id, named_user.id] == Enum.map(results, & &1.id)
  75. assert Enum.at(results, 0).search_rank > Enum.at(results, 1).search_rank
  76. end
  77. test "finds users, considering density of matched tokens" do
  78. u1 = insert(:user, %{name: "Bar Bar plus Word Word"})
  79. u2 = insert(:user, %{name: "Word Word Bar Bar Bar"})
  80. assert [u2.id, u1.id] == Enum.map(User.search("bar word"), & &1.id)
  81. end
  82. test "finds users, boosting ranks of friends and followers" do
  83. u1 = insert(:user)
  84. u2 = insert(:user, %{name: "Doe"})
  85. follower = insert(:user, %{name: "Doe"})
  86. friend = insert(:user, %{name: "Doe"})
  87. {:ok, follower} = User.follow(follower, u1)
  88. {:ok, u1} = User.follow(u1, friend)
  89. assert [friend.id, follower.id, u2.id] --
  90. Enum.map(User.search("doe", resolve: false, for_user: u1), & &1.id) == []
  91. end
  92. test "finds followings of user by partial name" do
  93. lizz = insert(:user, %{name: "Lizz"})
  94. jimi = insert(:user, %{name: "Jimi"})
  95. following_lizz = insert(:user, %{name: "Jimi Hendrix"})
  96. following_jimi = insert(:user, %{name: "Lizz Wright"})
  97. follower_lizz = insert(:user, %{name: "Jimi"})
  98. {:ok, lizz} = User.follow(lizz, following_lizz)
  99. {:ok, _jimi} = User.follow(jimi, following_jimi)
  100. {:ok, _follower_lizz} = User.follow(follower_lizz, lizz)
  101. assert Enum.map(User.search("jimi", following: true, for_user: lizz), & &1.id) == [
  102. following_lizz.id
  103. ]
  104. assert User.search("lizz", following: true, for_user: lizz) == []
  105. end
  106. test "find local and remote users for authenticated users" do
  107. u1 = insert(:user, %{name: "lain"})
  108. u2 = insert(:user, %{name: "ebn", nickname: "lain@mastodon.social", local: false})
  109. u3 = insert(:user, %{nickname: "lain@pleroma.soykaf.com", local: false})
  110. results =
  111. "lain"
  112. |> User.search(for_user: u1)
  113. |> Enum.map(& &1.id)
  114. |> Enum.sort()
  115. assert [u1.id, u2.id, u3.id] == results
  116. end
  117. test "find only local users for unauthenticated users" do
  118. %{id: id} = insert(:user, %{name: "lain"})
  119. insert(:user, %{name: "ebn", nickname: "lain@mastodon.social", local: false})
  120. insert(:user, %{nickname: "lain@pleroma.soykaf.com", local: false})
  121. assert [%{id: ^id}] = User.search("lain")
  122. end
  123. test "find only local users for authenticated users when `limit_to_local_content` is `:all`" do
  124. Pleroma.Config.put([:instance, :limit_to_local_content], :all)
  125. %{id: id} = insert(:user, %{name: "lain"})
  126. insert(:user, %{name: "ebn", nickname: "lain@mastodon.social", local: false})
  127. insert(:user, %{nickname: "lain@pleroma.soykaf.com", local: false})
  128. assert [%{id: ^id}] = User.search("lain")
  129. end
  130. test "find all users for unauthenticated users when `limit_to_local_content` is `false`" do
  131. Pleroma.Config.put([:instance, :limit_to_local_content], false)
  132. u1 = insert(:user, %{name: "lain"})
  133. u2 = insert(:user, %{name: "ebn", nickname: "lain@mastodon.social", local: false})
  134. u3 = insert(:user, %{nickname: "lain@pleroma.soykaf.com", local: false})
  135. results =
  136. "lain"
  137. |> User.search()
  138. |> Enum.map(& &1.id)
  139. |> Enum.sort()
  140. assert [u1.id, u2.id, u3.id] == results
  141. end
  142. test "does not yield false-positive matches" do
  143. insert(:user, %{name: "John Doe"})
  144. Enum.each(["mary", "a", ""], fn query ->
  145. assert [] == User.search(query)
  146. end)
  147. end
  148. test "works with URIs" do
  149. user = insert(:user)
  150. results =
  151. User.search("http://mastodon.example.org/users/admin", resolve: true, for_user: user)
  152. result = results |> List.first()
  153. user = User.get_cached_by_ap_id("http://mastodon.example.org/users/admin")
  154. assert length(results) == 1
  155. expected =
  156. result
  157. |> Map.put(:search_rank, nil)
  158. |> Map.put(:search_type, nil)
  159. |> Map.put(:last_digest_emailed_at, nil)
  160. |> Map.put(:multi_factor_authentication_settings, nil)
  161. |> Map.put(:notification_settings, nil)
  162. assert user == expected
  163. end
  164. test "excludes a blocked users from search result" do
  165. user = insert(:user, %{nickname: "Bill"})
  166. [blocked_user | users] = Enum.map(0..3, &insert(:user, %{nickname: "john#{&1}"}))
  167. blocked_user2 =
  168. insert(
  169. :user,
  170. %{nickname: "john awful", ap_id: "https://awful-and-rude-instance.com/user/bully"}
  171. )
  172. User.block_domain(user, "awful-and-rude-instance.com")
  173. User.block(user, blocked_user)
  174. account_ids = User.search("john", for_user: refresh_record(user)) |> collect_ids
  175. assert account_ids == collect_ids(users)
  176. refute Enum.member?(account_ids, blocked_user.id)
  177. refute Enum.member?(account_ids, blocked_user2.id)
  178. assert length(account_ids) == 3
  179. end
  180. test "local user has the same search_rank as for users with the same nickname, but another domain" do
  181. user = insert(:user)
  182. insert(:user, nickname: "lain@mastodon.social")
  183. insert(:user, nickname: "lain")
  184. insert(:user, nickname: "lain@pleroma.social")
  185. assert User.search("lain@localhost", resolve: true, for_user: user)
  186. |> Enum.each(fn u -> u.search_rank == 0.5 end)
  187. end
  188. test "localhost is the part of the domain" do
  189. user = insert(:user)
  190. insert(:user, nickname: "another@somedomain")
  191. insert(:user, nickname: "lain")
  192. insert(:user, nickname: "lain@examplelocalhost")
  193. result = User.search("lain@examplelocalhost", resolve: true, for_user: user)
  194. assert Enum.each(result, fn u -> u.search_rank == 0.5 end)
  195. assert length(result) == 2
  196. end
  197. test "local user search with users" do
  198. user = insert(:user)
  199. local_user = insert(:user, nickname: "lain")
  200. insert(:user, nickname: "another@localhost.com")
  201. insert(:user, nickname: "localhost@localhost.com")
  202. [result] = User.search("lain@localhost", resolve: true, for_user: user)
  203. assert Map.put(result, :search_rank, nil) |> Map.put(:search_type, nil) == local_user
  204. end
  205. test "works with idna domains" do
  206. user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
  207. results = User.search("lain@zetsubou.みんな", resolve: false, for_user: user)
  208. result = List.first(results)
  209. assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
  210. end
  211. test "works with idna domains converted input" do
  212. user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
  213. results =
  214. User.search("lain@zetsubou." <> to_string(:idna.encode("zetsubou.みんな")),
  215. resolve: false,
  216. for_user: user
  217. )
  218. result = List.first(results)
  219. assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
  220. end
  221. test "works with idna domains and bad chars in domain" do
  222. user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
  223. results =
  224. User.search("lain@zetsubou!@#$%^&*()+,-/:;<=>?[]'_{}|~`.みんな",
  225. resolve: false,
  226. for_user: user
  227. )
  228. result = List.first(results)
  229. assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
  230. end
  231. test "works with idna domains and query as link" do
  232. user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
  233. results =
  234. User.search("https://zetsubou.みんな/users/lain",
  235. resolve: false,
  236. for_user: user
  237. )
  238. result = List.first(results)
  239. assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
  240. end
  241. end
  242. end