コード例 #1
0
    def setUp(self) -> None:
        super().setUp()

        def callback(update: Update):
            update.message.reply(text='answer')

        self.handler = DefaultHandler("default", callback=callback)
        now = timezone.datetime(2001, 1, 1, tzinfo=timezone.utc)
        bot = Bot.objects.create(name="Bot", token="token")
        user = User.objects.create(user_id=2, is_bot=False, username='******')
        self.chat = Chat.objects.create(chat_id='2',
                                        type="private",
                                        bot=bot,
                                        username='******')
        message = Message.objects.create(
            direction=Message.DIRECTION_IN,
            message_id="1",
            date=now,
            text="Help",
            chat=self.chat,
            from_user=user,
        )
        self.update = Update.objects.create(
            update_id="1",
            bot=bot,
            message=message,
        )
コード例 #2
0
class TestCaseTestCase(ChatbotTestCase):
    def setUp(self) -> None:
        super().setUp()

        def callback(update: Update):
            update.message.reply(text='answer')

        self.handler = DefaultHandler("default", callback=callback)
        now = timezone.datetime(2001, 1, 1, tzinfo=timezone.utc)
        bot = Bot.objects.create(name="Bot", token="token")
        user = User.objects.create(user_id=2, is_bot=False, username='******')
        self.chat = Chat.objects.create(chat_id='2',
                                        type="private",
                                        bot=bot,
                                        username='******')
        message = Message.objects.create(
            direction=Message.DIRECTION_IN,
            message_id="1",
            date=now,
            text="Help",
            chat=self.chat,
            from_user=user,
        )
        self.update = Update.objects.create(
            update_id="1",
            bot=bot,
            message=message,
        )

    @patch("django_chatbot.telegram.api.requests")
    def test_requests_get_will_not_invoked(self, mocked_requests):
        """Api.send_message must be monkey patched"""

        self.handler.handle_update(self.update)

        mocked_requests.get.assert_not_called()
        mocked_requests.post.assert_not_called()

    def test_patched_send_message__return_right_message(self):
        self.handler.handle_update(self.update)

        self.assertEqual(Message.objects.count(), 2)
        answer = Message.objects.get(direction=Message.DIRECTION_OUT)
        bot_user = answer.from_user

        self.assertEqual(bot_user.user_id, 1)
        self.assertEqual(answer.from_user, bot_user)
        self.assertEqual(answer.chat, self.chat)
        self.assertEqual(answer.text, "answer")
コード例 #3
0
ファイル: test_forms.py プロジェクト: vyvojer/django-chatbot
 def get_bot(self) -> Bot:
     self.patcher = patch(
         "testapp.tests.test_forms.handlers",
         [DefaultHandler("default", form_class=self.handler_form)],
     )
     self.patcher.start()
     bot = Bot.objects.create(
         name="bot",
         token="token",
         root_handlerconf="testapp.tests.test_forms",
     )
     return bot
コード例 #4
0
 def setUp(self) -> None:
     super().setUp()
     self.patcher = patch(
         "testapp.tests.test_forms.handlers",
         [DefaultHandler('default', form_class=self.handler_form)],
     )
     self.patcher.start()
     load_handlers.cache_clear()
     self.bot = Bot.objects.create(
         name='bot',
         token='token',
         root_handlerconf='testapp.tests.test_forms')
     self.mock_user = MockUser(self.bot)
コード例 #5
0
#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
#  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
#  THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# *****************************************************************************

# *****************************************************************************
#  MIT License
#
#
#  Permission is hereby granted, free of charge, to any person obtaining a copy
#  of this software and associated documentation files (the "Software"),
#  to deal in the Software without restriction, including without limitation
#  the rights to use, copy, modify, merge, publish, distribute, sublicense,
#  and/or sell copies of the Software, and to permit persons to whom
#  the Software is furnished to do so, subject to the following conditions:
#
#
from django_chatbot.handlers import CommandHandler, DefaultHandler
from dummybot import callbacks

handlers = [
    CommandHandler(
        name="help",
        command="/help",
        callback=callbacks.help,
    ),
    DefaultHandler(name="default", callback=callbacks.default),
]