mirror of https://github.com/zulip/zulip.git
tests: Test get_widget_data() helper.
We also remove some unreachable code. Calling split() always returns at least one token, even if it's just the empty string. This is tested directly on this commit, plus messages with empty content get rejected pretty early in the execution path.
This commit is contained in:
parent
f3f30d29f9
commit
9e8930f6de
|
@ -10,9 +10,8 @@ from zerver.models import SubMessage
|
|||
def get_widget_data(content: str) -> Tuple[Optional[str], Optional[str]]:
|
||||
valid_widget_types = ['tictactoe', 'poll', 'todo']
|
||||
tokens = content.split(' ')
|
||||
if not tokens:
|
||||
return None, None
|
||||
|
||||
# tokens[0] will always exist
|
||||
if tokens[0].startswith('/'):
|
||||
widget_type = tokens[0][1:]
|
||||
if widget_type in valid_widget_types:
|
||||
|
|
|
@ -6,6 +6,8 @@ from zerver.models import SubMessage
|
|||
|
||||
from zerver.lib.test_classes import ZulipTestCase
|
||||
|
||||
from zerver.lib.widget import get_widget_data
|
||||
|
||||
from zerver.lib.validator import check_widget_content
|
||||
|
||||
class WidgetContentTestCase(ZulipTestCase):
|
||||
|
@ -81,6 +83,26 @@ class WidgetContentTestCase(ZulipTestCase):
|
|||
result = self.api_post(sender_email, "/api/v1/messages", payload)
|
||||
self.assert_json_error_contains(result, 'Widgets: widget_type is not in widget_content')
|
||||
|
||||
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 = [
|
||||
'',
|
||||
' ',
|
||||
'this is an ordinary message',
|
||||
'/bogus_command',
|
||||
'/me shrugs',
|
||||
'use /poll',
|
||||
]
|
||||
|
||||
for message in test_messages:
|
||||
self.assertEqual(get_widget_data(content=message), (None, None))
|
||||
|
||||
# Add a positive check for context
|
||||
self.assertEqual(get_widget_data(content='/tictactoe'), ('tictactoe', None))
|
||||
|
||||
def test_tictactoe(self) -> None:
|
||||
# The tictactoe widget is mostly useful as a code sample,
|
||||
# and it also helps us get test coverage that could apply
|
||||
|
|
Loading…
Reference in New Issue