remove all endpoints marked as deprecated (#91)
[akkoma] / test / pleroma / web / pleroma_api / controllers / chat_controller_test.exs
index fa6b9db65ea5d44a88f1d83a54f02de5643d50f6..1114da242aad153c53d708a102a82b6fdabfaae6 100644 (file)
@@ -1,5 +1,5 @@
 # Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
 # SPDX-License-Identifier: AGPL-3.0-only
 defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
   use Pleroma.Web.ConnCase
@@ -22,7 +22,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
       {:ok, create} = CommonAPI.post_chat_message(other_user, user, "sup")
       {:ok, _create} = CommonAPI.post_chat_message(other_user, user, "sup part 2")
       {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
-      object = Object.normalize(create, false)
+      object = Object.normalize(create, fetch: false)
       cm_ref = MessageReference.for_chat_and_object(chat, object)
 
       assert cm_ref.unread == true
@@ -52,7 +52,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
       {:ok, create} = CommonAPI.post_chat_message(other_user, user, "sup")
       {:ok, _create} = CommonAPI.post_chat_message(other_user, user, "sup part 2")
       {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
-      object = Object.normalize(create, false)
+      object = Object.normalize(create, fetch: false)
       cm_ref = MessageReference.for_chat_and_object(chat, object)
 
       assert cm_ref.unread == true
@@ -106,6 +106,9 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
     end
 
     test "it works with an attachment", %{conn: conn, user: user} do
+      clear_config([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
+      clear_config([Pleroma.Uploaders.Local, :uploads], "uploads")
+
       file = %Plug.Upload{
         content_type: "image/jpeg",
         path: Path.absname("test/fixtures/image.jpg"),
@@ -158,7 +161,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
 
       {:ok, other_message} = CommonAPI.post_chat_message(recipient, user, "nico nico ni")
 
-      object = Object.normalize(message, false)
+      object = Object.normalize(message, fetch: false)
 
       chat = Chat.get(user.id, recipient.ap_id)
 
@@ -176,7 +179,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
       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)
+      object = Object.normalize(other_message, fetch: false)
       cm_ref = MessageReference.for_chat_and_object(chat, object)
 
       result =
@@ -211,12 +214,12 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
 
       assert String.match?(
                next,
-               ~r(#{api_endpoint}.*/messages\?id=.*&limit=\d+&max_id=.*; rel=\"next\"$)
+               ~r(#{api_endpoint}.*/messages\?limit=\d+&max_id=.*; rel=\"next\"$)
              )
 
       assert String.match?(
                prev,
-               ~r(#{api_endpoint}.*/messages\?id=.*&limit=\d+&min_id=.*; rel=\"prev\"$)
+               ~r(#{api_endpoint}.*/messages\?limit=\d+&min_id=.*; rel=\"prev\"$)
              )
 
       assert length(result) == 20
@@ -229,12 +232,12 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
 
       assert String.match?(
                next,
-               ~r(#{api_endpoint}.*/messages\?id=.*&limit=\d+&max_id=.*; rel=\"next\"$)
+               ~r(#{api_endpoint}.*/messages\?limit=\d+&max_id=.*; rel=\"next\"$)
              )
 
       assert String.match?(
                prev,
-               ~r(#{api_endpoint}.*/messages\?id=.*&limit=\d+&max_id=.*&min_id=.*; rel=\"prev\"$)
+               ~r(#{api_endpoint}.*/messages\?limit=\d+&max_id=.*&min_id=.*; rel=\"prev\"$)
              )
 
       assert length(result) == 10
@@ -264,9 +267,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
@@ -303,7 +307,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
     end
   end
 
-  describe "GET /api/v1/pleroma/chats" do
+  describe "GET /api/v2/pleroma/chats" do
     setup do: oauth_access(["read:chats"])
 
     test "it does not return chats with deleted users", %{conn: conn, user: user} do
@@ -315,7 +319,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
 
       result =
         conn
-        |> get("/api/v1/pleroma/chats")
+        |> get("/api/v2/pleroma/chats")
         |> json_response_and_validate_schema(200)
 
       assert length(result) == 0
@@ -328,7 +332,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
 
       result =
         conn
-        |> get("/api/v1/pleroma/chats")
+        |> get("/api/v2/pleroma/chats")
         |> json_response_and_validate_schema(200)
 
       assert length(result) == 1
@@ -337,13 +341,42 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
 
       result =
         conn
-        |> get("/api/v1/pleroma/chats")
+        |> get("/api/v2/pleroma/chats")
         |> json_response_and_validate_schema(200)
 
       assert length(result) == 0
     end
 
-    test "it returns all chats", %{conn: conn, user: user} do
+    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/v2/pleroma/chats")
+        |> json_response_and_validate_schema(200)
+
+      assert length(result) == 1
+
+      User.mute(user, recipient)
+
+      result =
+        conn
+        |> get("/api/v2/pleroma/chats")
+        |> json_response_and_validate_schema(200)
+
+      assert length(result) == 0
+
+      result =
+        conn
+        |> get("/api/v2/pleroma/chats?with_muted=true")
+        |> json_response_and_validate_schema(200)
+
+      assert length(result) == 1
+    end
+
+    test "it paginates chats", %{conn: conn, user: user} do
       Enum.each(1..30, fn _ ->
         recipient = insert(:user)
         {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
@@ -351,10 +384,18 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
 
       result =
         conn
-        |> get("/api/v1/pleroma/chats")
+        |> get("/api/v2/pleroma/chats")
+        |> json_response_and_validate_schema(200)
+
+      assert length(result) == 20
+      last_id = List.last(result)["id"]
+
+      result =
+        conn
+        |> get("/api/v2/pleroma/chats?max_id=#{last_id}")
         |> json_response_and_validate_schema(200)
 
-      assert length(result) == 30
+      assert length(result) == 10
     end
 
     test "it return a list of chats the current user is participating in, in descending order of updates",
@@ -364,18 +405,18 @@ 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)
 
       result =
         conn
-        |> get("/api/v1/pleroma/chats")
+        |> get("/api/v2/pleroma/chats")
         |> json_response_and_validate_schema(200)
 
       ids = Enum.map(result, & &1["id"])
@@ -402,7 +443,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
 
       result =
         conn
-        |> get("/api/v1/pleroma/chats")
+        |> get("/api/v2/pleroma/chats")
         |> json_response_and_validate_schema(200)
 
       account_ids = Enum.map(result, &get_in(&1, ["account", "id"]))