mirror of
https://github.com/zulip/zulip.git
synced 2025-11-03 05:23:35 +00:00
API users, particularly bots, can now send a field
called "widget_content" that will be turned into
a submessage for the web app to look at. (Other
clients can still rely on "content" to be there,
although it's up to the bot author to make the
experience good for those clients as well.)
Right now widget_content will be a JSON string that
encodes a "zform" widget with "choices." Our first
example will be a trivia bot, where users will see
something like this:
Which fruit is orange in color?
[A] orange
[B] blackberry
[C] strawberry
The letters will be turned into buttons on the webapp
and have canned replies.
This commit has a few parts:
- receive widget_content in the request (simply
validating that it's a string)
- parse the JSON in check_message and deeply
validate its structure
- turn it into a submessage in widget.py
57 lines
1.8 KiB
Python
57 lines
1.8 KiB
Python
from typing import Dict, Any
|
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
|
|
|
from zerver.lib.validator import check_widget_content
|
|
|
|
class WidgetContentTestCase(ZulipTestCase):
|
|
def test_validation(self) -> None:
|
|
def assert_error(obj: object, msg: str) -> None:
|
|
self.assertEqual(check_widget_content(obj), msg)
|
|
|
|
assert_error(5,
|
|
'widget_content is not a dict')
|
|
|
|
assert_error({},
|
|
'widget_type is not in widget_content')
|
|
|
|
assert_error(dict(widget_type='whatever'),
|
|
'extra_data is not in widget_content')
|
|
|
|
assert_error(dict(widget_type='zform', extra_data=4),
|
|
'extra_data is not a dict')
|
|
|
|
assert_error(dict(widget_type='bogus', extra_data={}),
|
|
'unknown widget type: bogus')
|
|
|
|
extra_data = dict() # type: Dict[str, Any]
|
|
obj = dict(widget_type='zform', extra_data=extra_data)
|
|
|
|
assert_error(obj, 'zform is missing type field')
|
|
|
|
extra_data['type'] = 'bogus'
|
|
assert_error(obj, 'unknown zform type: bogus')
|
|
|
|
extra_data['type'] = 'choices'
|
|
assert_error(obj, 'heading key is missing from extra_data')
|
|
|
|
extra_data['heading'] = 'whatever'
|
|
assert_error(obj, 'choices key is missing from extra_data')
|
|
|
|
extra_data['choices'] = 99
|
|
assert_error(obj, 'extra_data["choices"] is not a list')
|
|
|
|
extra_data['choices'] = [99]
|
|
assert_error(obj, 'extra_data["choices"][0] is not a dict')
|
|
|
|
extra_data['choices'] = [
|
|
dict(long_name='foo', reply='bar'),
|
|
]
|
|
assert_error(obj, 'short_name key is missing from extra_data["choices"][0]')
|
|
|
|
extra_data['choices'] = [
|
|
dict(short_name='a', long_name='foo', reply='bar'),
|
|
]
|
|
|
|
self.assertEqual(check_widget_content(obj), None)
|