Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
push_notification: Check users count for plans to be downgraded.
Sahil Batra63363ba50a3e
diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.pyindex fa2581a15f7..0a18bad162a 100644--- a/corporate/lib/stripe.py+++ b/corporate/lib/stripe.py@@ -4386,21 +4386,34 @@ def get_push_status_for_remote_request(else:current_plan = None- if current_plan is not None:- if current_plan.status in [- CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE,- CustomerPlan.DOWNGRADE_AT_END_OF_FREE_TRIAL,- ]:- # Plans scheduled to end- expected_end_timestamp = datetime_to_timestamp(- billing_session.get_next_billing_cycle(current_plan)+ user_count: Optional[int] = None+ if current_plan is None:+ try:+ user_count = billing_session.current_count_for_billed_licenses()+ except MissingDataError:+ return PushNotificationsEnabledStatus(+ can_push=False,+ expected_end_timestamp=None,+ message="Missing data",)++ if user_count > MAX_USERS_WITHOUT_PLAN:return PushNotificationsEnabledStatus(- can_push=True,- expected_end_timestamp=expected_end_timestamp,- message="Scheduled end",+ can_push=False,+ expected_end_timestamp=None,+ message="No plan many users",)+ return PushNotificationsEnabledStatus(+ can_push=True,+ expected_end_timestamp=None,+ message="No plan few users",+ )++ if current_plan.status not in [+ CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE,+ CustomerPlan.DOWNGRADE_AT_END_OF_FREE_TRIAL,+ ]:# Current plan, no expected end.return PushNotificationsEnabledStatus(can_push=True,@@ -4411,21 +4424,24 @@ def get_push_status_for_remote_request(try:user_count = billing_session.current_count_for_billed_licenses()except MissingDataError:- return PushNotificationsEnabledStatus(- can_push=False,- expected_end_timestamp=None,- message="Missing data",- )+ user_count = None- if user_count > MAX_USERS_WITHOUT_PLAN:+ if user_count is not None and user_count <= MAX_USERS_WITHOUT_PLAN:+ # We have an expiring plan, but we know we have few enough+ # users that once the plan expires, we will enter the "No plan+ # few users" case, so don't notify users about the plan+ # expiring via sending expected_end_timestamp.return PushNotificationsEnabledStatus(- can_push=False,+ can_push=True,expected_end_timestamp=None,- message="No plan many users",+ message="Expiring plan few users",)+ expected_end_timestamp = datetime_to_timestamp(+ billing_session.get_next_billing_cycle(current_plan)+ )return PushNotificationsEnabledStatus(can_push=True,- expected_end_timestamp=None,- message="No plan",+ expected_end_timestamp=expected_end_timestamp,+ message="Scheduled end",)diff --git a/zerver/tests/test_push_notifications.py b/zerver/tests/test_push_notifications.pyindex 41e4fbe75e0..259b23759cd 100644--- a/zerver/tests/test_push_notifications.py+++ b/zerver/tests/test_push_notifications.py@@ -94,6 +94,7 @@ from zerver.models.clients import get_clientfrom zerver.models.realms import get_realmfrom zerver.models.scheduled_jobs import NotificationTriggersfrom zerver.models.streams import get_stream+from zilencer.lib.remote_counts import MissingDataErrorfrom zilencer.models import RemoteZulipServerAuditLogfrom zilencer.views import DevicesToCleanUpDict@@ -2306,9 +2307,69 @@ class AnalyticsBouncerTest(BouncerTestCase):return_value=dummy_customer_plan,):with mock.patch(- "corporate.lib.stripe.RemoteRealmBillingSession.get_next_billing_cycle",- return_value=dummy_date,- ) as m, self.assertLogs("zulip.analytics", level="INFO") as info_log:+ "corporate.lib.stripe.RemoteRealmBillingSession.current_count_for_billed_licenses",+ return_value=11,+ ):+ with mock.patch(+ "corporate.lib.stripe.RemoteRealmBillingSession.get_next_billing_cycle",+ return_value=dummy_date,+ ) as m, self.assertLogs("zulip.analytics", level="INFO") as info_log:+ send_server_data_to_push_bouncer(consider_usage_statistics=False)+ m.assert_called()+ realms = Realm.objects.all()+ for realm in realms:+ self.assertEqual(realm.push_notifications_enabled, True)+ self.assertEqual(+ realm.push_notifications_enabled_end_timestamp,+ dummy_date,+ )+ self.assertIn(+ "INFO:zulip.analytics:Reported 0 records",+ info_log.output[0],+ )++ with mock.patch(+ "corporate.lib.stripe.RemoteRealmBillingSession.get_customer",+ return_value=dummy_customer,+ ):+ with mock.patch(+ "corporate.lib.stripe.get_current_plan_by_customer",+ return_value=dummy_customer_plan,+ ):+ with mock.patch(+ "corporate.lib.stripe.RemoteRealmBillingSession.current_count_for_billed_licenses",+ side_effect=MissingDataError,+ ):+ with mock.patch(+ "corporate.lib.stripe.RemoteRealmBillingSession.get_next_billing_cycle",+ return_value=dummy_date,+ ) as m, self.assertLogs("zulip.analytics", level="INFO") as info_log:+ send_server_data_to_push_bouncer(consider_usage_statistics=False)+ m.assert_called()+ realms = Realm.objects.all()+ for realm in realms:+ self.assertEqual(realm.push_notifications_enabled, True)+ self.assertEqual(+ realm.push_notifications_enabled_end_timestamp,+ dummy_date,+ )+ self.assertIn(+ "INFO:zulip.analytics:Reported 0 records",+ info_log.output[0],+ )++ with mock.patch(+ "corporate.lib.stripe.RemoteRealmBillingSession.get_customer",+ return_value=dummy_customer,+ ):+ with mock.patch(+ "corporate.lib.stripe.get_current_plan_by_customer",+ return_value=dummy_customer_plan,+ ):+ with mock.patch(+ "corporate.lib.stripe.RemoteRealmBillingSession.current_count_for_billed_licenses",+ return_value=10,+ ):send_server_data_to_push_bouncer(consider_usage_statistics=False)m.assert_called()realms = Realm.objects.all()@@ -2316,12 +2377,8 @@ class AnalyticsBouncerTest(BouncerTestCase):self.assertEqual(realm.push_notifications_enabled, True)self.assertEqual(realm.push_notifications_enabled_end_timestamp,- dummy_date,+ None,)- self.assertIn(- "INFO:zulip.analytics:Reported 0 records",- info_log.output[0],- )dummy_customer_plan = mock.MagicMock()dummy_customer_plan.status = CustomerPlan.ACTIVE