import os os.environ['TG_TOKEN']='test' import threading import unittest from unittest.mock import Mock, patch import bot class Forward(unittest.TestCase): def test_plain(self): self.assertIsNone(bot.forwarded_text({'text':'hello'})) def test_hidden(self): text=bot.forwarded_text({'forward_origin':{'type':'hidden_user','sender_user_name':'Имя'},'text':'Привет'}) self.assertIn('Имя',text) self.assertIn('Привет',text) def test_caption(self): self.assertIn('Подпись',bot.forwarded_text({'forward_origin':{'type':'channel','chat':{'title':'Канал','username':'channel'}},'caption':'Подпись'})) def test_no_text(self): self.assertIn('без подписи',bot.forwarded_text({'forward_origin':{'type':'hidden_user'}})) def test_legacy(self): self.assertIn('Legacy',bot.forwarded_text({'forward_date':1,'forward_sender_name':'Legacy','text':'test'})) def test_slash_not_command(self): b=bot.Bot.__new__(bot.Bot); b.message_lock=threading.RLock() b.active={}; b.thread_for=Mock(return_value='tid'); b.codex=Mock() b.codex.call.return_value={'turn':{'id':'turn'}} with patch.object(bot,'ALLOWED',{1}),patch.object(bot,'tg'),patch.object(bot,'send'): b.handle_message({'chat':{'id':1,'type':'private'},'from':{'id':1},'text':'/stop','forward_origin':{'type':'hidden_user'}}) self.assertEqual(b.codex.call.call_args.args[0],'turn/start') self.assertIn('/stop', b.codex.call.call_args.kwargs['input'][0]['text']) def test_unauthorized(self): b=bot.Bot.__new__(bot.Bot); b.message_lock=threading.RLock(); b.thread_for=Mock() with patch.object(bot,'ALLOWED',{1}),patch.object(bot,'send'): b.handle_message({'chat':{'id':2,'type':'private'},'from':{'id':2},'text':'test','forward_origin':{'type':'hidden_user'}}) b.thread_for.assert_not_called() if __name__ == '__main__': unittest.main()