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
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 # Note: as in Mastodon, `is_discoverable` doesn't anyhow relate to user searchability
69 test "includes non-discoverable users in results" do
70 insert(:user, %{nickname: "john 3000", is_discoverable: false})
71 insert(:user, %{nickname: "john 3001"})
73 users = User.search("john")
74 assert Enum.count(users) == 2
77 test "excludes service actors from results" do
78 insert(:user, actor_type: "Application", nickname: "user1")
79 service = insert(:user, actor_type: "Service", nickname: "user2")
80 person = insert(:user, actor_type: "Person", nickname: "user3")
82 assert [found_user1, found_user2] = User.search("user")
83 assert [found_user1.id, found_user2.id] -- [service.id, person.id] == []
86 test "accepts limit parameter" do
87 Enum.each(0..4, &insert(:user, %{nickname: "john#{&1}"}))
88 assert length(User.search("john", limit: 3)) == 3
89 assert length(User.search("john")) == 5
92 test "accepts offset parameter" do
93 Enum.each(0..4, &insert(:user, %{nickname: "john#{&1}"}))
94 assert length(User.search("john", limit: 3)) == 3
95 assert length(User.search("john", limit: 3, offset: 3)) == 2
98 defp clear_virtual_fields(user) do
99 Map.merge(user, %{search_rank: nil, search_type: nil})
102 test "finds a user by full nickname or its leading fragment" do
103 user = insert(:user, %{nickname: "john"})
105 Enum.each(["john", "jo", "j"], fn query ->
109 |> clear_virtual_fields()
113 test "finds a user by full name or leading fragment(s) of its words" do
114 user = insert(:user, %{name: "John Doe"})
116 Enum.each(["John Doe", "JOHN", "doe", "j d", "j", "d"], fn query ->
120 |> clear_virtual_fields()
124 test "matches by leading fragment of user domain" do
125 user = insert(:user, %{nickname: "arandom@dude.com"})
126 insert(:user, %{nickname: "iamthedude"})
128 assert [user.id] == User.search("dud") |> Enum.map(& &1.id)
131 test "ranks full nickname match higher than full name match" do
132 nicknamed_user = insert(:user, %{nickname: "hj@shigusegubu.club"})
133 named_user = insert(:user, %{nickname: "xyz@sample.com", name: "HJ"})
135 results = User.search("hj")
137 assert [nicknamed_user.id, named_user.id] == Enum.map(results, & &1.id)
138 assert Enum.at(results, 0).search_rank > Enum.at(results, 1).search_rank
141 test "finds users, considering density of matched tokens" do
142 u1 = insert(:user, %{name: "Bar Bar plus Word Word"})
143 u2 = insert(:user, %{name: "Word Word Bar Bar Bar"})
145 assert [u2.id, u1.id] == Enum.map(User.search("bar word"), & &1.id)
148 test "finds users, boosting ranks of friends and followers" do
150 u2 = insert(:user, %{name: "Doe"})
151 follower = insert(:user, %{name: "Doe"})
152 friend = insert(:user, %{name: "Doe"})
154 {:ok, follower, u1} = User.follow(follower, u1)
155 {:ok, u1, friend} = User.follow(u1, friend)
157 assert [friend.id, follower.id, u2.id] --
158 Enum.map(User.search("doe", resolve: false, for_user: u1), & &1.id) == []
161 test "finds followings of user by partial name" do
162 lizz = insert(:user, %{name: "Lizz"})
163 jimi = insert(:user, %{name: "Jimi"})
164 following_lizz = insert(:user, %{name: "Jimi Hendrix"})
165 following_jimi = insert(:user, %{name: "Lizz Wright"})
166 follower_lizz = insert(:user, %{name: "Jimi"})
168 {:ok, lizz, following_lizz} = User.follow(lizz, following_lizz)
169 {:ok, _jimi, _following_jimi} = User.follow(jimi, following_jimi)
170 {:ok, _follower_lizz, _lizz} = User.follow(follower_lizz, lizz)
172 assert Enum.map(User.search("jimi", following: true, for_user: lizz), & &1.id) == [
176 assert User.search("lizz", following: true, for_user: lizz) == []
179 test "find local and remote users for authenticated users" do
180 u1 = insert(:user, %{name: "lain"})
181 u2 = insert(:user, %{name: "ebn", nickname: "lain@mastodon.social", local: false})
182 u3 = insert(:user, %{nickname: "lain@pleroma.soykaf.com", local: false})
186 |> User.search(for_user: u1)
190 assert [u1.id, u2.id, u3.id] == results
193 test "find only local users for unauthenticated users" do
194 %{id: id} = insert(:user, %{name: "lain"})
195 insert(:user, %{name: "ebn", nickname: "lain@mastodon.social", local: false})
196 insert(:user, %{nickname: "lain@pleroma.soykaf.com", local: false})
198 assert [%{id: ^id}] = User.search("lain")
201 test "find only local users for authenticated users when `limit_to_local_content` is `:all`" do
202 clear_config([:instance, :limit_to_local_content], :all)
204 %{id: id} = insert(:user, %{name: "lain"})
205 insert(:user, %{name: "ebn", nickname: "lain@mastodon.social", local: false})
206 insert(:user, %{nickname: "lain@pleroma.soykaf.com", local: false})
208 assert [%{id: ^id}] = User.search("lain")
211 test "find all users for unauthenticated users when `limit_to_local_content` is `false`" do
212 clear_config([:instance, :limit_to_local_content], false)
214 u1 = insert(:user, %{name: "lain"})
215 u2 = insert(:user, %{name: "ebn", nickname: "lain@mastodon.social", local: false})
216 u3 = insert(:user, %{nickname: "lain@pleroma.soykaf.com", local: false})
224 assert [u1.id, u2.id, u3.id] == results
227 test "does not yield false-positive matches" do
228 insert(:user, %{name: "John Doe"})
230 Enum.each(["mary", "a", ""], fn query ->
231 assert [] == User.search(query)
235 test "works with URIs" do
239 User.search("http://mastodon.example.org/users/admin", resolve: true, for_user: user)
241 result = results |> List.first()
243 user = User.get_cached_by_ap_id("http://mastodon.example.org/users/admin")
245 assert length(results) == 1
249 |> Map.put(:search_rank, nil)
250 |> Map.put(:search_type, nil)
251 |> Map.put(:last_digest_emailed_at, nil)
252 |> Map.put(:multi_factor_authentication_settings, nil)
253 |> Map.put(:notification_settings, nil)
255 assert user == expected
258 test "excludes a blocked users from search result" do
259 user = insert(:user, %{nickname: "Bill"})
261 [blocked_user | users] = Enum.map(0..3, &insert(:user, %{nickname: "john#{&1}"}))
266 %{nickname: "john awful", ap_id: "https://awful-and-rude-instance.com/user/bully"}
269 User.block_domain(user, "awful-and-rude-instance.com")
270 User.block(user, blocked_user)
272 account_ids = User.search("john", for_user: refresh_record(user)) |> collect_ids
274 assert account_ids == collect_ids(users)
275 refute Enum.member?(account_ids, blocked_user.id)
276 refute Enum.member?(account_ids, blocked_user2.id)
277 assert length(account_ids) == 3
280 test "local user has the same search_rank as for users with the same nickname, but another domain" do
282 insert(:user, nickname: "lain@mastodon.social")
283 insert(:user, nickname: "lain")
284 insert(:user, nickname: "lain@pleroma.social")
286 assert User.search("lain@localhost", resolve: true, for_user: user)
287 |> Enum.each(fn u -> u.search_rank == 0.5 end)
290 test "localhost is the part of the domain" do
292 insert(:user, nickname: "another@somedomain")
293 insert(:user, nickname: "lain")
294 insert(:user, nickname: "lain@examplelocalhost")
296 result = User.search("lain@examplelocalhost", resolve: true, for_user: user)
297 assert Enum.each(result, fn u -> u.search_rank == 0.5 end)
298 assert length(result) == 2
301 test "local user search with users" do
303 local_user = insert(:user, nickname: "lain")
304 insert(:user, nickname: "another@localhost.com")
305 insert(:user, nickname: "localhost@localhost.com")
307 [result] = User.search("lain@localhost", resolve: true, for_user: user)
308 assert Map.put(result, :search_rank, nil) |> Map.put(:search_type, nil) == local_user
311 test "works with idna domains" do
312 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
314 results = User.search("lain@zetsubou.みんな", resolve: false, for_user: user)
316 result = List.first(results)
318 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
321 test "works with idna domains converted input" do
322 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
325 User.search("lain@zetsubou." <> to_string(:idna.encode("zetsubou.みんな")),
330 result = List.first(results)
332 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
335 test "works with idna domains and bad chars in domain" do
336 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
339 User.search("lain@zetsubou!@#$%^&*()+,-/:;<=>?[]'_{}|~`.みんな",
344 result = List.first(results)
346 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
349 test "works with idna domains and query as link" do
350 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
353 User.search("https://zetsubou.みんな/users/lain",
358 result = List.first(results)
360 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)