From 33e4d26a3d2e123afe39639e94066f225d1ef18b Mon Sep 17 00:00:00 2001 From: shibao Date: Sun, 28 Jul 2024 11:35:45 -0400 Subject: [PATCH] fix emails --- CHANGELOG.md | 1 + config/test.exs | 2 +- lib/cannery/{accounts => }/email.ex | 6 ++-- lib/cannery/{accounts => }/email_worker.ex | 2 +- test/cannery/email_worker_test.exs | 40 ++++++++++++++++++++++ test/support/fixtures.ex | 4 +++ 6 files changed, 50 insertions(+), 5 deletions(-) rename lib/cannery/{accounts => }/email.ex (91%) rename lib/cannery/{accounts => }/email_worker.ex (76%) create mode 100644 test/cannery/email_worker_test.exs diff --git a/CHANGELOG.md b/CHANGELOG.md index d879da1..4cd2b10 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ # v0.9.11 +- Fix an issue with emails not being able to be sent for real this time - Update deps # v0.9.10 diff --git a/config/test.exs b/config/test.exs index 6543239..1cd94ad 100644 --- a/config/test.exs +++ b/config/test.exs @@ -32,4 +32,4 @@ config :logger, level: :warning config :phoenix, :plug_init_mode, :runtime # Disable Oban -config :cannery, Oban, queues: false, plugins: false +config :cannery, Oban, queues: false, plugins: false, testing: :manual diff --git a/lib/cannery/accounts/email.ex b/lib/cannery/email.ex similarity index 91% rename from lib/cannery/accounts/email.ex rename to lib/cannery/email.ex index 4bc096f..bf52af7 100644 --- a/lib/cannery/accounts/email.ex +++ b/lib/cannery/email.ex @@ -26,21 +26,21 @@ defmodule Cannery.Email do end @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 |> base_email(dgettext("emails", "Confirm your Cannery account")) |> html_email(:confirm_email_html, %{user: user, url: url}) |> text_email(:confirm_email_text, %{user: user, url: url}) end - def generate_email("reset_password", user, %{url: url}) do + def generate_email("reset_password", user, %{"url" => url}) do user |> base_email(dgettext("emails", "Reset your Cannery password")) |> html_email(:reset_password_html, %{user: user, url: url}) |> text_email(:reset_password_text, %{user: user, url: url}) end - def generate_email("update_email", user, %{url: url}) do + def generate_email("update_email", user, %{"url" => url}) do user |> base_email(dgettext("emails", "Update your Cannery email")) |> html_email(:update_email_html, %{user: user, url: url}) diff --git a/lib/cannery/accounts/email_worker.ex b/lib/cannery/email_worker.ex similarity index 76% rename from lib/cannery/accounts/email_worker.ex rename to lib/cannery/email_worker.ex index fb6c1c7..bc8eee5 100644 --- a/lib/cannery/accounts/email_worker.ex +++ b/lib/cannery/email_worker.ex @@ -7,7 +7,7 @@ defmodule Cannery.EmailWorker do alias Cannery.{Accounts, Email, Mailer} @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() end end diff --git a/test/cannery/email_worker_test.exs b/test/cannery/email_worker_test.exs new file mode 100644 index 0000000..20ae6e8 --- /dev/null +++ b/test/cannery/email_worker_test.exs @@ -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 diff --git a/test/support/fixtures.ex b/test/support/fixtures.ex index ce1f7ba..afa0d45 100644 --- a/test/support/fixtures.ex +++ b/test/support/fixtures.ex @@ -48,6 +48,10 @@ defmodule Cannery.Fixtures do def extract_user_token(fun) do %{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_key |> Atom.to_string()