Add spec for AccountController.relationships
authorEgor Kislitsyn <egor@kislitsyn.com>
Tue, 7 Apr 2020 14:29:05 +0000 (18:29 +0400)
committerEgor Kislitsyn <egor@kislitsyn.com>
Mon, 13 Apr 2020 14:16:07 +0000 (18:16 +0400)
lib/pleroma/web/api_spec/operations/account_operation.ex
lib/pleroma/web/api_spec/schemas/account_relationship_response.ex [new file with mode: 0644]
lib/pleroma/web/api_spec/schemas/account_relationships_response.ex [new file with mode: 0644]
lib/pleroma/web/mastodon_api/controllers/account_controller.ex
test/web/api_spec/account_operation_test.exs
test/web/mastodon_api/controllers/account_controller_test.exs

index d7b56cc2b86f63bfe6f1ffcb27981d539996d970..352f66e9d163f7921fc24025dbb46cc405ab3c0e 100644 (file)
@@ -4,10 +4,12 @@
 
 defmodule Pleroma.Web.ApiSpec.AccountOperation do
   alias OpenApiSpex.Operation
+  alias OpenApiSpex.Schema
   alias Pleroma.Web.ApiSpec.Helpers
   alias Pleroma.Web.ApiSpec.Schemas.Account
   alias Pleroma.Web.ApiSpec.Schemas.AccountCreateRequest
   alias Pleroma.Web.ApiSpec.Schemas.AccountCreateResponse
+  alias Pleroma.Web.ApiSpec.Schemas.AccountRelationshipsResponse
   alias Pleroma.Web.ApiSpec.Schemas.AccountUpdateCredentialsRequest
 
   @spec open_api_operation(atom) :: Operation.t()
@@ -60,7 +62,27 @@ defmodule Pleroma.Web.ApiSpec.AccountOperation do
   end
 
   def relationships_operation do
-    :ok
+    %Operation{
+      tags: ["accounts"],
+      summary: "Check relationships to other accounts",
+      operationId: "AccountController.relationships",
+      description: "Find out whether a given account is followed, blocked, muted, etc.",
+      security: [%{"oAuth" => ["read:follows"]}],
+      parameters: [
+        Operation.parameter(
+          :id,
+          :query,
+          %Schema{
+            oneOf: [%Schema{type: :array, items: %Schema{type: :string}}, %Schema{type: :string}]
+          },
+          "Account IDs",
+          example: "123"
+        )
+      ],
+      responses: %{
+        200 => Operation.response("Account", "application/json", AccountRelationshipsResponse)
+      }
+    }
   end
 
   def show_operation do
diff --git a/lib/pleroma/web/api_spec/schemas/account_relationship_response.ex b/lib/pleroma/web/api_spec/schemas/account_relationship_response.ex
new file mode 100644 (file)
index 0000000..9974b94
--- /dev/null
@@ -0,0 +1,43 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.ApiSpec.Schemas.AccountRelationshipResponse do
+  alias OpenApiSpex.Schema
+
+  require OpenApiSpex
+
+  OpenApiSpex.schema(%{
+    title: "AccountRelationshipResponse",
+    description: "Response schema for an account relationship",
+    type: :object,
+    properties: %{
+      id: %Schema{type: :string},
+      following: %Schema{type: :boolean},
+      showing_reblogs: %Schema{type: :boolean},
+      followed_by: %Schema{type: :boolean},
+      blocking: %Schema{type: :boolean},
+      blocked_by: %Schema{type: :boolean},
+      muting: %Schema{type: :boolean},
+      muting_notifications: %Schema{type: :boolean},
+      requested: %Schema{type: :boolean},
+      domain_blocking: %Schema{type: :boolean},
+      endorsed: %Schema{type: :boolean}
+    },
+    example: %{
+      "JSON" => %{
+        "id" => "1",
+        "following" => true,
+        "showing_reblogs" => true,
+        "followed_by" => true,
+        "blocking" => false,
+        "blocked_by" => false,
+        "muting" => false,
+        "muting_notifications" => false,
+        "requested" => false,
+        "domain_blocking" => false,
+        "endorsed" => false
+      }
+    }
+  })
+end
diff --git a/lib/pleroma/web/api_spec/schemas/account_relationships_response.ex b/lib/pleroma/web/api_spec/schemas/account_relationships_response.ex
new file mode 100644 (file)
index 0000000..2ca6323
--- /dev/null
@@ -0,0 +1,55 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.ApiSpec.Schemas.AccountRelationshipsResponse do
+  require OpenApiSpex
+
+  OpenApiSpex.schema(%{
+    title: "AccountRelationshipsResponse",
+    description: "Response schema for account relationships",
+    type: :array,
+    items: Pleroma.Web.ApiSpec.Schemas.AccountRelationshipResponse,
+    example: [
+      %{
+        "id" => "1",
+        "following" => true,
+        "showing_reblogs" => true,
+        "followed_by" => true,
+        "blocking" => false,
+        "blocked_by" => true,
+        "muting" => false,
+        "muting_notifications" => false,
+        "requested" => false,
+        "domain_blocking" => false,
+        "endorsed" => true
+      },
+      %{
+        "id" => "2",
+        "following" => true,
+        "showing_reblogs" => true,
+        "followed_by" => true,
+        "blocking" => false,
+        "blocked_by" => true,
+        "muting" => true,
+        "muting_notifications" => false,
+        "requested" => true,
+        "domain_blocking" => false,
+        "endorsed" => false
+      },
+      %{
+        "id" => "3",
+        "following" => true,
+        "showing_reblogs" => true,
+        "followed_by" => true,
+        "blocking" => true,
+        "blocked_by" => false,
+        "muting" => true,
+        "muting_notifications" => false,
+        "requested" => false,
+        "domain_blocking" => true,
+        "endorsed" => false
+      }
+    ]
+  })
+end
index 9c986b3b20c50236242ecdb759695c84f9faa3a7..1652e3a1b4fd0826eb2d20b8f7d1f270acbe262e 100644 (file)
@@ -83,7 +83,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
   plug(
     OpenApiSpex.Plug.CastAndValidate,
     [render_error: Pleroma.Web.ApiSpec.RenderError]
-    when action in [:create, :verify_credentials, :update_credentials]
+    when action in [:create, :verify_credentials, :update_credentials, :relationships]
   )
 
   action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
