Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
home: Add `sponsorship_pending` page_param.
Aman Agrawalbe6f467f42fb
diff --git a/zerver/lib/home.py b/zerver/lib/home.pyindex 6767547e780..a0d0a43c239 100644--- a/zerver/lib/home.py+++ b/zerver/lib/home.py@@ -27,6 +27,7 @@ from zproject.config import get_configclass BillingInfo:show_billing: boolshow_plans: bool+ sponsorship_pending: bool@dataclass@@ -75,25 +76,35 @@ def promote_sponsoring_zulip_in_realm(realm: Realm) -> bool:def get_billing_info(user_profile: Optional[UserProfile]) -> BillingInfo:+ # See https://zulip.com/help/roles-and-permissions for clarity.show_billing = Falseshow_plans = False- if settings.CORPORATE_ENABLED and user_profile is not None:- if user_profile.has_billing_access:- from corporate.models import CustomerPlan, get_customer_by_realm-- customer = get_customer_by_realm(user_profile.realm)- if customer is not None:- if customer.sponsorship_pending:- show_billing = True- elif CustomerPlan.objects.filter(customer=customer).exists():- show_billing = True-- if not user_profile.is_guest and user_profile.realm.plan_type == Realm.PLAN_TYPE_LIMITED:+ sponsorship_pending = False+ # This query runs on home page load, so we want to avoid+ # hitting the database if possible. So, we only run it for the user+ # types that can actually see the billing info.+ if (+ settings.CORPORATE_ENABLED+ and user_profile is not None+ and (user_profile.has_billing_access or user_profile.is_realm_owner)+ ):+ from corporate.models import CustomerPlan, get_customer_by_realm++ customer = get_customer_by_realm(user_profile.realm)+ if customer is not None:+ if customer.sponsorship_pending:+ sponsorship_pending = True++ if CustomerPlan.objects.filter(customer=customer).exists():+ show_billing = True++ if user_profile.realm.plan_type == Realm.PLAN_TYPE_LIMITED:show_plans = Truereturn BillingInfo(show_billing=show_billing,show_plans=show_plans,+ sponsorship_pending=sponsorship_pending,)@@ -202,6 +213,7 @@ def build_page_params_for_home_page_load(show_billing=billing_info.show_billing,promote_sponsoring_zulip=promote_sponsoring_zulip_in_realm(realm),show_plans=billing_info.show_plans,+ sponsorship_pending=billing_info.sponsorship_pending,show_webathena=user_permission_info.show_webathena,# Adding two_fa_enabled as condition saves us 3 queries when# 2FA is not enabled.diff --git a/zerver/tests/test_home.py b/zerver/tests/test_home.pyindex 0bfc720abf6..2df8468382d 100644--- a/zerver/tests/test_home.py+++ b/zerver/tests/test_home.py@@ -212,6 +212,7 @@ class HomeTest(ZulipTestCase):"show_billing","show_plans","show_webathena",+ "sponsorship_pending","starred_messages","stop_words","subscriptions",@@ -363,6 +364,7 @@ class HomeTest(ZulipTestCase):"show_billing","show_plans","show_webathena",+ "sponsorship_pending","test_suite","translation_data","two_fa_enabled",@@ -837,6 +839,7 @@ class HomeTest(ZulipTestCase):billing_info = get_billing_info(user)self.assertFalse(billing_info.show_billing)self.assertFalse(billing_info.show_plans)+ self.assertFalse(billing_info.sponsorship_pending)# realm owner, with inactive CustomerPlan and realm plan_type SELF_HOSTED -> show only billing linkcustomer = Customer.objects.create(realm=get_realm("zulip"), stripe_customer_id="cus_id")@@ -852,6 +855,7 @@ class HomeTest(ZulipTestCase):billing_info = get_billing_info(user)self.assertTrue(billing_info.show_billing)self.assertFalse(billing_info.show_plans)+ self.assertFalse(billing_info.sponsorship_pending)# realm owner, with inactive CustomerPlan and realm plan_type LIMITED -> show billing link and plansdo_change_realm_plan_type(user.realm, Realm.PLAN_TYPE_LIMITED, acting_user=None)@@ -859,26 +863,31 @@ class HomeTest(ZulipTestCase):billing_info = get_billing_info(user)self.assertTrue(billing_info.show_billing)self.assertTrue(billing_info.show_plans)+ self.assertFalse(billing_info.sponsorship_pending)# Always false without CORPORATE_ENABLEDwith self.settings(CORPORATE_ENABLED=False):billing_info = get_billing_info(user)self.assertFalse(billing_info.show_billing)self.assertFalse(billing_info.show_plans)+ self.assertFalse(billing_info.sponsorship_pending)# Always false without a UserProfilewith self.settings(CORPORATE_ENABLED=True):billing_info = get_billing_info(None)self.assertFalse(billing_info.show_billing)self.assertFalse(billing_info.show_plans)+ self.assertFalse(billing_info.sponsorship_pending)- # realm admin, with CustomerPlan and realm plan_type LIMITED -> show only billing plans+ # realm admin, with CustomerPlan and realm plan_type LIMITED -> don't show any links+ # Only billing admin and realm owner have access to billing.user.role = UserProfile.ROLE_REALM_ADMINISTRATORuser.save(update_fields=["role"])with self.settings(CORPORATE_ENABLED=True):billing_info = get_billing_info(user)self.assertFalse(billing_info.show_billing)- self.assertTrue(billing_info.show_plans)+ self.assertFalse(billing_info.show_plans)+ self.assertFalse(billing_info.sponsorship_pending)# billing admin, with CustomerPlan and realm plan_type STANDARD -> show only billing linkuser.role = UserProfile.ROLE_MEMBER@@ -889,6 +898,7 @@ class HomeTest(ZulipTestCase):billing_info = get_billing_info(user)self.assertTrue(billing_info.show_billing)self.assertFalse(billing_info.show_plans)+ self.assertFalse(billing_info.sponsorship_pending)# billing admin, with CustomerPlan and realm plan_type PLUS -> show only billing linkdo_change_realm_plan_type(user.realm, Realm.PLAN_TYPE_PLUS, acting_user=None)@@ -897,6 +907,7 @@ class HomeTest(ZulipTestCase):billing_info = get_billing_info(user)self.assertTrue(billing_info.show_billing)self.assertFalse(billing_info.show_plans)+ self.assertFalse(billing_info.sponsorship_pending)# member, with CustomerPlan and realm plan_type STANDARD -> neither billing link or plansdo_change_realm_plan_type(user.realm, Realm.PLAN_TYPE_STANDARD, acting_user=None)@@ -906,6 +917,7 @@ class HomeTest(ZulipTestCase):billing_info = get_billing_info(user)self.assertFalse(billing_info.show_billing)self.assertFalse(billing_info.show_plans)+ self.assertFalse(billing_info.sponsorship_pending)# guest, with CustomerPlan and realm plan_type SELF_HOSTED -> neither billing link or plansuser.role = UserProfile.ROLE_GUEST@@ -915,6 +927,7 @@ class HomeTest(ZulipTestCase):billing_info = get_billing_info(user)self.assertFalse(billing_info.show_billing)self.assertFalse(billing_info.show_plans)+ self.assertFalse(billing_info.sponsorship_pending)# billing admin, but no CustomerPlan and realm plan_type SELF_HOSTED -> neither billing link or plansuser.role = UserProfile.ROLE_MEMBER@@ -925,21 +938,24 @@ class HomeTest(ZulipTestCase):billing_info = get_billing_info(user)self.assertFalse(billing_info.show_billing)self.assertFalse(billing_info.show_plans)+ self.assertFalse(billing_info.sponsorship_pending)- # billing admin, with sponsorship pending and relam plan_type SELF_HOSTED -> show only billing link+ # billing admin, with sponsorship pending and realm plan_type SELF_HOSTED -> show only sponsorship pending linkcustomer.sponsorship_pending = Truecustomer.save(update_fields=["sponsorship_pending"])with self.settings(CORPORATE_ENABLED=True):billing_info = get_billing_info(user)- self.assertTrue(billing_info.show_billing)+ self.assertFalse(billing_info.show_billing)self.assertFalse(billing_info.show_plans)+ self.assertTrue(billing_info.sponsorship_pending)- # billing admin, no customer object and relam plan_type SELF_HOSTED -> neither billing link or plans+ # billing admin, no customer object and realm plan_type SELF_HOSTED -> no linkscustomer.delete()with self.settings(CORPORATE_ENABLED=True):billing_info = get_billing_info(user)self.assertFalse(billing_info.show_billing)self.assertFalse(billing_info.show_plans)+ self.assertFalse(billing_info.sponsorship_pending)def test_promote_sponsoring_zulip_in_realm(self) -> None:realm = get_realm("zulip")