test: add tests for smtp mailer config

This commit is contained in:
Simon 2026-03-11 09:18:37 +01:00
parent f53a3ce3cc
commit c4135308e6
Signed by: simon
GPG key ID: 40E7A58C4AA1EDB2
9 changed files with 440 additions and 0 deletions

46
test/mv/mailer_test.exs Normal file
View file

@ -0,0 +1,46 @@
defmodule Mv.MailerTest do
@moduledoc """
Unit tests for Mv.Mailer, in particular send_test_email/1.
Uses Swoosh.Adapters.Test (configured in test.exs); no real SMTP. Asserts
success/error contract and that one test email is sent on success.
"""
use Mv.DataCase, async: true
import Swoosh.TestAssertions
alias Mv.Mailer
describe "send_test_email/1" do
test "returns {:ok, email} and sends one email with expected subject/body when successful" do
to_email = "test-#{System.unique_integer([:positive])}@example.com"
assert {:ok, _email} = Mailer.send_test_email(to_email)
assert_email_sent(fn email ->
to_addresses = Enum.map(email.to, &elem(&1, 1))
subject = email.subject || ""
body = email.html_body || email.text_body || ""
to_email in to_addresses and
(String.contains?(subject, "Test") or String.contains?(body, "test"))
end)
end
test "returns {:error, reason} for invalid email address" do
result = Mailer.send_test_email("not-an-email")
assert {:error, _reason} = result
end
test "uses mail_from as sender" do
to_email = "recipient-#{System.unique_integer([:positive])}@example.com"
assert {:ok, _} = Mailer.send_test_email(to_email)
assert_email_sent(fn email ->
{_name, from_email} = Mailer.mail_from()
from_addresses = Enum.map(email.from, &elem(&1, 1))
from_email in from_addresses
end)
end
end
end