Add unfavoriting to TwAPI.
[akkoma] / test / web / twitter_api / twitter_api_test.exs
index a4c9bd7e72b580ec472985ef3bdc258ee5226e60..88e6dd6841bde419abc97f016ce687aceed3e6f5 100644 (file)
@@ -4,6 +4,9 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
   alias Pleroma.Web.TwitterAPI.TwitterAPI
   alias Pleroma.{Activity, User, Object, Repo}
   alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
+  alias Pleroma.Web.ActivityPub.ActivityPub
+
+  import Pleroma.Factory
 
   test "create a status" do
     user = UserBuilder.build(%{ap_id: "142344"})
@@ -177,4 +180,37 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
 
     assert TwitterAPI.add_user_links(text, mentions) == expected_text
   end
+
+  test "it favorites a status, returns the updated status" do
+    user = insert(:user)
+    note_activity = insert(:note_activity)
+    activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"])
+
+    {:ok, status} = TwitterAPI.favorite(user, note_activity)
+    updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
+
+    assert status == ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user})
+  end
+
+  test "it unfavorites a status, returns the updated status" do
+    user = insert(:user)
+    note_activity = insert(:note_activity)
+    activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"])
+    object = Object.get_by_ap_id(note_activity.data["object"]["id"])
+
+    {:ok, like_activity, object } = ActivityPub.like(user, object)
+    updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
+    assert ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user})["fave_num"] == 1
+
+    {:ok, status} = TwitterAPI.unfavorite(user, note_activity)
+    updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
+
+    assert status["fave_num"] == 0
+  end
+
+  setup do
+    Supervisor.terminate_child(Pleroma.Supervisor, ConCache)
+    Supervisor.restart_child(Pleroma.Supervisor, ConCache)
+    :ok
+  end
 end