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,
)
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(

View File

@ -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",

View File

@ -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