fix reset confirmation email in admin section
authorMaksim Pechnikov <parallel588@gmail.com>
Sat, 11 Jul 2020 04:20:35 +0000 (07:20 +0300)
committerMaksim Pechnikov <parallel588@gmail.com>
Sat, 11 Jul 2020 04:20:35 +0000 (07:20 +0300)
lib/pleroma/application_requirements.ex
lib/pleroma/user.ex
lib/pleroma/web/admin_api/controllers/admin_api_controller.ex
test/application_requirements_test.exs
test/user_test.exs
test/web/admin_api/controllers/admin_api_controller_test.exs

index 88575a498f6db653c5b40a631961089ad1ba7660..f0f34734e88c8664aaf37dc9eb80844914f02ec1 100644 (file)
@@ -16,6 +16,7 @@ defmodule Pleroma.ApplicationRequirements do
   @spec verify!() :: :ok | VerifyError.t()
   def verify! do
     :ok
+    |> check_confirmation_accounts!
     |> check_migrations_applied!()
     |> check_rum!()
     |> handle_result()
@@ -24,6 +25,23 @@ defmodule Pleroma.ApplicationRequirements do
   defp handle_result(:ok), do: :ok
   defp handle_result({:error, message}), do: raise(VerifyError, message: message)
 
+  # Checks account confirmation email
+  #
+  def check_confirmation_accounts!(:ok) do
+    if Pleroma.Config.get([:instance, :account_activation_required]) &&
+         not Pleroma.Config.get([Pleroma.Emails.Mailer, :enabled]) do
+      Logger.error(
+        "To use confirmation an user account need to enable and setting mailer.\nIf you want to start Pleroma anyway, set\nconfig :pleroma, :instance, account_activation_required: false\nOtherwise setup and enable mailer."
+      )
+
+      {:error, "Confirmation account: Mailer is disabled"}
+    else
+      :ok
+    end
+  end
+
+  def check_confirmation_accounts!(result), do: result
+
   # Checks for pending migrations.
   #
   def check_migrations_applied!(:ok) do
index b9989f9018194122f2499918afedf2f88d73ad4f..711258ac703f1fd7e57d16788b255a40b98f1f1b 100644 (file)
@@ -709,21 +709,25 @@ defmodule Pleroma.User do
     end
   end
 
-  def try_send_confirmation_email(%User{} = user) do
-    if user.confirmation_pending &&
-         Config.get([:instance, :account_activation_required]) do
-      user
-      |> Pleroma.Emails.UserEmail.account_confirmation_email()
-      |> Pleroma.Emails.Mailer.deliver_async()
-
+  @spec try_send_confirmation_email(User.t()) :: {:ok, :enqueued | :noop}
+  def try_send_confirmation_email(%User{confirmation_pending: true} = user) do
+    if Config.get([:instance, :account_activation_required]) do
+      send_confirmation_email(user)
       {:ok, :enqueued}
     else
       {:ok, :noop}
     end
   end
 
-  def try_send_confirmation_email(users) do
-    Enum.each(users, &try_send_confirmation_email/1)
+  def try_send_confirmation_email(_), do: {:ok, :noop}
+
+  @spec send_confirmation_email(Uset.t()) :: User.t()
+  def send_confirmation_email(%User{} = user) do
+    user
+    |> Pleroma.Emails.UserEmail.account_confirmation_email()
+    |> Pleroma.Emails.Mailer.deliver_async()
+
+    user
   end
 
   def needs_update?(%User{local: true}), do: false
index e5f14269a1992fc9218b3a9f1ca87aa8638b18f5..c10181bae62ca77ae3bbe71b9a815309302f2f8b 100644 (file)
@@ -616,29 +616,24 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
   end
 
   def confirm_email(%{assigns: %{user: admin}} = conn, %{"nicknames" => nicknames}) do
-    users = nicknames |> Enum.map(&User.get_cached_by_nickname/1)
+    users = Enum.map(nicknames, &User.get_cached_by_nickname/1)
 
     User.toggle_confirmation(users)
 
-    ModerationLog.insert_log(%{
-      actor: admin,
-      subject: users,
-      action: "confirm_email"
-    })
+    ModerationLog.insert_log(%{actor: admin, subject: users, action: "confirm_email"})
 
     json(conn, "")
   end
 
   def resend_confirmation_email(%{assigns: %{user: admin}} = conn, %{"nicknames" => nicknames}) do
-    users = nicknames |> Enum.map(&User.get_cached_by_nickname/1)
-
-    User.try_send_confirmation_email(users)
+    users =
+      Enum.map(nicknames, fn nickname ->
+        nickname
+        |> User.get_cached_by_nickname()
+        |> User.send_confirmation_email()
+      end)
 
