Add test for cart deactivation on crypto payment confirmation

Added test_finalize_invoice_deactivates_cart to verify that when a
cryptocurrency payment is confirmed, the user's active cart is replaced
with a new empty cart.

The test verifies:
- User starts with a non-empty cart
- After finalize_invoice(), user gets a new empty cart
- New cart has different ID than the old cart

This ensures the UX improvement works correctly - users see an empty
cart after their crypto payment is confirmed.
This commit is contained in:
Russell Ballestrini 2025-09-23 10:47:09 -04:00
parent 067107d3c0
commit 0b5b0d2461

View file

@ -310,6 +310,62 @@ class CryptoWatcherIntegrationTests(unittest.TestCase):
# Check inventory was deducted
self.assertEqual(inventory.quantity, 7) # 10 - 3
@patch("make_post_sell.lib.crypto_watcher.send_purchase_email")
@patch("make_post_sell.lib.crypto_watcher.send_sale_email")
def test_finalize_invoice_deactivates_cart(self, mock_sale_email, mock_purchase_email):
"""Test that finalizing invoice creates new empty cart for user."""
with transaction.manager:
dbsession = get_tm_session(self.session_factory, transaction.manager)
# Get objects from database
user = dbsession.query(User).filter_by(id=self.user_id).first()
shop = dbsession.query(Shop).filter_by(id=self.shop_id).first()
product = dbsession.query(Product).filter_by(id=self.product_id).first()
# Create and set up an active cart with products
old_cart = shop.create_new_cart_for_user(user)
old_cart.add_product(product)
dbsession.add(old_cart)
dbsession.flush()
# Verify cart has products
self.assertFalse(old_cart.is_empty)
old_cart_id = old_cart.id
# Create invoice and payment
invoice = Invoice(user)
invoice.shop = shop
invoice.new_line_item(product=product, quantity=1)
dbsession.add(invoice)
payment = CryptoPayment(
invoice=invoice,
address="test_cart_address",
account_index=0,
subaddress_index=1,
coin_type="XMR",
expected_amount=1000000000000, # 1 XMR in atomic units
rate_locked_usd_per_coin=Decimal("150.00"),
quote_expires_at_ms=int(time.time() * 1000) + 900000,
)
dbsession.add(payment)
dbsession.flush()
# Create mock request
mock_request = MagicMock()
mock_request.dbsession = dbsession
mock_request.registry.settings = {"app.email.enabled": "true"}
# Finalize the invoice
finalize_invoice(mock_request, payment)
dbsession.flush()
# Check that a new cart was created
new_cart = shop.get_active_cart_for_user(user)
self.assertIsNotNone(new_cart)
self.assertNotEqual(new_cart.id, old_cart_id)
self.assertTrue(new_cart.is_empty)
def test_process_payment_no_transfers(self):
"""Test processing payment with no incoming transfers."""
with transaction.manager: