1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.UserSearchTest do
10 import Pleroma.Factory
13 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
17 describe "User.search" do
18 setup do: clear_config([:instance, :limit_to_local_content])
20 test "returns a resolved user as the first result" do
21 clear_config([:instance, :limit_to_local_content], false)
22 user = insert(:user, %{nickname: "no_relation", ap_id: "https://lain.com/users/lain"})
23 _user = insert(:user, %{nickname: "com_user"})
25 [first_user, _second_user] = User.search("https://lain.com/users/lain", resolve: true)
27 assert first_user.id == user.id
30 test "returns a user with matching ap_id as the first result" do
31 user = insert(:user, %{nickname: "no_relation", ap_id: "https://lain.com/users/lain"})
32 _user = insert(:user, %{nickname: "com_user"})
34 [first_user, _second_user] = User.search("https://lain.com/users/lain")
36 assert first_user.id == user.id
39 test "doesn't die if two users have the same uri" do
40 insert(:user, %{uri: "https://gensokyo.2hu/@raymoo"})
41 insert(:user, %{uri: "https://gensokyo.2hu/@raymoo"})
42 assert [_first_user, _second_user] = User.search("https://gensokyo.2hu/@raymoo")
45 test "returns a user with matching uri as the first result" do
48 nickname: "no_relation",
49 ap_id: "https://lain.com/users/lain",
50 uri: "https://lain.com/@lain"
53 _user = insert(:user, %{nickname: "com_user"})
55 [first_user, _second_user] = User.search("https://lain.com/@lain")
57 assert first_user.id == user.id
60 test "excludes invisible users from results" do
61 user = insert(:user, %{nickname: "john t1000"})
62 insert(:user, %{invisible: true, nickname: "john t800"})
64 [found_user] = User.search("john")
65 assert found_user.id == user.id
68 test "excludes deactivated users from results" do
69 user = insert(:user, %{nickname: "john t1000"})
70 insert(:user, %{is_active: false, nickname: "john t800"})
72 [found_user] = User.search("john")
73 assert found_user.id == user.id
76 # Note: as in Mastodon, `is_discoverable` doesn't anyhow relate to user searchability
77 test "includes non-discoverable users in results" do
78 insert(:user, %{nickname: "john 3000", is_discoverable: false})
79 insert(:user, %{nickname: "john 3001"})
81 users = User.search("john")
82 assert Enum.count(users) == 2
85 test "excludes service actors from results" do
86 insert(:user, actor_type: "Application", nickname: "user1")
87 service = insert(:user, actor_type: "Service", nickname: "user2")
88 person = insert(:user, actor_type: "Person", nickname: "user3")
90 assert [found_user1, found_user2] = User.search("user")
91 assert [found_user1.id, found_user2.id] -- [service.id, person.id] == []
94 test "accepts limit parameter" do
95 Enum.each(0..4, &insert(:user, %{nickname: "john#{&1}"}))
96 assert length(User.search("john", limit: 3)) == 3
97 assert length(User.search("john")) == 5
100 test "accepts offset parameter" do
101 Enum.each(0..4, &insert(:user, %{nickname: "john#{&1}"}))
102 assert length(User.search("john", limit: 3)) == 3
103 assert length(User.search("john", limit: 3, offset: 3)) == 2
106 defp clear_virtual_fields(user) do
107 Map.merge(user, %{search_rank: nil, search_type: nil})
110 test "finds a user by full nickname or its leading fragment" do
111 user = insert(:user, %{nickname: "john"})
113 Enum.each(["john", "jo", "j"], fn query ->
117 |> clear_virtual_fields()
121 test "finds a user by full name or leading fragment(s) of its words" do
122 user = insert(:user, %{name: "John Doe"})
124 Enum.each(["John Doe", "JOHN", "doe", "j d", "j", "d"], fn query ->
128 |> clear_virtual_fields()
132 test "matches by leading fragment of user domain" do
133 user = insert(:user, %{nickname: "arandom@dude.com"})
134 insert(:user, %{nickname: "iamthedude"})
136 assert [user.id] == User.search("dud") |> Enum.map(& &1.id)
139 test "ranks full nickname match higher than full name match" do
140 nicknamed_user = insert(:user, %{nickname: "hj@shigusegubu.club"})
141 named_user = insert(:user, %{nickname: "xyz@sample.com", name: "HJ"})
143 results = User.search("hj")
145 assert [nicknamed_user.id, named_user.id] == Enum.map(results, & &1.id)
146 assert Enum.at(results, 0).search_rank > Enum.at(results, 1).search_rank
149 test "finds users, considering density of matched tokens" do
150 u1 = insert(:user, %{name: "Bar Bar plus Word Word"})
151 u2 = insert(:user, %{name: "Word Word Bar Bar Bar"})
153 assert [u2.id, u1.id] == Enum.map(User.search("bar word"), & &1.id)
156 test "finds users, boosting ranks of friends and followers" do
158 u2 = insert(:user, %{name: "Doe"})
159 follower = insert(:user, %{name: "Doe"})
160 friend = insert(:user, %{name: "Doe"})
162 {:ok, follower, u1} = User.follow(follower, u1)
163 {:ok, u1, friend} = User.follow(u1, friend)
165 assert [friend.id, follower.id, u2.id] --
166 Enum.map(User.search("doe", resolve: false, for_user: u1), & &1.id) == []
169 test "finds followings of user by partial name" do
170 lizz = insert(:user, %{name: "Lizz"})
171 jimi = insert(:user, %{name: "Jimi"})
172 following_lizz = insert(:user, %{name: "Jimi Hendrix"})
173 following_jimi = insert(:user, %{name: "Lizz Wright"})
174 follower_lizz = insert(:user, %{name: "Jimi"})
176 {:ok, lizz, following_lizz} = User.follow(lizz, following_lizz)
177 {:ok, _jimi, _following_jimi} = User.follow(jimi, following_jimi)
178 {:ok, _follower_lizz, _lizz} = User.follow(follower_lizz, lizz)
180 assert Enum.map(User.search("jimi", following: true, for_user: lizz), & &1.id) == [
184 assert User.search("lizz", following: true, for_user: lizz) == []
187 test "find local and remote users for authenticated users" do
188 u1 = insert(:user, %{name: "lain"})
189 u2 = insert(:user, %{name: "ebn", nickname: "lain@mastodon.social", local: false})
190 u3 = insert(:user, %{nickname: "lain@pleroma.soykaf.com", local: false})
194 |> User.search(for_user: u1)
198 assert [u1.id, u2.id, u3.id] == results
201 test "find only local users for unauthenticated users" do
202 %{id: id} = insert(:user, %{name: "lain"})
203 insert(:user, %{name: "ebn", nickname: "lain@mastodon.social", local: false})
204 insert(:user, %{nickname: "lain@pleroma.soykaf.com", local: false})
206 assert [%{id: ^id}] = User.search("lain")
209 test "find only local users for authenticated users when `limit_to_local_content` is `:all`" do
210 clear_config([:instance, :limit_to_local_content], :all)
212 %{id: id} = insert(:user, %{name: "lain"})
213 insert(:user, %{name: "ebn", nickname: "lain@mastodon.social", local: false})
214 insert(:user, %{nickname: "lain@pleroma.soykaf.com", local: false})
216 assert [%{id: ^id}] = User.search("lain")
219 test "find all users for unauthenticated users when `limit_to_local_content` is `false`" do
220 clear_config([:instance, :limit_to_local_content], false)
222 u1 = insert(:user, %{name: "lain"})
223 u2 = insert(:user, %{name: "ebn", nickname: "lain@mastodon.social", local: false})
224 u3 = insert(:user, %{nickname: "lain@pleroma.soykaf.com", local: false})
232 assert [u1.id, u2.id, u3.id] == results
235 test "does not yield false-positive matches" do
236 insert(:user, %{name: "John Doe"})
238 Enum.each(["mary", "a", ""], fn query ->
239 assert [] == User.search(query)
243 test "works with URIs" do
247 User.search("http://mastodon.example.org/users/admin", resolve: true, for_user: user)
249 result = results |> List.first()
251 user = User.get_cached_by_ap_id("http://mastodon.example.org/users/admin")
253 assert length(results) == 1
257 |> Map.put(:search_rank, nil)
258 |> Map.put(:search_type, nil)
259 |> Map.put(:last_digest_emailed_at, nil)
260 |> Map.put(:multi_factor_authentication_settings, nil)
261 |> Map.put(:notification_settings, nil)
263 assert user == expected
266 test "excludes a blocked users from search result" do
267 user = insert(:user, %{nickname: "Bill"})
269 [blocked_user | users] = Enum.map(0..3, &insert(:user, %{nickname: "john#{&1}"}))
274 %{nickname: "john awful", ap_id: "https://awful-and-rude-instance.com/user/bully"}
277 User.block_domain(user, "awful-and-rude-instance.com")
278 User.block(user, blocked_user)
280 account_ids = User.search("john", for_user: refresh_record(user)) |> collect_ids
282 assert account_ids == collect_ids(users)
283 refute Enum.member?(account_ids, blocked_user.id)
284 refute Enum.member?(account_ids, blocked_user2.id)
285 assert length(account_ids) == 3
288 test "local user has the same search_rank as for users with the same nickname, but another domain" do
290 insert(:user, nickname: "lain@mastodon.social")
291 insert(:user, nickname: "lain")
292 insert(:user, nickname: "lain@pleroma.social")
294 assert User.search("lain@localhost", resolve: true, for_user: user)
295 |> Enum.each(fn u -> u.search_rank == 0.5 end)
298 test "localhost is the part of the domain" do
300 insert(:user, nickname: "another@somedomain")
301 insert(:user, nickname: "lain")
302 insert(:user, nickname: "lain@examplelocalhost")
304 result = User.search("lain@examplelocalhost", resolve: true, for_user: user)
305 assert Enum.each(result, fn u -> u.search_rank == 0.5 end)
306 assert length(result) == 2
309 test "local user search with users" do
311 local_user = insert(:user, nickname: "lain")
312 insert(:user, nickname: "another@localhost.com")
313 insert(:user, nickname: "localhost@localhost.com")
315 [result] = User.search("lain@localhost", resolve: true, for_user: user)
316 assert Map.put(result, :search_rank, nil) |> Map.put(:search_type, nil) == local_user
319 test "works with idna domains" do
320 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
322 results = User.search("lain@zetsubou.みんな", resolve: false, for_user: user)
324 result = List.first(results)
326 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
329 test "works with idna domains converted input" do
330 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
333 User.search("lain@zetsubou." <> to_string(:idna.encode("zetsubou.みんな")),
338 result = List.first(results)
340 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
343 test "works with idna domains and bad chars in domain" do
344 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
347 User.search("lain@zetsubou!@#$%^&*()+,-/:;<=>?[]'_{}|~`.みんな",
352 result = List.first(results)
354 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
357 test "works with idna domains and query as link" do
358 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
361 User.search("https://zetsubou.みんな/users/lain",
366 result = List.first(results)
368 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)