Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
event_schema: Extract check_realm_update.
Steve Howella6796e9e8646
diff --git a/zerver/lib/event_schema.py b/zerver/lib/event_schema.pyindex c891b969489..641a2642eb7 100644--- a/zerver/lib/event_schema.py+++ b/zerver/lib/event_schema.py@@ -5,9 +5,18 @@ It will contain schemas (aka validators) for Zulip events.Right now it's only intended to be used by test code."""-from typing import Dict, Sequence, Tuple+from typing import Any, Dict, Sequence, Tuple-from zerver.lib.validator import Validator, check_dict_only, check_int+from zerver.lib.validator import (+ Validator,+ check_bool,+ check_dict_only,+ check_int,+ check_string,+ check_union,+ equals,+)+from zerver.models import Realmdef check_events_dict(@@ -35,3 +44,55 @@ def check_events_dict(required_keys=list(required_keys) + [("id", check_int)],optional_keys=optional_keys,)+++check_value = check_union(+ [+ # force vertical formatting+ check_bool,+ check_int,+ check_string,+ ]+)+++"""+realm/update events are flexible for values;+we will use a more strict checker to check+types in a context-specific manner+"""+_check_realm_update = check_events_dict(+ required_keys=[+ ("type", equals("realm")),+ ("op", equals("update")),+ ("property", check_string),+ ("value", check_value),+ ]+)+++def check_realm_update(var_name: str, event: Dict[str, Any],) -> None:+ """+ Realm updates have these two fields:++ property+ value++ We check not only the basic schema, but also that+ the value people actually matches the type from+ Realm.property_types that we have configured+ for the property.+ """+ _check_realm_update(var_name, event)+ prop = event["property"]+ value = event["value"]++ property_type = Realm.property_types[prop]+ if property_type in (bool, int, str):+ assert isinstance(value, property_type)+ elif property_type == (int, type(None)):+ assert isinstance(value, int)+ elif property_type == (str, type(None)):+ assert isinstance(value, str)+ else:+ raise AssertionError(f"Unexpected property type {property_type}")diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.pyindex 3615dcd66cd..0903762fdb2 100644--- a/zerver/tests/test_events.py+++ b/zerver/tests/test_events.py@@ -89,7 +89,7 @@ from zerver.lib.actions import (remove_members_from_user_group,try_update_realm_custom_profile_field,)-from zerver.lib.event_schema import check_events_dict+from zerver.lib.event_schema import check_events_dict, check_realm_updatefrom zerver.lib.events import apply_events, fetch_initial_state_data, post_process_statefrom zerver.lib.markdown import MentionDatafrom zerver.lib.message import render_markdown@@ -2471,24 +2471,7 @@ class RealmPropertyActionTest(BaseAction):vals = test_values.get(name)property_type = Realm.property_types[name]if property_type is bool:- validator: Validator[object] = check_boolvals = bool_tests- elif property_type is str:- validator = check_string- elif property_type == (str, type(None)):- validator = check_string- elif property_type is int:- validator = check_int- elif property_type == (int, type(None)):- validator = check_int- else:- raise AssertionError(f"Unexpected property type {property_type}")- schema_checker = check_events_dict([- ('type', equals('realm')),- ('op', equals('update')),- ('property', equals(name)),- ('value', validator),- ])if vals is None:raise AssertionError(f'No test created for {name}')@@ -2511,7 +2494,7 @@ class RealmPropertyActionTest(BaseAction):RealmAuditLog.OLD_VALUE: {'property': name, 'value': old_value},RealmAuditLog.NEW_VALUE: {'property': name, 'value': val}})).count(), 1)- schema_checker('events[0]', events[0])+ check_realm_update('events[0]', events[0])def test_change_realm_property(self) -> None:for prop in Realm.property_types: