Merge branch 'pleroma-password' into 'develop'
[akkoma] / test / pleroma / web / common_api_test.exs
index d19d484b78471f9cf94b940b23476f27c52cf7ee..2ece9280663e406fb6c7b29c8bccf09f1c4053b6 100644 (file)
@@ -1,10 +1,10 @@
 # Pleroma: A lightweight social networking server
 # Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
 # SPDX-License-Identifier: AGPL-3.0-only
 
 defmodule Pleroma.Web.CommonAPITest do
 # SPDX-License-Identifier: AGPL-3.0-only
 
 defmodule Pleroma.Web.CommonAPITest do
-  use Pleroma.DataCase
   use Oban.Testing, repo: Pleroma.Repo
   use Oban.Testing, repo: Pleroma.Repo
+  use Pleroma.DataCase
 
   alias Pleroma.Activity
   alias Pleroma.Chat
 
   alias Pleroma.Activity
   alias Pleroma.Chat
@@ -39,7 +39,7 @@ defmodule Pleroma.Web.CommonAPITest do
           poll: %{expires_in: 600, options: ["reimu", "marisa"]}
         })
 
           poll: %{expires_in: 600, options: ["reimu", "marisa"]}
         })
 
-      object = Object.normalize(activity)
+      object = Object.normalize(activity, fetch: false)
 
       assert object.data["type"] == "Question"
       assert object.data["oneOf"] |> length() == 2
 
       assert object.data["type"] == "Question"
       assert object.data["oneOf"] |> length() == 2
@@ -95,6 +95,20 @@ defmodule Pleroma.Web.CommonAPITest do
   describe "posting chat messages" do
     setup do: clear_config([:instance, :chat_limit])
 
   describe "posting chat messages" do
     setup do: clear_config([:instance, :chat_limit])
 
+    test "it posts a self-chat" do
+      author = insert(:user)
+      recipient = author
+
+      {:ok, activity} =
+        CommonAPI.post_chat_message(
+          author,
+          recipient,
+          "remember to buy milk when milk truk arive"
+        )
+
+      assert activity.data["type"] == "Create"
+    end
+
     test "it posts a chat message without content but with an attachment" do
       author = insert(:user)
       recipient = insert(:user)
     test "it posts a chat message without content but with an attachment" do
       author = insert(:user)
       recipient = insert(:user)
@@ -160,7 +174,7 @@ defmodule Pleroma.Web.CommonAPITest do
 
       assert other_user.ap_id not in activity.recipients
 
 
       assert other_user.ap_id not in activity.recipients
 
-      object = Object.normalize(activity, false)
+      object = Object.normalize(activity, fetch: false)
 
       assert object.data["content"] == "uguu<br/>uguuu"
     end
 
       assert object.data["content"] == "uguu<br/>uguuu"
     end
@@ -180,7 +194,7 @@ defmodule Pleroma.Web.CommonAPITest do
 
       assert other_user.ap_id not in activity.recipients
 
 
       assert other_user.ap_id not in activity.recipients
 
-      object = Object.normalize(activity, false)
+      object = Object.normalize(activity, fetch: false)
 
       assert object.data["content"] ==
                "<a href=\"https://example.org\" rel=\"ugc\">https://example.org</a> is the site of <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{
 
       assert object.data["content"] ==
                "<a href=\"https://example.org\" rel=\"ugc\">https://example.org</a> is the site of <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{
@@ -201,7 +215,7 @@ defmodule Pleroma.Web.CommonAPITest do
 
       assert activity.data["type"] == "Create"
       assert activity.local
 
       assert activity.data["type"] == "Create"
       assert activity.local
-      object = Object.normalize(activity)
+      object = Object.normalize(activity, fetch: false)
 
       assert object.data["type"] == "ChatMessage"
       assert object.data["to"] == [recipient.ap_id]
 
       assert object.data["type"] == "ChatMessage"
       assert object.data["to"] == [recipient.ap_id]
@@ -267,7 +281,7 @@ defmodule Pleroma.Web.CommonAPITest do
 
       clear_config([:instance, :federating], true)
 
 
       clear_config([:instance, :federating], true)
 
-      Object.normalize(post, false)
+      Object.normalize(post, fetch: false)
       |> Object.prune()
 
       with_mock Pleroma.Web.Federator,
       |> Object.prune()
 
       with_mock Pleroma.Web.Federator,
@@ -477,7 +491,7 @@ defmodule Pleroma.Web.CommonAPITest do
     user = insert(:user)
     {:ok, activity} = CommonAPI.post(user, %{status: "#2hu #2HU"})
 
     user = insert(:user)
     {:ok, activity} = CommonAPI.post(user, %{status: "#2hu #2HU"})
 
-    object = Object.normalize(activity)
+    object = Object.normalize(activity, fetch: false)
 
     assert object.data["tag"] == ["2hu"]
   end
 
     assert object.data["tag"] == ["2hu"]
   end
@@ -486,10 +500,23 @@ defmodule Pleroma.Web.CommonAPITest do
     user = insert(:user)
     {:ok, activity} = CommonAPI.post(user, %{status: ":firefox:"})
 
     user = insert(:user)
     {:ok, activity} = CommonAPI.post(user, %{status: ":firefox:"})
 
-    assert Object.normalize(activity).data["emoji"]["firefox"]
+    assert Object.normalize(activity, fetch: false).data["emoji"]["firefox"]
   end
 
   describe "posting" do
   end
 
   describe "posting" do
+    test "it adds an emoji on an external site" do
+      user = insert(:user)
+      {:ok, activity} = CommonAPI.post(user, %{status: "hey :external_emoji:"})
+
+      assert %{"external_emoji" => url} = Object.normalize(activity).data["emoji"]
+      assert url == "https://example.com/emoji.png"
+
+      {:ok, activity} = CommonAPI.post(user, %{status: "hey :blank:"})
+
+      assert %{"blank" => url} = Object.normalize(activity).data["emoji"]
+      assert url == "#{Pleroma.Web.base_url()}/emoji/blank.png"
+    end
+
     test "deactivated users can't post" do
       user = insert(:user, deactivated: true)
       assert {:error, _} = CommonAPI.post(user, %{status: "ye"})
     test "deactivated users can't post" do
       user = insert(:user, deactivated: true)
       assert {:error, _} = CommonAPI.post(user, %{status: "ye"})
@@ -525,7 +552,7 @@ defmodule Pleroma.Web.CommonAPITest do
           content_type: "text/html"
         })
 
           content_type: "text/html"
         })
 
-      object = Object.normalize(activity)
+      object = Object.normalize(activity, fetch: false)
 
       assert object.data["content"] == "<p><b>2hu</b></p>alert(&#39;xss&#39;)"
       assert object.data["source"] == post
 
       assert object.data["content"] == "<p><b>2hu</b></p>alert(&#39;xss&#39;)"
       assert object.data["source"] == post
@@ -542,7 +569,7 @@ defmodule Pleroma.Web.CommonAPITest do
           content_type: "text/markdown"
         })
 
           content_type: "text/markdown"
         })
 
-      object = Object.normalize(activity)
+      object = Object.normalize(activity, fetch: false)
 
       assert object.data["content"] == "<p><b>2hu</b></p>alert(&#39;xss&#39;)"
       assert object.data["source"] == post
 
       assert object.data["content"] == "<p><b>2hu</b></p>alert(&#39;xss&#39;)"
       assert object.data["source"] == post
@@ -622,7 +649,7 @@ defmodule Pleroma.Web.CommonAPITest do
       assert {:error, "The status is over the character limit"} =
                CommonAPI.post(user, %{status: "foobar"})
 
       assert {:error, "The status is over the character limit"} =
                CommonAPI.post(user, %{status: "foobar"})
 
-      assert {:ok, activity} = CommonAPI.post(user, %{status: "12345"})
+      assert {:ok, _activity} = CommonAPI.post(user, %{status: "12345"})
     end
 
     test "it can handle activities that expire" do
     end
 
     test "it can handle activities that expire" do
@@ -908,12 +935,34 @@ defmodule Pleroma.Web.CommonAPITest do
       assert CommonAPI.thread_muted?(user, activity)
     end
 
       assert CommonAPI.thread_muted?(user, activity)
     end
 
+    test "add expiring mute", %{user: user, activity: activity} do
+      {:ok, _} = CommonAPI.add_mute(user, activity, %{expires_in: 60})
+      assert CommonAPI.thread_muted?(user, activity)
+
+      worker = Pleroma.Workers.MuteExpireWorker
+      args = %{"op" => "unmute_conversation", "user_id" => user.id, "activity_id" => activity.id}
+
+      assert_enqueued(
+        worker: worker,
+        args: args
+      )
+
+      assert :ok = perform_job(worker, args)
+      refute CommonAPI.thread_muted?(user, activity)
+    end
+
     test "remove mute", %{user: user, activity: activity} do
       CommonAPI.add_mute(user, activity)
       {:ok, _} = CommonAPI.remove_mute(user, activity)
       refute CommonAPI.thread_muted?(user, activity)
     end
 
     test "remove mute", %{user: user, activity: activity} do
       CommonAPI.add_mute(user, activity)
       {:ok, _} = CommonAPI.remove_mute(user, activity)
       refute CommonAPI.thread_muted?(user, activity)
     end
 
