diff --git a/zerver/lib/event_schema.py b/zerver/lib/event_schema.py index 9929b8e619..4b5583b5c2 100644 --- a/zerver/lib/event_schema.py +++ b/zerver/lib/event_schema.py @@ -178,6 +178,40 @@ def check_realm_bot_add(var_name: str, event: Dict[str, Any],) -> None: raise AssertionError(f"Unknown bot_type: {bot_type}") +_check_bot_for_delete = check_dict_only( + required_keys=[ + # for legacy reasons we have a dict here + # with only one key + ("user_id", check_int), + ] +) + +check_realm_bot_delete = check_events_dict( + required_keys=[ + ("type", equals("realm_bot")), + ("op", equals("delete")), + ("bot", _check_bot_for_delete), + ] +) + +_check_bot_for_remove = check_dict_only( + required_keys=[ + # Why does remove have full_name but delete doesn't? + # Why do we have both a remove and a delete event + # for bots? I don't know the answer as I write this. + ("full_name", check_string), + ("user_id", check_int), + ] +) + +check_realm_bot_remove = check_events_dict( + required_keys=[ + ("type", equals("realm_bot")), + ("op", equals("remove")), + ("bot", _check_bot_for_remove), + ] +) + _check_bot_for_update = check_dict_only( required_keys=[ # force vertical diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py index f936e9cd49..1b6ce643c6 100644 --- a/zerver/tests/test_events.py +++ b/zerver/tests/test_events.py @@ -93,6 +93,8 @@ from zerver.lib.event_schema import ( basic_stream_fields, check_events_dict, check_realm_bot_add, + check_realm_bot_delete, + check_realm_bot_remove, check_realm_bot_update, check_realm_update, check_stream_create, @@ -1848,19 +1850,12 @@ class NormalActionsTest(BaseAction): check_realm_bot_update('events[0]', events[0], 'owner_id') change_bot_owner_checker_user('events[1]', events[1]) - change_bot_owner_checker_bot = check_events_dict([ - ('type', equals('realm_bot')), - ('op', equals('delete')), - ('bot', check_dict_only([ - ('user_id', check_int), - ])), - ]) self.user_profile = self.example_user('aaron') owner = self.example_user('hamlet') bot = self.create_bot('test1', full_name='Test1 Testerson') action = lambda: do_change_bot_owner(bot, owner, self.user_profile) events = self.verify_action(action, num_events=2) - change_bot_owner_checker_bot('events[0]', events[0]) + check_realm_bot_delete('events[0]', events[0]) change_bot_owner_checker_user('events[1]', events[1]) previous_owner = self.example_user('aaron') @@ -1884,18 +1879,10 @@ class NormalActionsTest(BaseAction): check_realm_bot_update('events[0]', events[0], 'services') def test_do_deactivate_user(self) -> None: - bot_deactivate_checker = check_events_dict([ - ('type', equals('realm_bot')), - ('op', equals('remove')), - ('bot', check_dict_only([ - ('full_name', check_string), - ('user_id', check_int), - ])), - ]) bot = self.create_bot('test') action = lambda: do_deactivate_user(bot) events = self.verify_action(action, num_events=2) - bot_deactivate_checker('events[1]', events[1]) + check_realm_bot_remove('events[1]', events[1]) def test_do_reactivate_user(self) -> None: bot = self.create_bot('test')