From cb29769a224104882ed7572087f8cd2db48475ef Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 12 Oct 2020 16:42:59 -0500 Subject: [PATCH] Make User.confirm/1 and User.approve/1 idempotent --- lib/pleroma/user.ex | 8 ++++++-- test/user_test.exs | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 4329fde12..c6767cfca 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -1590,7 +1590,7 @@ def approve(users) when is_list(users) do end) end - def approve(%User{} = user) do + def approve(%User{approval_pending: true} = user) do with chg <- change(user, approval_pending: false), {:ok, user} <- update_and_set_cache(chg) do post_register_action(user) @@ -1598,6 +1598,8 @@ def approve(%User{} = user) do end end + def approve(%User{} = user), do: {:ok, user} + def confirm(users) when is_list(users) do Repo.transaction(fn -> Enum.map(users, fn user -> @@ -1606,7 +1608,7 @@ def confirm(users) when is_list(users) do end) end - def confirm(%User{} = user) do + def confirm(%User{confirmation_pending: true} = user) do with chg <- confirmation_changeset(user, need_confirmation: false), {:ok, user} <- update_and_set_cache(chg) do post_register_action(user) @@ -1614,6 +1616,8 @@ def confirm(%User{} = user) do end end + def confirm(%User{} = user), do: {:ok, user} + def update_notification_settings(%User{} = user, settings) do user |> cast(%{notification_settings: settings}, []) diff --git a/test/user_test.exs b/test/user_test.exs index e83275fa5..18a143919 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -1371,6 +1371,17 @@ test "it sends welcome email if it is set" do html_body: "Welcome to #{instance_name}" ) end + + test "approving an approved user does not trigger post-register actions" do + clear_config([:welcome, :email, :enabled], true) + + user = insert(:user, approval_pending: false) + User.approve(user) + + ObanHelpers.perform_all() + + assert_no_email_sent() + end end describe "confirm" do @@ -1424,6 +1435,15 @@ test "sends approval emails when `approval_pending: true`" do html_body: admin_email.html_body ) end + + test "confirming a confirmed user does not trigger post-register actions" do + user = insert(:user, confirmation_pending: false, approval_pending: true) + User.confirm(user) + + ObanHelpers.perform_all() + + assert_no_email_sent() + end end describe "delete" do