[#1149] Oban mailer job. Adjusted tests.

This commit is contained in:
Ivan Tashkinov 2019-08-14 21:42:21 +03:00
parent 0e1c481a94
commit a180c1360e
7 changed files with 35 additions and 1 deletions

View File

@ -233,6 +233,7 @@ defp hackney_pool_children do
defp after_supervisor_start do defp after_supervisor_start do
with digest_config <- Application.get_env(:pleroma, :email_notifications)[:digest], with digest_config <- Application.get_env(:pleroma, :email_notifications)[:digest],
true <- digest_config[:active] do true <- digest_config[:active] do
# TODO: consider replacing with `quantum` scheduler
PleromaJobQueue.schedule( PleromaJobQueue.schedule(
digest_config[:schedule], digest_config[:schedule],
:digest_emails, :digest_emails,

View File

@ -9,6 +9,8 @@ defmodule Pleroma.Emails.Mailer do
The module contains functions to delivery email using Swoosh.Mailer. The module contains functions to delivery email using Swoosh.Mailer.
""" """
alias Pleroma.Repo
alias Pleroma.Workers.Mailer, as: MailerWorker
alias Swoosh.DeliveryError alias Swoosh.DeliveryError
@otp_app :pleroma @otp_app :pleroma
@ -17,9 +19,18 @@ defmodule Pleroma.Emails.Mailer do
@spec enabled?() :: boolean() @spec enabled?() :: boolean()
def enabled?, do: Pleroma.Config.get([__MODULE__, :enabled]) def enabled?, do: Pleroma.Config.get([__MODULE__, :enabled])
defdelegate worker_args(queue), to: Pleroma.Workers.Helper
@doc "add email to queue" @doc "add email to queue"
def deliver_async(email, config \\ []) do def deliver_async(email, config \\ []) do
PleromaJobQueue.enqueue(:mailer, __MODULE__, [:deliver_async, email, config]) encoded_email =
email
|> :erlang.term_to_binary()
|> Base.encode64()
%{"op" => "email", "encoded_email" => encoded_email, "config" => config}
|> MailerWorker.new(worker_args(:mailer))
|> Repo.insert()
end end
@doc "callback to perform send email from queue" @doc "callback to perform send email from queue"

View File

@ -11,6 +11,15 @@ defmodule Pleroma.Workers.Mailer do
max_attempts: Pleroma.Config.get([:workers, :retries, :compile_time_default]) max_attempts: Pleroma.Config.get([:workers, :retries, :compile_time_default])
@impl Oban.Worker @impl Oban.Worker
def perform(%{"op" => "email", "encoded_email" => encoded_email, "config" => config}) do
email =
encoded_email
|> Base.decode64!()
|> :erlang.binary_to_term()
Pleroma.Emails.Mailer.deliver(email, config)
end
def perform(%{"op" => "digest_email", "user_id" => user_id}) do def perform(%{"op" => "digest_email", "user_id" => user_id}) do
user = User.get_by_id(user_id) user = User.get_by_id(user_id)
Pleroma.DigestEmailWorker.perform(user) Pleroma.DigestEmailWorker.perform(user)

View File

@ -4,6 +4,7 @@ defmodule Mix.Tasks.Pleroma.DigestTest do
import Pleroma.Factory import Pleroma.Factory
import Swoosh.TestAssertions import Swoosh.TestAssertions
alias Pleroma.Tests.ObanHelpers
alias Pleroma.Web.CommonAPI alias Pleroma.Web.CommonAPI
setup_all do setup_all do
@ -39,6 +40,8 @@ test "Sends digest to the given user" do
:ok = Mix.Tasks.Pleroma.Digest.run(["test", user2.nickname, yesterday_date]) :ok = Mix.Tasks.Pleroma.Digest.run(["test", user2.nickname, yesterday_date])
ObanHelpers.perform_all()
assert_receive {:mix_shell, :info, [message]} assert_receive {:mix_shell, :info, [message]}
assert message =~ "Digest email have been sent" assert message =~ "Digest email have been sent"

View File

@ -11,6 +11,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
alias Pleroma.Object alias Pleroma.Object
alias Pleroma.Repo alias Pleroma.Repo
alias Pleroma.ScheduledActivity alias Pleroma.ScheduledActivity
alias Pleroma.Tests.ObanHelpers
alias Pleroma.User alias Pleroma.User
alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.CommonAPI alias Pleroma.Web.CommonAPI
@ -3871,6 +3872,7 @@ test "it creates a PasswordResetToken record for user", %{user: user} do
end end
test "it sends an email to user", %{user: user} do test "it sends an email to user", %{user: user} do
ObanHelpers.perform_all()
token_record = Repo.get_by(Pleroma.PasswordResetToken, user_id: user.id) token_record = Repo.get_by(Pleroma.PasswordResetToken, user_id: user.id)
email = Pleroma.Emails.UserEmail.password_reset_email(user, token_record.token) email = Pleroma.Emails.UserEmail.password_reset_email(user, token_record.token)
@ -3934,6 +3936,8 @@ test "resend account confirmation email", %{conn: conn, user: user} do
|> post("/api/v1/pleroma/accounts/confirmation_resend?email=#{user.email}") |> post("/api/v1/pleroma/accounts/confirmation_resend?email=#{user.email}")
|> json_response(:no_content) |> json_response(:no_content)
ObanHelpers.perform_all()
email = Pleroma.Emails.UserEmail.account_confirmation_email(user) email = Pleroma.Emails.UserEmail.account_confirmation_email(user)
notify_email = Pleroma.Config.get([:instance, :notify_email]) notify_email = Pleroma.Config.get([:instance, :notify_email])
instance_name = Pleroma.Config.get([:instance, :name]) instance_name = Pleroma.Config.get([:instance, :name])

View File

@ -12,6 +12,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
alias Pleroma.Notification alias Pleroma.Notification
alias Pleroma.Object alias Pleroma.Object
alias Pleroma.Repo alias Pleroma.Repo
alias Pleroma.Tests.ObanHelpers
alias Pleroma.User alias Pleroma.User
alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.CommonAPI alias Pleroma.Web.CommonAPI
@ -1099,6 +1100,7 @@ test "it creates a PasswordResetToken record for user", %{user: user} do
end end
test "it sends an email to user", %{user: user} do test "it sends an email to user", %{user: user} do
ObanHelpers.perform_all()
token_record = Repo.get_by(Pleroma.PasswordResetToken, user_id: user.id) token_record = Repo.get_by(Pleroma.PasswordResetToken, user_id: user.id)
email = Pleroma.Emails.UserEmail.password_reset_email(user, token_record.token) email = Pleroma.Emails.UserEmail.password_reset_email(user, token_record.token)
@ -1209,6 +1211,8 @@ test "it sends confirmation email", %{conn: conn, user: user} do
|> assign(:user, user) |> assign(:user, user)
|> post("/api/account/resend_confirmation_email?email=#{user.email}") |> post("/api/account/resend_confirmation_email?email=#{user.email}")
ObanHelpers.perform_all()
email = Pleroma.Emails.UserEmail.account_confirmation_email(user) email = Pleroma.Emails.UserEmail.account_confirmation_email(user)
notify_email = Pleroma.Config.get([:instance, :notify_email]) notify_email = Pleroma.Config.get([:instance, :notify_email])
instance_name = Pleroma.Config.get([:instance, :name]) instance_name = Pleroma.Config.get([:instance, :name])

View File

@ -7,6 +7,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
alias Pleroma.Activity alias Pleroma.Activity
alias Pleroma.Object alias Pleroma.Object
alias Pleroma.Repo alias Pleroma.Repo
alias Pleroma.Tests.ObanHelpers
alias Pleroma.User alias Pleroma.User
alias Pleroma.UserInviteToken alias Pleroma.UserInviteToken
alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.ActivityPub
@ -321,6 +322,7 @@ test "it sends confirmation email if :account_activation_required is specified i
} }
{:ok, user} = TwitterAPI.register_user(data) {:ok, user} = TwitterAPI.register_user(data)
ObanHelpers.perform_all()
assert user.info.confirmation_pending assert user.info.confirmation_pending