This commit is contained in:
parent
61952f986d
commit
d7dd7d1959
3 changed files with 66 additions and 31 deletions
|
|
@ -1,18 +1,22 @@
|
|||
defmodule Mv.MailerSmtpConfigTest do
|
||||
@moduledoc """
|
||||
Unit tests for Mv.Mailer.smtp_config/0.
|
||||
Integration-style tests for `Mv.Mailer.smtp_config/0`.
|
||||
|
||||
Ensures both port 587 (STARTTLS) and 465 (implicit SSL) work:
|
||||
- 587: sockopts must NOT contain :verify (gen_tcp:connect would raise ArgumentError).
|
||||
- 465: sockopts MUST contain :verify so initial ssl:connect uses verify_none/verify_peer.
|
||||
Uses ENV to drive config; async: false.
|
||||
With the default test mailer adapter (`Swoosh.Adapters.Test`), `smtp_config/0`
|
||||
must return `[]` so per-send SMTP opts never bypass the test mailbox.
|
||||
|
||||
Port 587 vs 465 / `:verify` in `sockopts` are covered by
|
||||
`Mv.Smtp.ConfigBuilderTest`; here we assert `smtp_config/0` wiring from
|
||||
`Mv.Config` when the mailer adapter is temporarily set to
|
||||
`Swoosh.Adapters.Local` (`async: false`, global Application env).
|
||||
|
||||
Uses `Mv.DataCase` so `Mv.Config.smtp_username/0` and `smtp_password/0` (Settings
|
||||
fallback when ENV is unset) run under the SQL sandbox like the rest of the suite.
|
||||
"""
|
||||
use Mv.DataCase, async: false
|
||||
|
||||
alias Mv.Mailer
|
||||
|
||||
defp set_smtp_env(key, value), do: System.put_env(key, value)
|
||||
|
||||
defp clear_smtp_env do
|
||||
System.delete_env("SMTP_HOST")
|
||||
System.delete_env("SMTP_PORT")
|
||||
|
|
@ -22,31 +26,64 @@ defmodule Mv.MailerSmtpConfigTest do
|
|||
System.delete_env("SMTP_SSL")
|
||||
end
|
||||
|
||||
describe "smtp_config/0" do
|
||||
describe "smtp_config/0 with Swoosh.Adapters.Test" do
|
||||
setup do
|
||||
previous = Application.get_env(:mv, Mv.Mailer)
|
||||
Application.put_env(:mv, Mv.Mailer, adapter: Swoosh.Adapters.Test)
|
||||
|
||||
on_exit(fn ->
|
||||
Application.put_env(:mv, Mv.Mailer, previous)
|
||||
end)
|
||||
|
||||
:ok
|
||||
end
|
||||
|
||||
test "returns empty list when SMTP_* ENV is set so the test adapter is not overridden" do
|
||||
System.put_env("SMTP_HOST", "smtp.example.com")
|
||||
System.put_env("SMTP_PORT", "587")
|
||||
System.put_env("SMTP_SSL", "tls")
|
||||
on_exit(fn -> clear_smtp_env() end)
|
||||
|
||||
assert Mailer.smtp_config() == []
|
||||
end
|
||||
end
|
||||
|
||||
describe "smtp_config/0 with a non-Test mailer adapter" do
|
||||
setup do
|
||||
previous = Application.get_env(:mv, Mv.Mailer)
|
||||
Application.put_env(:mv, Mv.Mailer, adapter: Swoosh.Adapters.Local)
|
||||
|
||||
on_exit(fn ->
|
||||
Application.put_env(:mv, Mv.Mailer, previous)
|
||||
end)
|
||||
|
||||
:ok
|
||||
end
|
||||
|
||||
test "port 587 (TLS): does not include :verify in sockopts so gen_tcp:connect does not crash" do
|
||||
set_smtp_env("SMTP_HOST", "smtp.example.com")
|
||||
set_smtp_env("SMTP_PORT", "587")
|
||||
set_smtp_env("SMTP_SSL", "tls")
|
||||
System.put_env("SMTP_HOST", "smtp.example.com")
|
||||
System.put_env("SMTP_PORT", "587")
|
||||
System.put_env("SMTP_SSL", "tls")
|
||||
on_exit(fn -> clear_smtp_env() end)
|
||||
|
||||
config = Mailer.smtp_config()
|
||||
|
||||
assert config != [], "expected non-empty config when SMTP_HOST is set"
|
||||
assert config != [],
|
||||
"expected non-empty config when SMTP_HOST is set and adapter is not Test"
|
||||
|
||||
sockopts = Keyword.get(config, :sockopts, [])
|
||||
|
||||
refute Keyword.has_key?(sockopts, :verify),
|
||||
"for 587 gen_tcp is used first; sockopts must not contain :verify"
|
||||
after
|
||||
clear_smtp_env()
|
||||
end
|
||||
|
||||
test "port 465 (SSL): includes :verify in sockopts so initial ssl:connect accepts verify mode" do
|
||||
set_smtp_env("SMTP_HOST", "smtp.example.com")
|
||||
set_smtp_env("SMTP_PORT", "465")
|
||||
set_smtp_env("SMTP_SSL", "ssl")
|
||||
System.put_env("SMTP_HOST", "smtp.example.com")
|
||||
System.put_env("SMTP_PORT", "465")
|
||||
System.put_env("SMTP_SSL", "ssl")
|
||||
on_exit(fn -> clear_smtp_env() end)
|
||||
|
||||
config = Mailer.smtp_config()
|
||||
|
||||
assert config != []
|
||||
sockopts = Keyword.get(config, :sockopts, [])
|
||||
|
||||
|
|
@ -54,36 +91,32 @@ defmodule Mv.MailerSmtpConfigTest do
|
|||
"for 465 initial connection is ssl:connect; sockopts must contain :verify"
|
||||
|
||||
assert Keyword.get(sockopts, :verify) in [:verify_none, :verify_peer]
|
||||
after
|
||||
clear_smtp_env()
|
||||
end
|
||||
|
||||
test "builds TLS mode for port 587 (STARTTLS)" do
|
||||
set_smtp_env("SMTP_HOST", "smtp.example.com")
|
||||
set_smtp_env("SMTP_PORT", "587")
|
||||
set_smtp_env("SMTP_SSL", "tls")
|
||||
System.put_env("SMTP_HOST", "smtp.example.com")
|
||||
System.put_env("SMTP_PORT", "587")
|
||||
System.put_env("SMTP_SSL", "tls")
|
||||
on_exit(fn -> clear_smtp_env() end)
|
||||
|
||||
config = Mailer.smtp_config()
|
||||
|
||||
assert config != []
|
||||
assert Keyword.get(config, :tls) == :always
|
||||
assert Keyword.get(config, :ssl) == false
|
||||
after
|
||||
clear_smtp_env()
|
||||
end
|
||||
|
||||
test "builds SSL mode for port 465 (implicit SSL)" do
|
||||
set_smtp_env("SMTP_HOST", "smtp.example.com")
|
||||
set_smtp_env("SMTP_PORT", "465")
|
||||
set_smtp_env("SMTP_SSL", "ssl")
|
||||
System.put_env("SMTP_HOST", "smtp.example.com")
|
||||
System.put_env("SMTP_PORT", "465")
|
||||
System.put_env("SMTP_SSL", "ssl")
|
||||
on_exit(fn -> clear_smtp_env() end)
|
||||
|
||||
config = Mailer.smtp_config()
|
||||
|
||||
assert config != []
|
||||
assert Keyword.get(config, :ssl) == true
|
||||
assert Keyword.get(config, :tls) == :never
|
||||
after
|
||||
clear_smtp_env()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue