From 012bb5dcc9bfbf6f3ea210ec4e865f3adcea9dfd Mon Sep 17 00:00:00 2001 From: Alex S Date: Mon, 8 Apr 2019 16:01:28 +0700 Subject: [PATCH] renaming expire_at -> expires_at keyword style change --- docs/api/admin_api.md | 6 +++--- lib/mix/tasks/pleroma/user.ex | 14 ++++++------- lib/pleroma/user_invite_token.ex | 12 +++++------ .../web/admin_api/views/account_view.ex | 2 +- ...50946_add_fields_to_user_invite_tokens.exs | 2 +- test/tasks/user_test.exs | 12 +++++------ .../admin_api/admin_api_controller_test.exs | 20 +++++++++---------- test/web/twitter_api/twitter_api_test.exs | 14 ++++++------- 8 files changed, 41 insertions(+), 41 deletions(-) diff --git a/docs/api/admin_api.md b/docs/api/admin_api.md index 638b235b8..8befa8ea0 100644 --- a/docs/api/admin_api.md +++ b/docs/api/admin_api.md @@ -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`) diff --git a/lib/mix/tasks/pleroma/user.ex b/lib/mix/tasks/pleroma/user.ex index 80b07d1ac..441168df2 100644 --- a/lib/mix/tasks/pleroma/user.ex +++ b/lib/mix/tasks/pleroma/user.ex @@ -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 @@ def run(["invite" | rest]) 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 @@ def run(["invite" | rest]) 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 @@ def run(["invites"]) 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 = diff --git a/lib/pleroma/user_invite_token.ex b/lib/pleroma/user_invite_token.ex index f08309485..11f1dcb16 100644 --- a/lib/pleroma/user_invite_token.ex +++ b/lib/pleroma/user_invite_token.ex @@ -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 @@ defp add_token(changeset) 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 @@ def valid_invite?(%{invite_type: "reusable_date_limited"} = invite) 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 diff --git a/lib/pleroma/web/admin_api/views/account_view.ex b/lib/pleroma/web/admin_api/views/account_view.ex index fd7917500..28bb667d8 100644 --- a/lib/pleroma/web/admin_api/views/account_view.ex +++ b/lib/pleroma/web/admin_api/views/account_view.ex @@ -32,7 +32,7 @@ def render("invite.json", %{invite: invite}) 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 diff --git a/priv/repo/migrations/20190404050946_add_fields_to_user_invite_tokens.exs b/priv/repo/migrations/20190404050946_add_fields_to_user_invite_tokens.exs index 46fa1cb32..211a14135 100644 --- a/priv/repo/migrations/20190404050946_add_fields_to_user_invite_tokens.exs +++ b/priv/repo/migrations/20190404050946_add_fields_to_user_invite_tokens.exs @@ -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") diff --git a/test/tasks/user_test.exs b/test/tasks/user_test.exs index 630ac06c1..242265da5 100644 --- a/test/tasks/user_test.exs +++ b/test/tasks/user_test.exs @@ -248,11 +248,11 @@ test "invite token is generated" 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 @@ test "token is generated with max use" 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 @@ test "invites are listed" 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 @@ test "invites are listed" 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([ diff --git a/test/web/admin_api/admin_api_controller_test.exs b/test/web/admin_api/admin_api_controller_test.exs index 67ee0f3fa..d44392c9d 100644 --- a/test/web/admin_api/admin_api_controller_test.exs +++ b/test/web/admin_api/admin_api_controller_test.exs @@ -654,26 +654,26 @@ test "without options" 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 @@ test "with max_use" 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 @@ test "with invite" 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 @@ test "with token" 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, diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index 519141c96..798a009fe 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -462,7 +462,7 @@ test "returns error on expired token" 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 @@ test "returns user on success", %{check_fn: check_fn} 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 @@ test "returns user on token which expired tomorrow", %{check_fn: check_fn} 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 @@ test "returns user on success, after him registration fails" 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 @@ test "returns user on success" 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 @@ test "error after max uses" 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 @@ test "returns error on overdue date" 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)