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