Add OpenAPI spec for SuggestionController
[akkoma] / test / web / mastodon_api / controllers / timeline_controller_test.exs
index 6d8f81b75ac30817d28aa1f74ca969f78d0d8f87..f8b9289f37cb04c7d893e667ea3d8d7b152a9efd 100644 (file)
@@ -34,7 +34,7 @@ defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do
         conn
         |> assign(:user, user)
         |> get("/api/v1/timelines/home")
-        |> json_response(200)
+        |> json_response_and_validate_schema(200)
 
       assert Enum.all?(response, fn n ->
                get_in(n, ["account", "pleroma", "relationship"]) == %{}
@@ -42,7 +42,7 @@ defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do
     end
 
     test "the home timeline", %{user: user, conn: conn} do
-      uri = "/api/v1/timelines/home?with_relationships=true"
+      uri = "/api/v1/timelines/home?with_relationships=1"
 
       following = insert(:user, nickname: "followed")
       third_user = insert(:user, nickname: "repeated")
@@ -53,7 +53,7 @@ defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do
 
       ret_conn = get(conn, uri)
 
-      assert Enum.empty?(json_response(ret_conn, :ok))
+      assert Enum.empty?(json_response_and_validate_schema(ret_conn, :ok))
 
       {:ok, _user} = User.follow(user, following)
 
@@ -78,7 +78,7 @@ defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do
                    "pleroma" => %{"relationship" => %{"following" => true}}
                  }
                }
-             ] = json_response(ret_conn, :ok)
+             ] = json_response_and_validate_schema(ret_conn, :ok)
 
       {:ok, _user} = User.follow(third_user, user)
 
@@ -104,7 +104,7 @@ defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do
                    "pleroma" => %{"relationship" => %{"following" => true}}
                  }
                }
-             ] = json_response(ret_conn, :ok)
+             ] = json_response_and_validate_schema(ret_conn, :ok)
     end
 
     test "the home timeline when the direct messages are excluded", %{user: user, conn: conn} do
@@ -115,9 +115,9 @@ defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do
 
       {:ok, private_activity} = CommonAPI.post(user, %{status: ".", visibility: "private"})
 
-      conn = get(conn, "/api/v1/timelines/home", %{"exclude_visibilities" => ["direct"]})
+      conn = get(conn, "/api/v1/timelines/home?exclude_visibilities[]=direct")
 
-      assert status_ids = json_response(conn, :ok) |> Enum.map(& &1["id"])
+      assert status_ids = json_response_and_validate_schema(conn, :ok) |> Enum.map(& &1["id"])
       assert public_activity.id in status_ids
       assert unlisted_activity.id in status_ids
       assert private_activity.id in status_ids
@@ -134,17 +134,17 @@ defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do
 
       _activity = insert(:note_activity, local: false)
 
-      conn = get(conn, "/api/v1/timelines/public", %{"local" => "False"})
+      conn = get(conn, "/api/v1/timelines/public?local=False")
 
-      assert length(json_response(conn, :ok)) == 2
+      assert length(json_response_and_validate_schema(conn, :ok)) == 2
 
-      conn = get(build_conn(), "/api/v1/timelines/public", %{"local" => "True"})
+      conn = get(build_conn(), "/api/v1/timelines/public?local=True")
 
-      assert [%{"content" => "test"}] = json_response(conn, :ok)
+      assert [%{"content" => "test"}] = json_response_and_validate_schema(conn, :ok)
 
-      conn = get(build_conn(), "/api/v1/timelines/public", %{"local" => "1"})
+      conn = get(build_conn(), "/api/v1/timelines/public?local=1")
 
-      assert [%{"content" => "test"}] = json_response(conn, :ok)
+      assert [%{"content" => "test"}] = json_response_and_validate_schema(conn, :ok)
     end
 
     test "the public timeline includes only public statuses for an authenticated user" do
@@ -156,7 +156,7 @@ defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do
       {:ok, _activity} = CommonAPI.post(user, %{status: "test", visibility: "direct"})
 
       res_conn = get(conn, "/api/v1/timelines/public")
-      assert length(json_response(res_conn, 200)) == 1
+      assert length(json_response_and_validate_schema(res_conn, 200)) == 1
     end
   end
 
@@ -174,15 +174,15 @@ defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do
     setup do: clear_config([:restrict_unauthenticated, :timelines, :federated], true)
 
     test "if user is unauthenticated", %{conn: conn} do
-      res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "true"})
+      res_conn = get(conn, "/api/v1/timelines/public?local=true")
 
-      assert json_response(res_conn, :unauthorized) == %{
+      assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
                "error" => "authorization required for timeline view"
              }
 
-      res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "false"})
+      res_conn = get(conn, "/api/v1/timelines/public?local=false")
 
-      assert json_response(res_conn, :unauthorized) == %{
+      assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
                "error" => "authorization required for timeline view"
              }
     end
@@ -190,11 +190,11 @@ defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do
     test "if user is authenticated" do
       %{conn: conn} = oauth_access(["read:statuses"])
 
-      res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "true"})
-      assert length(json_response(res_conn, 200)) == 1
+      res_conn = get(conn, "/api/v1/timelines/public?local=true")
+      assert length(json_response_and_validate_schema(res_conn, 200)) == 1
 
-      res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "false"})
-      assert length(json_response(res_conn, 200)) == 2
+      res_conn = get(conn, "/api/v1/timelines/public?local=false")
+      assert length(json_response_and_validate_schema(res_conn, 200)) == 2
     end
   end
 
@@ -204,24 +204,24 @@ defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do
     setup do: clear_config([:restrict_unauthenticated, :timelines, :local], true)
 
     test "if user is unauthenticated", %{conn: conn} do
-      res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "true"})
+      res_conn = get(conn, "/api/v1/timelines/public?local=true")
 
-      assert json_response(res_conn, :unauthorized) == %{
+      assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
                "error" => "authorization required for timeline view"
              }
 
-      res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "false"})
-      assert length(json_response(res_conn, 200)) == 2
+      res_conn = get(conn, "/api/v1/timelines/public?local=false")
+      assert length(json_response_and_validate_schema(res_conn, 200)) == 2
     end
 
     test "if user is authenticated", %{conn: _conn} do
       %{conn: conn} = oauth_access(["read:statuses"])
 
-      res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "true"})
-      assert length(json_response(res_conn, 200)) == 1
+      res_conn = get(conn, "/api/v1/timelines/public?local=true")
+      assert length(json_response_and_validate_schema(res_conn, 200)) == 1
 
-      res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "false"})
-      assert length(json_response(res_conn, 200)) == 2
+      res_conn = get(conn, "/api/v1/timelines/public?local=false")
+      assert length(json_response_and_validate_schema(res_conn, 200)) == 2
     end
   end
 
@@ -231,12 +231,12 @@ defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do
     setup do: clear_config([:restrict_unauthenticated, :timelines, :federated], true)
 
     test "if user is unauthenticated", %{conn: conn} do
-      res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "true"})
-      assert length(json_response(res_conn, 200)) == 1
+      res_conn = get(conn, "/api/v1/timelines/public?local=true")
+      assert length(json_response_and_validate_schema(res_conn, 200)) == 1
 
-      res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "false"})
+      res_conn = get(conn, "/api/v1/timelines/public?local=false")
 
-      assert json_response(res_conn, :unauthorized) == %{
+      assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
                "error" => "authorization required for timeline view"
              }
     end
@@ -244,11 +244,11 @@ defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do
     test "if user is authenticated", %{conn: _conn} do
       %{conn: conn} = oauth_access(["read:statuses"])
 
-      res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "true"})
-      assert length(json_response(res_conn, 200)) == 1
+      res_conn = get(conn, "/api/v1/timelines/public?local=true")
+      assert length(json_response_and_validate_schema(res_conn, 200)) == 1
 
-      res_conn = get(conn, "/api/v1/timelines/public", %{"local" => "false"})
-      assert length(json_response(res_conn, 200)) == 2
+      res_conn = get(conn, "/api/v1/timelines/public?local=false")
+      assert length(json_response_and_validate_schema(res_conn, 200)) == 2
     end
   end
 
@@ -279,7 +279,7 @@ defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do
       # Only direct should be visible here
       res_conn = get(conn_user_two, "api/v1/timelines/direct")
 
-      [status] = json_response(res_conn, :ok)
+      assert [status] = json_response_and_validate_schema(res_conn, :ok)
 
       assert %{"visibility" => "direct"} = status
       assert status["url"] != direct.data["id"]
@@ -291,14 +291,14 @@ defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do
         |> assign(:token, insert(:oauth_token, user: user_one, scopes: ["read:statuses"]))
         |> get("api/v1/timelines/direct")
 