@@ -229,7 +229,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
   end
 
   @doc "GET /api/v1/accounts/relationships"
-  def relationships(%{assigns: %{user: user}} = conn, %{"id" => id}) do
+  def relationships(%{assigns: %{user: user}} = conn, %{id: id}) do
     targets = User.get_all_by_ids(List.wrap(id))
 
     render(conn, "relationships.json", user: user, targets: targets)
index a540590743d27555fc25a824eec1863630b63fbd..58a38d8affacff44bc276da7216e5042a1c8aefc 100644 (file)
@@ -9,6 +9,7 @@ defmodule Pleroma.Web.ApiSpec.AccountOperationTest do
   alias Pleroma.Web.ApiSpec.Schemas.Account
   alias Pleroma.Web.ApiSpec.Schemas.AccountCreateRequest
   alias Pleroma.Web.ApiSpec.Schemas.AccountCreateResponse
+  alias Pleroma.Web.ApiSpec.Schemas.AccountRelationshipsResponse
   alias Pleroma.Web.ApiSpec.Schemas.AccountUpdateCredentialsRequest
 
   import OpenApiSpex.TestAssertions
@@ -84,4 +85,27 @@ defmodule Pleroma.Web.ApiSpec.AccountOperationTest do
 
     assert_schema(json, "Account", api_spec)
   end
+
+  test "AccountRelationshipsResponse example matches schema" do
+    api_spec = ApiSpec.spec()
+    schema = AccountRelationshipsResponse.schema()
+    assert_schema(schema.example, "AccountRelationshipsResponse", api_spec)
+  end
+
+  test "/api/v1/accounts/relationships produces AccountRelationshipsResponse", %{
+    conn: conn
+  } do
+    token = insert(:oauth_token, scopes: ["read", "write"])
+    other_user = insert(:user)
+    {:ok, _user} = Pleroma.User.follow(token.user, other_user)
+    api_spec = ApiSpec.spec()
+
+    assert [relationship] =
+             conn
+             |> put_req_header("authorization", "Bearer " <> token.token)
+             |> get("/api/v1/accounts/relationships?id=#{other_user.id}")
+             |> json_response(:ok)
+
+    assert_schema([relationship], "AccountRelationshipsResponse", api_spec)
+  end
 end
index 6fe46af3c07e57d9995c7f4e881c9a6ceabeab53..060a7c1cd26522aa22b31637995e77196a1902ca 100644 (file)
@@ -1062,14 +1062,18 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do
     setup do: oauth_access(["read:follows"])
 
     test "returns the relationships for the current user", %{user: user, conn: conn} do
-      other_user = insert(:user)
+      %{id: other_user_id} = other_user = insert(:user)
       {:ok, _user} = User.follow(user, other_user)
 
-      conn = get(conn, "/api/v1/accounts/relationships", %{"id" => [other_user.id]})
-
-      assert [relationship] = json_response(conn, 200)
+      assert [%{"id" => ^other_user_id}] =
+               conn
+               |> get("/api/v1/accounts/relationships?id=#{other_user.id}")
+               |> json_response(200)
 
-      assert to_string(other_user.id) == relationship["id"]
+      assert [%{"id" => ^other_user_id}] =
+               conn
+               |> get("/api/v1/accounts/relationships?id[]=#{other_user.id}")
+               |> json_response(200)
     end
 
     test "returns an empty list on a bad request", %{conn: conn} do