Add test for following with subscription
[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.Repo
7 alias Pleroma.User
8 use Pleroma.DataCase
9
10 import Pleroma.Factory
11
12 setup_all do
13 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
14 :ok
15 end
16
17 describe "User.search" do
18 setup do: clear_config([:instance, :limit_to_local_content])
19
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"})
24
25 [first_user, _second_user] = User.search("https://lain.com/users/lain", resolve: true)
26
27 assert first_user.id == user.id
28 end
29
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"})
33
34 [first_user, _second_user] = User.search("https://lain.com/users/lain")
35
36 assert first_user.id == user.id
37 end
38
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")
43 end
44
45 test "returns a user with matching uri as the first result" do
46 user =
47 insert(:user, %{
48 nickname: "no_relation",
49 ap_id: "https://lain.com/users/lain",
50 uri: "https://lain.com/@lain"
51 })
52
53 _user = insert(:user, %{nickname: "com_user"})
54
55 [first_user, _second_user] = User.search("https://lain.com/@lain")
56
57 assert first_user.id == user.id
58 end
59
60 test "excludes invisible users from results" do
61 user = insert(:user, %{nickname: "john t1000"})
62 insert(:user, %{invisible: true, nickname: "john t800"})
63
64 [found_user] = User.search("john")
65 assert found_user.id == user.id
66 end
67
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"})
72
73 users = User.search("john")
74 assert Enum.count(users) == 2
75 end
76
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")
81
82 assert [found_user1, found_user2] = User.search("user")
83 assert [found_user1.id, found_user2.id] -- [service.id, person.id] == []
84 end
85
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
90 end
91
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
96 end
97
98 defp clear_virtual_fields(user) do
99 Map.merge(user, %{search_rank: nil, search_type: nil})
100 end
101
102 test "finds a user by full nickname or its leading fragment" do
103 user = insert(:user, %{nickname: "john"})
104
105 Enum.each(["john", "jo", "j"], fn query ->
106 assert user ==
107 User.search(query)
108 |> List.first()
109 |> clear_virtual_fields()
110 end)
111 end
112
113 test "finds a user by full name or leading fragment(s) of its words" do
114 user = insert(:user, %{name: "John Doe"})
115
116 Enum.each(["John Doe", "JOHN", "doe", "j d", "j", "d"], fn query ->
117 assert user ==
118 User.search(query)
119 |> List.first()
120 |> clear_virtual_fields()
121 end)
122 end
123
124 test "matches by leading fragment of user domain" do
125 user = insert(:user, %{nickname: "arandom@dude.com"})
126 insert(:user, %{nickname: "iamthedude"})
127
128 assert [user.id] == User.search("dud") |> Enum.map(& &1.id)
129 end
130
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"})
134
135 results = User.search("hj")
136
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
139 end
140
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"})
144
145 assert [u2.id, u1.id] == Enum.map(User.search("bar word"), & &1.id)
146 end
147
148 test "finds users, boosting ranks of friends and followers" do
149 u1 = insert(:user)
150 u2 = insert(:user, %{name: "Doe"})
151 follower = insert(:user, %{name: "Doe"})
152 friend = insert(:user, %{name: "Doe"})
153
154 {:ok, follower, u1} = User.follow(follower, u1)
155 {:ok, u1, friend} = User.follow(u1, friend)
156
157 assert [friend.id, follower.id, u2.id] --
158 Enum.map(User.search("doe", resolve: false, for_user: u1), & &1.id) == []
159 end
160
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"})
167
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)
171
172 assert Enum.map(User.search("jimi", following: true, for_user: lizz), & &1.id) == [
173 following_lizz.id
174 ]
175
176 assert User.search("lizz", following: true, for_user: lizz) == []
177 end
178
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})
183
184 results =
185 "lain"
186 |> User.search(for_user: u1)
187 |> Enum.map(& &1.id)
188 |> Enum.sort()
189
190 assert [u1.id, u2.id, u3.id] == results
191 end
192
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})
197
198 assert [%{id: ^id}] = User.search("lain")
199 end
200
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)
203
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})
207
208 assert [%{id: ^id}] = User.search("lain")
209 end
210
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)
213
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})
217
218 results =
219 "lain"
220 |> User.search()
221 |> Enum.map(& &1.id)
222 |> Enum.sort()
223
224 assert [u1.id, u2.id, u3.id] == results
225 end
226
227 test "does not yield false-positive matches" do
228 insert(:user, %{name: "John Doe"})
229
230 Enum.each(["mary", "a", ""], fn query ->
231 assert [] == User.search(query)
232 end)
233 end
234
235 test "works with URIs" do
236 user = insert(:user)
237
238 results =
239 User.search("http://mastodon.example.org/users/admin", resolve: true, for_user: user)
240
241 result = results |> List.first()
242
243 user = User.get_cached_by_ap_id("http://mastodon.example.org/users/admin")
244
245 assert length(results) == 1
246
247 expected =
248 result
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)
254
255 assert user == expected
256 end
257
258 test "excludes a blocked users from search result" do
259 user = insert(:user, %{nickname: "Bill"})
260
261 [blocked_user | users] = Enum.map(0..3, &insert(:user, %{nickname: "john#{&1}"}))
262
263 blocked_user2 =
264 insert(
265 :user,
266 %{nickname: "john awful", ap_id: "https://awful-and-rude-instance.com/user/bully"}
267 )
268
269 User.block_domain(user, "awful-and-rude-instance.com")
270 User.block(user, blocked_user)
271
272 account_ids = User.search("john", for_user: refresh_record(user)) |> collect_ids
273
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
278 end
279
280 test "local user has the same search_rank as for users with the same nickname, but another domain" do
281 user = insert(:user)
282 insert(:user, nickname: "lain@mastodon.social")
283 insert(:user, nickname: "lain")
284 insert(:user, nickname: "lain@pleroma.social")
285
286 assert User.search("lain@localhost", resolve: true, for_user: user)
287 |> Enum.each(fn u -> u.search_rank == 0.5 end)
288 end
289
290 test "localhost is the part of the domain" do
291 user = insert(:user)
292 insert(:user, nickname: "another@somedomain")
293 insert(:user, nickname: "lain")
294 insert(:user, nickname: "lain@examplelocalhost")
295
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
299 end
300
301 test "local user search with users" do
302 user = insert(:user)
303 local_user = insert(:user, nickname: "lain")
304 insert(:user, nickname: "another@localhost.com")
305 insert(:user, nickname: "localhost@localhost.com")
306
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
309 end
310
311 test "works with idna domains" do
312 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
313
314 results = User.search("lain@zetsubou.みんな", resolve: false, for_user: user)
315
316 result = List.first(results)
317
318 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
319 end
320
321 test "works with idna domains converted input" do
322 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
323
324 results =
325 User.search("lain@zetsubou." <> to_string(:idna.encode("zetsubou.みんな")),
326 resolve: false,
327 for_user: user
328 )
329
330 result = List.first(results)
331
332 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
333 end
334
335 test "works with idna domains and bad chars in domain" do
336 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
337
338 results =
339 User.search("lain@zetsubou!@#$%^&*()+,-/:;<=>?[]'_{}|~`.みんな",
340 resolve: false,
341 for_user: user
342 )
343
344 result = List.first(results)
345
346 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
347 end
348
349 test "works with idna domains and query as link" do
350 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
351
352 results =
353 User.search("https://zetsubou.みんな/users/lain",
354 resolve: false,
355 for_user: user
356 )
357
358 result = List.first(results)
359
360 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
361 end
362 end
363 end