ChatMessageReferences: Adjust views
[akkoma] / test / web / pleroma_api / controllers / chat_controller_test.exs
index 037dd229728b696f4d99cf0a35c54c0ed2ed4cd9..bd4024c09965aa62a73ecfe6fa5ef1400a56776e 100644 (file)
@@ -5,6 +5,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
   use Pleroma.Web.ConnCase, async: true
 
   alias Pleroma.Chat
+  alias Pleroma.ChatMessageReference
   alias Pleroma.Object
   alias Pleroma.User
   alias Pleroma.Web.ActivityPub.ActivityPub
@@ -53,6 +54,20 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
       assert result["chat_id"] == chat.id |> to_string()
     end
 
+    test "it fails if there is no content", %{conn: conn, user: user} do
+      other_user = insert(:user)
+
+      {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
+
+      result =
+        conn
+        |> put_req_header("content-type", "application/json")
+        |> post("/api/v1/pleroma/chats/#{chat.id}/messages")
+        |> json_response_and_validate_schema(400)
+
+      assert result
+    end
+
     test "it works with an attachment", %{conn: conn, user: user} do
       file = %Plug.Upload{
         content_type: "image/jpg",
@@ -70,20 +85,18 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
         conn
         |> put_req_header("content-type", "application/json")
         |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{
-          "content" => "Hallo!!",
           "media_id" => to_string(upload.id)
         })
         |> json_response_and_validate_schema(200)
 
-      assert result["content"] == "Hallo!!"
-      assert result["chat_id"] == chat.id |> to_string()
+      assert result["attachment"]
     end
   end
 
   describe "DELETE /api/v1/pleroma/chats/:id/messages/:message_id" do
     setup do: oauth_access(["write:statuses"])
 
-    test "it deletes a message for the author of the message", %{conn: conn, user: user} do
+    test "it deletes a message from the chat", %{conn: conn, user: user} do
       recipient = insert(:user)
 
       {:ok, message} =
@@ -95,23 +108,32 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
 
       chat = Chat.get(user.id, recipient.ap_id)
 
+      cm_ref = ChatMessageReference.for_chat_and_object(chat, object)
+
+      # Deleting your own message removes the message and the reference
       result =
         conn
         |> put_req_header("content-type", "application/json")
-        |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{object.id}")
+        |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{cm_ref.id}")
         |> json_response_and_validate_schema(200)
 
-      assert result["id"] == to_string(object.id)
+      assert result["id"] == cm_ref.id
+      refute ChatMessageReference.get_by_id(cm_ref.id)
+      assert %{data: %{"type" => "Tombstone"}} = Object.get_by_id(object.id)
 
+      # Deleting other people's messages just removes the reference
       object = Object.normalize(other_message, false)
+      cm_ref = ChatMessageReference.for_chat_and_object(chat, object)
 
       result =
         conn
         |> put_req_header("content-type", "application/json")
-        |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{object.id}")
-        |> json_response(400)
+        |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{cm_ref.id}")
+        |> json_response_and_validate_schema(200)
 
-      assert result == %{"error" => "could_not_delete"}
+      assert result["id"] == cm_ref.id
+      refute ChatMessageReference.get_by_id(cm_ref.id)
+      assert Object.get_by_id(object.id)
     end
   end