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: - Params:
- *optional* `invite` => [ - *optional* `invite` => [
- *optional* `max_use` (integer) - *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) - Response: invite token (base64 string)
@ -226,7 +226,7 @@ Note: Available `:permission_group` is currently moderator and admin. 404 is ret
"id": integer, "id": integer,
"token": string, "token": string,
"used": boolean, "used": boolean,
"expire_at": date, "expires_at": date,
"uses": integer, "uses": integer,
"max_use": integer, "max_use": integer,
"invite_type": string (possible values: `one_time`, `reusable`, `date_limited`, `reusable_date_limited`) "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, "id": integer,
"token": string, "token": string,
"used": boolean, "used": boolean,
"expire_at": date, "expires_at": date,
"uses": integer, "uses": integer,
"max_use": integer, "max_use": integer,
"invite_type": string (possible values: `one_time`, `reusable`, `date_limited`, `reusable_date_limited`) "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...] mix pleroma.user invite [OPTION...]
Options: 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 - `--max_use NUMBER` - maximum numbers of token uses
## List generated invites ## List generated invites
@ -304,14 +304,14 @@ defmodule Mix.Tasks.Pleroma.User do
{options, [], []} = {options, [], []} =
OptionParser.parse(rest, OptionParser.parse(rest,
strict: [ strict: [
expire_at: :string, expires_at: :string,
max_use: :integer max_use: :integer
] ]
) )
options = options =
options options
|> Keyword.update(:expire_at, {:ok, nil}, fn |> Keyword.update(:expires_at, {:ok, nil}, fn
nil -> {:ok, nil} nil -> {:ok, nil}
val -> Date.from_iso8601(val) val -> Date.from_iso8601(val)
end) end)
@ -319,8 +319,8 @@ defmodule Mix.Tasks.Pleroma.User do
Common.start_pleroma() Common.start_pleroma()
with {:ok, val} <- options[:expire_at], with {:ok, val} <- options[:expires_at],
options = Map.put(options, :expire_at, val), options = Map.put(options, :expires_at, val),
{:ok, invite} <- UserInviteToken.create_invite(options) do {:ok, invite} <- UserInviteToken.create_invite(options) do
Mix.shell().info( Mix.shell().info(
"Generated user invite token " <> String.replace(invite.invite_type, "_", " ") "Generated user invite token " <> String.replace(invite.invite_type, "_", " ")
@ -348,8 +348,8 @@ defmodule Mix.Tasks.Pleroma.User do
UserInviteToken.list_invites() UserInviteToken.list_invites()
|> Enum.each(fn invite -> |> Enum.each(fn invite ->
expire_info = expire_info =
with expire_at when not is_nil(expire_at) <- invite.expire_at do with expires_at when not is_nil(expires_at) <- invite.expires_at do
" | Expire at: #{Date.to_string(expire_at)}" " | Expires at: #{Date.to_string(expires_at)}"
end end
using_info = using_info =

View File

@ -16,7 +16,7 @@ defmodule Pleroma.UserInviteToken do
field(:token, :string) field(:token, :string)
field(:used, :boolean, default: false) field(:used, :boolean, default: false)
field(:max_use, :integer) field(:max_use, :integer)
field(:expire_at, :date) field(:expires_at, :date)
field(:uses, :integer, default: 0) field(:uses, :integer, default: 0)
field(:invite_type, :string) field(:invite_type, :string)
@ -26,7 +26,7 @@ defmodule Pleroma.UserInviteToken do
@spec create_invite(map()) :: UserInviteToken.t() @spec create_invite(map()) :: UserInviteToken.t()
def create_invite(params \\ %{}) do def create_invite(params \\ %{}) do
%UserInviteToken{} %UserInviteToken{}
|> cast(params, ~w(max_use expire_at)a) |> cast(params, [:max_use, :expires_at])
|> add_token() |> add_token()
|> assign_type() |> assign_type()
|> Repo.insert() |> Repo.insert()
@ -37,11 +37,11 @@ defmodule Pleroma.UserInviteToken do
put_change(changeset, :token, token) put_change(changeset, :token, token)
end 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") put_change(changeset, :invite_type, "reusable_date_limited")
end 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") put_change(changeset, :invite_type, "date_limited")
end end
@ -95,8 +95,8 @@ defmodule Pleroma.UserInviteToken do
not_overdue_date?(invite) and invite.uses < invite.max_use and not invite.used not_overdue_date?(invite) and invite.uses < invite.max_use and not invite.used
end end
defp not_overdue_date?(%{expire_at: expire_at} = invite) do defp not_overdue_date?(%{expires_at: expires_at} = invite) do
Date.compare(Date.utc_today(), expire_at) in [:lt, :eq] || Date.compare(Date.utc_today(), expires_at) in [:lt, :eq] ||
(update_invite!(invite, %{used: true}) && false) (update_invite!(invite, %{used: true}) && false)
end end

View File

@ -32,7 +32,7 @@ defmodule Pleroma.Web.AdminAPI.AccountView do
"id" => invite.id, "id" => invite.id,
"token" => invite.token, "token" => invite.token,
"used" => invite.used, "used" => invite.used,
"expire_at" => invite.expire_at, "expires_at" => invite.expires_at,
"uses" => invite.uses, "uses" => invite.uses,
"max_use" => invite.max_use, "max_use" => invite.max_use,
"invite_type" => invite.invite_type "invite_type" => invite.invite_type

View File

@ -3,7 +3,7 @@ defmodule Pleroma.Repo.Migrations.AddFieldsToUserInviteTokens do
def change do def change do
alter table(:user_invite_tokens) do alter table(:user_invite_tokens) do
add(:expire_at, :date) add(:expires_at, :date)
add(:uses, :integer, default: 0) add(:uses, :integer, default: 0)
add(:max_use, :integer) add(:max_use, :integer)
add(:invite_type, :string, default: "one_time") 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" assert message =~ "Generated user invite token one time"
end end
test "token is generated with expire_at" do test "token is generated with expires_at" do
assert capture_io(fn -> assert capture_io(fn ->
Mix.Tasks.Pleroma.User.run([ Mix.Tasks.Pleroma.User.run([
"invite", "invite",
"--expire-at", "--expires-at",
Date.to_string(Date.utc_today()) Date.to_string(Date.utc_today())
]) ])
end) end)
@ -274,13 +274,13 @@ defmodule Mix.Tasks.Pleroma.UserTest do
assert message =~ "Generated user invite token reusable" assert message =~ "Generated user invite token reusable"
end 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 -> assert capture_io(fn ->
Mix.Tasks.Pleroma.User.run([ Mix.Tasks.Pleroma.User.run([
"invite", "invite",
"--max-use", "--max-use",
"5", "5",
"--expire-at", "--expires-at",
Date.to_string(Date.utc_today()) Date.to_string(Date.utc_today())
]) ])
end) end)
@ -295,7 +295,7 @@ defmodule Mix.Tasks.Pleroma.UserTest do
{:ok, invite} = Pleroma.UserInviteToken.create_invite() {:ok, invite} = Pleroma.UserInviteToken.create_invite()
{:ok, invite2} = {: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 -> # assert capture_io(fn ->
Mix.Tasks.Pleroma.User.run([ Mix.Tasks.Pleroma.User.run([
@ -315,7 +315,7 @@ defmodule Mix.Tasks.Pleroma.UserTest do
describe "running revoke_invite" do describe "running revoke_invite" do
test "invite is revoked" 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 -> assert capture_io(fn ->
Mix.Tasks.Pleroma.User.run([ Mix.Tasks.Pleroma.User.run([

View File

@ -654,26 +654,26 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
token = json_response(conn, 200) token = json_response(conn, 200)
invite = UserInviteToken.find_by_token!(token) invite = UserInviteToken.find_by_token!(token)
refute invite.used refute invite.used
refute invite.expire_at refute invite.expires_at
refute invite.max_use refute invite.max_use
assert invite.invite_type == "one_time" assert invite.invite_type == "one_time"
end end
test "with expire_at" do test "with expires_at" do
admin = insert(:user, info: %{is_admin: true}) admin = insert(:user, info: %{is_admin: true})
conn = conn =
build_conn() build_conn()
|> assign(:user, admin) |> assign(:user, admin)
|> get("/api/pleroma/admin/invite_token", %{ |> 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) token = json_response(conn, 200)
invite = UserInviteToken.find_by_token!(token) invite = UserInviteToken.find_by_token!(token)
refute invite.used refute invite.used
assert invite.expire_at == Date.utc_today() assert invite.expires_at == Date.utc_today()
refute invite.max_use refute invite.max_use
assert invite.invite_type == "date_limited" assert invite.invite_type == "date_limited"
end end
@ -691,25 +691,25 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
token = json_response(conn, 200) token = json_response(conn, 200)
invite = UserInviteToken.find_by_token!(token) invite = UserInviteToken.find_by_token!(token)
refute invite.used refute invite.used
refute invite.expire_at refute invite.expires_at
assert invite.max_use == 150 assert invite.max_use == 150
assert invite.invite_type == "reusable" assert invite.invite_type == "reusable"
end end
test "with max use and expire_at" do test "with max use and expires_at" do
admin = insert(:user, info: %{is_admin: true}) admin = insert(:user, info: %{is_admin: true})
conn = conn =
build_conn() build_conn()
|> assign(:user, admin) |> assign(:user, admin)
|> get("/api/pleroma/admin/invite_token", %{ |> 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) token = json_response(conn, 200)
invite = UserInviteToken.find_by_token!(token) invite = UserInviteToken.find_by_token!(token)
refute invite.used refute invite.used
assert invite.expire_at == Date.utc_today() assert invite.expires_at == Date.utc_today()
assert invite.max_use == 150 assert invite.max_use == 150
assert invite.invite_type == "reusable_date_limited" assert invite.invite_type == "reusable_date_limited"
end end
@ -739,7 +739,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
assert json_response(conn, 200) == %{ assert json_response(conn, 200) == %{
"invites" => [ "invites" => [
%{ %{
"expire_at" => nil, "expires_at" => nil,
"id" => invite.id, "id" => invite.id,
"invite_type" => "one_time", "invite_type" => "one_time",
"max_use" => nil, "max_use" => nil,
@ -763,7 +763,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
|> post("/api/pleroma/admin/revoke_invite", %{"token" => invite.token}) |> post("/api/pleroma/admin/revoke_invite", %{"token" => invite.token})
assert json_response(conn, 200) == %{ assert json_response(conn, 200) == %{
"expire_at" => nil, "expires_at" => nil,
"id" => invite.id, "id" => invite.id,
"invite_type" => "one_time", "invite_type" => "one_time",
"max_use" => nil, "max_use" => nil,

View File

@ -462,7 +462,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
end end
test "returns user on success", %{check_fn: check_fn} do 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) check_fn.(invite)
@ -472,7 +472,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
end end
test "returns user on token which expired tomorrow", %{check_fn: check_fn} do 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) check_fn.(invite)
@ -482,7 +482,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
end end
test "returns an error on overdue date", %{data: data} do 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) data = Map.put(data, "token", invite.token)
@ -562,7 +562,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
end end
test "returns user on success" do 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 = %{ data = %{
"nickname" => "vinny", "nickname" => "vinny",
@ -585,7 +585,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
end end
test "error after max uses" do 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) UserInviteToken.update_invite!(invite, uses: 99)
@ -625,7 +625,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
test "returns error on overdue date" do test "returns error on overdue date" do
{:ok, invite} = {: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 = %{ data = %{
"nickname" => "GrimReaper", "nickname" => "GrimReaper",
@ -645,7 +645,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
test "returns error on with overdue date and after max" do test "returns error on with overdue date and after max" do
{:ok, invite} = {: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) UserInviteToken.update_invite!(invite, uses: 100)