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: class BillingInfo:
show_billing: bool show_billing: bool
show_plans: bool show_plans: bool
sponsorship_pending: bool
@dataclass @dataclass
@@ -75,25 +76,35 @@ def promote_sponsoring_zulip_in_realm(realm: Realm) -> bool:
def get_billing_info(user_profile: Optional[UserProfile]) -> BillingInfo: def get_billing_info(user_profile: Optional[UserProfile]) -> BillingInfo:
# See https://zulip.com/help/roles-and-permissions for clarity.
show_billing = False show_billing = False
show_plans = False show_plans = False
if settings.CORPORATE_ENABLED and user_profile is not None: sponsorship_pending = False
if user_profile.has_billing_access: # 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 from corporate.models import CustomerPlan, get_customer_by_realm
customer = get_customer_by_realm(user_profile.realm) customer = get_customer_by_realm(user_profile.realm)
if customer is not None: if customer is not None:
if customer.sponsorship_pending: if customer.sponsorship_pending:
show_billing = True sponsorship_pending = True
elif CustomerPlan.objects.filter(customer=customer).exists():
if CustomerPlan.objects.filter(customer=customer).exists():
show_billing = True 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 show_plans = True
return BillingInfo( return BillingInfo(
show_billing=show_billing, show_billing=show_billing,
show_plans=show_plans, 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, show_billing=billing_info.show_billing,
promote_sponsoring_zulip=promote_sponsoring_zulip_in_realm(realm), promote_sponsoring_zulip=promote_sponsoring_zulip_in_realm(realm),
show_plans=billing_info.show_plans, show_plans=billing_info.show_plans,
sponsorship_pending=billing_info.sponsorship_pending,
show_webathena=user_permission_info.show_webathena, show_webathena=user_permission_info.show_webathena,
# Adding two_fa_enabled as condition saves us 3 queries when # Adding two_fa_enabled as condition saves us 3 queries when
# 2FA is not enabled. # 2FA is not enabled.

View File

@@ -212,6 +212,7 @@ class HomeTest(ZulipTestCase):
"show_billing", "show_billing",
"show_plans", "show_plans",
"show_webathena", "show_webathena",
"sponsorship_pending",
"starred_messages", "starred_messages",
"stop_words", "stop_words",
"subscriptions", "subscriptions",
@@ -363,6 +364,7 @@ class HomeTest(ZulipTestCase):
"show_billing", "show_billing",
"show_plans", "show_plans",
"show_webathena", "show_webathena",
"sponsorship_pending",
"test_suite", "test_suite",
"translation_data", "translation_data",
"two_fa_enabled", "two_fa_enabled",
@@ -837,6 +839,7 @@ class HomeTest(ZulipTestCase):
billing_info = get_billing_info(user) billing_info = get_billing_info(user)
self.assertFalse(billing_info.show_billing) self.assertFalse(billing_info.show_billing)
self.assertFalse(billing_info.show_plans) 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 # 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") 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) billing_info = get_billing_info(user)
self.assertTrue(billing_info.show_billing) self.assertTrue(billing_info.show_billing)
self.assertFalse(billing_info.show_plans) 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 # 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) 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) billing_info = get_billing_info(user)
self.assertTrue(billing_info.show_billing) self.assertTrue(billing_info.show_billing)
self.assertTrue(billing_info.show_plans) self.assertTrue(billing_info.show_plans)
self.assertFalse(billing_info.sponsorship_pending)
# Always false without CORPORATE_ENABLED # Always false without CORPORATE_ENABLED
with self.settings(CORPORATE_ENABLED=False): with self.settings(CORPORATE_ENABLED=False):
billing_info = get_billing_info(user) billing_info = get_billing_info(user)
self.assertFalse(billing_info.show_billing) self.assertFalse(billing_info.show_billing)
self.assertFalse(billing_info.show_plans) self.assertFalse(billing_info.show_plans)
self.assertFalse(billing_info.sponsorship_pending)
# Always false without a UserProfile # Always false without a UserProfile
with self.settings(CORPORATE_ENABLED=True): with self.settings(CORPORATE_ENABLED=True):
billing_info = get_billing_info(None) billing_info = get_billing_info(None)
self.assertFalse(billing_info.show_billing) self.assertFalse(billing_info.show_billing)
self.assertFalse(billing_info.show_plans) 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.role = UserProfile.ROLE_REALM_ADMINISTRATOR
user.save(update_fields=["role"]) user.save(update_fields=["role"])
with self.settings(CORPORATE_ENABLED=True): with self.settings(CORPORATE_ENABLED=True):
billing_info = get_billing_info(user) billing_info = get_billing_info(user)
self.assertFalse(billing_info.show_billing) 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 # billing admin, with CustomerPlan and realm plan_type STANDARD -> show only billing link
user.role = UserProfile.ROLE_MEMBER user.role = UserProfile.ROLE_MEMBER
@@ -889,6 +898,7 @@ class HomeTest(ZulipTestCase):
billing_info = get_billing_info(user) billing_info = get_billing_info(user)
self.assertTrue(billing_info.show_billing) self.assertTrue(billing_info.show_billing)
self.assertFalse(billing_info.show_plans) 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 # 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) 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) billing_info = get_billing_info(user)
self.assertTrue(billing_info.show_billing) self.assertTrue(billing_info.show_billing)
self.assertFalse(billing_info.show_plans) 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 # 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) 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) billing_info = get_billing_info(user)
self.assertFalse(billing_info.show_billing) self.assertFalse(billing_info.show_billing)
self.assertFalse(billing_info.show_plans) 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 # guest, with CustomerPlan and realm plan_type SELF_HOSTED -> neither billing link or plans
user.role = UserProfile.ROLE_GUEST user.role = UserProfile.ROLE_GUEST
@@ -915,6 +927,7 @@ class HomeTest(ZulipTestCase):
billing_info = get_billing_info(user) billing_info = get_billing_info(user)
self.assertFalse(billing_info.show_billing) self.assertFalse(billing_info.show_billing)
self.assertFalse(billing_info.show_plans) 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 # billing admin, but no CustomerPlan and realm plan_type SELF_HOSTED -> neither billing link or plans
user.role = UserProfile.ROLE_MEMBER user.role = UserProfile.ROLE_MEMBER
@@ -925,21 +938,24 @@ class HomeTest(ZulipTestCase):
billing_info = get_billing_info(user) billing_info = get_billing_info(user)
self.assertFalse(billing_info.show_billing) self.assertFalse(billing_info.show_billing)
self.assertFalse(billing_info.show_plans) 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.sponsorship_pending = True
customer.save(update_fields=["sponsorship_pending"]) customer.save(update_fields=["sponsorship_pending"])
with self.settings(CORPORATE_ENABLED=True): with self.settings(CORPORATE_ENABLED=True):
billing_info = get_billing_info(user) 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.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() customer.delete()
with self.settings(CORPORATE_ENABLED=True): with self.settings(CORPORATE_ENABLED=True):
billing_info = get_billing_info(user) billing_info = get_billing_info(user)
self.assertFalse(billing_info.show_billing) self.assertFalse(billing_info.show_billing)
self.assertFalse(billing_info.show_plans) self.assertFalse(billing_info.show_plans)
self.assertFalse(billing_info.sponsorship_pending)
def test_promote_sponsoring_zulip_in_realm(self) -> None: def test_promote_sponsoring_zulip_in_realm(self) -> None:
realm = get_realm("zulip") realm = get_realm("zulip")