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