+    test "remove mute by ids", %{user: user, activity: activity} do
+      CommonAPI.add_mute(user, activity)
+      {:ok, _} = CommonAPI.remove_mute(user.id, activity.id)
+      refute CommonAPI.thread_muted?(user, activity)
+    end
+
     test "check that mutes can't be duplicate", %{user: user, activity: activity} do
       CommonAPI.add_mute(user, activity)
       {:error, _} = CommonAPI.add_mute(user, activity)
     test "check that mutes can't be duplicate", %{user: user, activity: activity} do
       CommonAPI.add_mute(user, activity)
       {:error, _} = CommonAPI.add_mute(user, activity)
@@ -1071,7 +1120,7 @@ defmodule Pleroma.Web.CommonAPITest do
 
     test "cancels a pending follow for a local user" do
       follower = insert(:user)
 
     test "cancels a pending follow for a local user" do
       follower = insert(:user)
-      followed = insert(:user, locked: true)
+      followed = insert(:user, is_locked: true)
 
       assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
                CommonAPI.follow(follower, followed)
 
       assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
                CommonAPI.follow(follower, followed)
@@ -1093,7 +1142,7 @@ defmodule Pleroma.Web.CommonAPITest do
 
     test "cancels a pending follow for a remote user" do
       follower = insert(:user)
 
     test "cancels a pending follow for a remote user" do
       follower = insert(:user)
-      followed = insert(:user, locked: true, local: false, ap_enabled: true)
+      followed = insert(:user, is_locked: true, local: false, ap_enabled: true)
 
       assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
                CommonAPI.follow(follower, followed)
 
       assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
                CommonAPI.follow(follower, followed)
@@ -1116,7 +1165,7 @@ defmodule Pleroma.Web.CommonAPITest do
 
   describe "accept_follow_request/2" do
     test "after acceptance, it sets all existing pending follow request states to 'accept'" do
 
   describe "accept_follow_request/2" do
     test "after acceptance, it sets all existing pending follow request states to 'accept'" do
-      user = insert(:user, locked: true)
+      user = insert(:user, is_locked: true)
       follower = insert(:user)
       follower_two = insert(:user)
 
       follower = insert(:user)
       follower_two = insert(:user)
 
@@ -1136,7 +1185,7 @@ defmodule Pleroma.Web.CommonAPITest do
     end
 
     test "after rejection, it sets all existing pending follow request states to 'reject'" do
     end
 
     test "after rejection, it sets all existing pending follow request states to 'reject'" do
-      user = insert(:user, locked: true)
+      user = insert(:user, is_locked: true)
       follower = insert(:user)
       follower_two = insert(:user)
 
       follower = insert(:user)
       follower_two = insert(:user)
 
@@ -1156,7 +1205,7 @@ defmodule Pleroma.Web.CommonAPITest do
     end
 
     test "doesn't create a following relationship if the corresponding follow request doesn't exist" do
     end
 
     test "doesn't create a following relationship if the corresponding follow request doesn't exist" do
-      user = insert(:user, locked: true)
+      user = insert(:user, is_locked: true)
       not_follower = insert(:user)
       CommonAPI.accept_follow_request(not_follower, user)
 
       not_follower = insert(:user)
       CommonAPI.accept_follow_request(not_follower, user)
 
@@ -1175,7 +1224,7 @@ defmodule Pleroma.Web.CommonAPITest do
           poll: %{options: ["Yes", "No"], expires_in: 20}
         })
 
           poll: %{options: ["Yes", "No"], expires_in: 20}
         })
 
-      object = Object.normalize(activity)
+      object = Object.normalize(activity, fetch: false)
 
       {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
 
 
       {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
 
@@ -1195,7 +1244,7 @@ defmodule Pleroma.Web.CommonAPITest do
           length: 180_000
         })
 
           length: 180_000
         })
 
-      object = Object.normalize(activity)
+      object = Object.normalize(activity, fetch: false)
 
       assert object.data["title"] == "lain radio episode 1"
 
 
       assert object.data["title"] == "lain radio episode 1"
 
@@ -1214,7 +1263,7 @@ defmodule Pleroma.Web.CommonAPITest do
           visibility: "private"
         })
 
           visibility: "private"
         })
 
-      object = Object.normalize(activity)
+      object = Object.normalize(activity, fetch: false)
 
       assert object.data["title"] == "lain radio episode 1"
 
 
       assert object.data["title"] == "lain radio episode 1"
 
@@ -1241,4 +1290,128 @@ defmodule Pleroma.Web.CommonAPITest do
              } = CommonAPI.get_user("")
     end
   end
              } = CommonAPI.get_user("")
     end
   end
