- Params:
- *optional* `invite` => [
- *optional* `max_use` (integer)
- - *optional* `expire_at` (date string e.g. "2019-04-07")
+ - *optional* `expires_at` (date string e.g. "2019-04-07")
]
- Response: invite token (base64 string)
"id": integer,
"token": string,
"used": boolean,
- "expire_at": date,
+ "expires_at": date,
"uses": integer,
"max_use": integer,
"invite_type": string (possible values: `one_time`, `reusable`, `date_limited`, `reusable_date_limited`)
"id": integer,
"token": string,
"used": boolean,
- "expire_at": date,
+ "expires_at": date,
"uses": integer,
"max_use": integer,
"invite_type": string (possible values: `one_time`, `reusable`, `date_limited`, `reusable_date_limited`)
mix pleroma.user invite [OPTION...]
Options:
- - `--expire_at DATE` - last day on which token is active (e.g. "2019-04-05")
+ - `--expires_at DATE` - last day on which token is active (e.g. "2019-04-05")
- `--max_use NUMBER` - maximum numbers of token uses
## List generated invites
{options, [], []} =
OptionParser.parse(rest,
strict: [
- expire_at: :string,
+ expires_at: :string,
max_use: :integer
]
)
options =
options
- |> Keyword.update(:expire_at, {:ok, nil}, fn
+ |> Keyword.update(:expires_at, {:ok, nil}, fn
nil -> {:ok, nil}
val -> Date.from_iso8601(val)
end)
Common.start_pleroma()
- with {:ok, val} <- options[:expire_at],
- options = Map.put(options, :expire_at, val),
+ with {:ok, val} <- options[:expires_at],
+ options = Map.put(options, :expires_at, val),
{:ok, invite} <- UserInviteToken.create_invite(options) do
Mix.shell().info(
"Generated user invite token " <> String.replace(invite.invite_type, "_", " ")
UserInviteToken.list_invites()
|> Enum.each(fn invite ->
expire_info =
- with expire_at when not is_nil(expire_at) <- invite.expire_at do
- " | Expire at: #{Date.to_string(expire_at)}"
+ with expires_at when not is_nil(expires_at) <- invite.expires_at do
+ " | Expires at: #{Date.to_string(expires_at)}"
end
using_info =
field(:token, :string)
field(:used, :boolean, default: false)
field(:max_use, :integer)
- field(:expire_at, :date)
+ field(:expires_at, :date)
field(:uses, :integer, default: 0)
field(:invite_type, :string)
@spec create_invite(map()) :: UserInviteToken.t()
def create_invite(params \\ %{}) do
%UserInviteToken{}
- |> cast(params, ~w(max_use expire_at)a)
+ |> cast(params, [:max_use, :expires_at])
|> add_token()
|> assign_type()
|> Repo.insert()
put_change(changeset, :token, token)
end
- defp assign_type(%{changes: %{max_use: _max_use, expire_at: _expire_at}} = changeset) do
+ defp assign_type(%{changes: %{max_use: _max_use, expires_at: _expires_at}} = changeset) do
put_change(changeset, :invite_type, "reusable_date_limited")
end
- defp assign_type(%{changes: %{expire_at: _expire_at}} = changeset) do
+ defp assign_type(%{changes: %{expires_at: _expires_at}} = changeset) do
put_change(changeset, :invite_type, "date_limited")
end
not_overdue_date?(invite) and invite.uses < invite.max_use and not invite.used
end
- defp not_overdue_date?(%{expire_at: expire_at} = invite) do
- Date.compare(Date.utc_today(), expire_at) in [:lt, :eq] ||
+ defp not_overdue_date?(%{expires_at: expires_at} = invite) do
+ Date.compare(Date.utc_today(), expires_at) in [:lt, :eq] ||
(update_invite!(invite, %{used: true}) && false)
end
"id" => invite.id,
"token" => invite.token,
"used" => invite.used,
- "expire_at" => invite.expire_at,
+ "expires_at" => invite.expires_at,
"uses" => invite.uses,
"max_use" => invite.max_use,
"invite_type" => invite.invite_type
def change do
alter table(:user_invite_tokens) do
- add(:expire_at, :date)
+ add(:expires_at, :date)
add(:uses, :integer, default: 0)
add(:max_use, :integer)
add(:invite_type, :string, default: "one_time")
assert message =~ "Generated user invite token one time"
end
- test "token is generated with expire_at" do
+ test "token is generated with expires_at" do
assert capture_io(fn ->
Mix.Tasks.Pleroma.User.run([
"invite",
- "--expire-at",
+ "--expires-at",
Date.to_string(Date.utc_today())
])
end)
assert message =~ "Generated user invite token reusable"
end
- test "token is generated with max use and expire date" do
+ test "token is generated with max use and expires date" do
assert capture_io(fn ->
Mix.Tasks.Pleroma.User.run([
"invite",
"--max-use",
"5",
- "--expire-at",
+ "--expires-at",
Date.to_string(Date.utc_today())
])
end)
{:ok, invite} = Pleroma.UserInviteToken.create_invite()
{:ok, invite2} =
- Pleroma.UserInviteToken.create_invite(%{expire_at: Date.utc_today(), max_use: 15})
+ Pleroma.UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 15})
# assert capture_io(fn ->
Mix.Tasks.Pleroma.User.run([
describe "running revoke_invite" do
test "invite is revoked" do
- {:ok, invite} = Pleroma.UserInviteToken.create_invite(%{expire_at: Date.utc_today()})
+ {:ok, invite} = Pleroma.UserInviteToken.create_invite(%{expires_at: Date.utc_today()})
assert capture_io(fn ->
Mix.Tasks.Pleroma.User.run([
token = json_response(conn, 200)
invite = UserInviteToken.find_by_token!(token)
refute invite.used
- refute invite.expire_at
+ refute invite.expires_at
refute invite.max_use
assert invite.invite_type == "one_time"
end
- test "with expire_at" do
+ test "with expires_at" do
admin = insert(:user, info: %{is_admin: true})
conn =
build_conn()
|> assign(:user, admin)
|> get("/api/pleroma/admin/invite_token", %{
- "invite" => %{"expire_at" => Date.to_string(Date.utc_today())}
+ "invite" => %{"expires_at" => Date.to_string(Date.utc_today())}
})
token = json_response(conn, 200)
invite = UserInviteToken.find_by_token!(token)
refute invite.used
- assert invite.expire_at == Date.utc_today()
+ assert invite.expires_at == Date.utc_today()
refute invite.max_use
assert invite.invite_type == "date_limited"
end
token = json_response(conn, 200)
invite = UserInviteToken.find_by_token!(token)
refute invite.used
- refute invite.expire_at
+ refute invite.expires_at
assert invite.max_use == 150
assert invite.invite_type == "reusable"
end
- test "with max use and expire_at" do
+ test "with max use and expires_at" do
admin = insert(:user, info: %{is_admin: true})
conn =
build_conn()
|> assign(:user, admin)
|> get("/api/pleroma/admin/invite_token", %{
- "invite" => %{"max_use" => 150, "expire_at" => Date.to_string(Date.utc_today())}
+ "invite" => %{"max_use" => 150, "expires_at" => Date.to_string(Date.utc_today())}
})
token = json_response(conn, 200)
invite = UserInviteToken.find_by_token!(token)
refute invite.used
- assert invite.expire_at == Date.utc_today()
+ assert invite.expires_at == Date.utc_today()
assert invite.max_use == 150
assert invite.invite_type == "reusable_date_limited"
end
assert json_response(conn, 200) == %{
"invites" => [
%{
- "expire_at" => nil,
+ "expires_at" => nil,
"id" => invite.id,
"invite_type" => "one_time",
"max_use" => nil,
|> post("/api/pleroma/admin/revoke_invite", %{"token" => invite.token})
assert json_response(conn, 200) == %{
- "expire_at" => nil,
+ "expires_at" => nil,
"id" => invite.id,
"invite_type" => "one_time",
"max_use" => nil,
end
test "returns user on success", %{check_fn: check_fn} do
- {:ok, invite} = UserInviteToken.create_invite(%{expire_at: Date.utc_today()})
+ {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today()})
check_fn.(invite)
end
test "returns user on token which expired tomorrow", %{check_fn: check_fn} do
- {:ok, invite} = UserInviteToken.create_invite(%{expire_at: Date.add(Date.utc_today(), 1)})
+ {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), 1)})
check_fn.(invite)
end
test "returns an error on overdue date", %{data: data} do
- {:ok, invite} = UserInviteToken.create_invite(%{expire_at: Date.add(Date.utc_today(), -1)})
+ {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1)})
data = Map.put(data, "token", invite.token)
end
test "returns user on success" do
- {:ok, invite} = UserInviteToken.create_invite(%{expire_at: Date.utc_today(), max_use: 100})
+ {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
data = %{
"nickname" => "vinny",
end
test "error after max uses" do
- {:ok, invite} = UserInviteToken.create_invite(%{expire_at: Date.utc_today(), max_use: 100})
+ {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
UserInviteToken.update_invite!(invite, uses: 99)
test "returns error on overdue date" do
{:ok, invite} =
- UserInviteToken.create_invite(%{expire_at: Date.add(Date.utc_today(), -1), max_use: 100})
+ UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
data = %{
"nickname" => "GrimReaper",
test "returns error on with overdue date and after max" do
{:ok, invite} =
- UserInviteToken.create_invite(%{expire_at: Date.add(Date.utc_today(), -1), max_use: 100})
+ UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
UserInviteToken.update_invite!(invite, uses: 100)