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:
Steve Howell 2018-08-23 12:51:17 +00:00 committed by Tim Abbott
parent f3f30d29f9
commit 9e8930f6de
2 changed files with 23 additions and 2 deletions

View File

@ -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:

View File

@ -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