77from telegram import Chat , Message , MessageEntity , User
88
99from bot .group_config import GroupConfig
10+ from telegram .ext import ApplicationHandlerStop
11+
1012from bot .handlers .anti_spam import (
1113 extract_urls ,
1214 handle_inline_keyboard_spam ,
@@ -396,8 +398,9 @@ async def test_handles_naive_datetime_from_database(
396398 patch ("bot.handlers.anti_spam.get_group_config_for_update" , return_value = group_config ),
397399 patch ("bot.handlers.anti_spam.get_database" , return_value = mock_db ),
398400 ):
399- # Should not raise TypeError
400- await handle_new_user_spam (mock_update , mock_context )
401+ # Should not raise TypeError, but raises ApplicationHandlerStop on violation
402+ with pytest .raises (ApplicationHandlerStop ):
403+ await handle_new_user_spam (mock_update , mock_context )
401404
402405 mock_update .message .delete .assert_called_once ()
403406
@@ -462,7 +465,8 @@ async def test_deletes_forwarded_message(
462465 patch ("bot.handlers.anti_spam.get_group_config_for_update" , return_value = group_config ),
463466 patch ("bot.handlers.anti_spam.get_database" , return_value = mock_db ),
464467 ):
465- await handle_new_user_spam (mock_update , mock_context )
468+ with pytest .raises (ApplicationHandlerStop ):
469+ await handle_new_user_spam (mock_update , mock_context )
466470
467471 mock_update .message .delete .assert_called_once ()
468472
@@ -492,7 +496,8 @@ async def test_deletes_message_with_non_whitelisted_link(
492496 patch ("bot.handlers.anti_spam.get_group_config_for_update" , return_value = group_config ),
493497 patch ("bot.handlers.anti_spam.get_database" , return_value = mock_db ),
494498 ):
495- await handle_new_user_spam (mock_update , mock_context )
499+ with pytest .raises (ApplicationHandlerStop ):
500+ await handle_new_user_spam (mock_update , mock_context )
496501
497502 mock_update .message .delete .assert_called_once ()
498503
@@ -543,7 +548,8 @@ async def test_sends_warning_on_first_violation(
543548 patch ("bot.handlers.anti_spam.get_group_config_for_update" , return_value = group_config ),
544549 patch ("bot.handlers.anti_spam.get_database" , return_value = mock_db ),
545550 ):
546- await handle_new_user_spam (mock_update , mock_context )
551+ with pytest .raises (ApplicationHandlerStop ):
552+ await handle_new_user_spam (mock_update , mock_context )
547553
548554 mock_context .bot .send_message .assert_called_once ()
549555
@@ -568,7 +574,8 @@ async def test_no_warning_on_second_violation(
568574 patch ("bot.handlers.anti_spam.get_group_config_for_update" , return_value = group_config ),
569575 patch ("bot.handlers.anti_spam.get_database" , return_value = mock_db ),
570576 ):
571- await handle_new_user_spam (mock_update , mock_context )
577+ with pytest .raises (ApplicationHandlerStop ):
578+ await handle_new_user_spam (mock_update , mock_context )
572579
573580 mock_context .bot .send_message .assert_not_called ()
574581
@@ -593,7 +600,8 @@ async def test_restricts_user_at_threshold(
593600 patch ("bot.handlers.anti_spam.get_group_config_for_update" , return_value = group_config ),
594601 patch ("bot.handlers.anti_spam.get_database" , return_value = mock_db ),
595602 ):
596- await handle_new_user_spam (mock_update , mock_context )
603+ with pytest .raises (ApplicationHandlerStop ):
604+ await handle_new_user_spam (mock_update , mock_context )
597605
598606 mock_context .bot .restrict_chat_member .assert_called_once ()
599607
@@ -618,7 +626,8 @@ async def test_sends_restriction_notification_at_threshold(
618626 patch ("bot.handlers.anti_spam.get_group_config_for_update" , return_value = group_config ),
619627 patch ("bot.handlers.anti_spam.get_database" , return_value = mock_db ),
620628 ):
621- await handle_new_user_spam (mock_update , mock_context )
629+ with pytest .raises (ApplicationHandlerStop ):
630+ await handle_new_user_spam (mock_update , mock_context )
622631
623632 # Should call send_message for restriction notification (not first warning)
624633 mock_context .bot .send_message .assert_called_once ()
@@ -645,7 +654,8 @@ async def test_deletes_message_with_external_reply(
645654 patch ("bot.handlers.anti_spam.get_group_config_for_update" , return_value = group_config ),
646655 patch ("bot.handlers.anti_spam.get_database" , return_value = mock_db ),
647656 ):
648- await handle_new_user_spam (mock_update , mock_context )
657+ with pytest .raises (ApplicationHandlerStop ):
658+ await handle_new_user_spam (mock_update , mock_context )
649659
650660 mock_update .message .delete .assert_called_once ()
651661
@@ -670,7 +680,8 @@ async def test_deletes_message_with_story(
670680 patch ("bot.handlers.anti_spam.get_group_config_for_update" , return_value = group_config ),
671681 patch ("bot.handlers.anti_spam.get_database" , return_value = mock_db ),
672682 ):
673- await handle_new_user_spam (mock_update , mock_context )
683+ with pytest .raises (ApplicationHandlerStop ):
684+ await handle_new_user_spam (mock_update , mock_context )
674685
675686 mock_update .message .delete .assert_called_once ()
676687
@@ -713,7 +724,8 @@ async def test_continues_when_delete_fails(
713724 patch ("bot.handlers.anti_spam.get_group_config_for_update" , return_value = group_config ),
714725 patch ("bot.handlers.anti_spam.get_database" , return_value = mock_db ),
715726 ):
716- await handle_new_user_spam (mock_update , mock_context )
727+ with pytest .raises (ApplicationHandlerStop ):
728+ await handle_new_user_spam (mock_update , mock_context )
717729
718730 mock_context .bot .send_message .assert_called_once ()
719731
@@ -741,7 +753,8 @@ async def test_continues_when_send_warning_fails(
741753 patch ("bot.handlers.anti_spam.get_group_config_for_update" , return_value = group_config ),
742754 patch ("bot.handlers.anti_spam.get_database" , return_value = mock_db ),
743755 ):
744- await handle_new_user_spam (mock_update , mock_context )
756+ with pytest .raises (ApplicationHandlerStop ):
757+ await handle_new_user_spam (mock_update , mock_context )
745758
746759 mock_update .message .delete .assert_called_once ()
747760
@@ -769,7 +782,8 @@ async def test_continues_when_restrict_fails(
769782 patch ("bot.handlers.anti_spam.get_group_config_for_update" , return_value = group_config ),
770783 patch ("bot.handlers.anti_spam.get_database" , return_value = mock_db ),
771784 ):
772- await handle_new_user_spam (mock_update , mock_context )
785+ with pytest .raises (ApplicationHandlerStop ):
786+ await handle_new_user_spam (mock_update , mock_context )
773787
774788 mock_update .message .delete .assert_called_once ()
775789
0 commit comments