From 37b70a8e24934a78e2bbbfab365154e6d2953d41 Mon Sep 17 00:00:00 2001 From: Lauryn Menard Date: Mon, 30 Oct 2023 16:28:52 +0100 Subject: [PATCH] corporate: Exclude non-realm Customer objects in downgrade small realms. Updates query in downgrade_small_realms_behind_on_payments_as_needed to exclude Customer objects with realm=None. --- corporate/lib/stripe.py | 2 +- corporate/tests/test_stripe.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.py index 31a9a1f45f..45005e431b 100644 --- a/corporate/lib/stripe.py +++ b/corporate/lib/stripe.py @@ -1116,7 +1116,7 @@ def customer_has_last_n_invoices_open(customer: Customer, n: int) -> bool: def downgrade_small_realms_behind_on_payments_as_needed() -> None: - customers = Customer.objects.all().exclude(stripe_customer_id=None) + customers = Customer.objects.all().exclude(stripe_customer_id=None).exclude(realm=None) for customer in customers: realm = customer.realm assert realm is not None diff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.py index acafcb8d81..0ea8eecb15 100644 --- a/corporate/tests/test_stripe.py +++ b/corporate/tests/test_stripe.py @@ -3789,6 +3789,15 @@ class StripeTest(StripeTestCase): ) rows.append(Row(realm, Realm.PLAN_TYPE_STANDARD, plan, CustomerPlan.ACTIVE, False, False)) + # Customer objects without a realm should be excluded from query. + remote_server = RemoteZulipServer.objects.create( + uuid=str(uuid.uuid4()), + api_key="magic_secret_api_key", + hostname="demo.example.com", + contact_email="email@example.com", + ) + Customer.objects.create(remote_server=remote_server, stripe_customer_id="cus_xxx") + with patch("corporate.lib.stripe.void_all_open_invoices") as void_all_open_invoices_mock: downgrade_small_realms_behind_on_payments_as_needed()