renaming expire_at -> expires_at

keyword style change
This commit is contained in:
Alex S 2019-04-08 16:01:28 +07:00
parent 56c75aec12
commit 012bb5dcc9
8 changed files with 41 additions and 41 deletions

View File

@ -206,7 +206,7 @@ Note: Available `:permission_group` is currently moderator and admin. 404 is ret
- 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)
@ -226,7 +226,7 @@ Note: Available `:permission_group` is currently moderator and admin. 404 is ret
"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`)
@ -250,7 +250,7 @@ Note: Available `:permission_group` is currently moderator and admin. 404 is ret
"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`)

View File

@ -30,7 +30,7 @@ defmodule Mix.Tasks.Pleroma.User do
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
@ -304,14 +304,14 @@ defmodule Mix.Tasks.Pleroma.User do
{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)
@ -319,8 +319,8 @@ defmodule Mix.Tasks.Pleroma.User do
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, "_", " ")
@ -348,8 +348,8 @@ defmodule Mix.Tasks.Pleroma.User do
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 =

View File

@ -16,7 +16,7 @@ defmodule Pleroma.UserInviteToken do
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)
@ -26,7 +26,7 @@ defmodule Pleroma.UserInviteToken do
@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()
@ -37,11 +37,11 @@ defmodule Pleroma.UserInviteToken do
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
@ -95,8 +95,8 @@ defmodule Pleroma.UserInviteToken do
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

View File

@ -32,7 +32,7 @@ defmodule Pleroma.Web.AdminAPI.AccountView do
"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

View File

@ -3,7 +3,7 @@ defmodule Pleroma.Repo.Migrations.AddFieldsToUserInviteTokens do
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")

View File

@ -248,11 +248,11 @@ defmodule Mix.Tasks.Pleroma.UserTest do
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)
@ -274,13 +274,13 @@ defmodule Mix.Tasks.Pleroma.UserTest do
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)
@ -295,7 +295,7 @@ defmodule Mix.Tasks.Pleroma.UserTest do
{: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([
@ -315,7 +315,7 @@ defmodule Mix.Tasks.Pleroma.UserTest do
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([

View File

@ -654,26 +654,26 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
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
@ -691,25 +691,25 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
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
@ -739,7 +739,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
assert json_response(conn, 200) == %{
"invites" => [
%{
"expire_at" => nil,
"expires_at" => nil,
"id" => invite.id,
"invite_type" => "one_time",
"max_use" => nil,
@ -763,7 +763,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
|> 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,

View File

@ -462,7 +462,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
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)
@ -472,7 +472,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
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)
@ -482,7 +482,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
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)
@ -562,7 +562,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
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",
@ -585,7 +585,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
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)
@ -625,7 +625,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
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",
@ -645,7 +645,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
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)