+
+  describe "with `local` visibility" do
+    setup do: clear_config([:instance, :federating], true)
+
+    test "post" do
+      user = insert(:user)
+
+      with_mock Pleroma.Web.Federator, publish: fn _ -> :ok end do
+        {:ok, activity} = CommonAPI.post(user, %{status: "#2hu #2HU", visibility: "local"})
+
+        assert Visibility.is_local_public?(activity)
+        assert_not_called(Pleroma.Web.Federator.publish(activity))
+      end
+    end
+
+    test "delete" do
+      user = insert(:user)
+
+      {:ok, %Activity{id: activity_id}} =
+        CommonAPI.post(user, %{status: "#2hu #2HU", visibility: "local"})
+
+      with_mock Pleroma.Web.Federator, publish: fn _ -> :ok end do
+        assert {:ok, %Activity{data: %{"deleted_activity_id" => ^activity_id}} = activity} =
+                 CommonAPI.delete(activity_id, user)
+
+        assert Visibility.is_local_public?(activity)
+        assert_not_called(Pleroma.Web.Federator.publish(activity))
+      end
+    end
+
+    test "repeat" do
+      user = insert(:user)
+      other_user = insert(:user)
+
+      {:ok, %Activity{id: activity_id}} =
+        CommonAPI.post(other_user, %{status: "cofe", visibility: "local"})
+
+      with_mock Pleroma.Web.Federator, publish: fn _ -> :ok end do
+        assert {:ok, %Activity{data: %{"type" => "Announce"}} = activity} =
+                 CommonAPI.repeat(activity_id, user)
+
+        assert Visibility.is_local_public?(activity)
+        refute called(Pleroma.Web.Federator.publish(activity))
+      end
+    end
+
+    test "unrepeat" do
+      user = insert(:user)
+      other_user = insert(:user)
+
+      {:ok, %Activity{id: activity_id}} =
+        CommonAPI.post(other_user, %{status: "cofe", visibility: "local"})
+
+      assert {:ok, _} = CommonAPI.repeat(activity_id, user)
+
+      with_mock Pleroma.Web.Federator, publish: fn _ -> :ok end do
+        assert {:ok, %Activity{data: %{"type" => "Undo"}} = activity} =
+                 CommonAPI.unrepeat(activity_id, user)
+
+        assert Visibility.is_local_public?(activity)
+        refute called(Pleroma.Web.Federator.publish(activity))
+      end
+    end
+
+    test "favorite" do
+      user = insert(:user)
+      other_user = insert(:user)
+
+      {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe", visibility: "local"})
+
+      with_mock Pleroma.Web.Federator, publish: fn _ -> :ok end do
+        assert {:ok, %Activity{data: %{"type" => "Like"}} = activity} =
+                 CommonAPI.favorite(user, activity.id)
+
+        assert Visibility.is_local_public?(activity)
+        refute called(Pleroma.Web.Federator.publish(activity))
+      end
+    end
+
+    test "unfavorite" do
+      user = insert(:user)
+      other_user = insert(:user)
+
+      {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe", visibility: "local"})
+
+      {:ok, %Activity{}} = CommonAPI.favorite(user, activity.id)
+
+      with_mock Pleroma.Web.Federator, publish: fn _ -> :ok end do
+        assert {:ok, activity} = CommonAPI.unfavorite(activity.id, user)
+        assert Visibility.is_local_public?(activity)
+        refute called(Pleroma.Web.Federator.publish(activity))
+      end
+    end
+
+    test "react_with_emoji" do
+      user = insert(:user)
+      other_user = insert(:user)
+      {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe", visibility: "local"})
+
+      with_mock Pleroma.Web.Federator, publish: fn _ -> :ok end do
+        assert {:ok, %Activity{data: %{"type" => "EmojiReact"}} = activity} =
+                 CommonAPI.react_with_emoji(activity.id, user, "👍")
+
+        assert Visibility.is_local_public?(activity)
+        refute called(Pleroma.Web.Federator.publish(activity))
+      end
+    end
+
+    test "unreact_with_emoji" do
+      user = insert(:user)
+      other_user = insert(:user)
+      {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe", visibility: "local"})
+
+      {:ok, _reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍")
+
+      with_mock Pleroma.Web.Federator, publish: fn _ -> :ok end do
+        assert {:ok, %Activity{data: %{"type" => "Undo"}} = activity} =
+                 CommonAPI.unreact_with_emoji(activity.id, user, "👍")
+
+        assert Visibility.is_local_public?(activity)
+        refute called(Pleroma.Web.Federator.publish(activity))
+      end
+    end
+  end
 end
 end