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.

event_schema: Extract check_update_global_notifications.

Steve Howell0a6ce36ac932
diff --git a/zerver/lib/event_schema.py b/zerver/lib/event_schema.py
index 6a0b5fc9721..b877a82d035 100644
--- a/zerver/lib/event_schema.py
+++ b/zerver/lib/event_schema.py
@@ -5,7 +5,7 @@ It will contain schemas (aka validators) for Zulip events.
Right now it's only intended to be used by test code.
"""
-from typing import Any, Dict, Sequence, Tuple
+from typing import Any, Dict, Sequence, Tuple, Union
from zerver.lib.validator import (
Validator,
@@ -297,3 +297,29 @@ def check_update_display_settings(var_name: str, event: Dict[str, Any],) -> None
assert "language_name" in event.keys()
else:
assert "language_name" not in event.keys()
+
+
+_check_update_global_notifications = check_events_dict(
+ required_keys=[
+ ("type", equals("update_global_notifications")),
+ ("notification_name", check_string),
+ ("setting", check_value),
+ ("user", check_string),
+ ]
+)
+
+
+def check_update_global_notifications(
+ var_name: str, event: Dict[str, Any], desired_val: Union[bool, int, str],
+) -> None:
+ """
+ See UserProfile.notification_setting_types for
+ more details.
+ """
+ _check_update_global_notifications(var_name, event)
+ setting_name = event["notification_name"]
+ setting = event["setting"]
+ assert setting == desired_val
+
+ setting_type = UserProfile.notification_setting_types[setting_name]
+ assert isinstance(setting, setting_type)
diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py
index 2b864447081..e863e15e1b4 100644
--- a/zerver/tests/test_events.py
+++ b/zerver/tests/test_events.py
@@ -100,6 +100,7 @@ from zerver.lib.event_schema import (
check_subscription_peer_remove,
check_subscription_remove,
check_update_display_settings,
+ check_update_global_notifications,
)
from zerver.lib.events import apply_events, fetch_initial_state_data, post_process_state
from zerver.lib.markdown import MentionData
@@ -1551,12 +1552,6 @@ class NormalActionsTest(BaseAction):
# These settings are tested in their own tests.
continue
- schema_checker = check_events_dict([
- ('type', equals('update_global_notifications')),
- ('notification_name', equals(notification_setting)),
- ('user', check_string),
- ('setting', check_bool),
- ])
do_change_notification_settings(self.user_profile, notification_setting, False)
for setting_value in [True, False]:
@@ -1566,7 +1561,7 @@ class NormalActionsTest(BaseAction):
notification_setting,
setting_value,
log=False))
- schema_checker('events[0]', events[0])
+ check_update_global_notifications('events[0]', events[0], setting_value)
# Also test with notification_settings_null=True
events = self.verify_action(
@@ -1574,16 +1569,10 @@ class NormalActionsTest(BaseAction):
self.user_profile, notification_setting, setting_value, log=False),
notification_settings_null=True,
state_change_expected=False)
- schema_checker('events[0]', events[0])
+ check_update_global_notifications('events[0]', events[0], setting_value)
def test_change_notification_sound(self) -> None:
notification_setting = "notification_sound"
- schema_checker = check_events_dict([
- ('type', equals('update_global_notifications')),
- ('notification_name', equals(notification_setting)),
- ('user', check_string),
- ('setting', equals("ding")),
- ])
events = self.verify_action(
lambda: do_change_notification_settings(
@@ -1591,16 +1580,10 @@ class NormalActionsTest(BaseAction):
notification_setting,
'ding',
log=False))
- schema_checker('events[0]', events[0])
+ check_update_global_notifications('events[0]', events[0], 'ding')
def test_change_desktop_icon_count_display(self) -> None:
notification_setting = "desktop_icon_count_display"
- schema_checker = check_events_dict([
- ('type', equals('update_global_notifications')),
- ('notification_name', equals(notification_setting)),
- ('user', check_string),
- ('setting', equals(2)),
- ])
events = self.verify_action(
lambda: do_change_notification_settings(
@@ -1608,14 +1591,7 @@ class NormalActionsTest(BaseAction):
notification_setting,
2,
log=False))
- schema_checker('events[0]', events[0])
-
- schema_checker = check_events_dict([
- ('type', equals('update_global_notifications')),
- ('notification_name', equals(notification_setting)),
- ('user', check_string),
- ('setting', equals(1)),
- ])
+ check_update_global_notifications('events[0]', events[0], 2)
events = self.verify_action(
lambda: do_change_notification_settings(
@@ -1623,7 +1599,7 @@ class NormalActionsTest(BaseAction):
notification_setting,
1,
log=False))
- schema_checker('events[0]', events[0])
+ check_update_global_notifications('events[0]', events[0], 1)
def test_realm_update_plan_type(self) -> None:
realm = self.user_profile.realm