Add spec for AccountController.verify_credentials
authorEgor Kislitsyn <egor@kislitsyn.com>
Sun, 5 Apr 2020 20:15:37 +0000 (00:15 +0400)
committerEgor Kislitsyn <egor@kislitsyn.com>
Mon, 13 Apr 2020 13:38:59 +0000 (17:38 +0400)
lib/pleroma/web/api_spec.ex
lib/pleroma/web/api_spec/operations/account_operation.ex
lib/pleroma/web/api_spec/operations/app_operation.ex
lib/pleroma/web/api_spec/render_error.ex
lib/pleroma/web/api_spec/schemas/account.ex [new file with mode: 0644]
lib/pleroma/web/api_spec/schemas/account_emoji.ex [new file with mode: 0644]
lib/pleroma/web/api_spec/schemas/account_field.ex [new file with mode: 0644]
test/web/api_spec/account_operation_test.exs

index 41e48a0850a5bcd48cdd77e0aba92fd64e68d146..c85fe30d1888cac4bb2983b846fd90646f7fb1d2 100644 (file)
@@ -31,7 +31,7 @@ defmodule Pleroma.Web.ApiSpec do
               password: %OpenApiSpex.OAuthFlow{
                 authorizationUrl: "/oauth/authorize",
                 tokenUrl: "/oauth/token",
-                scopes: %{"read" => "read"}
+                scopes: %{"read" => "read", "write" => "write"}
               }
             }
           }
index 9085f1af1b2ab460ba59d5eddd4e761d3d7e85ef..3d2270c29007932eaf36091564e5149dea2e4876 100644 (file)
@@ -4,9 +4,10 @@
 
 defmodule Pleroma.Web.ApiSpec.AccountOperation do
   alias OpenApiSpex.Operation
+  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.Helpers
 
   @spec open_api_operation(atom) :: Operation.t()
   def open_api_operation(action) do
@@ -30,7 +31,16 @@ defmodule Pleroma.Web.ApiSpec.AccountOperation do
   end
 
   def verify_credentials_operation do
-    :ok
+    %Operation{
+      tags: ["accounts"],
+      description: "Test to make sure that the user token works.",
+      summary: "Verify account credentials",
+      operationId: "AccountController.verify_credentials",
+      security: [%{"oAuth" => ["read:accounts"]}],
+      responses: %{
+        200 => Operation.response("Account", "application/json", Account)
+      }
+    }
   end
 
   def update_credentials_operation do
index 26d8dbd421a7331367ad73fca00587971ee44923..935215c644d09bf9d0a3b1bd8d3f70c1733f85c4 100644 (file)
@@ -51,11 +51,7 @@ defmodule Pleroma.Web.ApiSpec.AppOperation do
       summary: "Verify your app works",
       description: "Confirm that the app's OAuth2 credentials work.",
       operationId: "AppController.verify_credentials",
-      security: [
-        %{
-          "oAuth" => ["read"]
-        }
-      ],
+      security: [%{"oAuth" => ["read"]}],
       responses: %{
         200 =>
           Operation.response("App", "application/json", %Schema{
index e063d115b9373bdf8edbdcec42b56f9696049e61..9184c43b6afb89f9ca2902acfcf69c874adb1bcb 100644 (file)
@@ -5,8 +5,8 @@
 defmodule Pleroma.Web.ApiSpec.RenderError do
   @behaviour Plug
 
-  alias Plug.Conn
   alias OpenApiSpex.Plug.JsonRenderError
+  alias Plug.Conn
 
   @impl Plug
   def init(opts), do: opts
diff --git a/lib/pleroma/web/api_spec/schemas/account.ex b/lib/pleroma/web/api_spec/schemas/account.ex
new file mode 100644 (file)
index 0000000..59c4ac4
--- /dev/null
@@ -0,0 +1,181 @@
+# 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.Account do
+  alias OpenApiSpex.Schema
+  alias Pleroma.Web.ApiSpec.Schemas.AccountEmoji
+  alias Pleroma.Web.ApiSpec.Schemas.AccountField
+
+  require OpenApiSpex
+
+  OpenApiSpex.schema(%{
+    title: "Account",
+    description: "Response schema for an account",
+    type: :object,
+    properties: %{
+      acct: %Schema{type: :string},
+      avatar_static: %Schema{type: :string},
+      avatar: %Schema{type: :string},
+      bot: %Schema{type: :boolean},
+      created_at: %Schema{type: :string, format: "date-time"},
+      display_name: %Schema{type: :string},
+      emojis: %Schema{type: :array, items: AccountEmoji},
+      fields: %Schema{type: :array, items: AccountField},
+      follow_requests_count: %Schema{type: :integer},
+      followers_count: %Schema{type: :integer},
+      following_count: %Schema{type: :integer},
+      header_static: %Schema{type: :string},
+      header: %Schema{type: :string},
+      id: %Schema{type: :string},
+      locked: %Schema{type: :boolean},
+      note: %Schema{type: :string},
+      statuses_count: %Schema{type: :integer},
+      url: %Schema{type: :string},
+      username: %Schema{type: :string},
+      pleroma: %Schema{
+        type: :object,
+        properties: %{
+          allow_following_move: %Schema{type: :boolean},
+          background_image: %Schema{type: :boolean, nullable: true},
+          chat_token: %Schema{type: :string},
+          confirmation_pending: %Schema{type: :boolean},
+          hide_favorites: %Schema{type: :boolean},
+          hide_followers_count: %Schema{type: :boolean},
+          hide_followers: %Schema{type: :boolean},
+          hide_follows_count: %Schema{type: :boolean},
+          hide_follows: %Schema{type: :boolean},
+          is_admin: %Schema{type: :boolean},
+          is_moderator: %Schema{type: :boolean},
+          skip_thread_containment: %Schema{type: :boolean},
+          tags: %Schema{type: :array, items: %Schema{type: :string}},
+          unread_conversation_count: %Schema{type: :integer},
+          notification_settings: %Schema{
+            type: :object,
+            properties: %{
+              followers: %Schema{type: :boolean},
+              follows: %Schema{type: :boolean},
+              non_followers: %Schema{type: :boolean},
+              non_follows: %Schema{type: :boolean},
+              privacy_option: %Schema{type: :boolean}
+            }
+          },
+          relationship: %Schema{
+            type: :object,
+            properties: %{
+              blocked_by: %Schema{type: :boolean},
+              blocking: %Schema{type: :boolean},
+              domain_blocking: %Schema{type: :boolean},
+              endorsed: %Schema{type: :boolean},
+              followed_by: %Schema{type: :boolean},
+              following: %Schema{type: :boolean},
+              id: %Schema{type: :string},
+              muting: %Schema{type: :boolean},
+              muting_notifications: %Schema{type: :boolean},
+              requested: %Schema{type: :boolean},
+              showing_reblogs: %Schema{type: :boolean},
+              subscribing: %Schema{type: :boolean}
+            }
+          },
+          settings_store: %Schema{
+            type: :object
+          }
+        }
+      },
+      source: %Schema{
+        type: :object,
+        properties: %{
+          fields: %Schema{type: :array, items: AccountField},
+          note: %Schema{type: :string},
+          privacy: %Schema{type: :string},
+          sensitive: %Schema{type: :boolean},
+          pleroma: %Schema{
+            type: :object,
+            properties: %{
+              actor_type: %Schema{type: :string},
+              discoverable: %Schema{type: :boolean},
+              no_rich_text: %Schema{type: :boolean},
+              show_role: %Schema{type: :boolean}
+            }
+          }
+        }
+      }
+    },
+    example: %{
+      "JSON" => %{
+        "acct" => "foobar",
+        "avatar" => "https://mypleroma.com/images/avi.png",
+        "avatar_static" => "https://mypleroma.com/images/avi.png",
+        "bot" => false,
+        "created_at" => "2020-03-24T13:05:58.000Z",
+        "display_name" => "foobar",
+        "emojis" => [],
+        "fields" => [],
+        "follow_requests_count" => 0,
+        "followers_count" => 0,
+        "following_count" => 1,
+        "header" => "https://mypleroma.com/images/banner.png",
+        "header_static" => "https://mypleroma.com/images/banner.png",
+        "id" => "9tKi3esbG7OQgZ2920",
+        "locked" => false,
+        "note" => "cofe",
+        "pleroma" => %{
+          "allow_following_move" => true,
+          "background_image" => nil,
+          "confirmation_pending" => true,
+          "hide_favorites" => true,
+          "hide_followers" => false,
+          "hide_followers_count" => false,
+          "hide_follows" => false,
+          "hide_follows_count" => false,
+          "is_admin" => false,
+          "is_moderator" => false,
+          "skip_thread_containment" => false,
+          "chat_token" =>
+            "SFMyNTY.g3QAAAACZAAEZGF0YW0AAAASOXRLaTNlc2JHN09RZ1oyOTIwZAAGc2lnbmVkbgYARNplS3EB.Mb_Iaqew2bN1I1o79B_iP7encmVCpTKC4OtHZRxdjKc",
+          "unread_conversation_count" => 0,
+          "tags" => [],
+          "notification_settings" => %{
+            "followers" => true,
+            "follows" => true,
+            "non_followers" => true,
+            "non_follows" => true,
+            "privacy_option" => false
+          },
+          "relationship" => %{
+            "blocked_by" => false,
+            "blocking" => false,
+            "domain_blocking" => false,
+            "endorsed" => false,
+            "followed_by" => false,
+            "following" => false,
+            "id" => "9tKi3esbG7OQgZ2920",
+            "muting" => false,
+            "muting_notifications" => false,
+            "requested" => false,
+            "showing_reblogs" => true,
+            "subscribing" => false
+          },
+          "settings_store" => %{
+            "pleroma-fe" => %{}
+          }
+        },
+        "source" => %{
+          "fields" => [],
+          "note" => "foobar",
+          "pleroma" => %{
+            "actor_type" => "Person",
+            "discoverable" => false,
+            "no_rich_text" => false,
+            "show_role" => true
+          },
+          "privacy" => "public",
+          "sensitive" => false
+        },
+        "statuses_count" => 0,
+        "url" => "https://mypleroma.com/users/foobar",
+        "username" => "foobar"
+      }
+    }
+  })
+end
diff --git a/lib/pleroma/web/api_spec/schemas/account_emoji.ex b/lib/pleroma/web/api_spec/schemas/account_emoji.ex
new file mode 100644 (file)
index 0000000..403b13b
--- /dev/null
@@ -0,0 +1,31 @@
+# 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.AccountEmoji do
+  alias OpenApiSpex.Schema
+
+  require OpenApiSpex
+
+  OpenApiSpex.schema(%{
+    title: "AccountEmoji",
+    description: "Response schema for account custom fields",
+    type: :object,
+    properties: %{
+      shortcode: %Schema{type: :string},
+      url: %Schema{type: :string},
+      static_url: %Schema{type: :string},
+      visible_in_picker: %Schema{type: :boolean}
+    },
+    example: %{
+      "JSON" => %{
+        "shortcode" => "fatyoshi",
+        "url" =>
+          "https://files.mastodon.social/custom_emojis/images/000/023/920/original/e57ecb623faa0dc9.png",
+        "static_url" =>
+          "https://files.mastodon.social/custom_emojis/images/000/023/920/static/e57ecb623faa0dc9.png",
+        "visible_in_picker" => true
+      }
+    }
+  })
+end
diff --git a/lib/pleroma/web/api_spec/schemas/account_field.ex b/lib/pleroma/web/api_spec/schemas/account_field.ex
new file mode 100644 (file)
index 0000000..8906d81
--- /dev/null
@@ -0,0 +1,28 @@
+# 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.AccountField do
+  alias OpenApiSpex.Schema
+
+  require OpenApiSpex
+
+  OpenApiSpex.schema(%{
+    title: "AccountField",
+    description: "Response schema for account custom fields",
+    type: :object,
+    properties: %{
+      name: %Schema{type: :string},
+      value: %Schema{type: :string},
+      verified_at: %Schema{type: :string, format: "date-time", nullable: true}
+    },
+    example: %{
+      "JSON" => %{
+        "name" => "Website",
+        "value" =>
+          "<a href=\"https://pleroma.com\" rel=\"me nofollow noopener noreferrer\" target=\"_blank\"><span class=\"invisible\">https://</span><span class=\"\">pleroma.com</span><span class=\"invisible\"></span></a>",
+        "verified_at" => "2019-08-29T04:14:55.571+00:00"
+      }
+    }
+  })
+end
index 4f8d04698a22e35a3bd600c03b777af68464d304..37501b8cc71284a1b028cff4d234e62ecf3843ce 100644 (file)
@@ -6,12 +6,19 @@ defmodule Pleroma.Web.ApiSpec.AccountOperationTest do
   use Pleroma.Web.ConnCase, async: true
 
   alias Pleroma.Web.ApiSpec
+  alias Pleroma.Web.ApiSpec.Schemas.Account
   alias Pleroma.Web.ApiSpec.Schemas.AccountCreateRequest
   alias Pleroma.Web.ApiSpec.Schemas.AccountCreateResponse
 
   import OpenApiSpex.TestAssertions
   import Pleroma.Factory
 
+  test "Account example matches schema" do
+    api_spec = ApiSpec.spec()
+    schema = Account.schema()
+    assert_schema(schema.example, "Account", api_spec)
+  end
+
   test "AccountCreateRequest example matches schema" do
     api_spec = ApiSpec.spec()
     schema = AccountCreateRequest.schema()