Don't delete new delete activity...
[akkoma] / test / web / activity_pub / activity_pub_test.exs
index bf9090d2f3dbe75b545e6f6b18f8bf337f2f0a76..a088e97be36318c3bd30fccd5f0e6f88645bfe3c 100644 (file)
@@ -1,12 +1,20 @@
 defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
   use Pleroma.DataCase
   alias Pleroma.Web.ActivityPub.ActivityPub
+  alias Pleroma.Web.ActivityPub.Utils
   alias Pleroma.{Activity, Object, User}
   alias Pleroma.Builders.ActivityBuilder
 
   import Pleroma.Factory
 
   describe "insertion" do
+    test "returns the activity if one with the same id is already in" do
+      activity = insert(:note_activity)
+      {:ok, new_activity}= ActivityPub.insert(activity.data)
+
+      assert activity == new_activity
+    end
+
     test "inserts a given map into the activity database, giving it an id if it has none." do
       data = %{
         "ok" => true
@@ -40,6 +48,13 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
     end
   end
 
+  describe "create activities" do
+    test "removes doubled 'to' recipients" do
+      {:ok, activity} = ActivityPub.create(["user1", "user1", "user2"], %User{ap_id: "1"}, "", %{})
+      assert activity.data["to"] == ["user1", "user2"]
+    end
+  end
+
   describe "fetch activities for recipients" do
     test "retrieve the activities for certain recipients" do
       {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})
@@ -54,13 +69,14 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
 
   describe "fetch activities in context" do
     test "retrieves activities that have a given context" do
-      {:ok, activity} = ActivityBuilder.insert(%{"context" => "2hu"})
-      {:ok, activity_two} = ActivityBuilder.insert(%{"context" => "2hu"})
-      {:ok, _activity_three} = ActivityBuilder.insert(%{"context" => "3hu"})
+      {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
+      {:ok, activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
+      {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
+      {:ok, _activity_four} = ActivityBuilder.insert(%{"type" => "Announce", "context" => "2hu"})
 
       activities = ActivityPub.fetch_activities_for_context("2hu")
 
-      assert activities == [activity, activity_two]
+      assert activities == [activity_two, activity]
     end
   end
 
@@ -125,6 +141,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
       assert like_activity.data["type"] == "Like"
       assert like_activity.data["object"] == object.data["id"]
       assert like_activity.data["to"] == [User.ap_followers(user), note_activity.data["actor"]]
+      assert like_activity.data["context"] == object.data["context"]
       assert object.data["like_count"] == 1
       assert object.data["likes"] == [user.ap_id]
 
@@ -174,6 +191,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
       assert announce_activity.data["to"] == [User.ap_followers(user), note_activity.data["actor"]]
       assert announce_activity.data["object"] == object.data["id"]
       assert announce_activity.data["actor"] == user.ap_id
+      assert announce_activity.data["context"] == object.data["context"]
     end
   end
 
@@ -200,7 +218,47 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
       follower = Repo.get_by(User, ap_id: activity.data["actor"])
       followed = Repo.get_by(User, ap_id: activity.data["object"])
 
-      assert activity == ActivityPub.fetch_latest_follow(follower, followed)
+      assert activity == Utils.fetch_latest_follow(follower, followed)
+    end
+  end
+
+  describe "following / unfollowing" do
+    test "creates a follow activity" do
+      follower = insert(:user)
+      followed = insert(:user)
+
+      {:ok, activity} = ActivityPub.follow(follower, followed)
+      assert activity.data["type"] == "Follow"
+      assert activity.data["actor"] == follower.ap_id
+      assert activity.data["object"] == followed.ap_id
+    end
+
+    test "creates an undo activity for the last follow" do
+      follower = insert(:user)
+      followed = insert(:user)
+
+      {:ok, follow_activity} = ActivityPub.follow(follower, followed)
+      {:ok, activity} = ActivityPub.unfollow(follower, followed)
+
+      assert activity.data["type"] == "Undo"
+      assert activity.data["actor"] == follower.ap_id
+      assert activity.data["object"] == follow_activity.data["id"]
+    end
+  end
+
+  describe "deletion" do
+    test "it creates a delete activity and deletes the original object" do
+      note = insert(:note_activity)
+      object = Object.get_by_ap_id(note.data["object"]["id"])
+      {:ok, delete} = ActivityPub.delete(object)
+
+      assert delete.data["type"] == "Delete"
+      assert delete.data["actor"] == note.data["actor"]
+      assert delete.data["object"] == note.data["object"]["id"]
+
+      assert Repo.get(Activity, delete.id) != nil
+
+      assert Repo.get(Object, object.id) == nil
     end
   end