2020-06-21 02:36:20 +02:00
|
|
|
import re
|
2020-06-11 00:54:34 +02:00
|
|
|
from typing import Any, Dict
|
2018-05-21 15:23:46 +02:00
|
|
|
|
2020-08-07 01:09:47 +02:00
|
|
|
import orjson
|
2020-06-21 02:36:20 +02:00
|
|
|
from django.core.exceptions import ValidationError
|
2021-06-13 17:00:45 +02:00
|
|
|
from django.http import HttpResponse
|
2018-08-22 20:38:34 +02:00
|
|
|
|
2018-05-21 15:23:46 +02:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.validator import check_widget_content
|
2021-06-13 17:00:45 +02:00
|
|
|
from zerver.lib.widget import get_widget_data, get_widget_type
|
|
|
|
from zerver.models import SubMessage, UserProfile
|
2018-08-23 14:51:17 +02:00
|
|
|
|
2018-05-21 15:23:46 +02:00
|
|
|
|
|
|
|
class WidgetContentTestCase(ZulipTestCase):
|
|
|
|
def test_validation(self) -> None:
|
|
|
|
def assert_error(obj: object, msg: str) -> None:
|
2020-06-21 02:36:20 +02:00
|
|
|
with self.assertRaisesRegex(ValidationError, re.escape(msg)):
|
|
|
|
check_widget_content(obj)
|
2018-05-21 15:23:46 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
assert_error(5, "widget_content is not a dict")
|
2018-05-21 15:23:46 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
assert_error({}, "widget_type is not in widget_content")
|
2018-05-21 15:23:46 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
assert_error(dict(widget_type="whatever"), "extra_data is not in widget_content")
|
2018-05-21 15:23:46 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
assert_error(dict(widget_type="zform", extra_data=4), "extra_data is not a dict")
|
2018-05-21 15:23:46 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
assert_error(dict(widget_type="bogus", extra_data={}), "unknown widget type: bogus")
|
2018-05-21 15:23:46 +02:00
|
|
|
|
2020-09-02 08:14:51 +02:00
|
|
|
extra_data: Dict[str, Any] = {}
|
2021-02-12 08:20:45 +01:00
|
|
|
obj = dict(widget_type="zform", extra_data=extra_data)
|
2018-05-21 15:23:46 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
assert_error(obj, "zform is missing type field")
|
2018-05-21 15:23:46 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
extra_data["type"] = "bogus"
|
|
|
|
assert_error(obj, "unknown zform type: bogus")
|
2018-05-21 15:23:46 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
extra_data["type"] = "choices"
|
|
|
|
assert_error(obj, "heading key is missing from extra_data")
|
2018-05-21 15:23:46 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
extra_data["heading"] = "whatever"
|
|
|
|
assert_error(obj, "choices key is missing from extra_data")
|
2018-05-21 15:23:46 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
extra_data["choices"] = 99
|
2018-05-21 15:23:46 +02:00
|
|
|
assert_error(obj, 'extra_data["choices"] is not a list')
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
extra_data["choices"] = [99]
|
2018-05-21 15:23:46 +02:00
|
|
|
assert_error(obj, 'extra_data["choices"][0] is not a dict')
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
extra_data["choices"] = [
|
|
|
|
dict(long_name="foo", reply="bar"),
|
2018-05-21 15:23:46 +02:00
|
|
|
]
|
|
|
|
assert_error(obj, 'short_name key is missing from extra_data["choices"][0]')
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
extra_data["choices"] = [
|
|
|
|
dict(short_name="a", long_name="foo", reply="bar"),
|
2018-05-21 15:23:46 +02:00
|
|
|
]
|
|
|
|
|
2020-06-21 02:36:20 +02:00
|
|
|
check_widget_content(obj)
|
2018-08-13 16:55:14 +02:00
|
|
|
|
|
|
|
def test_message_error_handling(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
sender = self.example_user("cordelia")
|
|
|
|
stream_name = "Verona"
|
2018-08-13 16:55:14 +02:00
|
|
|
|
|
|
|
payload = dict(
|
|
|
|
type="stream",
|
|
|
|
to=stream_name,
|
2021-02-12 08:20:45 +01:00
|
|
|
client="test suite",
|
|
|
|
topic="whatever",
|
|
|
|
content="whatever",
|
2018-08-13 16:55:14 +02:00
|
|
|
)
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
payload["widget_content"] = "{{{{{{" # unparsable
|
2020-03-10 11:48:26 +01:00
|
|
|
result = self.api_post(sender, "/api/v1/messages", payload)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assert_json_error_contains(result, "Widgets: API programmer sent invalid JSON")
|
2018-08-13 16:55:14 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
bogus_data = dict(color="red", foo="bar", x=2)
|
|
|
|
payload["widget_content"] = orjson.dumps(bogus_data).decode()
|
2020-03-10 11:48:26 +01:00
|
|
|
result = self.api_post(sender, "/api/v1/messages", payload)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assert_json_error_contains(result, "Widgets: widget_type is not in widget_content")
|
2018-08-22 20:38:34 +02:00
|
|
|
|
2018-08-23 14:51:17 +02:00
|
|
|
def test_get_widget_data_for_non_widget_messages(self) -> None:
|
|
|
|
# This is a pretty important test, despite testing the
|
|
|
|
# "negative" case. We never want widgets to interfere
|
|
|
|
# with normal messages.
|
|
|
|
|
|
|
|
test_messages = [
|
2021-02-12 08:20:45 +01:00
|
|
|
"",
|
|
|
|
" ",
|
|
|
|
"this is an ordinary message",
|
|
|
|
"/bogus_command",
|
|
|
|
"/me shrugs",
|
|
|
|
"use /poll",
|
2018-08-23 14:51:17 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
for message in test_messages:
|
|
|
|
self.assertEqual(get_widget_data(content=message), (None, None))
|
|
|
|
|
|
|
|
# Add a positive check for context
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(get_widget_data(content="/todo"), ("todo", None))
|
2018-08-23 14:51:17 +02:00
|
|
|
|
2018-08-23 15:19:43 +02:00
|
|
|
def test_explicit_widget_content(self) -> None:
|
|
|
|
# Users can send widget_content directly on messages
|
|
|
|
# using the `widget_content` field.
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
sender = self.example_user("cordelia")
|
|
|
|
stream_name = "Verona"
|
|
|
|
content = "does-not-matter"
|
2018-08-23 15:19:43 +02:00
|
|
|
zform_data = dict(
|
2021-02-12 08:20:45 +01:00
|
|
|
type="choices",
|
|
|
|
heading="Options:",
|
2018-08-23 15:19:43 +02:00
|
|
|
choices=[],
|
|
|
|
)
|
|
|
|
|
2020-06-25 01:28:06 +02:00
|
|
|
widget_content = dict(
|
2021-02-12 08:20:45 +01:00
|
|
|
widget_type="zform",
|
2020-06-25 01:28:06 +02:00
|
|
|
extra_data=zform_data,
|
2018-08-23 15:19:43 +02:00
|
|
|
)
|
|
|
|
|
2020-06-25 01:28:06 +02:00
|
|
|
check_widget_content(widget_content)
|
|
|
|
|
2018-08-23 15:19:43 +02:00
|
|
|
payload = dict(
|
|
|
|
type="stream",
|
|
|
|
to=stream_name,
|
2021-02-12 08:20:45 +01:00
|
|
|
client="test suite",
|
|
|
|
topic="whatever",
|
2018-08-23 15:19:43 +02:00
|
|
|
content=content,
|
2020-08-07 01:09:47 +02:00
|
|
|
widget_content=orjson.dumps(widget_content).decode(),
|
2018-08-23 15:19:43 +02:00
|
|
|
)
|
2020-03-10 11:48:26 +01:00
|
|
|
result = self.api_post(sender, "/api/v1/messages", payload)
|
2018-08-23 15:19:43 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
message = self.get_last_message()
|
|
|
|
self.assertEqual(message.content, content)
|
|
|
|
|
|
|
|
expected_submessage_content = dict(
|
|
|
|
widget_type="zform",
|
|
|
|
extra_data=zform_data,
|
|
|
|
)
|
|
|
|
|
|
|
|
submessage = SubMessage.objects.get(message_id=message.id)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(submessage.msg_type, "widget")
|
2020-08-07 01:09:47 +02:00
|
|
|
self.assertEqual(orjson.loads(submessage.content), expected_submessage_content)
|
2018-08-23 15:19:43 +02:00
|
|
|
|
2020-10-31 17:23:08 +01:00
|
|
|
def test_todo(self) -> None:
|
|
|
|
# This also helps us get test coverage that could apply
|
2018-08-23 14:40:24 +02:00
|
|
|
# to future widgets.
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
sender = self.example_user("cordelia")
|
|
|
|
stream_name = "Verona"
|
|
|
|
content = "/todo"
|
2018-08-23 14:40:24 +02:00
|
|
|
|
|
|
|
payload = dict(
|
|
|
|
type="stream",
|
|
|
|
to=stream_name,
|
2021-02-12 08:20:45 +01:00
|
|
|
client="test suite",
|
|
|
|
topic="whatever",
|
2018-08-23 14:40:24 +02:00
|
|
|
content=content,
|
|
|
|
)
|
2020-03-10 11:48:26 +01:00
|
|
|
result = self.api_post(sender, "/api/v1/messages", payload)
|
2018-08-23 14:40:24 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
message = self.get_last_message()
|
|
|
|
self.assertEqual(message.content, content)
|
|
|
|
|
|
|
|
expected_submessage_content = dict(
|
2020-10-31 17:23:08 +01:00
|
|
|
widget_type="todo",
|
2018-08-23 14:40:24 +02:00
|
|
|
extra_data=None,
|
|
|
|
)
|
|
|
|
|
|
|
|
submessage = SubMessage.objects.get(message_id=message.id)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(submessage.msg_type, "widget")
|
2020-08-07 01:09:47 +02:00
|
|
|
self.assertEqual(orjson.loads(submessage.content), expected_submessage_content)
|
2018-08-23 14:40:24 +02:00
|
|
|
|
2018-08-22 20:38:34 +02:00
|
|
|
def test_poll_command_extra_data(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
sender = self.example_user("cordelia")
|
|
|
|
stream_name = "Verona"
|
2019-01-28 18:37:28 +01:00
|
|
|
# We test for both trailing and leading spaces, along with blank lines
|
|
|
|
# for the poll options.
|
2021-02-12 08:20:45 +01:00
|
|
|
content = "/poll What is your favorite color?\n\nRed\nGreen \n\n Blue\n - Yellow"
|
2018-08-22 20:38:34 +02:00
|
|
|
|
|
|
|
payload = dict(
|
|
|
|
type="stream",
|
|
|
|
to=stream_name,
|
2021-02-12 08:20:45 +01:00
|
|
|
client="test suite",
|
|
|
|
topic="whatever",
|
2018-08-22 20:38:34 +02:00
|
|
|
content=content,
|
|
|
|
)
|
2020-03-10 11:48:26 +01:00
|
|
|
result = self.api_post(sender, "/api/v1/messages", payload)
|
2018-08-22 20:38:34 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
message = self.get_last_message()
|
|
|
|
self.assertEqual(message.content, content)
|
|
|
|
|
|
|
|
expected_submessage_content = dict(
|
|
|
|
widget_type="poll",
|
|
|
|
extra_data=dict(
|
2021-02-12 08:20:45 +01:00
|
|
|
options=["Red", "Green", "Blue", "Yellow"],
|
2018-08-22 20:38:34 +02:00
|
|
|
question="What is your favorite color?",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
submessage = SubMessage.objects.get(message_id=message.id)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(submessage.msg_type, "widget")
|
2020-08-07 01:09:47 +02:00
|
|
|
self.assertEqual(orjson.loads(submessage.content), expected_submessage_content)
|
2018-08-22 20:38:34 +02:00
|
|
|
|
|
|
|
# Now don't supply a question.
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
content = "/poll"
|
|
|
|
payload["content"] = content
|
2020-03-10 11:48:26 +01:00
|
|
|
result = self.api_post(sender, "/api/v1/messages", payload)
|
2018-08-22 20:38:34 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
expected_submessage_content = dict(
|
|
|
|
widget_type="poll",
|
|
|
|
extra_data=dict(
|
2019-01-28 18:37:28 +01:00
|
|
|
options=[],
|
2021-02-12 08:20:45 +01:00
|
|
|
question="",
|
2018-08-22 20:38:34 +02:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
message = self.get_last_message()
|
|
|
|
self.assertEqual(message.content, content)
|
|
|
|
submessage = SubMessage.objects.get(message_id=message.id)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(submessage.msg_type, "widget")
|
2020-08-07 01:09:47 +02:00
|
|
|
self.assertEqual(orjson.loads(submessage.content), expected_submessage_content)
|
2021-06-13 17:00:45 +02:00
|
|
|
|
|
|
|
def test_poll_permissions(self) -> None:
|
|
|
|
cordelia = self.example_user("cordelia")
|
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
stream_name = "Verona"
|
|
|
|
content = "/poll Preference?\n\nyes\nno"
|
|
|
|
|
|
|
|
payload = dict(
|
|
|
|
type="stream",
|
|
|
|
to=stream_name,
|
|
|
|
client="test suite",
|
|
|
|
topic="whatever",
|
|
|
|
content=content,
|
|
|
|
)
|
|
|
|
result = self.api_post(cordelia, "/api/v1/messages", payload)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
message = self.get_last_message()
|
|
|
|
|
|
|
|
def post(sender: UserProfile, data: Dict[str, object]) -> HttpResponse:
|
|
|
|
payload = dict(
|
|
|
|
message_id=message.id, msg_type="widget", content=orjson.dumps(data).decode()
|
|
|
|
)
|
|
|
|
return self.api_post(sender, "/api/v1/submessage", payload)
|
|
|
|
|
|
|
|
result = post(cordelia, dict(type="question", question="Tabs or spaces?"))
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
result = post(hamlet, dict(type="question", question="Tabs or spaces?"))
|
|
|
|
self.assert_json_error(result, "You can't edit a question unless you are the author.")
|
|
|
|
|
|
|
|
def test_poll_type_validation(self) -> None:
|
|
|
|
sender = self.example_user("cordelia")
|
|
|
|
stream_name = "Verona"
|
|
|
|
content = "/poll Preference?\n\nyes\nno"
|
|
|
|
|
|
|
|
payload = dict(
|
|
|
|
type="stream",
|
|
|
|
to=stream_name,
|
|
|
|
client="test suite",
|
|
|
|
topic="whatever",
|
|
|
|
content=content,
|
|
|
|
)
|
|
|
|
result = self.api_post(sender, "/api/v1/messages", payload)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
message = self.get_last_message()
|
|
|
|
|
|
|
|
def post_submessage(content: str) -> HttpResponse:
|
|
|
|
payload = dict(
|
|
|
|
message_id=message.id,
|
|
|
|
msg_type="widget",
|
|
|
|
content=content,
|
|
|
|
)
|
|
|
|
return self.api_post(sender, "/api/v1/submessage", payload)
|
|
|
|
|
|
|
|
def assert_error(content: str, error: str) -> None:
|
|
|
|
result = post_submessage(content)
|
|
|
|
self.assert_json_error_contains(result, error)
|
|
|
|
|
|
|
|
assert_error("bogus", "Invalid json for submessage")
|
|
|
|
assert_error('""', "not a dict")
|
|
|
|
assert_error("[]", "not a dict")
|
|
|
|
|
|
|
|
assert_error('{"type": "bogus"}', "Unknown type for poll data: bogus")
|
|
|
|
assert_error('{"type": "vote"}', "key is missing")
|
|
|
|
assert_error('{"type": "vote", "key": "1,1,", "vote": 99}', "Invalid poll data")
|
|
|
|
|
|
|
|
assert_error('{"type": "question"}', "key is missing")
|
|
|
|
assert_error('{"type": "question", "question": 7}', "not a string")
|
|
|
|
|
|
|
|
assert_error('{"type": "new_option"}', "key is missing")
|
|
|
|
assert_error('{"type": "new_option", "idx": 7, "option": 999}', "not a string")
|
2021-06-28 19:56:23 +02:00
|
|
|
assert_error('{"type": "new_option", "idx": -1, "option": "pizza"}', "too small")
|
|
|
|
assert_error('{"type": "new_option", "idx": 1001, "option": "pizza"}', "too large")
|
2021-06-13 17:00:45 +02:00
|
|
|
assert_error('{"type": "new_option", "idx": "bogus", "option": "maybe"}', "not an int")
|
|
|
|
|
|
|
|
def assert_success(data: Dict[str, object]) -> None:
|
|
|
|
content = orjson.dumps(data).decode()
|
|
|
|
result = post_submessage(content)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
# Note that we only validate for types. The server code may, for,
|
|
|
|
# example, allow a vote for a non-existing option, and we rely
|
|
|
|
# on the clients to ignore those.
|
|
|
|
|
|
|
|
assert_success(dict(type="vote", key="1,1", vote=1))
|
|
|
|
assert_success(dict(type="new_option", idx=7, option="maybe"))
|
|
|
|
assert_success(dict(type="question", question="what's for dinner?"))
|
|
|
|
|
2021-06-28 18:55:42 +02:00
|
|
|
def test_todo_type_validation(self) -> None:
|
|
|
|
sender = self.example_user("cordelia")
|
|
|
|
stream_name = "Verona"
|
|
|
|
content = "/todo"
|
|
|
|
|
|
|
|
payload = dict(
|
|
|
|
type="stream",
|
|
|
|
to=stream_name,
|
|
|
|
client="test suite",
|
|
|
|
topic="whatever",
|
|
|
|
content=content,
|
|
|
|
)
|
|
|
|
result = self.api_post(sender, "/api/v1/messages", payload)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
message = self.get_last_message()
|
|
|
|
|
|
|
|
def post_submessage(content: str) -> HttpResponse:
|
|
|
|
payload = dict(
|
|
|
|
message_id=message.id,
|
|
|
|
msg_type="widget",
|
|
|
|
content=content,
|
|
|
|
)
|
|
|
|
return self.api_post(sender, "/api/v1/submessage", payload)
|
|
|
|
|
|
|
|
def assert_error(content: str, error: str) -> None:
|
|
|
|
result = post_submessage(content)
|
|
|
|
self.assert_json_error_contains(result, error)
|
|
|
|
|
|
|
|
assert_error('{"type": "bogus"}', "Unknown type for todo data: bogus")
|
|
|
|
|
|
|
|
assert_error('{"type": "new_task"}', "key is missing")
|
|
|
|
assert_error(
|
|
|
|
'{"type": "new_task", "key": 7, "task": 7, "desc": "", "completed": false}',
|
|
|
|
'data["task"] is not a string',
|
|
|
|
)
|
2021-06-28 19:56:23 +02:00
|
|
|
assert_error(
|
|
|
|
'{"type": "new_task", "key": -1, "task": "eat", "desc": "", "completed": false}',
|
|
|
|
'data["key"] is too small',
|
|
|
|
)
|
|
|
|
assert_error(
|
|
|
|
'{"type": "new_task", "key": 1001, "task": "eat", "desc": "", "completed": false}',
|
|
|
|
'data["key"] is too large',
|
|
|
|
)
|
2021-06-28 18:55:42 +02:00
|
|
|
|
|
|
|
assert_error('{"type": "strike"}', "key is missing")
|
|
|
|
assert_error('{"type": "strike", "key": 999}', 'data["key"] is not a string')
|
|
|
|
|
|
|
|
def assert_success(data: Dict[str, object]) -> None:
|
|
|
|
content = orjson.dumps(data).decode()
|
|
|
|
result = post_submessage(content)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
assert_success(dict(type="new_task", key=7, task="eat", desc="", completed=False))
|
|
|
|
assert_success(dict(type="strike", key="5,9"))
|
|
|
|
|
2021-06-13 17:00:45 +02:00
|
|
|
def test_get_widget_type(self) -> None:
|
|
|
|
sender = self.example_user("cordelia")
|
|
|
|
stream_name = "Verona"
|
|
|
|
# We test for both trailing and leading spaces, along with blank lines
|
|
|
|
# for the poll options.
|
|
|
|
content = "/poll Preference?\n\nyes\nno"
|
|
|
|
|
|
|
|
payload = dict(
|
|
|
|
type="stream",
|
|
|
|
to=stream_name,
|
|
|
|
client="test suite",
|
|
|
|
topic="whatever",
|
|
|
|
content=content,
|
|
|
|
)
|
|
|
|
result = self.api_post(sender, "/api/v1/messages", payload)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
message = self.get_last_message()
|
|
|
|
|
|
|
|
[submessage] = SubMessage.objects.filter(message_id=message.id)
|
|
|
|
|
|
|
|
self.assertEqual(get_widget_type(message_id=message.id), "poll")
|
|
|
|
|
|
|
|
submessage.content = "bogus non json"
|
|
|
|
submessage.save()
|
|
|
|
self.assertEqual(get_widget_type(message_id=message.id), None)
|
|
|
|
|
|
|
|
submessage.content = '{"bogus": 1}'
|
|
|
|
submessage.save()
|
|
|
|
self.assertEqual(get_widget_type(message_id=message.id), None)
|
|
|
|
|
|
|
|
submessage.content = '{"widget_type": "todo"}'
|
|
|
|
submessage.save()
|
|
|
|
self.assertEqual(get_widget_type(message_id=message.id), "todo")
|