Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
tests: Move CheckMessageTest to test_message_send.py.
Mohit Gupta012622aa4a9d
diff --git a/zerver/tests/test_message_send.py b/zerver/tests/test_message_send.pyindex 4b2425fdeaf..c7c19f45664 100644--- a/zerver/tests/test_message_send.py+++ b/zerver/tests/test_message_send.py@@ -12,6 +12,7 @@ from django.utils.timezone import now as timezone_nowfrom zerver.decorator import JsonableErrorfrom zerver.lib.actions import (+ check_message,check_send_stream_message,do_change_is_api_super_user,do_change_stream_post_policy,@@ -27,6 +28,7 @@ from zerver.lib.actions import (internal_send_private_message,internal_send_stream_message,internal_send_stream_message_by_name,+ send_rate_limited_pm_notification_to_bot_owner,)from zerver.lib.addressee import Addresseefrom zerver.lib.cache import cache_delete, get_stream_cache_key@@ -1878,3 +1880,89 @@ class TestAddressee(ZulipTestCase):stream_id = result.stream_id()self.assertEqual(stream.id, stream_id)++class CheckMessageTest(ZulipTestCase):+ def test_basic_check_message_call(self) -> None:+ sender = self.example_user('othello')+ client = make_client(name="test suite")+ stream_name = 'España y Francia'+ self.make_stream(stream_name)+ topic_name = 'issue'+ message_content = 'whatever'+ addressee = Addressee.for_stream_name(stream_name, topic_name)+ ret = check_message(sender, client, addressee, message_content)+ self.assertEqual(ret['message'].sender.id, sender.id)++ def test_bot_pm_feature(self) -> None:+ """We send a PM to a bot's owner if their bot sends a message to+ an unsubscribed stream"""+ parent = self.example_user('othello')+ bot = do_create_user(+ email='[email protected]',+ password='',+ realm=parent.realm,+ full_name='',+ short_name='',+ bot_type=UserProfile.DEFAULT_BOT,+ bot_owner=parent,+ )+ bot.last_reminder = None++ sender = bot+ client = make_client(name="test suite")+ stream_name = 'Россия'+ topic_name = 'issue'+ addressee = Addressee.for_stream_name(stream_name, topic_name)+ message_content = 'whatever'+ old_count = message_stream_count(parent)++ # Try sending to stream that doesn't exist sends a reminder to+ # the sender+ with self.assertRaises(JsonableError):+ check_message(sender, client, addressee, message_content)++ new_count = message_stream_count(parent)+ self.assertEqual(new_count, old_count + 1)+ self.assertIn("that stream does not exist.", most_recent_message(parent).content)++ # Try sending to stream that exists with no subscribers soon+ # after; due to rate-limiting, this should send nothing.+ self.make_stream(stream_name)+ ret = check_message(sender, client, addressee, message_content)+ new_count = message_stream_count(parent)+ self.assertEqual(new_count, old_count + 1)++ # Try sending to stream that exists with no subscribers longer+ # after; this should send an error to the bot owner that the+ # stream doesn't exist+ assert(sender.last_reminder is not None)+ sender.last_reminder = sender.last_reminder - datetime.timedelta(hours=1)+ sender.save(update_fields=["last_reminder"])+ ret = check_message(sender, client, addressee, message_content)++ new_count = message_stream_count(parent)+ self.assertEqual(new_count, old_count + 2)+ self.assertEqual(ret['message'].sender.email, '[email protected]')+ self.assertIn("does not have any subscribers", most_recent_message(parent).content)++ def test_bot_pm_error_handling(self) -> None:+ # This just test some defensive code.+ cordelia = self.example_user('cordelia')+ test_bot = self.create_test_bot(+ short_name='test',+ user_profile=cordelia,+ )+ content = 'whatever'+ good_realm = test_bot.realm+ wrong_realm = get_realm("zephyr")+ wrong_sender = cordelia++ send_rate_limited_pm_notification_to_bot_owner(test_bot, wrong_realm, content)+ self.assertEqual(test_bot.last_reminder, None)++ send_rate_limited_pm_notification_to_bot_owner(wrong_sender, good_realm, content)+ self.assertEqual(test_bot.last_reminder, None)++ test_bot.realm.deactivated = True+ send_rate_limited_pm_notification_to_bot_owner(test_bot, good_realm, content)+ self.assertEqual(test_bot.last_reminder, None)diff --git a/zerver/tests/test_messages.py b/zerver/tests/test_messages.pyindex e2a6a833aea..7b8f255bb34 100644--- a/zerver/tests/test_messages.py+++ b/zerver/tests/test_messages.py@@ -6,17 +6,12 @@ from django.utils.timezone import now as timezone_nowfrom analytics.lib.counts import COUNT_STATSfrom analytics.models import RealmCount-from zerver.decorator import JsonableErrorfrom zerver.lib.actions import (- check_message,- do_create_user,gather_subscriptions_helper,get_active_presence_idle_user_ids,get_client,get_last_message_id,- send_rate_limited_pm_notification_to_bot_owner,)-from zerver.lib.addressee import Addresseefrom zerver.lib.message import (get_first_visible_message_id,maybe_update_first_visible_message_id,@@ -24,7 +19,6 @@ from zerver.lib.message import ()from zerver.lib.test_classes import ZulipTestCasefrom zerver.lib.test_helpers import (- make_client,message_stream_count,most_recent_message,most_recent_usermessage,@@ -187,92 +181,6 @@ class LogDictTest(ZulipTestCase):self.assertEqual(dct[DB_TOPIC_NAME], 'Copenhagen')self.assertEqual(dct['type'], 'stream')-class CheckMessageTest(ZulipTestCase):- def test_basic_check_message_call(self) -> None:- sender = self.example_user('othello')- client = make_client(name="test suite")- stream_name = 'España y Francia'- self.make_stream(stream_name)- topic_name = 'issue'- message_content = 'whatever'- addressee = Addressee.for_stream_name(stream_name, topic_name)- ret = check_message(sender, client, addressee, message_content)- self.assertEqual(ret['message'].sender.id, sender.id)-- def test_bot_pm_feature(self) -> None:- """We send a PM to a bot's owner if their bot sends a message to- an unsubscribed stream"""- parent = self.example_user('othello')- bot = do_create_user(- email='[email protected]',- password='',- realm=parent.realm,- full_name='',- short_name='',- bot_type=UserProfile.DEFAULT_BOT,- bot_owner=parent,- )- bot.last_reminder = None-- sender = bot- client = make_client(name="test suite")- stream_name = 'Россия'- topic_name = 'issue'- addressee = Addressee.for_stream_name(stream_name, topic_name)- message_content = 'whatever'- old_count = message_stream_count(parent)-- # Try sending to stream that doesn't exist sends a reminder to- # the sender- with self.assertRaises(JsonableError):- check_message(sender, client, addressee, message_content)-- new_count = message_stream_count(parent)- self.assertEqual(new_count, old_count + 1)- self.assertIn("that stream does not exist.", most_recent_message(parent).content)-- # Try sending to stream that exists with no subscribers soon- # after; due to rate-limiting, this should send nothing.- self.make_stream(stream_name)- ret = check_message(sender, client, addressee, message_content)- new_count = message_stream_count(parent)- self.assertEqual(new_count, old_count + 1)-- # Try sending to stream that exists with no subscribers longer- # after; this should send an error to the bot owner that the- # stream doesn't exist- assert(sender.last_reminder is not None)- sender.last_reminder = sender.last_reminder - datetime.timedelta(hours=1)- sender.save(update_fields=["last_reminder"])- ret = check_message(sender, client, addressee, message_content)-- new_count = message_stream_count(parent)- self.assertEqual(new_count, old_count + 2)- self.assertEqual(ret['message'].sender.email, '[email protected]')- self.assertIn("does not have any subscribers", most_recent_message(parent).content)-- def test_bot_pm_error_handling(self) -> None:- # This just test some defensive code.- cordelia = self.example_user('cordelia')- test_bot = self.create_test_bot(- short_name='test',- user_profile=cordelia,- )- content = 'whatever'- good_realm = test_bot.realm- wrong_realm = get_realm("zephyr")- wrong_sender = cordelia-- send_rate_limited_pm_notification_to_bot_owner(test_bot, wrong_realm, content)- self.assertEqual(test_bot.last_reminder, None)-- send_rate_limited_pm_notification_to_bot_owner(wrong_sender, good_realm, content)- self.assertEqual(test_bot.last_reminder, None)-- test_bot.realm.deactivated = True- send_rate_limited_pm_notification_to_bot_owner(test_bot, good_realm, content)- self.assertEqual(test_bot.last_reminder, None)-class MessageVisibilityTest(ZulipTestCase):def test_update_first_visible_message_id(self) -> None:Message.objects.all().delete()