Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
corporate: Replace unchecked cast calls with assert.
Anders Kaseorgb95fc4858de3
diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.pyindex 9704a1ba2ef..be12c3677da 100644--- a/corporate/lib/stripe.py+++ b/corporate/lib/stripe.py@@ -4,7 +4,7 @@ import osfrom datetime import datetime, timedeltafrom decimal import Decimalfrom functools import wraps-from typing import Any, Callable, Dict, Optional, Tuple, TypeVar, cast+from typing import Any, Callable, Dict, Optional, Tuple, TypeVarimport stripeimport ujson@@ -384,7 +384,8 @@ def process_initial_upgrade(user: UserProfile, licenses: int, automanage_licensestatement_descriptor='Zulip Standard')# Not setting a period start and end, but maybe we should? Unclear what will make things# most similar to the renewal case from an accounting perspective.- description = f"Payment (Card ending in {cast(stripe.Card, stripe_charge.source).last4})"+ assert isinstance(stripe_charge.source, stripe.Card)+ description = f"Payment (Card ending in {stripe_charge.source.last4})"stripe.InvoiceItem.create(amount=price_per_license * licenses * -1,currency='usd',diff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.pyindex 4b03168d9ec..b869e3752e8 100644--- a/corporate/tests/test_stripe.py+++ b/corporate/tests/test_stripe.py@@ -336,8 +336,9 @@ class StripeTestCase(ZulipTestCase):# Upgrade without talking to Stripedef local_upgrade(self, *args: Any) -> None:- class StripeMock:+ class StripeMock(Mock):def __init__(self, depth: int=1):+ super().__init__(spec=stripe.Card)self.id = 'id'self.created = '1000'self.last4 = '4242'@@ -1231,7 +1232,8 @@ class StripeTest(StripeTestCase):self.assertEqual(ujson.loads(response.content)['error_description'], 'card error')self.assert_json_error_contains(response, 'Your card was declined')for stripe_source in stripe_get_customer(stripe_customer_id).sources:- self.assertEqual(cast(stripe.Card, stripe_source).last4, '4242')+ assert isinstance(stripe_source, stripe.Card)+ self.assertEqual(stripe_source.last4, '4242')self.assertFalse(RealmAuditLog.objects.filter(event_type=RealmAuditLog.STRIPE_CARD_CHANGED).exists())# Replace with a card that's valid, but charging the card fails@@ -1243,7 +1245,8 @@ class StripeTest(StripeTestCase):self.assertEqual(ujson.loads(response.content)['error_description'], 'card error')self.assert_json_error_contains(response, 'Your card was declined')for stripe_source in stripe_get_customer(stripe_customer_id).sources:- self.assertEqual(cast(stripe.Card, stripe_source).last4, '0341')+ assert isinstance(stripe_source, stripe.Card)+ self.assertEqual(stripe_source.last4, '0341')self.assertEqual(len(list(stripe.Invoice.list(customer=stripe_customer_id, status='open'))), 1)self.assertEqual(1, RealmAuditLog.objects.filter(event_type=RealmAuditLog.STRIPE_CARD_CHANGED).count())@@ -1255,7 +1258,8 @@ class StripeTest(StripeTestCase):self.assert_json_success(response)number_of_sources = 0for stripe_source in stripe_get_customer(stripe_customer_id).sources:- self.assertEqual(cast(stripe.Card, stripe_source).last4, '4444')+ assert isinstance(stripe_source, stripe.Card)+ self.assertEqual(stripe_source.last4, '4444')number_of_sources += 1# Verify that we replaced the previous card, rather than adding a new oneself.assertEqual(number_of_sources, 1)