def test_validation(self) -> None: def assert_error(obj: object, msg: str) -> None: with self.assertRaisesRegex(ValidationError, re.escape(msg)): check_widget_content(obj) 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[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"), ] check_widget_content(obj)
def test_explicit_widget_content(self) -> None: # Users can send widget_content directly on messages # using the `widget_content` field. sender = self.example_user('cordelia') stream_name = 'Verona' content = 'does-not-matter' zform_data = dict( type='choices', heading='Options:', choices=[], ) widget_content = dict( widget_type='zform', extra_data=zform_data, ) check_widget_content(widget_content) payload = dict( type="stream", to=stream_name, client='test suite', topic='whatever', content=content, widget_content=ujson.dumps(widget_content), ) result = self.api_post(sender, "/api/v1/messages", payload) 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) self.assertEqual(submessage.msg_type, 'widget') self.assertEqual(ujson.loads(submessage.content), expected_submessage_content)
def test_explicit_widget_content(self) -> None: # Users can send widget_content directly on messages # using the `widget_content` field. sender = self.example_user("cordelia") stream_name = "Verona" content = "does-not-matter" zform_data = dict( type="choices", heading="Options:", choices=[], ) widget_content = dict( widget_type="zform", extra_data=zform_data, ) check_widget_content(widget_content) payload = dict( type="stream", to=stream_name, client="test suite", topic="whatever", content=content, widget_content=orjson.dumps(widget_content).decode(), ) result = self.api_post(sender, "/api/v1/messages", payload) 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) self.assertEqual(submessage.msg_type, "widget") self.assertEqual(orjson.loads(submessage.content), expected_submessage_content)
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)
def assert_error(obj: object, msg: str) -> None: self.assertEqual(check_widget_content(obj), msg)