Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

remote_billing: Fix handle_customer_migration_from_server_to_realms.

Mateusz Mandera3a12e41c35f0
diff --git a/corporate/tests/test_remote_billing.py b/corporate/tests/test_remote_billing.py
index 4b87bb43ff7..e7548e43e79 100644
--- a/corporate/tests/test_remote_billing.py
+++ b/corporate/tests/test_remote_billing.py
@@ -23,6 +23,7 @@ from corporate.models import (
get_customer_by_remote_server,
)
from corporate.views.remote_billing_page import generate_confirmation_link_for_server_deactivation
+from zerver.actions.realm_settings import do_deactivate_realm
from zerver.lib.remote_server import send_server_data_to_push_bouncer
from zerver.lib.test_classes import BouncerTestCase
from zerver.lib.timestamp import datetime_to_timestamp
@@ -575,6 +576,9 @@ class RemoteBillingAuthenticationTest(RemoteRealmBillingTestCase):
# <Realm: zulipinternal 1>, <Realm: zephyr 3>, <Realm: lear 4>, <Realm: zulip 2>
self.assert_length(Realm.objects.all(), 4)
+ # Make lear deactivated, to have verification for that case.
+ do_deactivate_realm(Realm.objects.get(string_id="lear"), acting_user=None)
+
# Delete any existing remote realms.
RemoteRealm.objects.all().delete()
@@ -618,9 +622,11 @@ class RemoteBillingAuthenticationTest(RemoteRealmBillingTestCase):
self.assertIsNone(get_current_plan_by_customer(server_customer))
# Check legacy CustomerPlan exists for all realms except bot realm.
+ no_customer_plan_realms = set()
for remote_realm in RemoteRealm.objects.all():
- if remote_realm.is_system_bot_realm:
+ if remote_realm.is_system_bot_realm or remote_realm.realm_deactivated:
self.assertIsNone(get_customer_by_remote_realm(remote_realm))
+ no_customer_plan_realms.add(remote_realm.host.split(".")[0])
continue
customer = get_customer_by_remote_realm(remote_realm)
@@ -632,6 +638,7 @@ class RemoteBillingAuthenticationTest(RemoteRealmBillingTestCase):
self.assertEqual(plan.status, CustomerPlan.ACTIVE)
self.assertEqual(plan.billing_cycle_anchor, start_date)
self.assertEqual(plan.end_date, end_date)
+ self.assertEqual(no_customer_plan_realms, {"zulipinternal", "lear"})
@responses.activate
def test_transfer_legacy_plan_scheduled_for_upgrade_from_server_to_realm(
diff --git a/corporate/views/remote_billing_page.py b/corporate/views/remote_billing_page.py
index fde0588307f..63354c2d7c9 100644
--- a/corporate/views/remote_billing_page.py
+++ b/corporate/views/remote_billing_page.py
@@ -47,11 +47,7 @@ from zerver.lib.exceptions import (
RemoteBillingAuthenticationError,
RemoteRealmServerMismatchError,
)
-from zerver.lib.remote_server import (
- RealmDataForAnalytics,
- UserDataForRemoteBilling,
- get_realms_info_for_push_bouncer,
-)
+from zerver.lib.remote_server import RealmDataForAnalytics, UserDataForRemoteBilling
from zerver.lib.response import json_success
from zerver.lib.send_email import FromAddress, send_email
from zerver.lib.timestamp import datetime_to_timestamp
@@ -200,9 +196,7 @@ def remote_realm_billing_finalize_login(
raise AssertionError
try:
- handle_customer_migration_from_server_to_realms(
- server=remote_server, realms=get_realms_info_for_push_bouncer()
- )
+ handle_customer_migration_from_server_to_realms(server=remote_server)
except Exception: # nocoverage
billing_logger.exception(
"%s: Failed to migrate customer from server (id: %s) to realms",
diff --git a/zilencer/views.py b/zilencer/views.py
index 59f93706795..3b13f4009f9 100644
--- a/zilencer/views.py
+++ b/zilencer/views.py
@@ -858,25 +858,28 @@ def update_remote_realm_data_for_server(
RemoteRealmAuditLog.objects.bulk_create(remote_realm_audit_logs)
-def get_human_user_realm_uuids(realms: List[RealmDataForAnalytics]) -> List[UUID]:
- billable_realm_uuids = []
- for realm in realms:
- # TODO: Remove the `zulipinternal` string_id check once no server is on 8.0-beta.
- if (
- realm.is_system_bot_realm
- or realm.deactivated
- or realm.host.startswith("zulipinternal.")
- or (settings.DEVELOPMENT and realm.host.startswith("analytics."))
- ): # nocoverage
- continue
- billable_realm_uuids.append(realm.uuid)
+def get_human_user_realm_uuids(
+ server: RemoteZulipServer,
+) -> List[UUID]:
+ query = RemoteRealm.objects.filter(
+ server=server,
+ realm_deactivated=False,
+ registration_deactivated=False,
+ is_system_bot_realm=False,
+ ).exclude(
+ host__startswith="zulipinternal.",
+ )
+ if settings.DEVELOPMENT: # nocoverage
+ query = query.exclude(host__startswith="analytics.")
+
+ billable_realm_uuids = list(query.values_list("uuid", flat=True))
return billable_realm_uuids
@transaction.atomic
def handle_customer_migration_from_server_to_realms(
- server: RemoteZulipServer, realms: List[RealmDataForAnalytics]
+ server: RemoteZulipServer,
) -> None:
server_billing_session = RemoteServerBillingSession(server)
server_customer = server_billing_session.get_customer()
@@ -900,7 +903,7 @@ def handle_customer_migration_from_server_to_realms(
# migrate.
return
- realm_uuids = get_human_user_realm_uuids(realms)
+ realm_uuids = get_human_user_realm_uuids(server)
if not realm_uuids:
return