mitgliederverwaltung/test/mv/mailer_test.exs

46 lines
1.5 KiB
Elixir

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