Merge branch 'develop' into 'feature/local-only-scope'
[akkoma] / test / pleroma / web / common_api_test.exs
index c5b90ad84297b7d569eac29dd92069a6f9125328..e1dddd21aeae73bf4b8abddd3595f532fe785acd 100644 (file)
@@ -1255,4 +1255,128 @@ defmodule Pleroma.Web.CommonAPITest do
              } = CommonAPI.get_user("")
     end
   end
+
+  describe "with `local_only` enabled" 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", local_only: true})
+
+        assert Activity.local_only?(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", local_only: true})
+
+      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 Activity.local_only?(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", local_only: true})
+
+      with_mock Pleroma.Web.Federator, publish: fn _ -> :ok end do
+        assert {:ok, %Activity{data: %{"type" => "Announce"}} = activity} =
+                 CommonAPI.repeat(activity_id, user)
+
+        assert Activity.local_only?(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", local_only: true})
+
+      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 Activity.local_only?(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", local_only: true})
+
+      with_mock Pleroma.Web.Federator, publish: fn _ -> :ok end do
+        assert {:ok, %Activity{data: %{"type" => "Like"}} = activity} =
+                 CommonAPI.favorite(user, activity.id)
+
+        assert Activity.local_only?(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", local_only: true})
+
+      {: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 Activity.local_only?(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", local_only: true})
+
+      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 Activity.local_only?(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", local_only: true})
+
+      {: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 Activity.local_only?(activity)
+        refute called(Pleroma.Web.Federator.publish(activity))
+      end
+    end
+  end
 end