home: Add sponsorship_pending page_param.

This will be used in gear menu to inform admin of their
sponsorship application status.

This includes some additional tweaks for when to show
billing and plans to users.
This commit is contained in:
Aman Agrawal
2023-11-04 13:24:04 +00:00
committed by Tim Abbott
parent 0012a52e88
commit be6f467f42
2 changed files with 43 additions and 15 deletions

View File

@@ -27,6 +27,7 @@ from zproject.config import get_config
class BillingInfo:
show_billing: bool
show_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 = False
show_plans = False
if settings.CORPORATE_ENABLED and user_profile is not None:
if user_profile.has_billing_access:
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:
show_billing = True
elif CustomerPlan.objects.filter(customer=customer).exists():
sponsorship_pending = True
if 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:
if user_profile.realm.plan_type == Realm.PLAN_TYPE_LIMITED:
show_plans = True
return 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.

View File

@@ -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 link
customer = 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 plans
do_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_ENABLED
with 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 UserProfile
with 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_ADMINISTRATOR
user.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 link
user.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 link
do_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 plans
do_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 plans
user.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 plans
user.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 link
customer.sponsorship_pending = True
customer.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 links
customer.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")