forked from AkkomaGang/akkoma
Add unit test for Pleroma API app controller
This commit is contained in:
parent
d02cf7b0cd
commit
33f063204e
3 changed files with 67 additions and 2 deletions
|
@ -10,11 +10,15 @@ defmodule Pleroma.Web.MastodonAPI.AppController do
|
||||||
|
|
||||||
use Pleroma.Web, :controller
|
use Pleroma.Web, :controller
|
||||||
|
|
||||||
|
alias Pleroma.Maps
|
||||||
|
alias Pleroma.User
|
||||||
alias Pleroma.Repo
|
alias Pleroma.Repo
|
||||||
alias Pleroma.Web.OAuth.App
|
alias Pleroma.Web.OAuth.App
|
||||||
alias Pleroma.Web.OAuth.Scopes
|
alias Pleroma.Web.OAuth.Scopes
|
||||||
alias Pleroma.Web.OAuth.Token
|
alias Pleroma.Web.OAuth.Token
|
||||||
|
|
||||||
|
require Logger
|
||||||
|
|
||||||
action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
|
action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
|
||||||
|
|
||||||
plug(:skip_auth when action in [:create, :verify_credentials])
|
plug(:skip_auth when action in [:create, :verify_credentials])
|
||||||
|
@ -26,13 +30,21 @@ defmodule Pleroma.Web.MastodonAPI.AppController do
|
||||||
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.AppOperation
|
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.AppOperation
|
||||||
|
|
||||||
@doc "POST /api/v1/apps"
|
@doc "POST /api/v1/apps"
|
||||||
def create(%{body_params: params} = conn, _params) do
|
def create(%{assigns: %{user: user}, body_params: params} = conn, _params) do
|
||||||
scopes = Scopes.fetch_scopes(params, ["read"])
|
scopes = Scopes.fetch_scopes(params, ["read"])
|
||||||
|
|
||||||
|
user_id =
|
||||||
|
with %User{id: id} <- user do
|
||||||
|
id
|
||||||
|
else
|
||||||
|
_ -> nil
|
||||||
|
end
|
||||||
|
|
||||||
app_attrs =
|
app_attrs =
|
||||||
params
|
params
|
||||||
|> Map.take([:client_name, :redirect_uris, :website])
|
|> Map.take([:client_name, :redirect_uris, :website])
|
||||||
|> Map.put(:scopes, scopes)
|
|> Map.put(:scopes, scopes)
|
||||||
|
|> Maps.put_if_present(:user_id, user_id)
|
||||||
|
|
||||||
with cs <- App.register_changeset(%App{}, app_attrs),
|
with cs <- App.register_changeset(%App{}, app_attrs),
|
||||||
false <- cs.changes[:client_name] == @local_mastodon_name,
|
false <- cs.changes[:client_name] == @local_mastodon_name,
|
||||||
|
|
|
@ -30,7 +30,7 @@ defmodule Pleroma.Web.OAuth.App do
|
||||||
|
|
||||||
@spec changeset(t(), map()) :: Ecto.Changeset.t()
|
@spec changeset(t(), map()) :: Ecto.Changeset.t()
|
||||||
def changeset(struct, params) do
|
def changeset(struct, params) do
|
||||||
cast(struct, params, [:client_name, :redirect_uris, :scopes, :website, :trusted])
|
cast(struct, params, [:client_name, :redirect_uris, :scopes, :website, :trusted, :user_id])
|
||||||
end
|
end
|
||||||
|
|
||||||
@spec register_changeset(t(), map()) :: Ecto.Changeset.t()
|
@spec register_changeset(t(), map()) :: Ecto.Changeset.t()
|
||||||
|
|
|
@ -0,0 +1,53 @@
|
||||||
|
# Pleroma: A lightweight social networking server
|
||||||
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
||||||
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
defmodule Pleroma.Web.PleromaAPI.AppControllerTest do
|
||||||
|
use Pleroma.Web.ConnCase, async: true
|
||||||
|
|
||||||
|
alias Pleroma.Web.OAuth.App
|
||||||
|
alias Pleroma.Web.Push
|
||||||
|
|
||||||
|
import Pleroma.Factory
|
||||||
|
|
||||||
|
test "apps", %{conn: conn} do
|
||||||
|
user = insert(:user)
|
||||||
|
app_attrs = build(:oauth_app)
|
||||||
|
|
||||||
|
creation =
|
||||||
|
conn
|
||||||
|
|> put_req_header("content-type", "application/json")
|
||||||
|
|> assign(:user, user)
|
||||||
|
|> post("/api/v1/apps", %{
|
||||||
|
client_name: app_attrs.client_name,
|
||||||
|
redirect_uris: app_attrs.redirect_uris
|
||||||
|
})
|
||||||
|
|
||||||
|
[app] = App.get_user_apps(user)
|
||||||
|
|
||||||
|
expected = %{
|
||||||
|
"name" => app.client_name,
|
||||||
|
"website" => app.website,
|
||||||
|
"client_id" => app.client_id,
|
||||||
|
"client_secret" => app.client_secret,
|
||||||
|
"id" => app.id |> to_string(),
|
||||||
|
"redirect_uri" => app.redirect_uris,
|
||||||
|
"vapid_key" => Push.vapid_config() |> Keyword.get(:public_key)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert expected == json_response_and_validate_schema(creation, 200)
|
||||||
|
|
||||||
|
response =
|
||||||
|
conn
|
||||||
|
|> put_req_header("content-type", "application/json")
|
||||||
|
|> assign(:user, user)
|
||||||
|
|> assign(:token, insert(:oauth_token, user: user, scopes: ["read", "follow"]))
|
||||||
|
|> get("/api/v1/pleroma/apps")
|
||||||
|
|> json_response_and_validate_schema(200)
|
||||||
|
|
||||||
|
[apps] = response
|
||||||
|
|
||||||
|
assert length(response) == 1
|
||||||
|
assert apps["client_id"] == app.client_id
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue