User search: Remove trigram and refactor the module
[akkoma] / test / user_search_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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, boosting ranks of friends and followers" do
78 u1 = insert(:user)
79 u2 = insert(:user, %{name: "Doe"})
80 follower = insert(:user, %{name: "Doe"})
81 friend = insert(:user, %{name: "Doe"})
82
83 {:ok, follower} = User.follow(follower, u1)
84 {:ok, u1} = User.follow(u1, friend)
85
86 assert [friend.id, follower.id, u2.id] --
87 Enum.map(User.search("doe", resolve: false, for_user: u1), & &1.id) == []
88 end
89
90 test "finds followers of user by partial name" do
91 u1 = insert(:user)
92 u2 = insert(:user, %{name: "Jimi"})
93 follower_jimi = insert(:user, %{name: "Jimi Hendrix"})
94 follower_lizz = insert(:user, %{name: "Lizz Wright"})
95 friend = insert(:user, %{name: "Jimi"})
96
97 {:ok, follower_jimi} = User.follow(follower_jimi, u1)
98 {:ok, _follower_lizz} = User.follow(follower_lizz, u2)
99 {:ok, u1} = User.follow(u1, friend)
100
101 assert Enum.map(User.search("jimi", following: true, for_user: u1), & &1.id) == [
102 follower_jimi.id
103 ]
104
105 assert User.search("lizz", following: true, for_user: u1) == []
106 end
107
108 test "find local and remote users for authenticated users" do
109 u1 = insert(:user, %{name: "lain"})
110 u2 = insert(:user, %{name: "ebn", nickname: "lain@mastodon.social", local: false})
111 u3 = insert(:user, %{nickname: "lain@pleroma.soykaf.com", local: false})
112
113 results =
114 "lain"
115 |> User.search(for_user: u1)
116 |> Enum.map(& &1.id)
117 |> Enum.sort()
118
119 assert [u1.id, u2.id, u3.id] == results
120 end
121
122 test "find only local users for unauthenticated users" do
123 %{id: id} = insert(:user, %{name: "lain"})
124 insert(:user, %{name: "ebn", nickname: "lain@mastodon.social", local: false})
125 insert(:user, %{nickname: "lain@pleroma.soykaf.com", local: false})
126
127 assert [%{id: ^id}] = User.search("lain")
128 end
129
130 test "find only local users for authenticated users when `limit_to_local_content` is `:all`" do
131 Pleroma.Config.put([:instance, :limit_to_local_content], :all)
132
133 %{id: id} = insert(:user, %{name: "lain"})
134 insert(:user, %{name: "ebn", nickname: "lain@mastodon.social", local: false})
135 insert(:user, %{nickname: "lain@pleroma.soykaf.com", local: false})
136
137 assert [%{id: ^id}] = User.search("lain")
138
139 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
140 end
141
142 test "find all users for unauthenticated users when `limit_to_local_content` is `false`" do
143 Pleroma.Config.put([:instance, :limit_to_local_content], false)
144
145 u1 = insert(:user, %{name: "lain"})
146 u2 = insert(:user, %{name: "ebn", nickname: "lain@mastodon.social", local: false})
147 u3 = insert(:user, %{nickname: "lain@pleroma.soykaf.com", local: false})
148
149 results =
150 "lain"
151 |> User.search()
152 |> Enum.map(& &1.id)
153 |> Enum.sort()
154
155 assert [u1.id, u2.id, u3.id] == results
156
157 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
158 end
159
160 test "does not yield false-positive matches" do
161 insert(:user, %{name: "John Doe"})
162
163 Enum.each(["mary", "a", ""], fn query ->
164 assert [] == User.search(query)
165 end)
166 end
167
168 test "works with URIs" do
169 user = insert(:user)
170
171 results =
172 User.search("http://mastodon.example.org/users/admin", resolve: true, for_user: user)
173
174 result = results |> List.first()
175
176 user = User.get_cached_by_ap_id("http://mastodon.example.org/users/admin")
177
178 assert length(results) == 1
179
180 expected =
181 result
182 |> Map.put(:search_rank, nil)
183 |> Map.put(:search_type, nil)
184 |> Map.put(:last_digest_emailed_at, nil)
185
186 assert user == expected
187 end
188
189 test "excludes a blocked users from search result" do
190 user = insert(:user, %{nickname: "Bill"})
191
192 [blocked_user | users] = Enum.map(0..3, &insert(:user, %{nickname: "john#{&1}"}))
193
194 blocked_user2 =
195 insert(
196 :user,
197 %{nickname: "john awful", ap_id: "https://awful-and-rude-instance.com/user/bully"}
198 )
199
200 User.block_domain(user, "awful-and-rude-instance.com")
201 User.block(user, blocked_user)
202
203 account_ids = User.search("john", for_user: refresh_record(user)) |> collect_ids
204
205 assert account_ids == collect_ids(users)
206 refute Enum.member?(account_ids, blocked_user.id)
207 refute Enum.member?(account_ids, blocked_user2.id)
208 assert length(account_ids) == 3
209 end
210
211 test "local user has the same search_rank as for users with the same nickname, but another domain" do
212 user = insert(:user)
213 insert(:user, nickname: "lain@mastodon.social")
214 insert(:user, nickname: "lain")
215 insert(:user, nickname: "lain@pleroma.social")
216
217 assert User.search("lain@localhost", resolve: true, for_user: user)
218 |> Enum.each(fn u -> u.search_rank == 0.5 end)
219 end
220
221 test "localhost is the part of the domain" do
222 user = insert(:user)
223 insert(:user, nickname: "another@somedomain")
224 insert(:user, nickname: "lain")
225 insert(:user, nickname: "lain@examplelocalhost")
226
227 result = User.search("lain@examplelocalhost", resolve: true, for_user: user)
228 assert Enum.each(result, fn u -> u.search_rank == 0.5 end)
229 assert length(result) == 2
230 end
231
232 test "local user search with users" do
233 user = insert(:user)
234 local_user = insert(:user, nickname: "lain")
235 insert(:user, nickname: "another@localhost.com")
236 insert(:user, nickname: "localhost@localhost.com")
237
238 [result] = User.search("lain@localhost", resolve: true, for_user: user)
239 assert Map.put(result, :search_rank, nil) |> Map.put(:search_type, nil) == local_user
240 end
241
242 test "works with idna domains" do
243 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
244
245 results = User.search("lain@zetsubou.みんな", resolve: false, for_user: user)
246
247 result = List.first(results)
248
249 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
250 end
251
252 test "works with idna domains converted input" do
253 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
254
255 results =
256 User.search("lain@zetsubou." <> to_string(:idna.encode("zetsubou.みんな")),
257 resolve: false,
258 for_user: user
259 )
260
261 result = List.first(results)
262
263 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
264 end
265
266 test "works with idna domains and bad chars in domain" do
267 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
268
269 results =
270 User.search("lain@zetsubou!@#$%^&*()+,-/:;<=>?[]'_{}|~`.みんな",
271 resolve: false,
272 for_user: user
273 )
274
275 result = List.first(results)
276
277 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
278 end
279
280 test "works with idna domains and query as link" do
281 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
282
283 results =
284 User.search("https://zetsubou.みんな/users/lain",
285 resolve: false,
286 for_user: user
287 )
288
289 result = List.first(results)
290
291 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
292 end
293 end
294 end