Fix MRF policies to also work with Update
[akkoma] / test / pleroma / web / activity_pub / utils_test.exs
index be9cd7d13eb45f6f5a3bd0326a9f7f9be2b4b512..e45af3aec35cc233580870c97a7b9d412c8ac981 100644 (file)
@@ -1,9 +1,9 @@
 # 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.ActivityPub.UtilsTest do
-  use Pleroma.DataCase
+  use Pleroma.DataCase, async: true
   alias Pleroma.Activity
   alias Pleroma.Object
   alias Pleroma.Repo
@@ -165,7 +165,7 @@ defmodule Pleroma.Web.ActivityPub.UtilsTest do
           }
         })
 
-      object = Object.normalize(activity)
+      object = Object.normalize(activity, fetch: false)
       {:ok, votes, object} = CommonAPI.vote(other_user, object, [0, 1])
       assert Enum.sort(Utils.get_existing_votes(other_user.ap_id, object)) == Enum.sort(votes)
     end
@@ -183,7 +183,7 @@ defmodule Pleroma.Web.ActivityPub.UtilsTest do
           }
         })
 
-      object = Object.normalize(activity)
+      object = Object.normalize(activity, fetch: false)
       {:ok, [vote], object} = CommonAPI.vote(other_user, object, [0])
       {:ok, _activity} = CommonAPI.favorite(user, activity.id)
       [fetched_vote] = Utils.get_existing_votes(other_user.ap_id, object)
@@ -213,27 +213,18 @@ defmodule Pleroma.Web.ActivityPub.UtilsTest do
       assert refresh_record(follow_activity).data["state"] == "accept"
       assert refresh_record(follow_activity_two).data["state"] == "accept"
     end
-  end
 
-  describe "update_follow_state/2" do
-    test "updates the state of the given follow activity" do
-      user = insert(:user, is_locked: true)
+    test "also updates the state of accepted follows" do
+      user = insert(:user)
       follower = insert(:user)
 
       {:ok, _, _, follow_activity} = CommonAPI.follow(follower, user)
       {:ok, _, _, follow_activity_two} = CommonAPI.follow(follower, user)
 
-      data =
-        follow_activity_two.data
-        |> Map.put("state", "accept")
-
-      cng = Ecto.Changeset.change(follow_activity_two, data: data)
-
-      {:ok, follow_activity_two} = Repo.update(cng)
-
-      {:ok, follow_activity_two} = Utils.update_follow_state(follow_activity_two, "reject")
+      {:ok, follow_activity_two} =
+        Utils.update_follow_state_for_all(follow_activity_two, "reject")
 
-      assert refresh_record(follow_activity).data["state"] == "pending"
+      assert refresh_record(follow_activity).data["state"] == "reject"
       assert refresh_record(follow_activity_two).data["state"] == "reject"
     end
   end
@@ -242,7 +233,7 @@ defmodule Pleroma.Web.ActivityPub.UtilsTest do
     test "updates likes" do
       user = insert(:user)
       activity = insert(:note_activity)
-      object = Object.normalize(activity)
+      object = Object.normalize(activity, fetch: false)
 
       assert {:ok, updated_object} =
                Utils.update_element_in_object(
@@ -302,7 +293,7 @@ defmodule Pleroma.Web.ActivityPub.UtilsTest do
   describe "get_existing_like/2" do
     test "fetches existing like" do
       note_activity = insert(:note_activity)
-      assert object = Object.normalize(note_activity)
+      assert object = Object.normalize(note_activity, fetch: false)
 
       user = insert(:user)
       refute Utils.get_existing_like(user.ap_id, object)
@@ -320,7 +311,7 @@ defmodule Pleroma.Web.ActivityPub.UtilsTest do
 
     test "fetches existing announce" do
       note_activity = insert(:note_activity)
-      assert object = Object.normalize(note_activity)
+      assert object = Object.normalize(note_activity, fetch: false)
       actor = insert(:user)
 
       {:ok, announce} = CommonAPI.repeat(note_activity.id, actor)
@@ -412,10 +403,9 @@ defmodule Pleroma.Web.ActivityPub.UtilsTest do
   describe "lazy_put_activity_defaults/2" do
     test "returns map with id and published data" do
       note_activity = insert(:note_activity)
-      object = Object.normalize(note_activity)
+      object = Object.normalize(note_activity, fetch: false)
       res = Utils.lazy_put_activity_defaults(%{"context" => object.data["id"]})
       assert res["context"] == object.data["id"]
-      assert res["context_id"] == object.id
       assert res["id"]
       assert res["published"]
     end
@@ -423,7 +413,6 @@ defmodule Pleroma.Web.ActivityPub.UtilsTest do
     test "returns map with fake id and published data" do
       assert %{
                "context" => "pleroma:fakecontext",
-               "context_id" => -1,
                "id" => "pleroma:fakeid",
                "published" => _
              } = Utils.lazy_put_activity_defaults(%{}, true)
@@ -431,7 +420,7 @@ defmodule Pleroma.Web.ActivityPub.UtilsTest do
 
     test "returns activity data with object" do
       note_activity = insert(:note_activity)
-      object = Object.normalize(note_activity)
+      object = Object.normalize(note_activity, fetch: false)
 
       res =
         Utils.lazy_put_activity_defaults(%{
@@ -440,13 +429,11 @@ defmodule Pleroma.Web.ActivityPub.UtilsTest do
         })
 
       assert res["context"] == object.data["id"]
-      assert res["context_id"] == object.id
       assert res["id"]
       assert res["published"]
       assert res["object"]["id"]
       assert res["object"]["published"]
       assert res["object"]["context"] == object.data["id"]
-      assert res["object"]["context_id"] == object.id
     end
   end