diff --git a/analytics/tests/test_counts.py b/analytics/tests/test_counts.py index a1fcde8255..2a947a32d8 100644 --- a/analytics/tests/test_counts.py +++ b/analytics/tests/test_counts.py @@ -116,6 +116,10 @@ class AnalyticsTestCase(ZulipTestCase): # used as defaults in self.assert_table_count self.current_property: Optional[str] = None + # Delete RemoteRealm registrations to have a clean slate - the relevant + # tests want to construct this from scratch. + RemoteRealm.objects.all().delete() + # Lightweight creation of users, streams, and messages def create_user(self, **kwargs: Any) -> UserProfile: self.name_counter += 1 diff --git a/corporate/tests/test_remote_billing.py b/corporate/tests/test_remote_billing.py index ec7e7c18c9..7c6e18c2ce 100644 --- a/corporate/tests/test_remote_billing.py +++ b/corporate/tests/test_remote_billing.py @@ -90,6 +90,8 @@ class RemoteBillingAuthenticationTest(BouncerTestCase): @responses.activate def test_remote_billing_authentication_flow_realm_not_registered(self) -> None: + RemoteRealm.objects.all().delete() + self.login("desdemona") desdemona = self.example_user("desdemona") realm = desdemona.realm diff --git a/tools/test-backend b/tools/test-backend index e802f631c4..20537efadc 100755 --- a/tools/test-backend +++ b/tools/test-backend @@ -52,7 +52,6 @@ not_yet_fully_covered = [ "analytics/views/activity_common.py", "analytics/views/installation_activity.py", "analytics/views/realm_activity.py", - "analytics/views/remote_activity.py", "analytics/views/stats.py", "analytics/views/support.py", # TODO: This is a work in progress and therefore without diff --git a/zerver/lib/test_classes.py b/zerver/lib/test_classes.py index bb58511419..77479fbe52 100644 --- a/zerver/lib/test_classes.py +++ b/zerver/lib/test_classes.py @@ -2385,14 +2385,13 @@ def get_topic_messages(user_profile: UserProfile, stream: Stream, topic_name: st class BouncerTestCase(ZulipTestCase): @override def setUp(self) -> None: + # Set a deterministic uuid for convenience. self.server_uuid = "6cde5f7a-1f7e-4978-9716-49f69ebfc9fe" - self.server = RemoteZulipServer( - uuid=self.server_uuid, - api_key="magic_secret_api_key", - hostname="demo.example.com", - last_updated=timezone_now(), - ) + self.server = RemoteZulipServer.objects.all().latest("id") + + self.server.uuid = self.server_uuid self.server.save() + super().setUp() @override diff --git a/zerver/tests/test_push_notifications.py b/zerver/tests/test_push_notifications.py index 0ff6281852..7703117873 100644 --- a/zerver/tests/test_push_notifications.py +++ b/zerver/tests/test_push_notifications.py @@ -1287,6 +1287,9 @@ class AnalyticsBouncerTest(BouncerTestCase): handling for this edge case nonetheless. """ + # Start by deleting existing registration, to have a clean slate. + RemoteRealm.objects.all().delete() + second_server = RemoteZulipServer.objects.create( uuid=uuid.uuid4(), api_key="magic_secret_api_key2", diff --git a/zilencer/management/commands/populate_db.py b/zilencer/management/commands/populate_db.py index 227c6dba11..b4b713a45f 100644 --- a/zilencer/management/commands/populate_db.py +++ b/zilencer/management/commands/populate_db.py @@ -1,6 +1,7 @@ import itertools import os import random +import uuid from collections import defaultdict from datetime import datetime, timedelta from typing import Any, Dict, List, Mapping, Sequence, Tuple @@ -37,6 +38,7 @@ from zerver.lib.bulk_create import bulk_create_streams from zerver.lib.generate_test_data import create_test_data, generate_topics from zerver.lib.onboarding import create_if_missing_realm_internal_bots from zerver.lib.push_notifications import logger as push_notifications_logger +from zerver.lib.remote_server import get_realms_info_for_push_bouncer from zerver.lib.server_initialization import create_internal_realm, create_users from zerver.lib.storage import static_path from zerver.lib.stream_color import STREAM_ASSIGNMENT_COLORS @@ -72,6 +74,8 @@ from zerver.models import ( get_user_by_delivery_email, get_user_profile_by_id, ) +from zilencer.models import RemoteZulipServer +from zilencer.views import update_remote_realm_data_for_server settings.USING_TORNADO = False # Disable using memcached caches to avoid 'unsupported pickle @@ -373,6 +377,18 @@ class Command(BaseCommand): zulip_realm.wildcard_mention_policy = Realm.WILDCARD_MENTION_POLICY_MEMBERS zulip_realm.save(update_fields=["wildcard_mention_policy"]) + # Realms should have matching RemoteRealm entries - simulating having realms registered + # with the bouncer, which is going to be the primary case for modern servers. Tests + # wanting to have missing registrations, or simulating legacy server scenarios, + # should delete RemoteRealms to explicit set things up. + server = RemoteZulipServer.objects.create( + uuid=uuid.uuid4(), + api_key="magic_secret_api_key", + hostname="demo.example.com", + last_updated=timezone_now(), + ) + update_remote_realm_data_for_server(server, get_realms_info_for_push_bouncer()) + # Create test Users (UserProfiles are automatically created, # as are subscriptions to the ability to receive personals). names = [