-      [status] = json_response(res_conn, :ok)
+      [status] = json_response_and_validate_schema(res_conn, :ok)
 
       assert %{"visibility" => "direct"} = status
 
       # Both should be visible here
       res_conn = get(conn_user_two, "api/v1/timelines/home")
 
-      [_s1, _s2] = json_response(res_conn, :ok)
+      [_s1, _s2] = json_response_and_validate_schema(res_conn, :ok)
 
       # Test pagination
       Enum.each(1..20, fn _ ->
@@ -311,13 +311,14 @@ defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do
 
       res_conn = get(conn_user_two, "api/v1/timelines/direct")
 
-      statuses = json_response(res_conn, :ok)
+      statuses = json_response_and_validate_schema(res_conn, :ok)
       assert length(statuses) == 20
 
-      res_conn =
-        get(conn_user_two, "api/v1/timelines/direct", %{max_id: List.last(statuses)["id"]})
+      max_id = List.last(statuses)["id"]
+
+      res_conn = get(conn_user_two, "api/v1/timelines/direct?max_id=#{max_id}")
 
-      [status] = json_response(res_conn, :ok)
+      assert [status] = json_response_and_validate_schema(res_conn, :ok)
 
       assert status["url"] != direct.data["id"]
     end
@@ -342,7 +343,7 @@ defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do
 
       res_conn = get(conn, "api/v1/timelines/direct")
 
-      [status] = json_response(res_conn, :ok)
+      [status] = json_response_and_validate_schema(res_conn, :ok)
       assert status["id"] == direct.id
     end
   end
@@ -359,7 +360,7 @@ defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do
 
       conn = get(conn, "/api/v1/timelines/list/#{list.id}")
 
-      assert [%{"id" => id}] = json_response(conn, :ok)
+      assert [%{"id" => id}] = json_response_and_validate_schema(conn, :ok)
 
       assert id == to_string(activity_two.id)
     end
@@ -382,7 +383,7 @@ defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do
 
       conn = get(conn, "/api/v1/timelines/list/#{list.id}")
 
-      assert [%{"id" => id}] = json_response(conn, :ok)
+      assert [%{"id" => id}] = json_response_and_validate_schema(conn, :ok)
 
       assert id == to_string(activity_one.id)
     end
@@ -399,14 +400,14 @@ defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do
 
       nconn = get(conn, "/api/v1/timelines/tag/2hu")
 
-      assert [%{"id" => id}] = json_response(nconn, :ok)
+      assert [%{"id" => id}] = json_response_and_validate_schema(nconn, :ok)
 
       assert id == to_string(activity.id)
 
       # works for different capitalization too
       nconn = get(conn, "/api/v1/timelines/tag/2HU")
 
-      assert [%{"id" => id}] = json_response(nconn, :ok)
+      assert [%{"id" => id}] = json_response_and_validate_schema(nconn, :ok)
 
       assert id == to_string(activity.id)
     end
@@ -418,22 +419,21 @@ defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do
       {:ok, activity_test1} = CommonAPI.post(user, %{status: "#test #test1"})
       {:ok, activity_none} = CommonAPI.post(user, %{status: "#test #none"})
 
-      any_test = get(conn, "/api/v1/timelines/tag/test", %{"any" => ["test1"]})
+      any_test = get(conn, "/api/v1/timelines/tag/test?any[]=test1")
 
-      [status_none, status_test1, status_test] = json_response(any_test, :ok)
+      [status_none, status_test1, status_test] = json_response_and_validate_schema(any_test, :ok)
 
       assert to_string(activity_test.id) == status_test["id"]
       assert to_string(activity_test1.id) == status_test1["id"]
       assert to_string(activity_none.id) == status_none["id"]
 
-      restricted_test =
-        get(conn, "/api/v1/timelines/tag/test", %{"all" => ["test1"], "none" => ["none"]})
+      restricted_test = get(conn, "/api/v1/timelines/tag/test?all[]=test1&none[]=none")
 
-      assert [status_test1] == json_response(restricted_test, :ok)
+      assert [status_test1] == json_response_and_validate_schema(restricted_test, :ok)
 
-      all_test = get(conn, "/api/v1/timelines/tag/test", %{"all" => ["none"]})
+      all_test = get(conn, "/api/v1/timelines/tag/test?all[]=none")
 
-      assert [status_none] == json_response(all_test, :ok)
+      assert [status_none] == json_response_and_validate_schema(all_test, :ok)
     end
   end
 end