swoosh_mailinator/test/swoosh_mlntr.exs

79 lines
1.3 KiB
Elixir
Raw Permalink Normal View History

2024-09-01 19:41:00 +00:00
# TODO rewrite with exunit
2024-06-03 22:56:22 +00:00
Mix.install(
[
{:swoosh, "~> 1.7"},
{:gen_smtp, "~> 1.1"},
{:hackney, "~> 1.8"},
{:swoosh_mailinator, path: "."}, #app: false }
]
)
defmodule Main do
import Swoosh.Email
import Swoosh.Mailer
2024-06-07 06:15:09 +00:00
def test_both() do
prep()
end
def test_text_only() do
prep()
|> Map.delete(:html_body)
end
def test_text_nil() do
prep()
|> text_body(nil)
end
2024-06-03 22:56:22 +00:00
2024-06-07 06:15:09 +00:00
def test_html_only() do
prep()
|> Map.delete(:text_body)
end
def test_html_nil() do
prep()
|> html_body(nil)
end
2024-06-03 22:56:22 +00:00
2024-09-01 19:41:00 +00:00
def test_both_absent() do
prep()
|> Map.delete(:html_body)
|> Map.delete(:text_body)
end
2024-06-07 06:15:09 +00:00
def prep do
mess = "mess"
2024-06-03 22:56:22 +00:00
new()
|> to("mlkjhg@kiujygtrfedc.com")
|> from({"Who Knows", "who.knows@nonexisting.tld"})
|> subject("Hello, Swoosh!")
|> html_body("<h1>Hello World</h1><p>"<>mess<>"</p>")
|> text_body(mess)
2024-06-07 06:15:09 +00:00
end
def send(email) do
email
2024-06-03 22:56:22 +00:00
|> deliver([
adapter: Swoosh.Adapters.Mailinator
])
end
end
2024-09-01 19:41:00 +00:00
# ok cases
Main.test_both() |> Main.send()
Main.test_text_only() |> Main.send()
Main.test_html_only() |> Main.send()
Main.test_text_nil() |> Main.send()
Main.test_html_nil() |> Main.send()
# failing cases
try do
Main.test_both_absent() |> Main.send()
rescue
FunctionClauseError -> "expected"
else
_ -> "shoud have failed"
end