fix emails

This commit is contained in:
shibao 2024-07-28 11:35:45 -04:00
parent 179d67a896
commit 33e4d26a3d
6 changed files with 50 additions and 5 deletions

View File

@ -1,4 +1,5 @@
# v0.9.11 # v0.9.11
- Fix an issue with emails not being able to be sent for real this time
- Update deps - Update deps
# v0.9.10 # v0.9.10

View File

@ -32,4 +32,4 @@ config :logger, level: :warning
config :phoenix, :plug_init_mode, :runtime config :phoenix, :plug_init_mode, :runtime
# Disable Oban # Disable Oban
config :cannery, Oban, queues: false, plugins: false config :cannery, Oban, queues: false, plugins: false, testing: :manual

View File

@ -26,21 +26,21 @@ defmodule Cannery.Email do
end end
@spec generate_email(key :: String.t(), User.t(), attrs :: map()) :: t() @spec generate_email(key :: String.t(), User.t(), attrs :: map()) :: t()
def generate_email("welcome", user, %{url: url}) do def generate_email("welcome", user, %{"url" => url}) do
user user
|> base_email(dgettext("emails", "Confirm your Cannery account")) |> base_email(dgettext("emails", "Confirm your Cannery account"))
|> html_email(:confirm_email_html, %{user: user, url: url}) |> html_email(:confirm_email_html, %{user: user, url: url})
|> text_email(:confirm_email_text, %{user: user, url: url}) |> text_email(:confirm_email_text, %{user: user, url: url})
end end
def generate_email("reset_password", user, %{url: url}) do def generate_email("reset_password", user, %{"url" => url}) do
user user
|> base_email(dgettext("emails", "Reset your Cannery password")) |> base_email(dgettext("emails", "Reset your Cannery password"))
|> html_email(:reset_password_html, %{user: user, url: url}) |> html_email(:reset_password_html, %{user: user, url: url})
|> text_email(:reset_password_text, %{user: user, url: url}) |> text_email(:reset_password_text, %{user: user, url: url})
end end
def generate_email("update_email", user, %{url: url}) do def generate_email("update_email", user, %{"url" => url}) do
user user
|> base_email(dgettext("emails", "Update your Cannery email")) |> base_email(dgettext("emails", "Update your Cannery email"))
|> html_email(:update_email_html, %{user: user, url: url}) |> html_email(:update_email_html, %{user: user, url: url})

View File

@ -7,7 +7,7 @@ defmodule Cannery.EmailWorker do
alias Cannery.{Accounts, Email, Mailer} alias Cannery.{Accounts, Email, Mailer}
@impl Oban.Worker @impl Oban.Worker
def perform(%Oban.Job{args: %{email: email, user_id: user_id, attrs: attrs}}) do def perform(%Oban.Job{args: %{"email" => email, "user_id" => user_id, "attrs" => attrs}}) do
Email.generate_email(email, user_id |> Accounts.get_user!(), attrs) |> Mailer.deliver() Email.generate_email(email, user_id |> Accounts.get_user!(), attrs) |> Mailer.deliver()
end end
end end

View File

@ -0,0 +1,40 @@
defmodule Cannery.EmailWorkerTest do
use Cannery.DataCase, async: true
def perform_job(worker, args) do
Oban.Testing.perform_job(worker, args, [])
end
test "sending welcome email" do
user = user_fixture()
{:ok, _user} =
perform_job(Cannery.EmailWorker, %{
"email" => "welcome",
"user_id" => user.id,
"attrs" => %{"url" => "test_url"}
})
end
test "sending reset password email" do
user = user_fixture()
{:ok, _user} =
perform_job(Cannery.EmailWorker, %{
"email" => "reset_password",
"user_id" => user.id,
"attrs" => %{"url" => "test_url"}
})
end
test "sending update email email" do
user = user_fixture()
{:ok, _user} =
perform_job(Cannery.EmailWorker, %{
"email" => "update_email",
"user_id" => user.id,
"attrs" => %{"url" => "test_url"}
})
end
end

View File

@ -48,6 +48,10 @@ defmodule Cannery.Fixtures do
def extract_user_token(fun) do def extract_user_token(fun) do
%{args: %{attrs: attrs, email: email_key, user_id: user_id}} = fun.(&"[TOKEN]#{&1}[TOKEN]") %{args: %{attrs: attrs, email: email_key, user_id: user_id}} = fun.(&"[TOKEN]#{&1}[TOKEN]")
attrs =
attrs
|> Map.new(fn {key, value} -> {Atom.to_string(key), value} end)
email = email =
email_key email_key
|> Atom.to_string() |> Atom.to_string()