Support OAuth App updating the website URL
This commit is contained in:
parent
e3a7c1d906
commit
751d63d4bb
@ -33,9 +33,10 @@ defmodule Pleroma.Web.MastodonAPI.AppController do
|
|||||||
app_attrs =
|
app_attrs =
|
||||||
params
|
params
|
||||||
|> Map.take([:client_name, :redirect_uris, :website])
|
|> Map.take([:client_name, :redirect_uris, :website])
|
||||||
|
|> Map.put(:scopes, scopes)
|
||||||
|> Maps.put_if_present(:user_id, user_id)
|
|> Maps.put_if_present(:user_id, user_id)
|
||||||
|
|
||||||
with {:ok, app} <- App.get_or_make(app_attrs, scopes) do
|
with {:ok, app} <- App.get_or_make(app_attrs) do
|
||||||
render(conn, "show.json", app: app)
|
render(conn, "show.json", app: app)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -67,35 +67,27 @@ defmodule Pleroma.Web.OAuth.App do
|
|||||||
with %__MODULE__{} = app <- Repo.get(__MODULE__, id) do
|
with %__MODULE__{} = app <- Repo.get(__MODULE__, id) do
|
||||||
app
|
app
|
||||||
|> changeset(params)
|
|> changeset(params)
|
||||||
|
|> validate_required([:scopes])
|
||||||
|> Repo.update()
|
|> Repo.update()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Gets app by attrs or create new with attrs.
|
Gets app by attrs or create new with attrs.
|
||||||
And updates the scopes if need.
|
Updates the attrs if needed.
|
||||||
"""
|
"""
|
||||||
@spec get_or_make(map(), list(String.t())) :: {:ok, t()} | {:error, Ecto.Changeset.t()}
|
@spec get_or_make(map()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}
|
||||||
def get_or_make(attrs, scopes) do
|
def get_or_make(attrs) do
|
||||||
with %__MODULE__{} = app <- Repo.get_by(__MODULE__, attrs) do
|
with %__MODULE__{} = app <- Repo.get_by(__MODULE__, client_name: attrs.client_name) do
|
||||||
update_scopes(app, scopes)
|
__MODULE__.update(app.id, Map.take(attrs, [:scopes, :website]))
|
||||||
else
|
else
|
||||||
_e ->
|
_e ->
|
||||||
%__MODULE__{}
|
%__MODULE__{}
|
||||||
|> register_changeset(Map.put(attrs, :scopes, scopes))
|
|> register_changeset(attrs)
|
||||||
|> Repo.insert()
|
|> Repo.insert()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp update_scopes(%__MODULE__{} = app, []), do: {:ok, app}
|
|
||||||
defp update_scopes(%__MODULE__{scopes: scopes} = app, scopes), do: {:ok, app}
|
|
||||||
|
|
||||||
defp update_scopes(%__MODULE__{} = app, scopes) do
|
|
||||||
app
|
|
||||||
|> change(%{scopes: scopes})
|
|
||||||
|> Repo.update()
|
|
||||||
end
|
|
||||||
|
|
||||||
@spec search(map()) :: {:ok, [t()], non_neg_integer()}
|
@spec search(map()) :: {:ok, [t()], non_neg_integer()}
|
||||||
def search(params) do
|
def search(params) do
|
||||||
query = from(a in __MODULE__)
|
query = from(a in __MODULE__)
|
||||||
|
@ -169,4 +169,34 @@ defmodule Pleroma.Web.MastodonAPI.AppControllerTest do
|
|||||||
|
|
||||||
assert List.first(Repo.all(App)).scopes == String.split(updated_scopes, " ")
|
assert List.first(Repo.all(App)).scopes == String.split(updated_scopes, " ")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "app website URL can be updated", %{conn: conn} do
|
||||||
|
client_name = "BleromaSE"
|
||||||
|
redirect_uris = "https://bleroma.app/oauth-callback"
|
||||||
|
website = "https://bleromase.com"
|
||||||
|
|
||||||
|
conn
|
||||||
|
|> put_req_header("content-type", "application/json")
|
||||||
|
|> post("/api/v1/apps", %{
|
||||||
|
client_name: client_name,
|
||||||
|
redirect_uris: redirect_uris,
|
||||||
|
website: website
|
||||||
|
})
|
||||||
|
|> json_response_and_validate_schema(200)
|
||||||
|
|
||||||
|
assert List.first(Repo.all(App)).website == website
|
||||||
|
|
||||||
|
updated_website = "https://bleromase2ultimateedition.com"
|
||||||
|
|
||||||
|
conn
|
||||||
|
|> put_req_header("content-type", "application/json")
|
||||||
|
|> post("/api/v1/apps", %{
|
||||||
|
client_name: client_name,
|
||||||
|
redirect_uris: redirect_uris,
|
||||||
|
website: updated_website
|
||||||
|
})
|
||||||
|
|> json_response_and_validate_schema(200)
|
||||||
|
|
||||||
|
assert List.first(Repo.all(App)).website == updated_website
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -12,20 +12,23 @@ defmodule Pleroma.Web.OAuth.AppTest do
|
|||||||
test "gets exist app" do
|
test "gets exist app" do
|
||||||
attrs = %{client_name: "Mastodon-Local", redirect_uris: "."}
|
attrs = %{client_name: "Mastodon-Local", redirect_uris: "."}
|
||||||
app = insert(:oauth_app, Map.merge(attrs, %{scopes: ["read", "write"]}))
|
app = insert(:oauth_app, Map.merge(attrs, %{scopes: ["read", "write"]}))
|
||||||
{:ok, %App{} = exist_app} = App.get_or_make(attrs, [])
|
{:ok, %App{} = exist_app} = App.get_or_make(attrs)
|
||||||
assert exist_app == app
|
assert exist_app == app
|
||||||
end
|
end
|
||||||
|
|
||||||
test "make app" do
|
test "make app" do
|
||||||
attrs = %{client_name: "Mastodon-Local", redirect_uris: "."}
|
attrs = %{client_name: "Mastodon-Local", redirect_uris: ".", scopes: ["write"]}
|
||||||
{:ok, %App{} = app} = App.get_or_make(attrs, ["write"])
|
{:ok, %App{} = app} = App.get_or_make(attrs)
|
||||||
assert app.scopes == ["write"]
|
assert app.scopes == ["write"]
|
||||||
end
|
end
|
||||||
|
|
||||||
test "gets exist app and updates scopes" do
|
test "gets exist app and updates scopes" do
|
||||||
attrs = %{client_name: "Mastodon-Local", redirect_uris: "."}
|
attrs = %{client_name: "Mastodon-Local", redirect_uris: ".", scopes: ["read", "write"]}
|
||||||
app = insert(:oauth_app, Map.merge(attrs, %{scopes: ["read", "write"]}))
|
app = insert(:oauth_app, attrs)
|
||||||
{:ok, %App{} = exist_app} = App.get_or_make(attrs, ["read", "write", "follow", "push"])
|
|
||||||
|
{:ok, %App{} = exist_app} =
|
||||||
|
App.get_or_make(%{attrs | scopes: ["read", "write", "follow", "push"]})
|
||||||
|
|
||||||
assert exist_app.id == app.id
|
assert exist_app.id == app.id
|
||||||
assert exist_app.scopes == ["read", "write", "follow", "push"]
|
assert exist_app.scopes == ["read", "write", "follow", "push"]
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user