1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.UserSearchTest do
10 import Pleroma.Factory
13 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
17 describe "User.search" do
18 setup do: clear_config([:instance, :limit_to_local_content])
20 test "excluded invisible users from results" do
21 user = insert(:user, %{nickname: "john t1000"})
22 insert(:user, %{invisible: true, nickname: "john t800"})
24 [found_user] = User.search("john")
25 assert found_user.id == user.id
28 test "accepts limit parameter" do
29 Enum.each(0..4, &insert(:user, %{nickname: "john#{&1}"}))
30 assert length(User.search("john", limit: 3)) == 3
31 assert length(User.search("john")) == 5
34 test "accepts offset parameter" do
35 Enum.each(0..4, &insert(:user, %{nickname: "john#{&1}"}))
36 assert length(User.search("john", limit: 3)) == 3
37 assert length(User.search("john", limit: 3, offset: 3)) == 2
40 test "finds a user by full or partial nickname" do
41 user = insert(:user, %{nickname: "john"})
43 Enum.each(["john", "jo", "j"], fn query ->
47 |> Map.put(:search_rank, nil)
48 |> Map.put(:search_type, nil)
52 test "finds a user by full or partial name" do
53 user = insert(:user, %{name: "John Doe"})
55 Enum.each(["John Doe", "JOHN", "doe", "j d", "j", "d"], fn query ->
59 |> Map.put(:search_rank, nil)
60 |> Map.put(:search_type, nil)
64 test "finds users, considering density of matched tokens" do
65 u1 = insert(:user, %{name: "Bar Bar plus Word Word"})
66 u2 = insert(:user, %{name: "Word Word Bar Bar Bar"})
68 assert [u2.id, u1.id] == Enum.map(User.search("bar word"), & &1.id)
71 test "finds users, boosting ranks of friends and followers" do
73 u2 = insert(:user, %{name: "Doe"})
74 follower = insert(:user, %{name: "Doe"})
75 friend = insert(:user, %{name: "Doe"})
77 {:ok, follower} = User.follow(follower, u1)
78 {:ok, u1} = User.follow(u1, friend)
80 assert [friend.id, follower.id, u2.id] --
81 Enum.map(User.search("doe", resolve: false, for_user: u1), & &1.id) == []
84 test "finds followers of user by partial name" do
86 u2 = insert(:user, %{name: "Jimi"})
87 follower_jimi = insert(:user, %{name: "Jimi Hendrix"})
88 follower_lizz = insert(:user, %{name: "Lizz Wright"})
89 friend = insert(:user, %{name: "Jimi"})
91 {:ok, follower_jimi} = User.follow(follower_jimi, u1)
92 {:ok, _follower_lizz} = User.follow(follower_lizz, u2)
93 {:ok, u1} = User.follow(u1, friend)
95 assert Enum.map(User.search("jimi", following: true, for_user: u1), & &1.id) == [
99 assert User.search("lizz", following: true, for_user: u1) == []
102 test "find local and remote users for authenticated users" do
103 u1 = insert(:user, %{name: "lain"})
104 u2 = insert(:user, %{name: "ebn", nickname: "lain@mastodon.social", local: false})
105 u3 = insert(:user, %{nickname: "lain@pleroma.soykaf.com", local: false})
109 |> User.search(for_user: u1)
113 assert [u1.id, u2.id, u3.id] == results
116 test "find only local users for unauthenticated users" do
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})
121 assert [%{id: ^id}] = User.search("lain")
124 test "find only local users for authenticated users when `limit_to_local_content` is `:all`" do
125 Pleroma.Config.put([:instance, :limit_to_local_content], :all)
127 %{id: id} = insert(:user, %{name: "lain"})
128 insert(:user, %{name: "ebn", nickname: "lain@mastodon.social", local: false})
129 insert(:user, %{nickname: "lain@pleroma.soykaf.com", local: false})
131 assert [%{id: ^id}] = User.search("lain")
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)
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})
147 assert [u1.id, u2.id, u3.id] == results
150 test "does not yield false-positive matches" do
151 insert(:user, %{name: "John Doe"})
153 Enum.each(["mary", "a", ""], fn query ->
154 assert [] == User.search(query)
158 test "works with URIs" do
162 User.search("http://mastodon.example.org/users/admin", resolve: true, for_user: user)
164 result = results |> List.first()
166 user = User.get_cached_by_ap_id("http://mastodon.example.org/users/admin")
168 assert length(results) == 1
172 |> Map.put(:search_rank, nil)
173 |> Map.put(:search_type, nil)
174 |> Map.put(:last_digest_emailed_at, nil)
175 |> Map.put(:multi_factor_authentication_settings, nil)
176 |> Map.put(:notification_settings, nil)
178 assert user == expected
181 test "excludes a blocked users from search result" do
182 user = insert(:user, %{nickname: "Bill"})
184 [blocked_user | users] = Enum.map(0..3, &insert(:user, %{nickname: "john#{&1}"}))
189 %{nickname: "john awful", ap_id: "https://awful-and-rude-instance.com/user/bully"}
192 User.block_domain(user, "awful-and-rude-instance.com")
193 User.block(user, blocked_user)
195 account_ids = User.search("john", for_user: refresh_record(user)) |> collect_ids
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
203 test "local user has the same search_rank as for users with the same nickname, but another domain" do
205 insert(:user, nickname: "lain@mastodon.social")
206 insert(:user, nickname: "lain")
207 insert(:user, nickname: "lain@pleroma.social")
209 assert User.search("lain@localhost", resolve: true, for_user: user)
210 |> Enum.each(fn u -> u.search_rank == 0.5 end)
213 test "localhost is the part of the domain" do
215 insert(:user, nickname: "another@somedomain")
216 insert(:user, nickname: "lain")
217 insert(:user, nickname: "lain@examplelocalhost")
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
224 test "local user search with users" do
226 local_user = insert(:user, nickname: "lain")
227 insert(:user, nickname: "another@localhost.com")
228 insert(:user, nickname: "localhost@localhost.com")
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
234 test "works with idna domains" do
235 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
237 results = User.search("lain@zetsubou.みんな", resolve: false, for_user: user)
239 result = List.first(results)
241 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
244 test "works with idna domains converted input" do
245 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
248 User.search("lain@zetsubou." <> to_string(:idna.encode("zetsubou.みんな")),
253 result = List.first(results)
255 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
258 test "works with idna domains and bad chars in domain" do
259 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
262 User.search("lain@zetsubou!@#$%^&*()+,-/:;<=>?[]'_{}|~`.みんな",
267 result = List.first(results)
269 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
272 test "works with idna domains and query as link" do
273 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
276 User.search("https://zetsubou.みんな/users/lain",
281 result = List.first(results)
283 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)