Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
register_push_device: Send `push_device` event with "pending" status.
Prakhar Pratyush735809788d72
diff --git a/zerver/actions/push_notifications.py b/zerver/actions/push_notifications.pynew file mode 100644index 00000000000..3d185b60c91--- /dev/null+++ b/zerver/actions/push_notifications.py@@ -0,0 +1,20 @@+from zerver.models import UserProfile+from zerver.models.push_notifications import PushDevice+from zerver.tornado.django_api import send_event_on_commit+++def do_register_push_device(+ user_profile: UserProfile, push_account_id: int, token_kind: str, push_key_bytes: bytes+) -> None:+ PushDevice.objects.update_or_create(+ user=user_profile,+ push_account_id=push_account_id,+ defaults={"token_kind": token_kind, "push_key": push_key_bytes, "error_code": None},+ )++ event = dict(+ type="push_device",+ push_account_id=push_account_id,+ status="pending",+ )+ send_event_on_commit(user_profile.realm, event, [user_profile.id])diff --git a/zerver/tests/test_push_registration.py b/zerver/tests/test_push_registration.pyindex f930b538f6e..0bb1b5dcacd 100644--- a/zerver/tests/test_push_registration.py+++ b/zerver/tests/test_push_registration.py@@ -250,10 +250,13 @@ class RegisterPushDeviceToServer(BouncerTestCase):payload = self.get_register_push_device_payload()- # Verify the created `PushDevice` row while the- # `register_push_device_to_bouncer` event is still not- # consumed by the `PushNotificationsWorker` worker.- with mock_queue_publish("zerver.views.push_notifications.queue_event_on_commit") as m:+ # Verify the created `PushDevice` row and `push_device` event+ # while the `register_push_device_to_bouncer` event is still+ # not consumed by the `PushNotificationsWorker` worker.+ with (+ self.capture_send_event_calls(expected_num_events=1) as events,+ mock_queue_publish("zerver.views.push_notifications.queue_event_on_commit") as m,+ ):result = self.client_post("/json/mobile_push/register", payload)self.assert_json_success(result)m.assert_called_once()@@ -261,6 +264,10 @@ class RegisterPushDeviceToServer(BouncerTestCase):self.assert_length(push_devices, 1)self.assertIsNone(push_devices[0].bouncer_device_id)self.assertEqual(push_devices[0].status, "pending")+ self.assertEqual(+ events[0]["event"],+ dict(type="push_device", push_account_id=2408, status="pending"),+ )queue_name = m.call_args[0][0]queue_message = m.call_args[0][1]@@ -288,7 +295,7 @@ class RegisterPushDeviceToServer(BouncerTestCase):# instead of a `do_register_remote_push_device` function call.with (self.settings(ZILENCER_ENABLED=False),- self.capture_send_event_calls(expected_num_events=1) as events,+ self.capture_send_event_calls(expected_num_events=2) as events,):payload["push_account_id"] = 5555result = self.client_post("/json/mobile_push/register", payload)@@ -299,6 +306,10 @@ class RegisterPushDeviceToServer(BouncerTestCase):self.assertEqual(push_devices[1].status, "active")self.assertEqual(events[0]["event"],+ dict(type="push_device", push_account_id=5555, status="pending"),+ )+ self.assertEqual(+ events[1]["event"],dict(type="push_device", push_account_id=5555, status="active"),)@@ -353,7 +364,7 @@ class RegisterPushDeviceToServer(BouncerTestCase):# Verify InvalidBouncerPublicKeyErrorinvalid_bouncer_public_key_payload = {**payload, "bouncer_public_key": "invalid public key"}- with self.capture_send_event_calls(expected_num_events=1) as events:+ with self.capture_send_event_calls(expected_num_events=2) as events:result = self.client_post("/json/mobile_push/register", invalid_bouncer_public_key_payload)@@ -364,6 +375,10 @@ class RegisterPushDeviceToServer(BouncerTestCase):self.assertEqual(push_devices[0].status, "failed")self.assertEqual(events[0]["event"],+ dict(type="push_device", push_account_id=2408, status="pending"),+ )+ self.assertEqual(+ events[1]["event"],dict(type="push_device",push_account_id=2408,@@ -374,13 +389,21 @@ class RegisterPushDeviceToServer(BouncerTestCase):# Retrying with correct payload results in success.# `error_code` of the same PushDevice row updated to None.- with self.capture_send_event_calls(expected_num_events=1):+ with self.capture_send_event_calls(expected_num_events=2) as events:result = self.client_post("/json/mobile_push/register", payload)self.assert_json_success(result)push_devices = PushDevice.objects.all()self.assert_length(push_devices, 1)self.assertIsNone(push_devices[0].error_code)self.assertEqual(push_devices[0].status, "active")+ self.assertEqual(+ events[0]["event"],+ dict(type="push_device", push_account_id=2408, status="pending"),+ )+ self.assertEqual(+ events[1]["event"],+ dict(type="push_device", push_account_id=2408, status="active"),+ )@activate_push_notification_service()@override_settings(ZILENCER_ENABLED=False)@@ -395,16 +418,21 @@ class RegisterPushDeviceToServer(BouncerTestCase):# Verify InvalidEncryptedPushRegistrationErrorinvalid_token_payload = self.get_register_push_device_payload(token="")- with self.capture_send_event_calls(expected_num_events=1) as events:+ with self.capture_send_event_calls(expected_num_events=2) as events:result = self.client_post("/json/mobile_push/register", invalid_token_payload)self.assert_json_success(result)push_devices = PushDevice.objects.all()self.assert_length(push_devices, 1)+ self.assertEqual(push_devices[0].status, "failed")self.assertEqual(push_devices[0].error_code, InvalidEncryptedPushRegistrationError.code.name)self.assertEqual(events[0]["event"],+ dict(type="push_device", push_account_id=2408, status="pending"),+ )+ self.assertEqual(+ events[1]["event"],dict(type="push_device",push_account_id=2408,@@ -430,15 +458,20 @@ class RegisterPushDeviceToServer(BouncerTestCase):)with (self.assertLogs(level="ERROR") as m,- self.capture_send_event_calls(expected_num_events=1) as events,+ self.capture_send_event_calls(expected_num_events=2) as events,):result = self.client_post("/json/mobile_push/register", liveness_timed_out_payload)self.assert_json_success(result)push_devices = PushDevice.objects.all()self.assert_length(push_devices, 1)+ self.assertEqual(push_devices[0].status, "failed")self.assertEqual(push_devices[0].error_code, RequestExpiredError.code.name)self.assertEqual(events[0]["event"],+ dict(type="push_device", push_account_id=2408, status="pending"),+ )+ self.assertEqual(+ events[1]["event"],dict(type="push_device",push_account_id=2408,@@ -473,7 +506,7 @@ class RegisterPushDeviceToServer(BouncerTestCase):with (self.assertLogs(level="ERROR") as m,- self.capture_send_event_calls(expected_num_events=0),+ self.capture_send_event_calls(expected_num_events=1) as events,):result = self.client_post("/json/mobile_push/register", payload)self.assert_json_success(result)@@ -481,6 +514,10 @@ class RegisterPushDeviceToServer(BouncerTestCase):self.assert_length(push_devices, 1)# We keep retrying until `RequestExpiredError` is raised.self.assertEqual(push_devices[0].status, "pending")+ self.assertEqual(+ events[0]["event"],+ dict(type="push_device", push_account_id=2408, status="pending"),+ )self.assertEqual(m.output[0],f"ERROR:root:Push device registration request for user_id={hamlet.id}, push_account_id=2408 failed.",diff --git a/zerver/views/push_notifications.py b/zerver/views/push_notifications.pyindex 350e11ea1cd..35338599e2a 100644--- a/zerver/views/push_notifications.py+++ b/zerver/views/push_notifications.py@@ -8,6 +8,7 @@ from django.urls import reversefrom django.utils.translation import gettext as _from pydantic import Json+from zerver.actions.push_notifications import do_register_push_devicefrom zerver.decorator import human_users_only, zulip_login_requiredfrom zerver.lib import redis_utilsfrom zerver.lib.exceptions import (@@ -320,11 +321,7 @@ def register_push_device(if already_registered:return json_success(request)- PushDevice.objects.update_or_create(- user=user_profile,- push_account_id=push_account_id,- defaults={"token_kind": token_kind, "push_key": push_key_bytes, "error_code": None},- )+ do_register_push_device(user_profile, push_account_id, token_kind, push_key_bytes)# We use a queue worker to make the request to the bouncer# to complete the registration, so that any transient failures