remote_billing: Fix handle_customer_migration_from_server_to_realms.

This was a bug from 4715a058b0 where this
was just incorrectly called. get_realms_info_for_push_bouncer() is a
function meant to be called on a self-hosted server - and this
handle_... call happens on the bouncer. Therefore this returns all
zulipchat realms in product.

With the way, handle_... is being called right now, there's no reason
for it to have an argument for passing a list of realms. It should just
fetch the relevant RemoteRealm entries  by itself, given the server arg.
This commit is contained in:
Mateusz Mandera 2024-01-12 19:05:57 +01:00 committed by Tim Abbott
parent 40f99d0990
commit 3a12e41c35
3 changed files with 27 additions and 23 deletions

View File

@ -23,6 +23,7 @@ from corporate.models import (
get_customer_by_remote_server, get_customer_by_remote_server,
) )
from corporate.views.remote_billing_page import generate_confirmation_link_for_server_deactivation 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.remote_server import send_server_data_to_push_bouncer
from zerver.lib.test_classes import BouncerTestCase from zerver.lib.test_classes import BouncerTestCase
from zerver.lib.timestamp import datetime_to_timestamp 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> # <Realm: zulipinternal 1>, <Realm: zephyr 3>, <Realm: lear 4>, <Realm: zulip 2>
self.assert_length(Realm.objects.all(), 4) 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. # Delete any existing remote realms.
RemoteRealm.objects.all().delete() RemoteRealm.objects.all().delete()
@ -618,9 +622,11 @@ class RemoteBillingAuthenticationTest(RemoteRealmBillingTestCase):
self.assertIsNone(get_current_plan_by_customer(server_customer)) self.assertIsNone(get_current_plan_by_customer(server_customer))
# Check legacy CustomerPlan exists for all realms except bot realm. # Check legacy CustomerPlan exists for all realms except bot realm.
no_customer_plan_realms = set()
for remote_realm in RemoteRealm.objects.all(): 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)) self.assertIsNone(get_customer_by_remote_realm(remote_realm))
no_customer_plan_realms.add(remote_realm.host.split(".")[0])
continue continue
customer = get_customer_by_remote_realm(remote_realm) customer = get_customer_by_remote_realm(remote_realm)
@ -632,6 +638,7 @@ class RemoteBillingAuthenticationTest(RemoteRealmBillingTestCase):
self.assertEqual(plan.status, CustomerPlan.ACTIVE) self.assertEqual(plan.status, CustomerPlan.ACTIVE)
self.assertEqual(plan.billing_cycle_anchor, start_date) self.assertEqual(plan.billing_cycle_anchor, start_date)
self.assertEqual(plan.end_date, end_date) self.assertEqual(plan.end_date, end_date)
self.assertEqual(no_customer_plan_realms, {"zulipinternal", "lear"})
@responses.activate @responses.activate
def test_transfer_legacy_plan_scheduled_for_upgrade_from_server_to_realm( def test_transfer_legacy_plan_scheduled_for_upgrade_from_server_to_realm(

View File

@ -47,11 +47,7 @@ from zerver.lib.exceptions import (
RemoteBillingAuthenticationError, RemoteBillingAuthenticationError,
RemoteRealmServerMismatchError, RemoteRealmServerMismatchError,
) )
from zerver.lib.remote_server import ( from zerver.lib.remote_server import RealmDataForAnalytics, UserDataForRemoteBilling
RealmDataForAnalytics,
UserDataForRemoteBilling,
get_realms_info_for_push_bouncer,
)
from zerver.lib.response import json_success from zerver.lib.response import json_success
from zerver.lib.send_email import FromAddress, send_email from zerver.lib.send_email import FromAddress, send_email
from zerver.lib.timestamp import datetime_to_timestamp from zerver.lib.timestamp import datetime_to_timestamp
@ -200,9 +196,7 @@ def remote_realm_billing_finalize_login(
raise AssertionError raise AssertionError
try: try:
handle_customer_migration_from_server_to_realms( handle_customer_migration_from_server_to_realms(server=remote_server)
server=remote_server, realms=get_realms_info_for_push_bouncer()
)
except Exception: # nocoverage except Exception: # nocoverage
billing_logger.exception( billing_logger.exception(
"%s: Failed to migrate customer from server (id: %s) to realms", "%s: Failed to migrate customer from server (id: %s) to realms",

View File

@ -858,25 +858,28 @@ def update_remote_realm_data_for_server(
RemoteRealmAuditLog.objects.bulk_create(remote_realm_audit_logs) RemoteRealmAuditLog.objects.bulk_create(remote_realm_audit_logs)
def get_human_user_realm_uuids(realms: List[RealmDataForAnalytics]) -> List[UUID]: def get_human_user_realm_uuids(
billable_realm_uuids = [] server: RemoteZulipServer,
for realm in realms: ) -> List[UUID]:
# TODO: Remove the `zulipinternal` string_id check once no server is on 8.0-beta. query = RemoteRealm.objects.filter(
if ( server=server,
realm.is_system_bot_realm realm_deactivated=False,
or realm.deactivated registration_deactivated=False,
or realm.host.startswith("zulipinternal.") is_system_bot_realm=False,
or (settings.DEVELOPMENT and realm.host.startswith("analytics.")) ).exclude(
): # nocoverage host__startswith="zulipinternal.",
continue )
billable_realm_uuids.append(realm.uuid) if settings.DEVELOPMENT: # nocoverage
query = query.exclude(host__startswith="analytics.")
billable_realm_uuids = list(query.values_list("uuid", flat=True))
return billable_realm_uuids return billable_realm_uuids
@transaction.atomic @transaction.atomic
def handle_customer_migration_from_server_to_realms( def handle_customer_migration_from_server_to_realms(
server: RemoteZulipServer, realms: List[RealmDataForAnalytics] server: RemoteZulipServer,
) -> None: ) -> None:
server_billing_session = RemoteServerBillingSession(server) server_billing_session = RemoteServerBillingSession(server)
server_customer = server_billing_session.get_customer() server_customer = server_billing_session.get_customer()
@ -900,7 +903,7 @@ def handle_customer_migration_from_server_to_realms(
# migrate. # migrate.
return return
realm_uuids = get_human_user_realm_uuids(realms) realm_uuids = get_human_user_realm_uuids(server)
if not realm_uuids: if not realm_uuids:
return return