Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
test_service_bot_system: Fix strict_optional errors.
Anders Kaseorg26fe6be16aab
diff --git a/mypy.ini b/mypy.iniindex e88391f7a8b..3d1a6a95352 100644--- a/mypy.ini+++ b/mypy.ini@@ -40,8 +40,6 @@ strict_optional = True# Tests (may be many issues in file; comment is just one error noted)-[mypy-zerver/tests/test_service_bot_system] #312: error: Argument 1 to "set_bot_config" has incompatible type "Optional[UserProfile]"; expected "UserProfile"-strict_optional = False[mypy-zerver/tests/test_outgoing_webhook_system] #33: error: Argument 1 to "MockServiceHandler" has incompatible type "None"; expected "str"strict_optional = False[mypy-zerver/tests/test_narrow] #515: error: Incompatible types in assignment (expression has type "None", variable has type "int")diff --git a/zerver/lib/test_classes.py b/zerver/lib/test_classes.pyindex 9544e3c3774..58df214cf98 100644--- a/zerver/lib/test_classes.py+++ b/zerver/lib/test_classes.py@@ -341,8 +341,7 @@ class ZulipTestCase(TestCase):def create_test_bot(self, short_name: str,user_profile: UserProfile,full_name: str='Foo Bot',- assert_json_error_msg: str=None,- **extras: Any) -> Optional[UserProfile]:+ **extras: Any) -> UserProfile:self.login_user(user_profile)bot_info = {'short_name': short_name,@@ -350,14 +349,27 @@ class ZulipTestCase(TestCase):}bot_info.update(extras)result = self.client_post("/json/bots", bot_info)- if assert_json_error_msg is not None:- self.assert_json_error(result, assert_json_error_msg)- return None- else:- self.assert_json_success(result)- bot_email = f'{short_name}[email protected]'- bot_profile = get_user(bot_email, user_profile.realm)- return bot_profile+ self.assert_json_success(result)+ bot_email = f'{short_name}[email protected]'+ bot_profile = get_user(bot_email, user_profile.realm)+ return bot_profile++ def fail_to_create_test_bot(+ self, short_name: str,+ user_profile: UserProfile,+ full_name: str='Foo Bot',+ *,+ assert_json_error_msg: str,+ **extras: Any,+ ) -> None:+ self.login_user(user_profile)+ bot_info = {+ 'short_name': short_name,+ 'full_name': full_name,+ }+ bot_info.update(extras)+ result = self.client_post("/json/bots", bot_info)+ self.assert_json_error(result, assert_json_error_msg)def login_with_return(self, email: str, password: Optional[str]=None,**kwargs: Any) -> HttpResponse:diff --git a/zerver/tests/test_bots.py b/zerver/tests/test_bots.pyindex df519923f2c..b722b4ba4ba 100644--- a/zerver/tests/test_bots.py+++ b/zerver/tests/test_bots.py@@ -1484,13 +1484,15 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):def test_create_embedded_bot_with_disabled_embedded_bots(self, **extras: Any) -> None:with self.settings(EMBEDDED_BOTS_ENABLED=False):- self.create_test_bot(short_name='embeddedservicebot',- user_profile=self.example_user("hamlet"),- bot_type=UserProfile.EMBEDDED_BOT,- service_name='followup',- config_data=ujson.dumps({'key': 'value'}),- assert_json_error_msg='Embedded bots are not enabled.',- **extras)+ self.fail_to_create_test_bot(+ short_name='embeddedservicebot',+ user_profile=self.example_user("hamlet"),+ bot_type=UserProfile.EMBEDDED_BOT,+ service_name='followup',+ config_data=ujson.dumps({'key': 'value'}),+ assert_json_error_msg='Embedded bots are not enabled.',+ **extras,+ )def test_create_embedded_bot(self, **extras: Any) -> None:bot_config_info = {'key': 'value'}@@ -1512,20 +1514,24 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):self.assertEqual(service.user_profile, bot)def test_create_embedded_bot_with_incorrect_service_name(self, **extras: Any) -> None:- self.create_test_bot(short_name='embeddedservicebot',- user_profile=self.example_user("hamlet"),- bot_type=UserProfile.EMBEDDED_BOT,- service_name='not_existing_service',- assert_json_error_msg='Invalid embedded bot name.',- **extras)+ self.fail_to_create_test_bot(+ short_name='embeddedservicebot',+ user_profile=self.example_user("hamlet"),+ bot_type=UserProfile.EMBEDDED_BOT,+ service_name='not_existing_service',+ assert_json_error_msg='Invalid embedded bot name.',+ **extras,+ )def test_create_embedded_bot_with_invalid_config_value(self, **extras: Any) -> None:- self.create_test_bot(short_name='embeddedservicebot',- user_profile=self.example_user("hamlet"),- service_name='followup',- config_data=ujson.dumps({'invalid': ['config', 'value']}),- assert_json_error_msg='config_data contains a value that is not a string',- **extras)+ self.fail_to_create_test_bot(+ short_name='embeddedservicebot',+ user_profile=self.example_user("hamlet"),+ service_name='followup',+ config_data=ujson.dumps({'invalid': ['config', 'value']}),+ assert_json_error_msg='config_data contains a value that is not a string',+ **extras,+ )# Test to create embedded bot with an incorrect config valueincorrect_bot_config_info = {'key': 'incorrect key'}diff --git a/zerver/tests/test_service_bot_system.py b/zerver/tests/test_service_bot_system.pyindex 1a2aec0a238..0881da53864 100644--- a/zerver/tests/test_service_bot_system.py+++ b/zerver/tests/test_service_bot_system.py@@ -1,4 +1,4 @@-from typing import Any, Callable, Mapping, Union+from typing import Any, Mapping, Unionfrom unittest import mockimport ujson@@ -37,6 +37,7 @@ class TestServiceBotBasics(ZulipTestCase):assert(not sender.is_bot)outgoing_bot = self._get_outgoing_bot()+ assert outgoing_bot.bot_type is not Noneevent_dict = get_service_bot_events(sender=sender,@@ -61,6 +62,7 @@ class TestServiceBotBasics(ZulipTestCase):assert(not sender.is_bot)outgoing_bot = self._get_outgoing_bot()+ assert outgoing_bot.bot_type is not None# If outgoing_bot is not in mentioned_user_ids,# we will skip over it. This tests an anomaly@@ -84,6 +86,7 @@ class TestServiceBotBasics(ZulipTestCase):assert(not sender.is_bot)outgoing_bot = self._get_outgoing_bot()+ assert outgoing_bot.bot_type is not Nonecordelia = self.example_user('cordelia')@@ -118,6 +121,7 @@ class TestServiceBotBasics(ZulipTestCase):assert(not sender.is_bot)outgoing_bot = self._get_outgoing_bot()+ assert outgoing_bot.bot_type is not Noneevent_dict = get_service_bot_events(sender=sender,@@ -415,9 +419,10 @@ class TestServiceBotEventTriggers(ZulipTestCase):trigger = 'mention'message_type = Recipient._type_names[Recipient.STREAM]- def check_values_passed(queue_name: Any,- trigger_event: Union[Mapping[Any, Any], Any],- x: Callable[[Any], None]=None) -> None:+ def check_values_passed(+ queue_name: Any,+ trigger_event: Union[Mapping[Any, Any], Any],+ ) -> None:self.assertEqual(queue_name, expected_queue_name)self.assertEqual(trigger_event["message"]["content"], content)self.assertEqual(trigger_event["message"]["display_recipient"], recipient)@@ -460,9 +465,10 @@ class TestServiceBotEventTriggers(ZulipTestCase):sender = self.user_profilerecipient = self.bot_profile- def check_values_passed(queue_name: Any,- trigger_event: Union[Mapping[Any, Any], Any],- x: Callable[[Any], None]=None) -> None:+ def check_values_passed(+ queue_name: Any,+ trigger_event: Union[Mapping[Any, Any], Any],+ ) -> None:self.assertEqual(queue_name, expected_queue_name)self.assertEqual(trigger_event["user_profile_id"], self.bot_profile.id)self.assertEqual(trigger_event["trigger"], "private_message")@@ -502,9 +508,10 @@ class TestServiceBotEventTriggers(ZulipTestCase):recipients = [self.bot_profile, self.second_bot_profile]profile_ids = [self.bot_profile.id, self.second_bot_profile.id]- def check_values_passed(queue_name: Any,- trigger_event: Union[Mapping[Any, Any], Any],- x: Callable[[Any], None]=None) -> None:+ def check_values_passed(+ queue_name: Any,+ trigger_event: Union[Mapping[Any, Any], Any],+ ) -> None:self.assertEqual(queue_name, expected_queue_name)self.assertIn(trigger_event["user_profile_id"], profile_ids)profile_ids.remove(trigger_event["user_profile_id"])