1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.UserSearchTest do
12 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
16 describe "User.search" do
17 setup do: clear_config([:instance, :limit_to_local_content])
19 test "returns a resolved user as the first result" do
20 clear_config([:instance, :limit_to_local_content], false)
21 user = insert(:user, %{nickname: "no_relation", ap_id: "https://lain.com/users/lain"})
22 _user = insert(:user, %{nickname: "com_user"})
24 [first_user, _second_user] = User.search("https://lain.com/users/lain", resolve: true)
26 assert first_user.id == user.id
29 test "returns a user with matching ap_id as the first result" do
30 user = insert(:user, %{nickname: "no_relation", ap_id: "https://lain.com/users/lain"})
31 _user = insert(:user, %{nickname: "com_user"})
33 [first_user, _second_user] = User.search("https://lain.com/users/lain")
35 assert first_user.id == user.id
38 test "doesn't die if two users have the same uri" do
39 insert(:user, %{uri: "https://gensokyo.2hu/@raymoo"})
40 insert(:user, %{uri: "https://gensokyo.2hu/@raymoo"})
41 assert [_first_user, _second_user] = User.search("https://gensokyo.2hu/@raymoo")
44 test "returns a user with matching uri as the first result" do
47 nickname: "no_relation",
48 ap_id: "https://lain.com/users/lain",
49 uri: "https://lain.com/@lain"
52 _user = insert(:user, %{nickname: "com_user"})
54 [first_user, _second_user] = User.search("https://lain.com/@lain")
56 assert first_user.id == user.id
59 test "excludes invisible users from results" do
60 user = insert(:user, %{nickname: "john t1000"})
61 insert(:user, %{invisible: true, nickname: "john t800"})
63 [found_user] = User.search("john")
64 assert found_user.id == user.id
67 test "excludes deactivated users from results" do
68 user = insert(:user, %{nickname: "john t1000"})
69 insert(:user, %{is_active: false, nickname: "john t800"})
71 [found_user] = User.search("john")
72 assert found_user.id == user.id
75 # Note: as in Mastodon, `is_discoverable` doesn't anyhow relate to user searchability
76 test "includes non-discoverable users in results" do
77 insert(:user, %{nickname: "john 3000", is_discoverable: false})
78 insert(:user, %{nickname: "john 3001"})
80 users = User.search("john")
81 assert Enum.count(users) == 2
84 test "excludes service actors from results" do
85 insert(:user, actor_type: "Application", nickname: "user1")
86 service = insert(:user, actor_type: "Service", nickname: "user2")
87 person = insert(:user, actor_type: "Person", nickname: "user3")
89 assert [found_user1, found_user2] = User.search("user")
90 assert [found_user1.id, found_user2.id] -- [service.id, person.id] == []
93 test "accepts limit parameter" do
94 Enum.each(0..4, &insert(:user, %{nickname: "john#{&1}"}))
95 assert length(User.search("john", limit: 3)) == 3
96 assert length(User.search("john")) == 5
99 test "accepts offset parameter" do
100 Enum.each(0..4, &insert(:user, %{nickname: "john#{&1}"}))
101 assert length(User.search("john", limit: 3)) == 3
102 assert length(User.search("john", limit: 3, offset: 3)) == 2
105 defp clear_virtual_fields(user) do
106 Map.merge(user, %{search_rank: nil, search_type: nil})
109 test "finds a user by full nickname or its leading fragment" do
110 user = insert(:user, %{nickname: "john"})
112 Enum.each(["john", "jo", "j"], fn query ->
116 |> clear_virtual_fields()
120 test "finds a user by full name or leading fragment(s) of its words" do
121 user = insert(:user, %{name: "John Doe"})
123 Enum.each(["John Doe", "JOHN", "doe", "j d", "j", "d"], fn query ->
127 |> clear_virtual_fields()
131 test "matches by leading fragment of user domain" do
132 user = insert(:user, %{nickname: "arandom@dude.com"})
133 insert(:user, %{nickname: "iamthedude"})
135 assert [user.id] == User.search("dud") |> Enum.map(& &1.id)
138 test "ranks full nickname match higher than full name match" do
139 nicknamed_user = insert(:user, %{nickname: "hj@shigusegubu.club"})
140 named_user = insert(:user, %{nickname: "xyz@sample.com", name: "HJ"})
142 results = User.search("hj")
144 assert [nicknamed_user.id, named_user.id] == Enum.map(results, & &1.id)
145 assert Enum.at(results, 0).search_rank > Enum.at(results, 1).search_rank
148 test "finds users, considering density of matched tokens" do
149 u1 = insert(:user, %{name: "Bar Bar plus Word Word"})
150 u2 = insert(:user, %{name: "Word Word Bar Bar Bar"})
152 assert [u2.id, u1.id] == Enum.map(User.search("bar word"), & &1.id)
155 test "finds users, boosting ranks of friends and followers" do
157 u2 = insert(:user, %{name: "Doe"})
158 follower = insert(:user, %{name: "Doe"})
159 friend = insert(:user, %{name: "Doe"})
161 {:ok, follower, u1} = User.follow(follower, u1)
162 {:ok, u1, friend} = User.follow(u1, friend)
164 assert [friend.id, follower.id, u2.id] --
165 Enum.map(User.search("doe", resolve: false, for_user: u1), & &1.id) == []
168 test "finds followings of user by partial name" do
169 lizz = insert(:user, %{name: "Lizz"})
170 jimi = insert(:user, %{name: "Jimi"})
171 following_lizz = insert(:user, %{name: "Jimi Hendrix"})
172 following_jimi = insert(:user, %{name: "Lizz Wright"})
173 follower_lizz = insert(:user, %{name: "Jimi"})
175 {:ok, lizz, following_lizz} = User.follow(lizz, following_lizz)
176 {:ok, _jimi, _following_jimi} = User.follow(jimi, following_jimi)
177 {:ok, _follower_lizz, _lizz} = User.follow(follower_lizz, lizz)
179 assert Enum.map(User.search("jimi", following: true, for_user: lizz), & &1.id) == [
183 assert User.search("lizz", following: true, for_user: lizz) == []
186 test "find local and remote users for authenticated users" do
187 u1 = insert(:user, %{name: "lain"})
188 u2 = insert(:user, %{name: "ebn", nickname: "lain@mastodon.social", local: false})
189 u3 = insert(:user, %{nickname: "lain@pleroma.soykaf.com", local: false})
193 |> User.search(for_user: u1)
197 assert [u1.id, u2.id, u3.id] == results
200 test "find only local users for unauthenticated users" do
201 %{id: id} = insert(:user, %{name: "lain"})
202 insert(:user, %{name: "ebn", nickname: "lain@mastodon.social", local: false})
203 insert(:user, %{nickname: "lain@pleroma.soykaf.com", local: false})
205 assert [%{id: ^id}] = User.search("lain")
208 test "find only local users for authenticated users when `limit_to_local_content` is `:all`" do
209 clear_config([:instance, :limit_to_local_content], :all)
211 %{id: id} = insert(:user, %{name: "lain"})
212 insert(:user, %{name: "ebn", nickname: "lain@mastodon.social", local: false})
213 insert(:user, %{nickname: "lain@pleroma.soykaf.com", local: false})
215 assert [%{id: ^id}] = User.search("lain")
218 test "find all users for unauthenticated users when `limit_to_local_content` is `false`" do
219 clear_config([:instance, :limit_to_local_content], false)
221 u1 = insert(:user, %{name: "lain"})
222 u2 = insert(:user, %{name: "ebn", nickname: "lain@mastodon.social", local: false})
223 u3 = insert(:user, %{nickname: "lain@pleroma.soykaf.com", local: false})
231 assert [u1.id, u2.id, u3.id] == results
234 test "does not yield false-positive matches" do
235 insert(:user, %{name: "John Doe"})
237 Enum.each(["mary", "a", ""], fn query ->
238 assert [] == User.search(query)
242 test "works with URIs" do
246 User.search("http://mastodon.example.org/users/admin", resolve: true, for_user: user)
248 result = results |> List.first()
250 user = User.get_cached_by_ap_id("http://mastodon.example.org/users/admin")
252 assert length(results) == 1
256 |> Map.put(:search_rank, nil)
257 |> Map.put(:search_type, nil)
258 |> Map.put(:last_digest_emailed_at, nil)
259 |> Map.put(:multi_factor_authentication_settings, nil)
260 |> Map.put(:notification_settings, nil)
262 assert user == expected
265 test "excludes a blocked users from search result" do
266 user = insert(:user, %{nickname: "Bill"})
268 [blocked_user | users] = Enum.map(0..3, &insert(:user, %{nickname: "john#{&1}"}))
273 %{nickname: "john awful", ap_id: "https://awful-and-rude-instance.com/user/bully"}
276 User.block_domain(user, "awful-and-rude-instance.com")
277 User.block(user, blocked_user)
279 account_ids = User.search("john", for_user: refresh_record(user)) |> collect_ids
281 assert account_ids == collect_ids(users)
282 refute Enum.member?(account_ids, blocked_user.id)
283 refute Enum.member?(account_ids, blocked_user2.id)
284 assert length(account_ids) == 3
287 test "local user has the same search_rank as for users with the same nickname, but another domain" do
289 insert(:user, nickname: "lain@mastodon.social")
290 insert(:user, nickname: "lain")
291 insert(:user, nickname: "lain@pleroma.social")
293 assert User.search("lain@localhost", resolve: true, for_user: user)
294 |> Enum.each(fn u -> u.search_rank == 0.5 end)
297 test "localhost is the part of the domain" do
299 insert(:user, nickname: "another@somedomain")
300 insert(:user, nickname: "lain")
301 insert(:user, nickname: "lain@examplelocalhost")
303 result = User.search("lain@examplelocalhost", resolve: true, for_user: user)
304 assert Enum.each(result, fn u -> u.search_rank == 0.5 end)
305 assert length(result) == 2
308 test "local user search with users" do
310 local_user = insert(:user, nickname: "lain")
311 insert(:user, nickname: "another@localhost.com")
312 insert(:user, nickname: "localhost@localhost.com")
314 [result] = User.search("lain@localhost", resolve: true, for_user: user)
315 assert Map.put(result, :search_rank, nil) |> Map.put(:search_type, nil) == local_user
318 test "works with idna domains" do
319 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
321 results = User.search("lain@zetsubou.みんな", resolve: false, for_user: user)
323 result = List.first(results)
325 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
328 test "works with idna domains converted input" do
329 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
332 User.search("lain@zetsubou." <> to_string(:idna.encode("zetsubou.みんな")),
337 result = List.first(results)
339 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
342 test "works with idna domains and bad chars in domain" do
343 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
346 User.search("lain@zetsubou!@#$%^&*()+,-/:;<=>?[]'_{}|~`.みんな",
351 result = List.first(results)
353 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
356 test "works with idna domains and query as link" do
357 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
360 User.search("https://zetsubou.みんな/users/lain",
365 result = List.first(results)
367 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)