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