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.

anchor_value: Fix parsing of large anchor values.

Sundar Guntnurcbb7fb8ac0a4
diff --git a/zerver/tests/test_message_fetch.py b/zerver/tests/test_message_fetch.py
index 36b5938753e..2cad4255636 100644
--- a/zerver/tests/test_message_fetch.py
+++ b/zerver/tests/test_message_fetch.py
@@ -2668,6 +2668,90 @@ class GetOldMessagesTest(ZulipTestCase):
{unsub_message_id, muted_message_id, first_message_id, extra_message_id},
)
+ def test_parse_anchor_value(self) -> None:
+ hamlet = self.example_user('hamlet')
+ cordelia = self.example_user('cordelia')
+
+ # Send the first message to Hamlet
+ first_message_id = self.send_personal_message(cordelia, hamlet)
+
+ # Send another message
+ self.send_personal_message(cordelia, hamlet)
+
+ user_profile = hamlet
+
+ # Check if the anchor value in response is correct for different
+ # values of anchor parameter in request
+
+ # With anchor input as first_unread, see if response anchor
+ # value is same as the id of first unread message of Hamlet
+ query_params = dict(
+ anchor="first_unread",
+ num_before=10,
+ num_after=10,
+ narrow='[]',
+ )
+ request = POSTRequestMock(query_params, user_profile)
+
+ payload = get_messages_backend(request, user_profile)
+ result = orjson.loads(payload.content)
+ self.assertEqual(result['anchor'], first_message_id)
+
+ # With anchor input as oldest, see if response anchor value is 0
+ query_params = dict(
+ anchor="oldest",
+ num_before=10,
+ num_after=10,
+ narrow='[]',
+ )
+ request = POSTRequestMock(query_params, user_profile)
+
+ payload = get_messages_backend(request, user_profile)
+ result = orjson.loads(payload.content)
+ self.assertEqual(result['anchor'], 0)
+
+ # With anchor input as newest, see if response
+ # anchor value is LARGER_THAN_MAX_MESSAGE_ID
+ query_params = dict(
+ anchor="newest",
+ num_before=10,
+ num_after=10,
+ narrow='[]',
+ )
+ request = POSTRequestMock(query_params, user_profile)
+
+ payload = get_messages_backend(request, user_profile)
+ result = orjson.loads(payload.content)
+ self.assertEqual(result['anchor'], LARGER_THAN_MAX_MESSAGE_ID)
+
+ # With anchor input negative, see if
+ # response anchor value is clamped to 0
+ query_params = dict(
+ anchor="-1",
+ num_before=10,
+ num_after=10,
+ narrow='[]',
+ )
+ request = POSTRequestMock(query_params, user_profile)
+
+ payload = get_messages_backend(request, user_profile)
+ result = orjson.loads(payload.content)
+ self.assertEqual(result['anchor'], 0)
+
+ # With anchor input more than LARGER_THAN_MAX_MESSAGE_ID,
+ # see if response anchor value is clamped down to LARGER_THAN_MAX_MESSAGE_ID
+ query_params = dict(
+ anchor="10000000000000001",
+ num_before=10,
+ num_after=10,
+ narrow='[]',
+ )
+ request = POSTRequestMock(query_params, user_profile)
+
+ payload = get_messages_backend(request, user_profile)
+ result = orjson.loads(payload.content)
+ self.assertEqual(result['anchor'], LARGER_THAN_MAX_MESSAGE_ID)
+
def test_use_first_unread_anchor_with_some_unread_messages(self) -> None:
user_profile = self.example_user('hamlet')
diff --git a/zerver/views/message_fetch.py b/zerver/views/message_fetch.py
index 395c0c08758..2b6a0a68948 100644
--- a/zerver/views/message_fetch.py
+++ b/zerver/views/message_fetch.py
@@ -839,6 +839,8 @@ def parse_anchor_value(anchor_val: Optional[str],
anchor = int(anchor_val)
if anchor < 0:
return 0
+ elif anchor > LARGER_THAN_MAX_MESSAGE_ID:
+ return LARGER_THAN_MAX_MESSAGE_ID
return anchor
except ValueError:
raise JsonableError(_("Invalid anchor"))