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