Skip to content

Commit 71c0a22

Browse files
committed
unify fake mailers
1 parent ef8512e commit 71c0a22

File tree

4 files changed

+41
-68
lines changed

4 files changed

+41
-68
lines changed

test/support/bamboo_fake_mailer.ex

-33
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
defmodule Support.SwooshFakeMailer do
2-
@moduledoc false
3-
4-
# Overrides the Swoosh `deliver!/1` function.
5-
#
6-
# Instead of sending an email it puts the fields in a mailbox so they can be
7-
# received in the test.
8-
#
9-
# Swoosh does include Swoosh.Adapters.Test, but its behaviour seems unreliable
10-
# in our case because we are catching then sending mail, meaning tests need
11-
# a lot of Process.sleep/1 calls to wait for mailboxes.
12-
#
13-
# It's also possible to use Swoosh.Adapters.Local and
14-
# Swoosh.Adapter.Local.Storage.Memory which you can directly inspect. This
15-
# could be used to check for specific mail.
1+
defmodule BoomNotifier.FakeMailer do
2+
@moduledoc """
3+
Fake mailer for Swoosh and Bamboo
4+
"""
5+
6+
@doc """
7+
Bamboo's email deliver that just sends message back
8+
"""
9+
def deliver_later!(email) do
10+
# Use email.to to specify test target pid
11+
email.to
12+
|> to_pid()
13+
|> send_back(email.to, email.from, email)
14+
end
1615

16+
@doc """
17+
Swoosh's email deliver that just sends message back
18+
"""
1719
def deliver!(email) do
1820
# Swoosh uses a {name, address} tuple for to and from.
1921
# The `name` will default to `""` when not specified in the email creation,
@@ -23,25 +25,20 @@ defmodule Support.SwooshFakeMailer do
2325
# this crashes OTP 21, specifically getting the from_addr via tuple match,
2426
# so get from_addr via elem/2 separately...
2527
# %{to: [{_name, email_to}], from: {_, from_addr}} = email
26-
%{to: [{_name, email_to}]} = email
28+
%{to: [{_name, email_to}], from: from} = email
29+
email_from = elem(from, 1)
2730

2831
# Use email.to to specify test target pid
29-
pid =
30-
email_to
31-
|> String.to_atom()
32-
|> Process.whereis()
33-
34-
send_back(pid, email)
32+
email_to
33+
|> to_pid()
34+
|> send_back(email_to, email_from, email)
3535
end
3636

37-
def send_back(nil, _), do: nil
38-
39-
def send_back(pid, email) do
40-
%{to: [{_name, email_to}], from: from} = email
41-
from_addr = elem(from, 1)
37+
defp send_back(nil, _, _, _), do: nil
4238

39+
defp send_back(pid, email_to, email_from, email) do
4340
send(pid, {:email_subject, email.subject})
44-
send(pid, {:email_from, from_addr})
41+
send(pid, {:email_from, email_from})
4542
send(pid, {:email_to, email_to})
4643
send(pid, {:email_text_body, text_body_lines(email.text_body)})
4744
send(pid, {:email_html_body, email.html_body})
@@ -52,4 +49,13 @@ defmodule Support.SwooshFakeMailer do
5249
|> Enum.map(&String.trim/1)
5350
|> Enum.reject(&(String.length(&1) == 0))
5451
end
52+
53+
defp to_pid(value) do
54+
value
55+
|> String.to_existing_atom()
56+
|> Process.whereis()
57+
rescue
58+
# raised by to_existing_atom
59+
ArgumentError -> nil
60+
end
5561
end

test/unit/error_info_test.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ defmodule ErrorInfoTest do
3737
use BoomNotifier,
3838
notifier: BoomNotifier.MailNotifier.Bamboo,
3939
options: [
40-
mailer: Support.BambooFakeMailer,
40+
mailer: BoomNotifier.FakeMailer,
4141
4242
4343
subject: "BOOM error caught"

test/unit/mailer_notifier_test.exs

+6-6
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ defmodule MailerNotifierTest do
4848
# since we are redefining the router modules for each notifier.
4949

5050
notifiers = [
51-
{"Bamboo", BoomNotifier.MailNotifier.Bamboo, Support.BambooFakeMailer},
52-
{"Swoosh", BoomNotifier.MailNotifier.Swoosh, Support.SwooshFakeMailer}
51+
{"Bamboo", BoomNotifier.MailNotifier.Bamboo},
52+
{"Swoosh", BoomNotifier.MailNotifier.Swoosh}
5353
]
5454

55-
for {name, mail_notifier_module, fake_mailer_module} <- notifiers do
55+
for {name, mail_notifier_module} <- notifiers do
5656
describe name do
5757
# Disable "redefining module" warnings, we have intent.
5858
:elixir_config.put(:ignore_module_conflict, true)
@@ -62,7 +62,7 @@ defmodule MailerNotifierTest do
6262
import Phoenix.Controller
6363

6464
@mail_notifier_module mail_notifier_module
65-
@fake_mailer_module fake_mailer_module
65+
@fake_mailer_module BoomNotifier.FakeMailer
6666
use BoomNotifier,
6767
notifier: @mail_notifier_module,
6868
options: [
@@ -95,7 +95,7 @@ defmodule MailerNotifierTest do
9595
use Phoenix.Router
9696

9797
@mail_notifier_module mail_notifier_module
98-
@fake_mailer_module fake_mailer_module
98+
@fake_mailer_module BoomNotifier.FakeMailer
9999
use BoomNotifier,
100100
notifier: @mail_notifier_module,
101101
options: [
@@ -376,7 +376,7 @@ defmodule MailerNotifierTest do
376376
describe "MailNotifier.validate_config" do
377377
setup do
378378
options = [
379-
mailer: Support.BambooFakeMailer,
379+
mailer: BoomNotifier.FakeMailer,
380380
from: "boom@mailer",
381381
to: "user@mail",
382382
subject: "boom mail",

0 commit comments

Comments
 (0)