from __future__ import annotations from datetime import datetime, timezone from decimal import Decimal from unittest.mock import AsyncMock, MagicMock from uuid import uuid4 import pytest from app.db.models import AppUser, UserBillingProfile, PaymentTransaction, Invoice from app.features.checkout.service import CheckoutService from app.features.admin.services.payment_service import AdminPaymentService @pytest.mark.asyncio async def test_checkout_billing_profile_syncs_updated_user_details() -> None: uid = uuid4() mock_user = MagicMock(spec=AppUser) mock_user.id = uid mock_user.display_name = "Updated User" mock_user.email = "user@example.com" mock_user.phone_number = "+491512345678" # New phone number mock_user.country = "Germany" # New country # Existing profile with OLD phone and country existing_profile = UserBillingProfile( id=uuid4(), user_id=uid, label="Default Auto-Generated", billing_name="Initial Name", billing_email="user@example.com", billing_phone="+919876543210", # Old phone number country="India", # Old country address_line1="Not Configured", city="Not Configured", state="Not Configured", postal_code="000000", is_default=True, ) mock_session = AsyncMock() mock_exec_res = MagicMock() mock_exec_res.scalar_one_or_none.return_value = existing_profile mock_session.execute.return_value = mock_exec_res mock_cart_repo = MagicMock() service = CheckoutService(session=mock_session, cart_repository=mock_cart_repo) profile = await service._billing_profile(mock_user, None) assert profile.billing_phone == "+491512345678" assert profile.country == "Germany" assert profile.billing_name == "Updated User" assert mock_session.flush.called @pytest.mark.asyncio async def test_render_invoice_html_uses_updated_phone_and_country() -> None: tx_id = uuid4() mock_tx = MagicMock(spec=PaymentTransaction) mock_tx.id = tx_id mock_tx.user_id = uuid4() mock_tx.gateway_transaction_id = "pay_test123" mock_tx.amount = Decimal("15.00") mock_tx.currency = "EUR" mock_tx.created_at = datetime(2026, 1, 15, tzinfo=timezone.utc) mock_tx.subscription_id = None mock_user = MagicMock(spec=AppUser) mock_user.display_name = "Hans Schmidt" mock_user.email = "hans@example.de" mock_user.phone_number = "+491701122334" # Updated phone mock_user.country = "Germany" # Updated country mock_invoice = MagicMock(spec=Invoice) mock_invoice.invoice_number = "INV-2026-0001" mock_invoice.billing_name = "Hans Schmidt" mock_invoice.billing_email = "hans@example.de" mock_invoice.billing_address = "Germany" mock_invoice.currency_code = "EUR" mock_invoice.total_amount = Decimal("15.00") mock_invoice.created_at = datetime(2026, 1, 15, tzinfo=timezone.utc) # Profile with older phone mock_profile = MagicMock(spec=UserBillingProfile) mock_profile.billing_name = "Hans Schmidt" mock_profile.billing_email = "hans@example.de" mock_profile.billing_phone = "+919999988888" mock_profile.country = "India" mock_profile.address_line1 = "Not Configured" mock_profile.city = "Not Configured" mock_profile.state = "Not Configured" mock_profile.postal_code = "000000" mock_session = AsyncMock() r_tx = MagicMock() r_tx.scalar_one_or_none.return_value = mock_tx r_user = MagicMock() r_user.scalar_one_or_none.return_value = mock_user r_inv = MagicMock() r_inv.scalar_one_or_none.return_value = mock_invoice r_prof = MagicMock() r_prof.scalar_one_or_none.return_value = mock_profile mock_session.execute.side_effect = [r_tx, r_user, r_inv, r_prof] svc = AdminPaymentService(mock_session) response = await svc.render_invoice_html("pay_test123") html = response.body.decode("utf-8") assert "+491701122334" in html assert "+919999988888" not in html assert "Germany" in html