mitgliederverwaltung/test/mv/mailer_smtp_config_test.exs
Simon d7dd7d1959
Some checks failed
continuous-integration/drone/push Build is failing
fix: failing tests
2026-04-07 14:54:29 +02:00

122 lines
3.9 KiB
Elixir

defmodule Mv.MailerSmtpConfigTest do
@moduledoc """
Integration-style tests for `Mv.Mailer.smtp_config/0`.
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 clear_smtp_env do
System.delete_env("SMTP_HOST")
System.delete_env("SMTP_PORT")
System.delete_env("SMTP_USERNAME")
System.delete_env("SMTP_PASSWORD")
System.delete_env("SMTP_PASSWORD_FILE")
System.delete_env("SMTP_SSL")
end
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
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 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"
end
test "port 465 (SSL): includes :verify in sockopts so initial ssl:connect accepts verify mode" do
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, [])
assert Keyword.has_key?(sockopts, :verify),
"for 465 initial connection is ssl:connect; sockopts must contain :verify"
assert Keyword.get(sockopts, :verify) in [:verify_none, :verify_peer]
end
test "builds TLS mode for port 587 (STARTTLS)" 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)
config = Mailer.smtp_config()
assert config != []
assert Keyword.get(config, :tls) == :always
assert Keyword.get(config, :ssl) == false
end
test "builds SSL mode for port 465 (implicit SSL)" do
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
end
end
end