event_schema: Extract check_delete_message.

There is a legacy format where we send
singular "message_id" instead of plural
"message_ids".

Then there are different fields for "private"
and "stream" message types.
This commit is contained in:
Steve Howell 2020-08-14 13:12:27 +00:00 committed by Tim Abbott
parent 88165aee6b
commit 6ec6525624
2 changed files with 79 additions and 40 deletions

View File

@ -218,6 +218,55 @@ default_streams_event = event_dict_type(
) )
check_default_streams = make_checker(default_streams_event) check_default_streams = make_checker(default_streams_event)
delete_message_event = event_dict_type(
required_keys=[
# force vertical
("type", Equals("delete_message")),
("message_type", EnumType(["private", "stream"])),
],
optional_keys=[
("message_id", int),
("message_ids", ListType(int)),
("stream_id", int),
("topic", str),
("recipient_id", int),
("sender_id", int),
],
)
_check_delete_message = make_checker(delete_message_event)
def check_delete_message(
var_name: str,
event: Dict[str, object],
message_type: str,
num_message_ids: int,
is_legacy: bool,
) -> None:
_check_delete_message(var_name, event)
keys = {"id", "type", "message_type"}
assert event["message_type"] == message_type
if message_type == "stream":
keys |= {"stream_id", "topic"}
elif message_type == "private":
keys |= {"recipient_id", "sender_id"}
else:
raise AssertionError("unexpected message_type")
if is_legacy:
assert num_message_ids == 1
keys.add("message_id")
else:
assert isinstance(event["message_ids"], list)
assert num_message_ids == len(event["message_ids"])
keys.add("message_ids")
assert set(event.keys()) == keys
_hotspot = DictType( _hotspot = DictType(
required_keys=[ required_keys=[
# force vertical # force vertical

View File

@ -97,6 +97,7 @@ from zerver.lib.event_schema import (
check_custom_profile_fields, check_custom_profile_fields,
check_default_stream_groups, check_default_stream_groups,
check_default_streams, check_default_streams,
check_delete_message,
check_events_dict, check_events_dict,
check_hotspots, check_hotspots,
check_invites_changed, check_invites_changed,
@ -147,14 +148,7 @@ from zerver.lib.test_helpers import (
stdout_suppressed, stdout_suppressed,
) )
from zerver.lib.topic import TOPIC_NAME from zerver.lib.topic import TOPIC_NAME
from zerver.lib.validator import ( from zerver.lib.validator import check_bool, check_dict_only, check_int, check_string, equals
check_bool,
check_dict_only,
check_int,
check_list,
check_string,
equals,
)
from zerver.models import ( from zerver.models import (
Attachment, Attachment,
Message, Message,
@ -1566,13 +1560,6 @@ class NormalActionsTest(BaseAction):
check_subscription_peer_add('events[1]', events[1]) check_subscription_peer_add('events[1]', events[1])
def test_do_delete_message_stream(self) -> None: def test_do_delete_message_stream(self) -> None:
schema_checker = check_events_dict([
('type', equals('delete_message')),
('message_ids', check_list(check_int, 2)),
('message_type', equals("stream")),
('stream_id', check_int),
('topic', check_string),
])
hamlet = self.example_user('hamlet') hamlet = self.example_user('hamlet')
msg_id = self.send_stream_message(hamlet, "Verona") msg_id = self.send_stream_message(hamlet, "Verona")
msg_id_2 = self.send_stream_message(hamlet, "Verona") msg_id_2 = self.send_stream_message(hamlet, "Verona")
@ -1584,20 +1571,19 @@ class NormalActionsTest(BaseAction):
lambda: do_delete_messages(self.user_profile.realm, messages), lambda: do_delete_messages(self.user_profile.realm, messages),
state_change_expected=True, state_change_expected=True,
) )
schema_checker('events[0]', events[0]) check_delete_message(
"events[0]",
events[0],
message_type="stream",
num_message_ids=2,
is_legacy=False,
)
def test_do_delete_message_stream_legacy(self) -> None: def test_do_delete_message_stream_legacy(self) -> None:
""" """
Test for legacy method of deleting messages which Test for legacy method of deleting messages which
sends an event per message to delete to the client. sends an event per message to delete to the client.
""" """
schema_checker = check_events_dict([
('type', equals('delete_message')),
('message_id', check_int),
('message_type', equals("stream")),
('stream_id', check_int),
('topic', check_string),
])
hamlet = self.example_user('hamlet') hamlet = self.example_user('hamlet')
msg_id = self.send_stream_message(hamlet, "Verona") msg_id = self.send_stream_message(hamlet, "Verona")
msg_id_2 = self.send_stream_message(hamlet, "Verona") msg_id_2 = self.send_stream_message(hamlet, "Verona")
@ -1610,16 +1596,15 @@ class NormalActionsTest(BaseAction):
state_change_expected=True, bulk_message_deletion=False, state_change_expected=True, bulk_message_deletion=False,
num_events=2 num_events=2
) )
schema_checker('events[0]', events[0]) check_delete_message(
"events[0]",
events[0],
message_type="stream",
num_message_ids=1,
is_legacy=True,
)
def test_do_delete_message_personal(self) -> None: def test_do_delete_message_personal(self) -> None:
schema_checker = check_events_dict([
('type', equals('delete_message')),
('message_ids', check_list(check_int, 1)),
('sender_id', check_int),
('message_type', equals("private")),
('recipient_id', check_int),
])
msg_id = self.send_personal_message( msg_id = self.send_personal_message(
self.example_user("cordelia"), self.example_user("cordelia"),
self.user_profile, self.user_profile,
@ -1630,16 +1615,15 @@ class NormalActionsTest(BaseAction):
lambda: do_delete_messages(self.user_profile.realm, [message]), lambda: do_delete_messages(self.user_profile.realm, [message]),
state_change_expected=True, state_change_expected=True,
) )
schema_checker('events[0]', events[0]) check_delete_message(
"events[0]",
events[0],
message_type="private",
num_message_ids=1,
is_legacy=False,
)
def test_do_delete_message_personal_legacy(self) -> None: def test_do_delete_message_personal_legacy(self) -> None:
schema_checker = check_events_dict([
('type', equals('delete_message')),
('message_id', check_int),
('sender_id', check_int),
('message_type', equals("private")),
('recipient_id', check_int),
])
msg_id = self.send_personal_message( msg_id = self.send_personal_message(
self.example_user("cordelia"), self.example_user("cordelia"),
self.user_profile, self.user_profile,
@ -1650,7 +1634,13 @@ class NormalActionsTest(BaseAction):
lambda: do_delete_messages(self.user_profile.realm, [message]), lambda: do_delete_messages(self.user_profile.realm, [message]),
state_change_expected=True, bulk_message_deletion=False state_change_expected=True, bulk_message_deletion=False
) )
schema_checker('events[0]', events[0]) check_delete_message(
"events[0]",
events[0],
message_type="private",
num_message_ids=1,
is_legacy=True,
)
def test_do_delete_message_no_max_id(self) -> None: def test_do_delete_message_no_max_id(self) -> None:
user_profile = self.example_user('aaron') user_profile = self.example_user('aaron')