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
|
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
|
2018-08-23 14:51:17 +02:00
|
|
|
from zerver.lib.widget import get_widget_data
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.models import SubMessage
|
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)
|