Merge branch 'admin_api_invite_tokens' into 'develop'

POST for creating invite tokens in admin api

See merge request pleroma/pleroma!1637
This commit is contained in:
feld 2019-09-18 21:11:37 +00:00
commit 25b4365656
5 changed files with 54 additions and 44 deletions

View file

@ -25,7 +25,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- **Breaking:** Configuration: A setting to explicitly disable the mailer was added, defaulting to true, if you are using a mailer add `config :pleroma, Pleroma.Emails.Mailer, enabled: true` to your config - **Breaking:** Configuration: A setting to explicitly disable the mailer was added, defaulting to true, if you are using a mailer add `config :pleroma, Pleroma.Emails.Mailer, enabled: true` to your config
- **Breaking:** Configuration: `/media/` is now removed when `base_url` is configured, append `/media/` to your `base_url` config to keep the old behaviour if desired - **Breaking:** Configuration: `/media/` is now removed when `base_url` is configured, append `/media/` to your `base_url` config to keep the old behaviour if desired
- **Breaking:** `/api/pleroma/notifications/read` is moved to `/api/v1/pleroma/notifications/read` and now supports `max_id` and responds with Mastodon API entities. - **Breaking:** `/api/pleroma/notifications/read` is moved to `/api/v1/pleroma/notifications/read` and now supports `max_id` and responds with Mastodon API entities.
- **Breaking:** `/api/pleroma/admin/users/invite_token` now uses `POST`, changed accepted params and returns full invite in json instead of only token string.
- Configuration: added `config/description.exs`, from which `docs/config.md` is generated - Configuration: added `config/description.exs`, from which `docs/config.md` is generated
- Configuration: OpenGraph and TwitterCard providers enabled by default
- Configuration: Filter.AnonymizeFilename added ability to retain file extension with custom text
- Mastodon API: `pleroma.thread_muted` key in the Status entity
- Federation: Return 403 errors when trying to request pages from a user's follower/following collections if they have `hide_followers`/`hide_follows` set - Federation: Return 403 errors when trying to request pages from a user's follower/following collections if they have `hide_followers`/`hide_follows` set
- NodeInfo: Return `skipThreadContainment` in `metadata` for the `skip_thread_containment` option - NodeInfo: Return `skipThreadContainment` in `metadata` for the `skip_thread_containment` option
- NodeInfo: Return `mailerEnabled` in `metadata` - NodeInfo: Return `mailerEnabled` in `metadata`

View file

@ -224,15 +224,25 @@ Note: Available `:permission_group` is currently moderator and admin. 404 is ret
## `/api/pleroma/admin/users/invite_token` ## `/api/pleroma/admin/users/invite_token`
### Get an account registration invite token ### Create an account registration invite token
- Methods: `GET` - Methods: `POST`
- Params: - Params:
- *optional* `invite` => [ - *optional* `max_use` (integer)
- *optional* `max_use` (integer) - *optional* `expires_at` (date string e.g. "2019-04-07")
- *optional* `expires_at` (date string e.g. "2019-04-07") - Response:
]
- Response: invite token (base64 string) ```json
{
"id": integer,
"token": string,
"used": boolean,
"expires_at": date,
"uses": integer,
"max_use": integer,
"invite_type": string (possible values: `one_time`, `reusable`, `date_limited`, `reusable_date_limited`)
}
```
## `/api/pleroma/admin/users/invites` ## `/api/pleroma/admin/users/invites`

View file

@ -400,13 +400,23 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
end end
end end
@doc "Get a account registeration invite token (base64 string)" @doc "Create an account registration invite token"
def get_invite_token(conn, params) do def create_invite_token(conn, params) do
options = params["invite"] || %{} opts = %{}
{:ok, invite} = UserInviteToken.create_invite(options)
conn opts =
|> json(invite.token) if params["max_use"],
do: Map.put(opts, :max_use, params["max_use"]),
else: opts
opts =
if params["expires_at"],
do: Map.put(opts, :expires_at, params["expires_at"]),
else: opts
{:ok, invite} = UserInviteToken.create_invite(opts)
json(conn, AccountView.render("invite.json", %{invite: invite}))
end end
@doc "Get list of created invites" @doc "Get list of created invites"

View file

@ -180,7 +180,7 @@ defmodule Pleroma.Web.Router do
post("/relay", AdminAPIController, :relay_follow) post("/relay", AdminAPIController, :relay_follow)
delete("/relay", AdminAPIController, :relay_unfollow) delete("/relay", AdminAPIController, :relay_unfollow)
get("/users/invite_token", AdminAPIController, :get_invite_token) post("/users/invite_token", AdminAPIController, :create_invite_token)
get("/users/invites", AdminAPIController, :invites) get("/users/invites", AdminAPIController, :invites)
post("/users/revoke_invite", AdminAPIController, :revoke_invite) post("/users/revoke_invite", AdminAPIController, :revoke_invite)
post("/users/email_invite", AdminAPIController, :email_invite) post("/users/email_invite", AdminAPIController, :email_invite)

View file

@ -574,18 +574,6 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
end end
end end
test "/api/pleroma/admin/users/invite_token" do
admin = insert(:user, info: %{is_admin: true})
conn =
build_conn()
|> assign(:user, admin)
|> put_req_header("accept", "application/json")
|> get("/api/pleroma/admin/users/invite_token")
assert conn.status == 200
end
test "/api/pleroma/admin/users/:nickname/password_reset" do test "/api/pleroma/admin/users/:nickname/password_reset" do
admin = insert(:user, info: %{is_admin: true}) admin = insert(:user, info: %{is_admin: true})
user = insert(:user) user = insert(:user)
@ -1064,7 +1052,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
"@#{admin.nickname} deactivated user @#{user.nickname}" "@#{admin.nickname} deactivated user @#{user.nickname}"
end end
describe "GET /api/pleroma/admin/users/invite_token" do describe "POST /api/pleroma/admin/users/invite_token" do
setup do setup do
admin = insert(:user, info: %{is_admin: true}) admin = insert(:user, info: %{is_admin: true})
@ -1076,10 +1064,10 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
end end
test "without options", %{conn: conn} do test "without options", %{conn: conn} do
conn = get(conn, "/api/pleroma/admin/users/invite_token") conn = post(conn, "/api/pleroma/admin/users/invite_token")
token = json_response(conn, 200) invite_json = json_response(conn, 200)
invite = UserInviteToken.find_by_token!(token) invite = UserInviteToken.find_by_token!(invite_json["token"])
refute invite.used refute invite.used
refute invite.expires_at refute invite.expires_at
refute invite.max_use refute invite.max_use
@ -1088,12 +1076,12 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
test "with expires_at", %{conn: conn} do test "with expires_at", %{conn: conn} do
conn = conn =
get(conn, "/api/pleroma/admin/users/invite_token", %{ post(conn, "/api/pleroma/admin/users/invite_token", %{
"invite" => %{"expires_at" => Date.to_string(Date.utc_today())} "expires_at" => Date.to_string(Date.utc_today())
}) })
token = json_response(conn, 200) invite_json = json_response(conn, 200)
invite = UserInviteToken.find_by_token!(token) invite = UserInviteToken.find_by_token!(invite_json["token"])
refute invite.used refute invite.used
assert invite.expires_at == Date.utc_today() assert invite.expires_at == Date.utc_today()
@ -1102,13 +1090,10 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
end end
test "with max_use", %{conn: conn} do test "with max_use", %{conn: conn} do
conn = conn = post(conn, "/api/pleroma/admin/users/invite_token", %{"max_use" => 150})
get(conn, "/api/pleroma/admin/users/invite_token", %{
"invite" => %{"max_use" => 150}
})
token = json_response(conn, 200) invite_json = json_response(conn, 200)
invite = UserInviteToken.find_by_token!(token) invite = UserInviteToken.find_by_token!(invite_json["token"])
refute invite.used refute invite.used
refute invite.expires_at refute invite.expires_at
assert invite.max_use == 150 assert invite.max_use == 150
@ -1117,12 +1102,13 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
test "with max use and expires_at", %{conn: conn} do test "with max use and expires_at", %{conn: conn} do
conn = conn =
get(conn, "/api/pleroma/admin/users/invite_token", %{ post(conn, "/api/pleroma/admin/users/invite_token", %{
"invite" => %{"max_use" => 150, "expires_at" => Date.to_string(Date.utc_today())} "max_use" => 150,
"expires_at" => Date.to_string(Date.utc_today())
}) })
token = json_response(conn, 200) invite_json = json_response(conn, 200)
invite = UserInviteToken.find_by_token!(token) invite = UserInviteToken.find_by_token!(invite_json["token"])
refute invite.used refute invite.used
assert invite.expires_at == Date.utc_today() assert invite.expires_at == Date.utc_today()
assert invite.max_use == 150 assert invite.max_use == 150