describe "User.search" do
setup do: clear_config([:instance, :limit_to_local_content])
+ test "returns a resolved user as the first result" do
+ Pleroma.Config.put([:instance, :limit_to_local_content], false)
+ user = insert(:user, %{nickname: "no_relation", ap_id: "https://lain.com/users/lain"})
+ _user = insert(:user, %{nickname: "com_user"})
+
+ [first_user, _second_user] = User.search("https://lain.com/users/lain", resolve: true)
+
+ assert first_user.id == user.id
+ end
+
+ test "returns a user with matching ap_id as the first result" do
+ user = insert(:user, %{nickname: "no_relation", ap_id: "https://lain.com/users/lain"})
+ _user = insert(:user, %{nickname: "com_user"})
+
+ [first_user, _second_user] = User.search("https://lain.com/users/lain")
+
+ assert first_user.id == user.id
+ end
+
+ test "doesn't die if two users have the same uri" do
+ insert(:user, %{uri: "https://gensokyo.2hu/@raymoo"})
+ insert(:user, %{uri: "https://gensokyo.2hu/@raymoo"})
+ assert [_first_user, _second_user] = User.search("https://gensokyo.2hu/@raymoo")
+ end
+
+ test "returns a user with matching uri as the first result" do
+ user =
+ insert(:user, %{
+ nickname: "no_relation",
+ ap_id: "https://lain.com/users/lain",
+ uri: "https://lain.com/@lain"
+ })
+
+ _user = insert(:user, %{nickname: "com_user"})
+
+ [first_user, _second_user] = User.search("https://lain.com/@lain")
+
+ assert first_user.id == user.id
+ end
+
test "excludes invisible users from results" do
user = insert(:user, %{nickname: "john t1000"})
insert(:user, %{invisible: true, nickname: "john t800"})
assert found_user.id == user.id
end
+ test "excludes users when discoverable is false" do
+ insert(:user, %{nickname: "john 3000", discoverable: false})
+ insert(:user, %{nickname: "john 3001"})
+
+ users = User.search("john")
+ assert Enum.count(users) == 1
+ end
+
test "excludes service actors from results" do
insert(:user, actor_type: "Application", nickname: "user1")
service = insert(:user, actor_type: "Service", nickname: "user2")
end)
end
- test "is not [yet] capable of matching by non-leading fragments (e.g. by domain)" do
- user1 = insert(:user, %{nickname: "iamthedude"})
- insert(:user, %{nickname: "arandom@dude.com"})
-
- assert [] == User.search("dude")
+ test "matches by leading fragment of user domain" do
+ user = insert(:user, %{nickname: "arandom@dude.com"})
+ insert(:user, %{nickname: "iamthedude"})
- # Matching by leading fragment works, though
- user1_id = user1.id
- assert ^user1_id = User.search("iam") |> List.first() |> Map.get(:id)
+ assert [user.id] == User.search("dud") |> Enum.map(& &1.id)
end
test "ranks full nickname match higher than full name match" do
Enum.map(User.search("doe", resolve: false, for_user: u1), & &1.id) == []
end
- test "finds followers of user by partial name" do
- u1 = insert(:user)
- u2 = insert(:user, %{name: "Jimi"})
- follower_jimi = insert(:user, %{name: "Jimi Hendrix"})
- follower_lizz = insert(:user, %{name: "Lizz Wright"})
- friend = insert(:user, %{name: "Jimi"})
+ test "finds followings of user by partial name" do
+ lizz = insert(:user, %{name: "Lizz"})
+ jimi = insert(:user, %{name: "Jimi"})
+ following_lizz = insert(:user, %{name: "Jimi Hendrix"})
+ following_jimi = insert(:user, %{name: "Lizz Wright"})
+ follower_lizz = insert(:user, %{name: "Jimi"})
- {:ok, follower_jimi} = User.follow(follower_jimi, u1)
- {:ok, _follower_lizz} = User.follow(follower_lizz, u2)
- {:ok, u1} = User.follow(u1, friend)
+ {:ok, lizz} = User.follow(lizz, following_lizz)
+ {:ok, _jimi} = User.follow(jimi, following_jimi)
+ {:ok, _follower_lizz} = User.follow(follower_lizz, lizz)
- assert Enum.map(User.search("jimi", following: true, for_user: u1), & &1.id) == [
- follower_jimi.id
+ assert Enum.map(User.search("jimi", following: true, for_user: lizz), & &1.id) == [
+ following_lizz.id
]
- assert User.search("lizz", following: true, for_user: u1) == []
+ assert User.search("lizz", following: true, for_user: lizz) == []
end
test "find local and remote users for authenticated users" do