Chat Controller: Add basic error handling.
[akkoma] / test / web / pleroma_api / controllers / chat_controller_test.exs
index cdb2683c8b4f74b754271213913268faeab17a16..75d4903ed74f7361d913632fa2ba308e39bfa8cf 100644 (file)
@@ -4,7 +4,9 @@
 defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
   use Pleroma.Web.ConnCase, async: true
 
+  alias Pleroma.Object
   alias Pleroma.Chat
+  alias Pleroma.Web.ActivityPub.ActivityPub
   alias Pleroma.Web.CommonAPI
 
   import Pleroma.Factory
@@ -49,6 +51,67 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
       assert result["content"] == "Hallo!!"
       assert result["chat_id"] == chat.id |> to_string()
     end
+
+    test "it works with an attachment", %{conn: conn, user: user} do
+      file = %Plug.Upload{
+        content_type: "image/jpg",
+        path: Path.absname("test/fixtures/image.jpg"),
+        filename: "an_image.jpg"
+      }
+
+      {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
+
+      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", %{
+          "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()
+    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
+      recipient = insert(:user)
+
+      {:ok, message} =
+        CommonAPI.post_chat_message(user, recipient, "Hello darkness my old friend")
+
+      {:ok, other_message} = CommonAPI.post_chat_message(recipient, user, "nico nico ni")
+
+      object = Object.normalize(message, false)
+
+      chat = Chat.get(user.id, recipient.ap_id)
+
+      result =
+        conn
+        |> put_req_header("content-type", "application/json")
+        |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{object.id}")
+        |> json_response_and_validate_schema(200)
+
+      assert result["id"] == to_string(object.id)
+
+      object = Object.normalize(other_message, false)
+
+      result =
+        conn
+        |> put_req_header("content-type", "application/json")
+        |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{object.id}")
+        |> json_response(400)
+
+      assert result == %{"error" => "could_not_delete"}
+    end
   end
 
   describe "GET /api/v1/pleroma/chats/:id/messages" do
@@ -126,6 +189,23 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
     end
   end
 
+  describe "GET /api/v1/pleroma/chats/:id" do
+    setup do: oauth_access(["read:statuses"])
+
+    test "it returns a chat", %{conn: conn, user: user} do
+      other_user = insert(:user)
+
+      {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
+
+      result =
+        conn
+        |> get("/api/v1/pleroma/chats/#{chat.id}")
+        |> json_response_and_validate_schema(200)
+
+      assert result["id"] == to_string(chat.id)
+    end
+  end
+
   describe "GET /api/v1/pleroma/chats" do
     setup do: oauth_access(["read:statuses"])