Add new Emoji Ecto.Type and fix emoji in Question
[akkoma] / test / user_search_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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 "excludes invisible users from results" do
21 user = insert(:user, %{nickname: "john t1000"})
22 insert(:user, %{invisible: true, nickname: "john t800"})
23
24 [found_user] = User.search("john")
25 assert found_user.id == user.id
26 end
27
28 test "excludes service actors from results" do
29 insert(:user, actor_type: "Application", nickname: "user1")
30 service = insert(:user, actor_type: "Service", nickname: "user2")
31 person = insert(:user, actor_type: "Person", nickname: "user3")
32
33 assert [found_user1, found_user2] = User.search("user")
34 assert [found_user1.id, found_user2.id] -- [service.id, person.id] == []
35 end
36
37 test "accepts limit parameter" do
38 Enum.each(0..4, &insert(:user, %{nickname: "john#{&1}"}))
39 assert length(User.search("john", limit: 3)) == 3
40 assert length(User.search("john")) == 5
41 end
42
43 test "accepts offset parameter" do
44 Enum.each(0..4, &insert(:user, %{nickname: "john#{&1}"}))
45 assert length(User.search("john", limit: 3)) == 3
46 assert length(User.search("john", limit: 3, offset: 3)) == 2
47 end
48
49 defp clear_virtual_fields(user) do
50 Map.merge(user, %{search_rank: nil, search_type: nil})
51 end
52
53 test "finds a user by full nickname or its leading fragment" do
54 user = insert(:user, %{nickname: "john"})
55
56 Enum.each(["john", "jo", "j"], fn query ->
57 assert user ==
58 User.search(query)
59 |> List.first()
60 |> clear_virtual_fields()
61 end)
62 end
63
64 test "finds a user by full name or leading fragment(s) of its words" do
65 user = insert(:user, %{name: "John Doe"})
66
67 Enum.each(["John Doe", "JOHN", "doe", "j d", "j", "d"], fn query ->
68 assert user ==
69 User.search(query)
70 |> List.first()
71 |> clear_virtual_fields()
72 end)
73 end
74
75 test "matches by leading fragment of user domain" do
76 user = insert(:user, %{nickname: "arandom@dude.com"})
77 insert(:user, %{nickname: "iamthedude"})
78
79 assert [user.id] == User.search("dud") |> Enum.map(& &1.id)
80 end
81
82 test "ranks full nickname match higher than full name match" do
83 nicknamed_user = insert(:user, %{nickname: "hj@shigusegubu.club"})
84 named_user = insert(:user, %{nickname: "xyz@sample.com", name: "HJ"})
85
86 results = User.search("hj")
87
88 assert [nicknamed_user.id, named_user.id] == Enum.map(results, & &1.id)
89 assert Enum.at(results, 0).search_rank > Enum.at(results, 1).search_rank
90 end
91
92 test "finds users, considering density of matched tokens" do
93 u1 = insert(:user, %{name: "Bar Bar plus Word Word"})
94 u2 = insert(:user, %{name: "Word Word Bar Bar Bar"})
95
96 assert [u2.id, u1.id] == Enum.map(User.search("bar word"), & &1.id)
97 end
98
99 test "finds users, boosting ranks of friends and followers" do
100 u1 = insert(:user)
101 u2 = insert(:user, %{name: "Doe"})
102 follower = insert(:user, %{name: "Doe"})
103 friend = insert(:user, %{name: "Doe"})
104
105 {:ok, follower} = User.follow(follower, u1)
106 {:ok, u1} = User.follow(u1, friend)
107
108 assert [friend.id, follower.id, u2.id] --
109 Enum.map(User.search("doe", resolve: false, for_user: u1), & &1.id) == []
110 end
111
112 test "finds followers of user by partial name" do
113 u1 = insert(:user)
114 u2 = insert(:user, %{name: "Jimi"})
115 follower_jimi = insert(:user, %{name: "Jimi Hendrix"})
116 follower_lizz = insert(:user, %{name: "Lizz Wright"})
117 friend = insert(:user, %{name: "Jimi"})
118
119 {:ok, follower_jimi} = User.follow(follower_jimi, u1)
120 {:ok, _follower_lizz} = User.follow(follower_lizz, u2)
121 {:ok, u1} = User.follow(u1, friend)
122
123 assert Enum.map(User.search("jimi", following: true, for_user: u1), & &1.id) == [
124 follower_jimi.id
125 ]
126
127 assert User.search("lizz", following: true, for_user: u1) == []
128 end
129
130 test "find local and remote users for authenticated users" do
131 u1 = insert(:user, %{name: "lain"})
132 u2 = insert(:user, %{name: "ebn", nickname: "lain@mastodon.social", local: false})
133 u3 = insert(:user, %{nickname: "lain@pleroma.soykaf.com", local: false})
134
135 results =
136 "lain"
137 |> User.search(for_user: u1)
138 |> Enum.map(& &1.id)
139 |> Enum.sort()
140
141 assert [u1.id, u2.id, u3.id] == results
142 end
143
144 test "find only local users for unauthenticated users" do
145 %{id: id} = insert(:user, %{name: "lain"})
146 insert(:user, %{name: "ebn", nickname: "lain@mastodon.social", local: false})
147 insert(:user, %{nickname: "lain@pleroma.soykaf.com", local: false})
148
149 assert [%{id: ^id}] = User.search("lain")
150 end
151
152 test "find only local users for authenticated users when `limit_to_local_content` is `:all`" do
153 Pleroma.Config.put([:instance, :limit_to_local_content], :all)
154
155 %{id: id} = insert(:user, %{name: "lain"})
156 insert(:user, %{name: "ebn", nickname: "lain@mastodon.social", local: false})
157 insert(:user, %{nickname: "lain@pleroma.soykaf.com", local: false})
158
159 assert [%{id: ^id}] = User.search("lain")
160 end
161
162 test "find all users for unauthenticated users when `limit_to_local_content` is `false`" do
163 Pleroma.Config.put([:instance, :limit_to_local_content], false)
164
165 u1 = insert(:user, %{name: "lain"})
166 u2 = insert(:user, %{name: "ebn", nickname: "lain@mastodon.social", local: false})
167 u3 = insert(:user, %{nickname: "lain@pleroma.soykaf.com", local: false})
168
169 results =
170 "lain"
171 |> User.search()
172 |> Enum.map(& &1.id)
173 |> Enum.sort()
174
175 assert [u1.id, u2.id, u3.id] == results
176 end
177
178 test "does not yield false-positive matches" do
179 insert(:user, %{name: "John Doe"})
180
181 Enum.each(["mary", "a", ""], fn query ->
182 assert [] == User.search(query)
183 end)
184 end
185
186 test "works with URIs" do
187 user = insert(:user)
188
189 results =
190 User.search("http://mastodon.example.org/users/admin", resolve: true, for_user: user)
191
192 result = results |> List.first()
193
194 user = User.get_cached_by_ap_id("http://mastodon.example.org/users/admin")
195
196 assert length(results) == 1
197
198 expected =
199 result
200 |> Map.put(:search_rank, nil)
201 |> Map.put(:search_type, nil)
202 |> Map.put(:last_digest_emailed_at, nil)
203 |> Map.put(:multi_factor_authentication_settings, nil)
204 |> Map.put(:notification_settings, nil)
205
206 assert user == expected
207 end
208
209 test "excludes a blocked users from search result" do
210 user = insert(:user, %{nickname: "Bill"})
211
212 [blocked_user | users] = Enum.map(0..3, &insert(:user, %{nickname: "john#{&1}"}))
213
214 blocked_user2 =
215 insert(
216 :user,
217 %{nickname: "john awful", ap_id: "https://awful-and-rude-instance.com/user/bully"}
218 )
219
220 User.block_domain(user, "awful-and-rude-instance.com")
221 User.block(user, blocked_user)
222
223 account_ids = User.search("john", for_user: refresh_record(user)) |> collect_ids
224
225 assert account_ids == collect_ids(users)
226 refute Enum.member?(account_ids, blocked_user.id)
227 refute Enum.member?(account_ids, blocked_user2.id)
228 assert length(account_ids) == 3
229 end
230
231 test "local user has the same search_rank as for users with the same nickname, but another domain" do
232 user = insert(:user)
233 insert(:user, nickname: "lain@mastodon.social")
234 insert(:user, nickname: "lain")
235 insert(:user, nickname: "lain@pleroma.social")
236
237 assert User.search("lain@localhost", resolve: true, for_user: user)
238 |> Enum.each(fn u -> u.search_rank == 0.5 end)
239 end
240
241 test "localhost is the part of the domain" do
242 user = insert(:user)
243 insert(:user, nickname: "another@somedomain")
244 insert(:user, nickname: "lain")
245 insert(:user, nickname: "lain@examplelocalhost")
246
247 result = User.search("lain@examplelocalhost", resolve: true, for_user: user)
248 assert Enum.each(result, fn u -> u.search_rank == 0.5 end)
249 assert length(result) == 2
250 end
251
252 test "local user search with users" do
253 user = insert(:user)
254 local_user = insert(:user, nickname: "lain")
255 insert(:user, nickname: "another@localhost.com")
256 insert(:user, nickname: "localhost@localhost.com")
257
258 [result] = User.search("lain@localhost", resolve: true, for_user: user)
259 assert Map.put(result, :search_rank, nil) |> Map.put(:search_type, nil) == local_user
260 end
261
262 test "works with idna domains" do
263 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
264
265 results = User.search("lain@zetsubou.みんな", resolve: false, for_user: user)
266
267 result = List.first(results)
268
269 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
270 end
271
272 test "works with idna domains converted input" do
273 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
274
275 results =
276 User.search("lain@zetsubou." <> to_string(:idna.encode("zetsubou.みんな")),
277 resolve: false,
278 for_user: user
279 )
280
281 result = List.first(results)
282
283 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
284 end
285
286 test "works with idna domains and bad chars in domain" do
287 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
288
289 results =
290 User.search("lain@zetsubou!@#$%^&*()+,-/:;<=>?[]'_{}|~`.みんな",
291 resolve: false,
292 for_user: user
293 )
294
295 result = List.first(results)
296
297 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
298 end
299
300 test "works with idna domains and query as link" do
301 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
302
303 results =
304 User.search("https://zetsubou.みんな/users/lain",
305 resolve: false,
306 for_user: user
307 )
308
309 result = List.first(results)
310
311 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
312 end
313 end
314 end