logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

user_search_test.exs (12190B)


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