Fix specs.
[akkoma] / test / web / activity_pub / activity_pub_test.exs
index bf9090d2f3dbe75b545e6f6b18f8bf337f2f0a76..8e747359194a630799085098c1490096176d2d5e 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"]})
@@ -125,6 +140,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 +190,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 +217,31 @@ 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