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 attachment checkers.

Steve Howell78a2059b8d11
diff --git a/zerver/lib/event_schema.py b/zerver/lib/event_schema.py
index 5c8ff36ceea..46411ead885 100644
--- a/zerver/lib/event_schema.py
+++ b/zerver/lib/event_schema.py
@@ -121,6 +121,55 @@ alert_words_event = event_dict_type(
)
check_alert_words = make_checker(alert_words_event)
+attachment_message_type = DictType(
+ required_keys=[
+ # force vertical
+ ("id", int),
+ ("date_sent", int),
+ ]
+)
+
+attachment_type = DictType(
+ required_keys=[
+ ("id", int),
+ ("name", str),
+ ("size", int),
+ ("path_id", str),
+ ("create_time", int),
+ ("messages", ListType(attachment_message_type)),
+ ]
+)
+
+attachment_add_event = event_dict_type(
+ required_keys=[
+ ("type", Equals("attachment")),
+ ("op", Equals("add")),
+ ("attachment", attachment_type),
+ ("upload_space_used", int),
+ ]
+)
+check_attachment_add = make_checker(attachment_add_event)
+
+attachment_remove_event = event_dict_type(
+ required_keys=[
+ ("type", Equals("attachment")),
+ ("op", Equals("remove")),
+ ("attachment", DictType([("id", int)])),
+ ("upload_space_used", int),
+ ]
+)
+check_attachment_remove = make_checker(attachment_remove_event)
+
+attachment_update_event = event_dict_type(
+ required_keys=[
+ ("type", Equals("attachment")),
+ ("op", Equals("update")),
+ ("attachment", attachment_type),
+ ("upload_space_used", int),
+ ]
+)
+check_attachment_update = make_checker(attachment_update_event)
+
custom_profile_field_type = DictType(
required_keys=[
("id", int),
diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py
index 2a99ec5092c..8c41ce9cdee 100644
--- a/zerver/tests/test_events.py
+++ b/zerver/tests/test_events.py
@@ -91,6 +91,9 @@ from zerver.lib.actions import (
)
from zerver.lib.event_schema import (
check_alert_words,
+ check_attachment_add,
+ check_attachment_remove,
+ check_attachment_update,
check_custom_profile_fields,
check_default_stream_groups,
check_default_streams,
@@ -1760,23 +1763,6 @@ class NormalActionsTest(BaseAction):
self.assertEqual(result['max_message_id'], -1)
def test_add_attachment(self) -> None:
- schema_checker = check_events_dict([
- ('type', equals('attachment')),
- ('op', equals('add')),
- ('attachment', check_dict_only([
- ('id', check_int),
- ('name', check_string),
- ('size', check_int),
- ('path_id', check_string),
- ('create_time', check_int),
- ('messages', check_list(check_dict_only([
- ('id', check_int),
- ('date_sent', check_int),
- ]))),
- ])),
- ('upload_space_used', equals(6)),
- ])
-
self.login('hamlet')
fp = StringIO("zulip!")
fp.name = "zulip.txt"
@@ -1795,30 +1781,14 @@ class NormalActionsTest(BaseAction):
events = self.verify_action(
lambda: do_upload(),
num_events=1, state_change_expected=False)
- schema_checker('events[0]', events[0])
+
+ check_attachment_add("events[0]", events[0])
+ self.assertEqual(events[0]["upload_space_used"], 6)
# Verify that the DB has the attachment marked as unclaimed
entry = Attachment.objects.get(file_name='zulip.txt')
self.assertEqual(entry.is_claimed(), False)
- # Now we send an actual message using this attachment.
- schema_checker = check_events_dict([
- ('type', equals('attachment')),
- ('op', equals('update')),
- ('attachment', check_dict_only([
- ('id', check_int),
- ('name', check_string),
- ('size', check_int),
- ('path_id', check_string),
- ('create_time', check_int),
- ('messages', check_list(check_dict_only([
- ('id', check_int),
- ('date_sent', check_int),
- ]))),
- ])),
- ('upload_space_used', equals(6)),
- ])
-
hamlet = self.example_user("hamlet")
self.subscribe(hamlet, "Denmark")
assert uri is not None
@@ -1826,22 +1796,17 @@ class NormalActionsTest(BaseAction):
events = self.verify_action(
lambda: self.send_stream_message(self.example_user("hamlet"), "Denmark", body, "test"),
num_events=2)
- schema_checker('events[0]', events[0])
- # Now remove the attachment
- schema_checker = check_events_dict([
- ('type', equals('attachment')),
- ('op', equals('remove')),
- ('attachment', check_dict_only([
- ('id', check_int),
- ])),
- ('upload_space_used', equals(0)),
- ])
+ check_attachment_update("events[0]", events[0])
+ self.assertEqual(events[0]["upload_space_used"], 6)
+ # Now remove the attachment
events = self.verify_action(
lambda: self.client_delete(f"/json/attachments/{entry.id}"),
num_events=1, state_change_expected=False)
- schema_checker('events[0]', events[0])
+
+ check_attachment_remove("events[0]", events[0])
+ self.assertEqual(events[0]["upload_space_used"], 0)
def test_notify_realm_export(self) -> None:
do_change_user_role(self.user_profile, UserProfile.ROLE_REALM_ADMINISTRATOR)