Merge remote-tracking branch 'remotes/origin/develop' into feature/object-hashtags...
[akkoma] / test / pleroma / web / pleroma_api / controllers / chat_controller_test.exs
index 6381f9757e5f33ff429d51aa7dc8e38754f0c1a9..415c3decdbca7ffb24d54fdf0648c94182fd29fb 100644 (file)
@@ -82,11 +82,13 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
       result =
         conn
         |> put_req_header("content-type", "application/json")
+        |> put_req_header("idempotency-key", "123")
         |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{"content" => "Hallo!!"})
         |> json_response_and_validate_schema(200)
 
       assert result["content"] == "Hallo!!"
       assert result["chat_id"] == chat.id |> to_string()
+      assert result["idempotency_key"] == "123"
     end
 
     test "it fails if there is no content", %{conn: conn, user: user} do
@@ -262,9 +264,10 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
       assert length(result) == 3
 
       # Trying to get the chat of a different user
+      other_user_chat = Chat.get(other_user.id, user.ap_id)
+
       conn
-      |> assign(:user, other_user)
-      |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
+      |> get("/api/v1/pleroma/chats/#{other_user_chat.id}/messages")
       |> json_response_and_validate_schema(404)
     end
   end
@@ -341,6 +344,35 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
       assert length(result) == 0
     end
 
+    test "it does not return chats with users you muted", %{conn: conn, user: user} do
+      recipient = insert(:user)
+
+      {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
+
+      result =
+        conn
+        |> get("/api/v1/pleroma/chats")
+        |> json_response_and_validate_schema(200)
+
+      assert length(result) == 1
+
+      User.mute(user, recipient)
+
+      result =
+        conn
+        |> get("/api/v1/pleroma/chats")
+        |> json_response_and_validate_schema(200)
+
+      assert length(result) == 0
+
+      result =
+        conn
+        |> get("/api/v1/pleroma/chats?with_muted=true")
+        |> json_response_and_validate_schema(200)
+
+      assert length(result) == 1
+    end
+
     test "it returns all chats", %{conn: conn, user: user} do
       Enum.each(1..30, fn _ ->
         recipient = insert(:user)
@@ -362,11 +394,11 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
       tridi = insert(:user)
 
       {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
-      :timer.sleep(1000)
-      {:ok, _chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
-      :timer.sleep(1000)
+      {:ok, chat_1} = time_travel(chat_1, -3)
+      {:ok, chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
+      {:ok, _chat_2} = time_travel(chat_2, -2)
       {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
-      :timer.sleep(1000)
+      {:ok, chat_3} = time_travel(chat_3, -1)
 
       # bump the second one
       {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)