## Copyright © 2026 Olaf Kolkman ## SPDX-License-Identifier: GPL-3.0-or-later from unittest.mock import patch from backend.app.services.email_service import send_test_email, send_verification_email def test_send_verification_email_uses_smtp_settings(monkeypatch): from backend.app.core.config import settings monkeypatch.setattr(settings, 'smtp_host', 'smtp.example.com') monkeypatch.setattr(settings, 'smtp_port', 587) monkeypatch.setattr(settings, 'smtp_from', 'LinkLog ') monkeypatch.setattr(settings, 'smtp_username', 'mailer') monkeypatch.setattr(settings, 'smtp_password', 'secret') monkeypatch.setattr(settings, 'smtp_use_tls', True) with patch('backend.app.services.email_service.SMTP') as smtp_class: smtp = smtp_class.return_value.__enter__.return_value send_verification_email('user@example.com', 'user', 'https://linklog.example/verify') smtp_class.assert_called_once_with('smtp.example.com', 587, timeout=10) smtp.starttls.assert_called_once_with() smtp.login.assert_called_once_with('mailer', 'secret') message = smtp.send_message.call_args.args[0] assert message['To'] == 'user@example.com' assert 'https://linklog.example/verify' in message.get_content() def test_send_test_email_uses_configured_recipient(monkeypatch): from backend.app.core.config import settings monkeypatch.setattr(settings, 'smtp_host', 'smtp.example.com') monkeypatch.setattr(settings, 'smtp_from', 'LinkLog ') with patch('backend.app.services.email_service.SMTP') as smtp_class: smtp = smtp_class.return_value.__enter__.return_value send_test_email('admin@example.com') message = smtp.send_message.call_args.args[0] assert message['To'] == 'admin@example.com' assert message['Subject'] == 'LinkLog SMTP test'