126 lines
4.6 KiB
Python
126 lines
4.6 KiB
Python
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, UserSubscription
|
|
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_preserves_historical_snapshot() -> None:
|
|
tx_id = uuid4()
|
|
sub_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 = sub_id
|
|
|
|
# User later updated their phone & country in User Profile
|
|
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
|
|
|
|
# Historical invoice snapshot from time of purchase
|
|
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 = "India"
|
|
mock_invoice.billing_profile_id = None
|
|
mock_invoice.currency_code = "EUR"
|
|
mock_invoice.total_amount = Decimal("15.00")
|
|
mock_invoice.created_at = datetime(2026, 1, 15, tzinfo=timezone.utc)
|
|
|
|
# Profile also later synced to updated details
|
|
mock_profile = MagicMock(spec=UserBillingProfile)
|
|
mock_profile.billing_name = "Hans Schmidt"
|
|
mock_profile.billing_email = "hans@example.de"
|
|
mock_profile.billing_phone = "+491701122334"
|
|
mock_profile.country = "Germany"
|
|
mock_profile.address_line1 = "Not Configured"
|
|
mock_profile.city = "Not Configured"
|
|
mock_profile.state = "Not Configured"
|
|
mock_profile.postal_code = "000000"
|
|
|
|
# Subscription stores the frozen billing_phone at time of purchase
|
|
mock_sub = MagicMock(spec=UserSubscription)
|
|
mock_sub.id = sub_id
|
|
mock_sub.payment_reference = None
|
|
mock_sub.metadata_json = {"billing_phone": "+919999988888"}
|
|
|
|
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
|
|
r_sub = MagicMock()
|
|
r_sub.scalar_one_or_none.return_value = mock_sub
|
|
r_plan = MagicMock()
|
|
r_plan.scalar_one_or_none.return_value = None
|
|
|
|
mock_session.execute.side_effect = [r_tx, r_user, r_inv, r_prof, r_sub, r_plan]
|
|
|
|
svc = AdminPaymentService(mock_session)
|
|
response = await svc.render_invoice_html("pay_test123")
|
|
|
|
html = response.body.decode("utf-8")
|
|
assert "+919999988888" in html
|
|
assert "+491701122334" not in html
|
|
assert "India" in html
|