Add spec for AccountController.relationships

This commit is contained in:
Egor Kislitsyn 2020-04-07 18:29:05 +04:00
parent ab400b2ddb
commit d7d6a83233
No known key found for this signature in database
GPG key ID: 1B49CB15B71E7805
6 changed files with 156 additions and 8 deletions

View 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

View file

@ -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

View file

@ -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

View 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)

View 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

View 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 [%{"id" => ^other_user_id}] =
conn
|> get("/api/v1/accounts/relationships?id=#{other_user.id}")
|> json_response(200)
assert [relationship] = json_response(conn, 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