1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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 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
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
30 test "finds a user by full or partial nickname" do
31 user = insert(:user, %{nickname: "john"})
33 Enum.each(["john", "jo", "j"], fn query ->
37 |> Map.put(:search_rank, nil)
38 |> Map.put(:search_type, nil)
42 test "finds a user by full or partial name" do
43 user = insert(:user, %{name: "John Doe"})
45 Enum.each(["John Doe", "JOHN", "doe", "j d", "j", "d"], fn query ->
49 |> Map.put(:search_rank, nil)
50 |> Map.put(:search_type, nil)
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"})
58 assert [u2.id, u1.id] == Enum.map(User.search("lain"), & &1.id)
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"})
65 assert [u2.id, u1.id] == Enum.map(User.search("bar word"), & &1.id)
68 test "finds users, boosting ranks of friends and followers" do
70 u2 = insert(:user, %{name: "Doe"})
71 follower = insert(:user, %{name: "Doe"})
72 friend = insert(:user, %{name: "Doe"})
74 {:ok, follower} = User.follow(follower, u1)
75 {:ok, u1} = User.follow(u1, friend)
77 assert [friend.id, follower.id, u2.id] --
78 Enum.map(User.search("doe", resolve: false, for_user: u1), & &1.id) == []
81 test "finds followers of user by partial name" do
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"})
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)
92 assert Enum.map(User.search("jimi", following: true, for_user: u1), & &1.id) == [
96 assert User.search("lizz", following: true, for_user: u1) == []
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})
106 |> User.search(for_user: u1)
110 assert [u1.id, u2.id, u3.id] == results
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})
118 assert [%{id: ^id}] = User.search("lain")
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)
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})
128 assert [%{id: ^id}] = User.search("lain")
130 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
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)
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})
146 assert [u1.id, u2.id, u3.id] == results
148 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
151 test "does not yield false-positive matches" do
152 insert(:user, %{name: "John Doe"})
154 Enum.each(["mary", "a", ""], fn query ->
155 assert [] == User.search(query)
159 test "works with URIs" do
163 User.search("http://mastodon.example.org/users/admin", resolve: true, for_user: user)
165 result = results |> List.first()
167 user = User.get_cached_by_ap_id("http://mastodon.example.org/users/admin")
169 assert length(results) == 1
173 |> Map.put(:search_rank, nil)
174 |> Map.put(:search_type, nil)
175 |> Map.put(:last_digest_emailed_at, nil)
177 assert user == expected
180 test "excludes a blocked users from search result" do
181 user = insert(:user, %{nickname: "Bill"})
183 [blocked_user | users] = Enum.map(0..3, &insert(:user, %{nickname: "john#{&1}"}))
188 %{nickname: "john awful", ap_id: "https://awful-and-rude-instance.com/user/bully"}
191 User.block_domain(user, "awful-and-rude-instance.com")
192 User.block(user, blocked_user)
194 account_ids = User.search("john", for_user: refresh_record(user)) |> collect_ids
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
202 test "local user has the same search_rank as for users with the same nickname, but another domain" do
204 insert(:user, nickname: "lain@mastodon.social")
205 insert(:user, nickname: "lain")
206 insert(:user, nickname: "lain@pleroma.social")
208 assert User.search("lain@localhost", resolve: true, for_user: user)
209 |> Enum.each(fn u -> u.search_rank == 0.5 end)
212 test "localhost is the part of the domain" do
214 insert(:user, nickname: "another@somedomain")
215 insert(:user, nickname: "lain")
216 insert(:user, nickname: "lain@examplelocalhost")
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
223 test "local user search with users" do
225 local_user = insert(:user, nickname: "lain")
226 insert(:user, nickname: "another@localhost.com")
227 insert(:user, nickname: "localhost@localhost.com")
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
233 test "works with idna domains" do
234 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
236 results = User.search("lain@zetsubou.みんな", resolve: false, for_user: user)
238 result = List.first(results)
240 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
243 test "works with idna domains converted input" do
244 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
247 User.search("lain@zetsubou." <> to_string(:idna.encode("zetsubou.みんな")),
252 result = List.first(results)
254 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
257 test "works with idna domains and bad chars in domain" do
258 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
261 User.search("lain@zetsubou!@#$%^&*()+,-/:;<=>?[]'_{}|~`.みんな",
266 result = List.first(results)
268 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)
271 test "works with idna domains and query as link" do
272 user = insert(:user, nickname: "lain@" <> to_string(:idna.encode("zetsubou.みんな")))
275 User.search("https://zetsubou.みんな/users/lain",
280 result = List.first(results)
282 assert user == result |> Map.put(:search_rank, nil) |> Map.put(:search_type, nil)