-    ModerationLog.insert_log(%{
-      actor: admin,
-      subject: users,
-      action: "resend_confirmation_email"
-    })
+    ModerationLog.insert_log(%{actor: admin, subject: users, action: "resend_confirmation_email"})
 
     json(conn, "")
   end
index 481cdfd73e6217e8b7900dfcd082fa0fa6951534..8c92be290c094637dc72c13d58ab0625312ec17d 100644 (file)
@@ -9,6 +9,42 @@ defmodule Pleroma.ApplicationRequirementsTest do
 
   alias Pleroma.Repo
 
+  describe "check_confirmation_accounts!" do
+    setup_with_mocks([
+      {Pleroma.ApplicationRequirements, [:passthrough],
+       [
+         check_migrations_applied!: fn _ -> :ok end
+       ]}
+    ]) do
+      :ok
+    end
+
+    setup do: clear_config([:instance, :account_activation_required])
+
+    test "raises if account confirmation is required but mailer isn't enable" do
+      Pleroma.Config.put([:instance, :account_activation_required], true)
+      Pleroma.Config.put([Pleroma.Emails.Mailer, :enabled], false)
+
+      assert_raise Pleroma.ApplicationRequirements.VerifyError,
+                   "Confirmation account: Mailer is disabled",
+                   fn ->
+                     capture_log(&Pleroma.ApplicationRequirements.verify!/0)
+                   end
+    end
+
+    test "doesn't do anything if account confirmation is disabled" do
+      Pleroma.Config.put([:instance, :account_activation_required], false)
+      Pleroma.Config.put([Pleroma.Emails.Mailer, :enabled], false)
+      assert Pleroma.ApplicationRequirements.verify!() == :ok
+    end
+
+    test "doesn't do anything if account confirmation is required and mailer is enabled" do
+      Pleroma.Config.put([:instance, :account_activation_required], true)
+      Pleroma.Config.put([Pleroma.Emails.Mailer, :enabled], true)
+      assert Pleroma.ApplicationRequirements.verify!() == :ok
+    end
+  end
+
   describe "check_rum!" do
     setup_with_mocks([
       {Pleroma.ApplicationRequirements, [:passthrough],
index 9788e09d9b24f5d74bace103993e53c6c6d5797a..21c03b470fab5ff4c1e098e10610ff5ecf1f54bc 100644 (file)
@@ -17,6 +17,7 @@ defmodule Pleroma.UserTest do
 
   import Pleroma.Factory
   import ExUnit.CaptureLog
+  import Swoosh.TestAssertions
 
   setup_all do
     Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
@@ -385,9 +386,11 @@ defmodule Pleroma.UserTest do
       password_confirmation: "test",
       email: "email@example.com"
     }
+
     setup do: clear_config([:instance, :autofollowed_nicknames])
     setup do: clear_config([:instance, :welcome_message])
     setup do: clear_config([:instance, :welcome_user_nickname])
+    setup do: clear_config([:instance, :account_activation_required])
 
     test "it autofollows accounts that are set for it" do
       user = insert(:user)
@@ -421,7 +424,14 @@ defmodule Pleroma.UserTest do
       assert activity.actor == welcome_user.ap_id
     end
 
-    setup do: clear_config([:instance, :account_activation_required])
+    test "it sends a confirm email" do
+      Pleroma.Config.put([:instance, :account_activation_required], true)
+
+      cng = User.register_changeset(%User{}, @full_user_data)
+      {:ok, registered_user} = User.register(cng)
+      ObanHelpers.perform_all()
+      assert_email_sent(Pleroma.Emails.UserEmail.account_confirmation_email(registered_user))
+    end
 
     test "it requires an email, name, nickname and password, bio is optional when account_activation_required is enabled" do
       Pleroma.Config.put([:instance, :account_activation_required], true)
index c2433f23c1480a69d216b901cdec9b3cc3bd46f9..b734a34a517e70e8e052c6bd183065a31b7a3954 100644 (file)
@@ -9,6 +9,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
   import ExUnit.CaptureLog
   import Mock
   import Pleroma.Factory
+  import Swoosh.TestAssertions
 
   alias Pleroma.Activity
   alias Pleroma.Config
@@ -1721,6 +1722,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
                "@#{admin.nickname} re-sent confirmation email for users: @#{first_user.nickname}, @#{
                  second_user.nickname
                }"
+
+      ObanHelpers.perform_all()
+      assert_email_sent(Pleroma.Emails.UserEmail.account_confirmation_email(first_user))
     end
   end