Merge branch 'develop' of akkoma.dev:AkkomaGang/akkoma into develop
[akkoma] / test / pleroma / user_search_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.UserSearchTest do
6 alias Pleroma.User
7 use Pleroma.DataCase
8
9 import Pleroma.Factory
10
11 setup_all do
12 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
13 :ok
14 end
15
16 describe "User.search" do
17 setup do: clear_config([:instance, :limit_to_local_content])
18
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"})
23
24 [first_user, _second_user] = User.search("https://lain.com/users/lain", resolve: true)
25
26 assert first_user.id == user.id
27 end
28
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"})
32
33 [first_user, _second_user] = User.search("https://lain.com/users/lain")
34
35 assert first_user.id == user.id
36 end
37
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")
42 end
43
44 test "returns a user with matching uri as the first result" do
45 user =
46 insert(:user, %{
47 nickname: "no_relation",
48 ap_id: "https://lain.com/users/lain",
49 uri: "https://lain.com/@lain"
50 })
51
52 _user = insert(:user, %{nickname: "com_user"})
53
54 [first_user, _second_user] = User.search("https://lain.com/@lain")
55
56 assert first_user.id == user.id
57 end
58
59 test "excludes invisible users from results" do
60 user = insert(:user, %{nickname: "john t1000"})
61 insert(:user, %{invisible: true, nickname: "john t800"})
62
63 [found_user] = User.search("john")
64 assert found_user.id == user.id
65 end
66
67 test "excludes deactivated users from results" do
68 user = insert(:user, %{nickname: "john t1000"})
69 insert(:user, %{is_active: false, nickname: "john t800"})
70
71 [found_user] = User.search("john")
72 assert found_user.id == user.id
73 end
74
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"})
79
80 users = User.search("john")
81 assert Enum.count(users) == 2
82 end
83
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")
88
89 assert [found_user1, found_user2] = User.search("user")
90 assert [found_user1.id, found_user2.id] -- [service.id, person.id] == []
91 end
92
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
97 end
98
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
103 end
104
105 defp clear_virtual_fields(user) do
106 Map.merge(user, %{search_rank: nil, search_type: nil})
107 end
108
109 test "finds a user by full nickname or its leading fragment" do
110 user = insert(:user, %{nickname: "john"})
111
112 Enum.each(["john", "jo", "j"], fn query ->
113 assert user ==
114 User.search(query)
115 |> List.first()
116 |> clear_virtual_fields()
117 end)
118 end
119
120 test "finds a user by full name or leading fragment(s) of its words" do
121 user = insert(:user, %{name: "John Doe"})
122
123 Enum.each(["John Doe", "JOHN", "doe", "j d", "j", "d"], fn query ->
124 assert user ==
125 User.search(query)
126 |> List.first()
127 |> clear_virtual_fields()
128 end)
129 end
130
131 test "matches by leading fragment of user domain" do
132 user = insert(:user, %{nickname: "arandom@dude.com"})
133 insert(:user, %{nickname: "iamthedude"})
134
135 assert [user.id] == User.search("dud") |> Enum.map(& &1.id)
136 end
137
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"})
141
142 results = User.search("hj")
143
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
146 end
147
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"})
151
152 assert [u2.id, u1.id] == Enum.map(User.search("bar word"), & &1.id)
153 end
154
155 test "finds users, boosting ranks of friends and followers" do
156 u1 = insert(:user)
157 u2 = insert(:user, %{name: "Doe"})
158 follower = insert(:user, %{name: "Doe"})
159 friend = insert(:user, %{name: "Doe"})
160
161 {:ok, follower, u1} = User.follow(follower, u1)
162 {:ok, u1, friend} = User.follow(u1, friend)
163
164 assert [friend.id, follower.id, u2.id] --
165 Enum.map(User.search("doe", resolve: false, for_user: u1), & &1.id) == []
166 end
167
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"})
174
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)
178
179 assert Enum.map(User.search("jimi", following: true, for_user: lizz), & &1.id) == [
180 following_lizz.id
181 ]
182
183 assert User.search("lizz", following: true, for_user: lizz) == []
184 end
185
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})
190
191 results =
192 "lain"
193 |> User.search(for_user: u1)
194 |> Enum.map(& &1.id)
195 |> Enum.sort()
196
197 assert [u1.id, u2.id, u3.id] == results
198 end
199
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})
204
205 assert [%{id: ^id}] = User.search("lain")
206 end
207
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)
210
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})
214
215 assert [%{id: ^id}] = User.search("lain")
216 end
217
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)
220
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})
224
225 results =
226 "lain"
227 |> User.search()
228 |> Enum.map(& &1.id)
229 |> Enum.sort()
230
231 assert [u1.id, u2.id, u3.id] == results
232 end
233
234 test "does not yield false-positive matches" do
235 insert(:user, %{name: "John Doe"})
236
237 Enum.each(["mary", "a", ""], fn query ->
238 assert [] == User.search(query)
239 end)
240 end
241
242 test "works with URIs" do
243 user = insert(:user)
244
245 results =
246 User.search("http://mastodon.example.org/users/admin", resolve: true, for_user: user)
247
248 result = results |> List.first()
249
250 user = User.get_cached_by_ap_id("http://mastodon.example.org/users/admin")
251
252 assert length(results) == 1
253
254 expected =
255 result
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)
261
262 assert user == expected
263 end
264
265 test "excludes a blocked users from search result" do
266 user = insert(:user, %{nickname: "Bill"})
267
268 [blocked_user | users] = Enum.map(0..3, &insert(:user, %{nickname: "john#{&1}"}))
269
270 blocked_user2 =
271 insert(
272 :user,
273 %{nickname: "john awful", ap_id: "https://awful-and-rude-instance.com/user/bully"}
274 )
275
276 User.block_domain(user, "awful-and-rude-instance.com")
277 User.block(user, blocked_user)
278
279 account_ids = User.search("john", for_user: refresh_record(user)) |> collect_ids
280
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
285 end
286
287 test "local user has the same search_rank as for users with the same nickname, but another domain" do
288 user = insert(:user)
289 insert(:user, nickname: "lain@mastodon.social")
290 insert(:user, nickname: "lain")
291 insert(:user, nickname: "lain@pleroma.social")
292
293 assert User.search("lain@localhost", resolve: true, for_user: user)
294 |> Enum.each(fn u -> u.search_rank == 0.5 end)
295 end
296
297 test "localhost is the part of the domain" do
298 user = insert(:user)
299 insert(:user, nickname: "another@somedomain")
300 insert(:user, nickname: "lain")
301 insert(:user, nickname: "lain@examplelocalhost")
302
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
306 end
307
308 test "local user search with users" do
309 user = insert(:user)
310 local_user = insert(:user, nickname: "lain")
311 insert(:user, nickname: "another@localhost.com")
312 insert(:user, nickname: "localhost@localhost.com")
313
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
316 end
317
318 test "works with idna domains" do
319 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
320
321 results = User.search("lain@zetsubou.みんな", resolve: false, for_user: user)
322
323 result = List.first(results)
324
325 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
326 end
327
328 test "works with idna domains converted input" do
329 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
330
331 results =
332 User.search("lain@zetsubou." <> to_string(:idna.encode("zetsubou.みんな")),
333 resolve: false,
334 for_user: user
335 )
336
337 result = List.first(results)
338
339 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
340 end
341
342 test "works with idna domains and bad chars in domain" do
343 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
344
345 results =
346 User.search("lain@zetsubou!@#$%^&*()+,-/:;<=>?[]'_{}|~`.みんな",
347 resolve: false,
348 for_user: user
349 )
350
351 result = List.first(results)
352
353 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
354 end
355
356 test "works with idna domains and query as link" do
357 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
358
359 results =
360 User.search("https://zetsubou.みんな/users/lain",
361 resolve: false,
362 for_user: user
363 )
364
365 result = List.first(results)
366
367 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
368 end
369 end
370 end