89 lines
2.7 KiB
Elixir
89 lines
2.7 KiB
Elixir
defmodule Mv.MailerSmtpConfigTest do
|
|
@moduledoc """
|
|
Unit 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.
|
|
"""
|
|
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")
|
|
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" do
|
|
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")
|
|
|
|
config = Mailer.smtp_config()
|
|
|
|
assert config != [], "expected non-empty config when SMTP_HOST is set"
|
|
|
|
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")
|
|
|
|
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]
|
|
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")
|
|
|
|
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")
|
|
|
|
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
|