Add spec for AccountController.follows

This commit is contained in:
Egor Kislitsyn 2020-04-09 20:12:09 +04:00
parent 68a979b824
commit ab185d3ea4
No known key found for this signature in database
GPG Key ID: 1B49CB15B71E7805
4 changed files with 51 additions and 7 deletions

View File

@ -10,6 +10,7 @@ defmodule Pleroma.Web.ApiSpec.AccountOperation do
alias Pleroma.Web.ApiSpec.Schemas.Account alias Pleroma.Web.ApiSpec.Schemas.Account
alias Pleroma.Web.ApiSpec.Schemas.AccountCreateRequest alias Pleroma.Web.ApiSpec.Schemas.AccountCreateRequest
alias Pleroma.Web.ApiSpec.Schemas.AccountCreateResponse alias Pleroma.Web.ApiSpec.Schemas.AccountCreateResponse
alias Pleroma.Web.ApiSpec.Schemas.AccountFollowsRequest
alias Pleroma.Web.ApiSpec.Schemas.AccountMuteRequest alias Pleroma.Web.ApiSpec.Schemas.AccountMuteRequest
alias Pleroma.Web.ApiSpec.Schemas.AccountRelationship alias Pleroma.Web.ApiSpec.Schemas.AccountRelationship
alias Pleroma.Web.ApiSpec.Schemas.AccountRelationshipsResponse alias Pleroma.Web.ApiSpec.Schemas.AccountRelationshipsResponse
@ -307,7 +308,19 @@ defmodule Pleroma.Web.ApiSpec.AccountOperation do
} }
end end
def follows_operation, do: :ok def follows_operation do
%Operation{
tags: ["accounts"],
summary: "Follows",
operationId: "AccountController.follows",
security: [%{"oAuth" => ["follow", "write:follows"]}],
requestBody: Helpers.request_body("Parameters", AccountFollowsRequest, required: true),
responses: %{
200 => Operation.response("Account", "application/json", Account)
}
}
end
def mutes_operation, do: :ok def mutes_operation, do: :ok
def blocks_operation, do: :ok def blocks_operation, do: :ok
def endorsements_operation, do: :ok def endorsements_operation, do: :ok

View File

@ -0,0 +1,18 @@
# 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.AccountFollowsRequest do
alias OpenApiSpex.Schema
require OpenApiSpex
OpenApiSpex.schema(%{
title: "AccountFollowsRequest",
description: "POST body for muting an account",
type: :object,
properties: %{
uri: %Schema{type: :string}
},
required: [:uri]
})
end

View File

@ -98,7 +98,8 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
:mute, :mute,
:unmute, :unmute,
:block, :block,
:unblock :unblock,
:follows
] ]
) )
@ -401,7 +402,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
end end
@doc "POST /api/v1/follows" @doc "POST /api/v1/follows"
def follows(%{assigns: %{user: follower}} = conn, %{"uri" => uri}) do def follows(%{assigns: %{user: follower}, body_params: %{uri: uri}} = conn, _) do
with {_, %User{} = followed} <- {:followed, User.get_cached_by_nickname(uri)}, with {_, %User{} = followed} <- {:followed, User.get_cached_by_nickname(uri)},
{_, true} <- {:followed, follower.id != followed.id}, {_, true} <- {:followed, follower.id != followed.id},
{:ok, follower, followed, _} <- CommonAPI.follow(follower, followed) do {:ok, follower, followed, _} <- CommonAPI.follow(follower, followed) do

View File

@ -667,11 +667,14 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do
assert %{"id" => _id, "following" => false} = json_response(ret_conn, 200) assert %{"id" => _id, "following" => false} = json_response(ret_conn, 200)
assert_schema(json_response(ret_conn, 200), "AccountRelationship", ApiSpec.spec()) assert_schema(json_response(ret_conn, 200), "AccountRelationship", ApiSpec.spec())
conn = post(conn, "/api/v1/follows", %{"uri" => other_user.nickname}) conn =
conn
|> put_req_header("content-type", "application/json")
|> post("/api/v1/follows", %{"uri" => other_user.nickname})
assert %{"id" => id} = json_response(conn, 200) assert %{"id" => id} = json_response(conn, 200)
assert id == to_string(other_user.id) assert id == to_string(other_user.id)
assert_schema(json_response(conn, 200), "AccountRelationship", ApiSpec.spec()) assert_schema(json_response(conn, 200), "Account", ApiSpec.spec())
end end
test "cancelling follow request", %{conn: conn} do test "cancelling follow request", %{conn: conn} do
@ -728,7 +731,12 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do
# self follow via uri # self follow via uri
user = User.get_cached_by_id(user.id) user = User.get_cached_by_id(user.id)
conn_res = post(conn, "/api/v1/follows", %{"uri" => user.nickname})
conn_res =
conn
|> put_req_header("content-type", "multipart/form-data")
|> post("/api/v1/follows", %{"uri" => user.nickname})
assert %{"error" => "Record not found"} = json_response(conn_res, 404) assert %{"error" => "Record not found"} = json_response(conn_res, 404)
# follow non existing user # follow non existing user
@ -736,7 +744,11 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do
assert %{"error" => "Record not found"} = json_response(conn_res, 404) assert %{"error" => "Record not found"} = json_response(conn_res, 404)
# follow non existing user via uri # follow non existing user via uri
conn_res = post(conn, "/api/v1/follows", %{"uri" => "doesntexist"}) conn_res =
conn
|> put_req_header("content-type", "multipart/form-data")
|> post("/api/v1/follows", %{"uri" => "doesntexist"})
assert %{"error" => "Record not found"} = json_response(conn_res, 404) assert %{"error" => "Record not found"} = json_response(conn_res, 404)
# unfollow non existing user # unfollow non existing user