Document AdminFE in changelog
[akkoma] / test / conversation_test.exs
index 150d55631d884a29a0eedb62cb3d83493b1d1c41..864b2eb03066ec28f3244f6d6626f63fcd89a040 100644 (file)
@@ -4,7 +4,9 @@
 
 defmodule Pleroma.ConversationTest do
   use Pleroma.DataCase
+  alias Pleroma.Activity
   alias Pleroma.Conversation
+  alias Pleroma.Object
   alias Pleroma.Web.CommonAPI
 
   import Pleroma.Factory
@@ -22,7 +24,8 @@ defmodule Pleroma.ConversationTest do
     user = insert(:user)
     {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey"})
 
-    context = activity.data["object"]["context"]
+    object = Pleroma.Object.normalize(activity)
+    context = object.data["context"]
 
     conversation = Conversation.get_for_ap_id(context)
 
@@ -37,7 +40,8 @@ defmodule Pleroma.ConversationTest do
     {:ok, activity} =
       CommonAPI.post(har, %{"status" => "Hey @#{jafnhar.nickname}", "visibility" => "direct"})
 
-    context = activity.data["object"]["context"]
+    object = Pleroma.Object.normalize(activity)
+    context = object.data["context"]
 
     conversation =
       Conversation.get_for_ap_id(context)
@@ -58,7 +62,8 @@ defmodule Pleroma.ConversationTest do
         "in_reply_to_status_id" => activity.id
       })
 
-    context = activity.data["object"]["context"]
+    object = Pleroma.Object.normalize(activity)
+    context = object.data["context"]
 
     conversation_two =
       Conversation.get_for_ap_id(context)
@@ -81,11 +86,12 @@ defmodule Pleroma.ConversationTest do
         "in_reply_to_status_id" => activity.id
       })
 
-    context = activity.data["object"]["context"]
+    object = Pleroma.Object.normalize(activity)
+    context = object.data["context"]
 
     conversation_three =
       Conversation.get_for_ap_id(context)
-      |> Repo.preload(:participations)
+      |> Repo.preload([:participations, :users])
 
     assert conversation_three.id == conversation.id
 
@@ -100,5 +106,70 @@ defmodule Pleroma.ConversationTest do
     assert Enum.find(conversation_three.participations, fn %{user_id: user_id} ->
              tridi.id == user_id
            end)
+
+    assert Enum.find(conversation_three.users, fn %{id: user_id} ->
+             har.id == user_id
+           end)
+
+    assert Enum.find(conversation_three.users, fn %{id: user_id} ->
+             jafnhar.id == user_id
+           end)
+
+    assert Enum.find(conversation_three.users, fn %{id: user_id} ->
+             tridi.id == user_id
+           end)
+  end
+
+  test "create_or_bump_for returns the conversation with participations" do
+    har = insert(:user)
+    jafnhar = insert(:user, local: false)
+
+    {:ok, activity} =
+      CommonAPI.post(har, %{"status" => "Hey @#{jafnhar.nickname}", "visibility" => "direct"})
+
+    {:ok, conversation} = Conversation.create_or_bump_for(activity)
+
+    assert length(conversation.participations) == 2
+
+    {:ok, activity} =
+      CommonAPI.post(har, %{"status" => "Hey @#{jafnhar.nickname}", "visibility" => "public"})
+
+    assert {:error, _} = Conversation.create_or_bump_for(activity)
+  end
+
+  test "create_or_bump_for does not normalize objects before checking the activity type" do
+    note = insert(:note)
+    note_id = note.data["id"]
+    Repo.delete(note)
+    refute Object.get_by_ap_id(note_id)
+
+    Tesla.Mock.mock(fn env ->
+      case env.url do
+        ^note_id ->
+          # TODO: add attributedTo and tag to the note factory
+          body =
+            note.data
+            |> Map.put("attributedTo", note.data["actor"])
+            |> Map.put("tag", [])
+            |> Jason.encode!()
+
+          %Tesla.Env{status: 200, body: body}
+      end
+    end)
+
+    undo = %Activity{
+      id: "fake",
+      data: %{
+        "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
+        "actor" => note.data["actor"],
+        "to" => [note.data["actor"]],
+        "object" => note_id,
+        "type" => "Undo"
+      }
+    }
+
+    Conversation.create_or_bump_for(undo)
+
+    refute Object.get_by_ap_id(note_id)
   end
 end