"""Tests for email service.""" from datetime import datetime, timedelta from unittest.mock import AsyncMock, patch import pytest from app.services.email_service import ( generate_booking_email, send_booking_notification, send_email, ) @pytest.mark.asyncio async def test_send_email_disabled(): """Test email sending when SMTP is disabled (default).""" result = await send_email("test@example.com", "Test Subject", "Test Body") assert result is True # Should succeed but only log @pytest.mark.asyncio async def test_send_email_with_smtp_mock(): """Test email sending with mocked SMTP.""" with patch("app.services.email_service.settings.smtp_enabled", True): with patch( "app.services.email_service.aiosmtplib.send", new_callable=AsyncMock ) as mock_send: result = await send_email("test@example.com", "Test", "Body") assert result is True mock_send.assert_called_once() def test_generate_booking_email_approved(test_booking, test_space): """Test email generation for approved booking.""" test_booking.space = test_space subject, body = generate_booking_email( test_booking, "approved", "user@example.com", "John Doe" ) assert subject == "Rezervare Aprobată" assert "John Doe" in body assert test_space.name in body assert "aprobată" in body def test_generate_booking_email_rejected(test_booking, test_space): """Test email generation for rejected booking with reason.""" test_booking.space = test_space subject, body = generate_booking_email( test_booking, "rejected", "user@example.com", "John Doe", extra_data={"rejection_reason": "Spațiul este în mentenanță"}, ) assert subject == "Rezervare Respinsă" assert "respinsă" in body assert "Spațiul este în mentenanță" in body def test_generate_booking_email_canceled(test_booking, test_space): """Test email generation for canceled booking.""" test_booking.space = test_space subject, body = generate_booking_email( test_booking, "canceled", "user@example.com", "John Doe", extra_data={"cancellation_reason": "Eveniment anulat"}, ) assert subject == "Rezervare Anulată" assert "anulată" in body assert "Eveniment anulat" in body def test_generate_booking_email_created(test_booking, test_space): """Test email generation for created booking (admin notification).""" test_booking.space = test_space subject, body = generate_booking_email( test_booking, "created", "admin@example.com", "Admin User" ) assert subject == "Cerere Nouă de Rezervare" assert "cerere de rezervare" in body assert test_space.name in body @pytest.mark.asyncio async def test_send_booking_notification(test_booking, test_space): """Test sending booking notification.""" test_booking.space = test_space result = await send_booking_notification( test_booking, "approved", "user@example.com", "John Doe" ) assert result is True