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_server: Handle data of non existent realms.

Sahil Batraf0d429d0f920
diff --git a/zerver/lib/remote_server.py b/zerver/lib/remote_server.py
index e29f2950a36..4b82fa9a1be 100644
--- a/zerver/lib/remote_server.py
+++ b/zerver/lib/remote_server.py
@@ -392,7 +392,16 @@ def send_server_data_to_push_bouncer(consider_usage_statistics: bool = True) ->
assert isinstance(response["realms"], dict) # for mypy
realms = response["realms"]
for realm_uuid, data in realms.items():
- realm = Realm.objects.get(uuid=realm_uuid)
+ try:
+ realm = Realm.objects.get(uuid=realm_uuid)
+ except Realm.DoesNotExist:
+ # This occurs if the installation's database was rebuilt
+ # from scratch or a realm was hard-deleted from the local
+ # database, after generating secrets and talking to the
+ # bouncer.
+ logger.warning("Received unexpected realm UUID from bouncer %s", realm_uuid)
+ continue
+
do_set_realm_property(
realm, "push_notifications_enabled", data["can_push"], acting_user=None
)
diff --git a/zerver/tests/test_push_notifications.py b/zerver/tests/test_push_notifications.py
index 1094d689421..b422f122d51 100644
--- a/zerver/tests/test_push_notifications.py
+++ b/zerver/tests/test_push_notifications.py
@@ -1909,6 +1909,36 @@ class AnalyticsBouncerTest(BouncerTestCase):
],
)
+ @override_settings(PUSH_NOTIFICATION_BOUNCER_URL="https://push.zulip.org.example.com")
+ @responses.activate
+ def test_non_existent_realm_uuid(self) -> None:
+ self.add_mock_response()
+ realm_info = get_realms_info_for_push_bouncer()
+
+ # Hard-delete a realm to test the non existent realm uuid case.
+ realm = Realm.objects.order_by("id").first()
+ assert realm is not None
+ deleted_realm_uuid = realm.uuid
+ realm.delete()
+
+ with mock.patch(
+ "zerver.lib.remote_server.get_realms_info_for_push_bouncer", return_value=realm_info
+ ) as m, self.assertLogs("zulip.analytics", level="WARNING") as analytics_logger:
+ 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, None)
+
+ self.assertEqual(
+ analytics_logger.output,
+ [
+ "WARNING:zulip.analytics:"
+ f"Received unexpected realm UUID from bouncer {deleted_realm_uuid}"
+ ],
+ )
+
class PushNotificationTest(BouncerTestCase):
@override