Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
test-stripe: Add helper to check latest ledger license counts.
Lauryn Menardbc2ef58a7d08
diff --git a/corporate/lib/test_stripe_class.py b/corporate/lib/test_stripe_class.pyindex e055f00855a..9859fa5c0fc 100644--- a/corporate/lib/test_stripe_class.py+++ b/corporate/lib/test_stripe_class.py@@ -78,6 +78,7 @@ from corporate.lib.stripe import (stripe_get_customer,)from corporate.models.customers import Customer+from corporate.models.licenses import LicenseLedgerfrom corporate.models.plans import CustomerPlanfrom corporate.models.stripe_state import Invoicefrom zerver.actions.users import do_deactivate_user@@ -1046,3 +1047,12 @@ class StripeTestCase(ZulipTestCase):else:response = self.client_patch(url, info)return response++ def check_last_ledger_entry_license_counts(+ self, plan: CustomerPlan, licenses: int, licenses_at_next_renewal: int+ ) -> LicenseLedger:+ ledger_entry = LicenseLedger.objects.filter(plan=plan).order_by("-id").first()+ assert ledger_entry is not None+ self.assertEqual(ledger_entry.licenses, licenses)+ self.assertEqual(ledger_entry.licenses_at_next_renewal, licenses_at_next_renewal)+ return ledger_entrydiff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.pyindex ba678a59731..d1cd1188e18 100644--- a/corporate/tests/test_stripe.py+++ b/corporate/tests/test_stripe.py@@ -763,21 +763,11 @@ class StripeTest(StripeTestCase):billing_session = RealmBillingSession(user=user, realm=realm)with patch("corporate.lib.stripe.get_latest_seat_count", return_value=12):billing_session.update_license_ledger_if_needed(self.now)- self.assertEqual(- LicenseLedger.objects.order_by("-id")- .values_list("licenses", "licenses_at_next_renewal")- .first(),- (12, 12),- )+ self.check_last_ledger_entry_license_counts(plan, 12, 12)with patch("corporate.lib.stripe.get_latest_seat_count", return_value=15):billing_session.update_license_ledger_if_needed(self.next_month)- self.assertEqual(- LicenseLedger.objects.order_by("-id")- .values_list("licenses", "licenses_at_next_renewal")- .first(),- (15, 15),- )+ self.check_last_ledger_entry_license_counts(plan, 15, 15)invoice_plans_as_needed(self.next_month)self.assertFalse(stripe.Invoice.list(customer=stripe_customer.id))@@ -818,12 +808,8 @@ class StripeTest(StripeTestCase):with patch("corporate.lib.stripe.get_latest_seat_count", return_value=19):billing_session.update_license_ledger_if_needed(add_months(free_trial_end_date, 10))- self.assertEqual(- LicenseLedger.objects.order_by("-id")- .values_list("licenses", "licenses_at_next_renewal")- .first(),- (19, 19),- )+ self.check_last_ledger_entry_license_counts(plan, 19, 19)+# Fast forward next_invoice_date to 10 months from the free_trial_end_dateplan.next_invoice_date = add_months(free_trial_end_date, 10)plan.save(update_fields=["next_invoice_date"])@@ -1213,14 +1199,10 @@ class StripeTest(StripeTestCase):schedule=None,toggle_license_management=False,)- self.assertEqual(- LicenseLedger.objects.filter(plan=plan).latest("id").licenses_at_next_renewal, 123- )+ self.check_last_ledger_entry_license_counts(plan, 123, 123)with time_machine.travel(self.now, tick=False):self.billing_session.do_update_plan(update_plan_request)- self.assertEqual(- LicenseLedger.objects.filter(plan=plan).latest("id").licenses_at_next_renewal, 125- )+ self.check_last_ledger_entry_license_counts(plan, 123, 125)invoice_plans_as_needed(free_trial_end_date)customer_plan.refresh_from_db()@@ -1429,10 +1411,9 @@ class StripeTest(StripeTestCase):[item.amount for item in additional_license_invoice.lines],)# Check LicenseLedger has the new amount- ledger_entry = LicenseLedger.objects.last()- assert ledger_entry is not None- self.assertEqual(ledger_entry.licenses, new_seat_count)- self.assertEqual(ledger_entry.licenses_at_next_renewal, new_seat_count)+ plan = get_current_plan_by_customer(customer)+ assert plan is not None+ self.check_last_ledger_entry_license_counts(plan, new_seat_count, new_seat_count)@mock_stripe()def test_upgrade_by_card_with_outdated_lower_seat_count(self, *mocks: Mock) -> None:@@ -1474,10 +1455,9 @@ class StripeTest(StripeTestCase):[item.amount for item in upgrade_invoice.lines],)# Check LicenseLedger has the reduced license count at renewal- ledger_entry = LicenseLedger.objects.last()- assert ledger_entry is not None- self.assertEqual(ledger_entry.licenses, self.seat_count)- self.assertEqual(ledger_entry.licenses_at_next_renewal, new_seat_count)+ plan = get_current_plan_by_customer(customer)+ assert plan is not None+ self.check_last_ledger_entry_license_counts(plan, self.seat_count, new_seat_count)# Check that we informed the support team about the potential billing error.from django.core.mail import outbox@@ -1539,10 +1519,11 @@ class StripeTest(StripeTestCase):[item.amount for item in upgrade_invoice.lines],)# Check LicenseLedger has the minimum license count- ledger_entry = LicenseLedger.objects.last()- assert ledger_entry is not None- self.assertEqual(ledger_entry.licenses, minimum_for_plan_tier)- self.assertEqual(ledger_entry.licenses_at_next_renewal, minimum_for_plan_tier)+ plan = get_current_plan_by_customer(customer)+ assert plan is not None+ self.check_last_ledger_entry_license_counts(+ plan, minimum_for_plan_tier, minimum_for_plan_tier+ )@mock_stripe()def test_customer_minimum_licenses_for_plan(self, *mocks: Mock) -> None:@@ -1562,10 +1543,9 @@ class StripeTest(StripeTestCase):assert customer is not Noneassert customer.stripe_customer_id is not None# Check LicenseLedger has the current seat count.- ledger_entry = LicenseLedger.objects.last()- assert ledger_entry is not None- self.assertEqual(ledger_entry.licenses, self.seat_count)- self.assertEqual(ledger_entry.licenses_at_next_renewal, self.seat_count)+ plan = get_current_plan_by_customer(customer)+ assert plan is not None+ self.check_last_ledger_entry_license_counts(plan, self.seat_count, self.seat_count)# We manually set customer.minimum_licenses to the current seat count,# which is below the general Plus plan minimum licenses.@@ -2612,12 +2592,7 @@ class StripeTest(StripeTestCase):billing_session = RealmBillingSession(user=user, realm=user.realm)with patch("corporate.lib.stripe.get_latest_seat_count", return_value=20):billing_session.update_license_ledger_if_needed(self.now)- self.assertEqual(- LicenseLedger.objects.order_by("-id")- .values_list("licenses", "licenses_at_next_renewal")- .first(),- (20, 20),- )+ self.check_last_ledger_entry_license_counts(plan, 20, 20)# Verify that we invoice them for the additional usersmocked = self.setup_mocked_stripe(invoice_plans_as_needed, self.next_month)@@ -2632,12 +2607,8 @@ class StripeTest(StripeTestCase):assert plan is not Noneself.assertEqual(get_realm("zulip").plan_type, Realm.PLAN_TYPE_LIMITED)self.assertEqual(plan.status, CustomerPlan.ENDED)- self.assertEqual(- LicenseLedger.objects.order_by("-id")- .values_list("licenses", "licenses_at_next_renewal")- .first(),- (20, 20),- )+ self.check_last_ledger_entry_license_counts(plan, 20, 20)+realm_audit_log = RealmAuditLog.objects.latest("id")self.assertEqual(realm_audit_log.event_type, AuditLogEventType.REALM_PLAN_TYPE_CHANGED)self.assertEqual(realm_audit_log.acting_user, None)@@ -2645,12 +2616,7 @@ class StripeTest(StripeTestCase):# Verify that we don't write LicenseLedger rows once we've downgradedwith patch("corporate.lib.stripe.get_latest_seat_count", return_value=40):billing_session.update_license_ledger_if_needed(self.next_year)- self.assertEqual(- LicenseLedger.objects.order_by("-id")- .values_list("licenses", "licenses_at_next_renewal")- .first(),- (20, 20),- )+ self.check_last_ledger_entry_license_counts(plan, 20, 20)# Verify that we call invoice_plan once more after cycle end but# don't invoice them for users added after the cycle end@@ -2721,12 +2687,7 @@ class StripeTest(StripeTestCase):with patch("corporate.lib.stripe.get_latest_seat_count", return_value=20):billing_session.update_license_ledger_if_needed(self.now)self.assertEqual(LicenseLedger.objects.filter(plan=monthly_plan).count(), 2)- self.assertEqual(- LicenseLedger.objects.order_by("-id")- .values_list("licenses", "licenses_at_next_renewal")- .first(),- (20, 20),- )+ self.check_last_ledger_entry_license_counts(monthly_plan, 20, 20)with (time_machine.travel(self.next_month, tick=False),@@ -3006,12 +2967,7 @@ class StripeTest(StripeTestCase):with patch("corporate.lib.stripe.get_latest_seat_count", return_value=20):billing_session.update_license_ledger_if_needed(self.now)self.assertEqual(LicenseLedger.objects.filter(plan=annual_plan).count(), 2)- self.assertEqual(- LicenseLedger.objects.order_by("-id")- .values_list("licenses", "licenses_at_next_renewal")- .first(),- (20, 20),- )+ self.check_last_ledger_entry_license_counts(annual_plan, 20, 20)# Check that we don't switch to monthly plan at next invoice date (which is used to charge user for# additional licenses) but at the end of current billing cycle.@@ -3324,10 +3280,7 @@ class StripeTest(StripeTestCase):with patch("corporate.lib.stripe.get_latest_seat_count", return_value=21):billing_session.update_license_ledger_if_needed(self.now)- last_ledger_entry = LicenseLedger.objects.order_by("id").last()- assert last_ledger_entry is not None- self.assertEqual(last_ledger_entry.licenses, 21)- self.assertEqual(last_ledger_entry.licenses_at_next_renewal, 21)+ last_ledger_entry = self.check_last_ledger_entry_license_counts(plan, 21, 21)self.login_user(user)@@ -3417,12 +3370,7 @@ class StripeTest(StripeTestCase):# part of the cyclewith patch("corporate.lib.stripe.get_latest_seat_count", return_value=20):billing_session.update_license_ledger_if_needed(self.now)- self.assertEqual(- LicenseLedger.objects.order_by("-id")- .values_list("licenses", "licenses_at_next_renewal")- .first(),- (20, 20),- )+ self.check_last_ledger_entry_license_counts(plan, 20, 20)# Verify that we don't invoice them for the additional users during free trial.mocked = self.setup_mocked_stripe(invoice_plans_as_needed, self.next_month)@@ -3438,22 +3386,12 @@ class StripeTest(StripeTestCase):self.assertIsNone(plan.next_invoice_date)self.assertEqual(plan.status, CustomerPlan.ENDED)self.assertEqual(get_realm("zulip").plan_type, Realm.PLAN_TYPE_LIMITED)- self.assertEqual(- LicenseLedger.objects.order_by("-id")- .values_list("licenses", "licenses_at_next_renewal")- .first(),- (20, 20),- )+ self.check_last_ledger_entry_license_counts(plan, 20, 20)# Verify that we don't write LicenseLedger rows once we've downgradedwith patch("corporate.lib.stripe.get_latest_seat_count", return_value=40):billing_session.update_license_ledger_if_needed(self.next_year)- self.assertEqual(- LicenseLedger.objects.order_by("-id")- .values_list("licenses", "licenses_at_next_renewal")- .first(),- (20, 20),- )+ self.check_last_ledger_entry_license_counts(plan, 20, 20)self.login_user(user)response = self.client_get("/billing/")@@ -3732,24 +3670,26 @@ class StripeTest(StripeTestCase):self.login_user(user)customer = Customer.objects.get_or_create(realm=user.realm)[0]+ reduced_seat_count = get_latest_seat_count(user.realm) - 2customer.exempt_from_license_number_check = Truecustomer.save()+ paid_license_count = 100with time_machine.travel(self.now, tick=False):- self.local_upgrade(100, False, CustomerPlan.BILLING_SCHEDULE_ANNUAL, True, False)+ self.local_upgrade(+ paid_license_count, False, CustomerPlan.BILLING_SCHEDULE_ANNUAL, True, False+ )with time_machine.travel(self.now, tick=False):result = self.client_billing_patch("/billing/plan",- {"licenses_at_next_renewal": get_latest_seat_count(user.realm) - 2},+ {"licenses_at_next_renewal": reduced_seat_count},)self.assert_json_success(result)- latest_license_ledger = LicenseLedger.objects.last()- assert latest_license_ledger is not None- self.assertEqual(- latest_license_ledger.licenses_at_next_renewal, get_latest_seat_count(user.realm) - 2- )+ plan = get_current_plan_by_customer(customer)+ assert plan is not None+ self.check_last_ledger_entry_license_counts(plan, paid_license_count, reduced_seat_count)def test_upgrade_exempt_from_license_number_check_realm_less_licenses_than_seat_count(self,@@ -3774,10 +3714,9 @@ class StripeTest(StripeTestCase):reduced_seat_count, False, CustomerPlan.BILLING_SCHEDULE_ANNUAL, True, False)- latest_license_ledger = LicenseLedger.objects.last()- assert latest_license_ledger is not None- self.assertEqual(latest_license_ledger.licenses_at_next_renewal, reduced_seat_count)- self.assertEqual(latest_license_ledger.licenses, reduced_seat_count)+ plan = get_current_plan_by_customer(customer)+ assert plan is not None+ self.check_last_ledger_entry_license_counts(plan, reduced_seat_count, reduced_seat_count)def test_update_licenses_of_automatic_plan_from_billing_page(self) -> None:user = self.example_user("hamlet")@@ -3899,10 +3838,7 @@ class StripeTest(StripeTestCase):with patch("corporate.lib.stripe.get_latest_seat_count", return_value=20):billing_session.update_license_ledger_if_needed(self.now)- last_ledger_entry = LicenseLedger.objects.order_by("id").last()- assert last_ledger_entry is not None- self.assertEqual(last_ledger_entry.licenses, 20)- self.assertEqual(last_ledger_entry.licenses_at_next_renewal, 20)+ last_ledger_entry = self.check_last_ledger_entry_license_counts(plan, 20, 20)do_deactivate_realm(get_realm("zulip"),@@ -7033,23 +6969,21 @@ class TestRemoteRealmBillingFlow(StripeTestCase, RemoteRealmBillingTestCase):RemoteRealmAuditLog.objects.count(),min_licenses + 10 - realm_user_count + audit_log_count,)- latest_ledger = LicenseLedger.objects.last()- assert latest_ledger is not None- self.assertEqual(latest_ledger.licenses, min_licenses + 10)+ paid_license_count = min_licenses + 10+ self.check_last_ledger_entry_license_counts(plan, paid_license_count, paid_license_count)with time_machine.travel(self.now + timedelta(days=3), tick=False):response = self.client_get(f"{self.billing_session.billing_base_url}/billing/", subdomain="selfhosting")- self.assertEqual(latest_ledger.licenses, 35)for substring in ["Zulip Business","Number of licenses",- f"{latest_ledger.licenses}",+ f"{paid_license_count}","January 2, 2013","Your plan will automatically renew on",- f"${80 * latest_ledger.licenses:,.2f}",+ f"${80 * paid_license_count:,.2f}","Visa ending in 4242","Update card",]:@@ -7261,23 +7195,23 @@ class TestRemoteRealmBillingFlow(StripeTestCase, RemoteRealmBillingTestCase):RemoteRealmAuditLog.objects.count(),min_licenses + 10 - realm_user_count + audit_log_count,)- latest_ledger = LicenseLedger.objects.last()- assert latest_ledger is not None- self.assertEqual(latest_ledger.licenses, min_licenses + 10)+ paid_license_count = min_licenses + 10+ self.check_last_ledger_entry_license_counts(+ plan, paid_license_count, paid_license_count+ )with time_machine.travel(self.now + timedelta(days=3), tick=False):response = self.client_get(f"{self.billing_session.billing_base_url}/billing/", subdomain="selfhosting")- self.assertEqual(latest_ledger.licenses, min_licenses + 10)for substring in ["Zulip Basic","Number of licenses",- f"{latest_ledger.licenses}",+ f"{paid_license_count}","February 1, 2012","Your plan will automatically renew on",- f"${3.5 * latest_ledger.licenses - flat_discount // 100 * 1:,.2f}",+ f"${3.5 * paid_license_count - flat_discount // 100 * 1:,.2f}","Visa ending in 4242","Update card",]:@@ -7469,23 +7403,21 @@ class TestRemoteRealmBillingFlow(StripeTestCase, RemoteRealmBillingTestCase):RemoteRealmAuditLog.objects.count(),min_licenses + 10 - realm_user_count + audit_log_count,)- latest_ledger = LicenseLedger.objects.last()- assert latest_ledger is not None- self.assertEqual(latest_ledger.licenses, min_licenses + 10)+ paid_license_count = min_licenses + 10+ self.check_last_ledger_entry_license_counts(plan, paid_license_count, paid_license_count)with time_machine.travel(self.now + timedelta(days=3), tick=False):response = self.client_get(f"{self.billing_session.billing_base_url}/billing/", subdomain="selfhosting")- self.assertEqual(latest_ledger.licenses, min_licenses + 10)for substring in ["Zulip Basic","Number of licenses",- f"{latest_ledger.licenses}",+ f"{paid_license_count}","February 2, 2012","Your plan will automatically renew on",- f"${3.5 * latest_ledger.licenses - flat_discount // 100 * 1:,.2f}",+ f"${3.5 * paid_license_count - flat_discount // 100 * 1:,.2f}","Visa ending in 4242","Update card",]:@@ -9013,9 +8945,8 @@ class TestRemoteServerBillingFlow(StripeTestCase, RemoteServerTestCase):RemoteRealmAuditLog.objects.count(),audit_log_count + 8,)- latest_ledger = LicenseLedger.objects.last()- assert latest_ledger is not None- self.assertEqual(latest_ledger.licenses, server_user_count + 8)+ paid_license_count = server_user_count + 8+ self.check_last_ledger_entry_license_counts(plan, paid_license_count, paid_license_count)# Login againresult = self.execute_remote_billing_authentication_flow(@@ -9449,23 +9380,23 @@ class TestRemoteServerBillingFlow(StripeTestCase, RemoteServerTestCase):RemoteRealmAuditLog.objects.count(),audit_log_count + 10,)- latest_ledger = LicenseLedger.objects.last()- assert latest_ledger is not None- self.assertEqual(latest_ledger.licenses, 28)+ paid_license_count = realm_user_count + 10+ self.check_last_ledger_entry_license_counts(+ plan, paid_license_count, paid_license_count+ )with time_machine.travel(self.now + timedelta(days=3), tick=False):response = self.client_get(f"{self.billing_session.billing_base_url}/billing/", subdomain="selfhosting")- self.assertEqual(latest_ledger.licenses, 28)for substring in ["Zulip Basic","Number of licenses",- f"{latest_ledger.licenses}",+ f"{paid_license_count}","February 1, 2012","Your plan will automatically renew on",- f"${3.5 * latest_ledger.licenses - flat_discount // 100 * 1:,.2f}",+ f"${3.5 * paid_license_count - flat_discount // 100 * 1:,.2f}","Visa ending in 4242","Update card",]:@@ -9659,23 +9590,21 @@ class TestRemoteServerBillingFlow(StripeTestCase, RemoteServerTestCase):RemoteRealmAuditLog.objects.count(),audit_log_count + 10,)- latest_ledger = LicenseLedger.objects.last()- assert latest_ledger is not None- self.assertEqual(latest_ledger.licenses, 28)+ paid_license_count = server_user_count + 10+ self.check_last_ledger_entry_license_counts(plan, paid_license_count, paid_license_count)with time_machine.travel(self.now + timedelta(days=3), tick=False):response = self.client_get(f"{self.billing_session.billing_base_url}/billing/", subdomain="selfhosting")- self.assertEqual(latest_ledger.licenses, 28)for substring in ["Zulip Basic","Number of licenses",- f"{latest_ledger.licenses}",+ f"{paid_license_count}","February 2, 2012","Your plan will automatically renew on",- f"${3.5 * latest_ledger.licenses - flat_discount // 100 * 1:,.2f}",+ f"${3.5 * paid_license_count - flat_discount // 100 * 1:,.2f}","Visa ending in 4242","Update card",]: