ChatControllerTest: Credo fixes.
[akkoma] / test / web / pleroma_api / controllers / chat_controller_test.exs
index 0750c7273a5d5435620c3311a018522d4e3774ff..07b698013e7c6af1bc9b1a05f8ff79994fc0f6c7 100644 (file)
@@ -5,8 +5,14 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
   use Pleroma.Web.ConnCase, async: true
 
   alias Pleroma.Chat
+  alias Pleroma.Web.ApiSpec
+  alias Pleroma.Web.ApiSpec.Schemas.ChatMessageResponse
+  alias Pleroma.Web.ApiSpec.Schemas.ChatMessagesResponse
+  alias Pleroma.Web.ApiSpec.Schemas.ChatResponse
+  alias Pleroma.Web.ApiSpec.Schemas.ChatsResponse
   alias Pleroma.Web.CommonAPI
 
+  import OpenApiSpex.TestAssertions
   import Pleroma.Factory
 
   describe "POST /api/v1/pleroma/chats/:id/messages" do
@@ -19,11 +25,13 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
 
       result =
         conn
+        |> put_req_header("content-type", "application/json")
         |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{"content" => "Hallo!!"})
         |> json_response(200)
 
       assert result["content"] == "Hallo!!"
       assert result["chat_id"] == chat.id |> to_string()
+      assert_schema(result, "ChatMessageResponse", ApiSpec.spec())
     end
   end
 
@@ -45,13 +53,15 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
         |> json_response(200)
 
       assert length(result) == 20
+      assert_schema(result, "ChatMessagesResponse", ApiSpec.spec())
 
       result =
         conn
-        |> get("/api/v1/pleroma/chats/#{chat.id}/messages", %{"max_id" => List.last(result)["id"]})
+        |> get("/api/v1/pleroma/chats/#{chat.id}/messages?max_id=#{List.last(result)["id"]}")
         |> json_response(200)
 
       assert length(result) == 10
+      assert_schema(result, "ChatMessagesResponse", ApiSpec.spec())
     end
 
     test "it returns the messages for a given chat", %{conn: conn, user: user} do
@@ -76,6 +86,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
       end)
 
       assert length(result) == 3
+      assert_schema(result, "ChatMessagesResponse", ApiSpec.spec())
 
       # Trying to get the chat of a different user
       result =
@@ -99,6 +110,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
         |> json_response(200)
 
       assert result["id"]
+      assert_schema(result, "ChatResponse", ApiSpec.spec())
     end
   end
 
@@ -117,13 +129,16 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
         |> json_response(200)
 
       assert length(result) == 20
+      assert_schema(result, "ChatsResponse", ApiSpec.spec())
 
       result =
         conn
-        |> get("/api/v1/pleroma/chats", %{max_id: List.last(result)["id"]})
+        |> get("/api/v1/pleroma/chats?max_id=#{List.last(result)["id"]}")
         |> json_response(200)
 
       assert length(result) == 10
+
+      assert_schema(result, "ChatsResponse", ApiSpec.spec())
     end
 
     test "it return a list of chats the current user is participating in, in descending order of updates",
@@ -154,6 +169,34 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
                chat_3.id |> to_string(),
                chat_1.id |> to_string()
              ]
+
+      assert_schema(result, "ChatsResponse", ApiSpec.spec())
+    end
+  end
+
+  describe "schemas" do
+    test "Chat example matches schema" do
+      api_spec = ApiSpec.spec()
+      schema = ChatResponse.schema()
+      assert_schema(schema.example, "ChatResponse", api_spec)
+    end
+
+    test "Chats example matches schema" do
+      api_spec = ApiSpec.spec()
+      schema = ChatsResponse.schema()
+      assert_schema(schema.example, "ChatsResponse", api_spec)
+    end
+
+    test "ChatMessage example matches schema" do
+      api_spec = ApiSpec.spec()
+      schema = ChatMessageResponse.schema()
+      assert_schema(schema.example, "ChatMessageResponse", api_spec)
+    end
+
+    test "ChatsMessage example matches schema" do
+      api_spec = ApiSpec.spec()
+      schema = ChatMessagesResponse.schema()
+      assert_schema(schema.example, "ChatMessagesResponse", api_spec)
     end
   end
 end