Beispiel #1
0
    def test_next_update_id(self, mocked_dispatcher: Mock):
        """update_id must be unique"""
        now = timezone.now()
        mock_user = MockUser(bot=self.bot)
        another_chat = Chat.objects.create(
            bot=self.another_bot,
            chat_id=18,
            type='private',
        )
        now = timezone.now()
        message_1 = Message.objects.create(message_id=7,
                                           chat=mock_user.chat,
                                           date=now,
                                           text="message_1")
        message_2_2 = Message.objects.create(message_id=8,
                                             chat=mock_user.chat,
                                             date=now,
                                             text="message_2_2")
        message_2_1 = Message.objects.create(message_id=9,
                                             chat=another_chat,
                                             date=now,
                                             text="message_2_1")
        Update.objects.create(update_id=7, bot=self.bot, message=message_1)
        Update.objects.create(update_id=9, bot=self.bot, message=message_2_2)
        Update.objects.create(update_id=8, bot=self.bot, message=message_2_1)

        self.assertEqual(mock_user._next_update_id(), 10)
Beispiel #2
0
    def test_callback_queries(self, mocked_dispatcher: Mock):
        mock_user = MockUser(bot=self.bot)
        another_user = MockUser(bot=self.bot)
        now = timezone.now()
        message_1 = Message.objects.create(message_id=7,
                                           chat=mock_user.chat,
                                           date=now,
                                           text="message_1")
        message_2 = Message.objects.create(message_id=8,
                                           chat=mock_user.chat,
                                           date=now,
                                           text="message_2")
        message_3 = Message.objects.create(message_id=8,
                                           chat=another_user.chat,
                                           date=now,
                                           text="message_2")
        callback_query_1 = CallbackQuery.objects.create(
            callback_query_id="22",
            bot=self.bot,
            from_user=mock_user.user,
            message=message_1)
        callback_query_2 = CallbackQuery.objects.create(
            callback_query_id="23",
            bot=self.bot,
            from_user=mock_user.user,
            message=message_2)
        CallbackQuery.objects.create(callback_query_id="24",
                                     bot=self.bot,
                                     from_user=another_user.user,
                                     message=message_3)

        self.assertQuerysetEqual(mock_user.callback_queries(),
                                 [callback_query_2, callback_query_1],
                                 transform=lambda x: x)
Beispiel #3
0
    def test_send_message(self, mocked_next_update_id: Mock,
                          mocked_next_message_id: Mock, mocked_now: Mock,
                          mocked_dispatcher: Mock):
        now = timezone.datetime(2000, 1, 1, tzinfo=timezone.utc)
        mocked_now.return_value = now
        mocked_next_message_id.return_value = 42
        mocked_next_update_id.return_value = 142
        test_user = MockUser(bot=self.bot, username='******')

        test_user.send_message("Help")

        user = types.User(id=START_USER_ID,
                          is_bot=False,
                          username='******',
                          first_name='u',
                          last_name='u')
        chat = types.Chat(id=START_USER_ID,
                          type='private',
                          username='******',
                          first_name='u',
                          last_name='u')
        message = types.Message(message_id=42,
                                date=now,
                                chat=chat,
                                from_user=user,
                                text='Help')
        update = types.Update(update_id=142, message=message)
        update_data = update.to_dict(date_as_timestamp=True)
        mocked_dispatcher.return_value.dispatch.assert_called_with(
            update_data=update_data)
 def setUp(self) -> None:
     super().setUp()
     self.bot = Bot.objects.create(
         name='bot', token='token',
         root_handlerconf='testapp.handlers'
     )
     self.user = MockUser(bot=self.bot)
class CommandsTestCase(TestCase):
    def setUp(self) -> None:
        super().setUp()
        self.bot = Bot.objects.create(name='bot',
                                      token='token',
                                      root_handlerconf='testapp.handlers')
        self.user = MockUser(bot=self.bot)

    def test_add_notes(self):
        self.user.send_message('/count')
Beispiel #6
0
    def test_send_callback_query_default_message(self,
                                                 mocked_next_update_id: Mock,
                                                 mocked_next_message_id: Mock,
                                                 mocked_now: Mock,
                                                 mocked_dispatcher: Mock):
        now = timezone.datetime(2000, 1, 1, tzinfo=timezone.utc)
        mocked_now.return_value = now
        mocked_next_message_id.return_value = 42
        mocked_next_update_id.return_value = 142
        test_user = MockUser(self.bot, username='******')
        message = Message.objects.create(
            chat=test_user.chat,
            date=timezone.now(),
            message_id=test_user._next_message_id(),
            text='Yes of No?',
            _reply_markup=types.InlineKeyboardMarkup(inline_keyboard=[[
                types.InlineKeyboardButton("Yes", callback_data="yes")
            ], [types.InlineKeyboardButton("No", callback_data="no")
                ]]).to_dict())

        test_user.send_callback_query(data='yes')

        user = types.User(id=START_USER_ID,
                          is_bot=False,
                          username='******',
                          first_name='u',
                          last_name='u')
        chat = types.Chat(id=START_USER_ID,
                          type='private',
                          username='******',
                          first_name='u',
                          last_name='u')
        message = types.Message(
            message_id=message.message_id,
            date=message.date,
            chat=chat,
        )
        callback_query = types.CallbackQuery(id='1',
                                             from_user=user,
                                             chat_instance='1',
                                             message=message,
                                             data='yes')

        update = types.Update(update_id=142, callback_query=callback_query)
        update_data = update.to_dict(date_as_timestamp=True)
        test_user.dispatcher.dispatch.assert_called_with(
            update_data=update_data)
Beispiel #7
0
 def test_next_callback_query(self, mocked_dispatcher: Mock):
     mock_user = MockUser(bot=self.bot)
     now = timezone.now()
     message_1 = Message.objects.create(message_id=7,
                                        chat=mock_user.chat,
                                        date=now,
                                        text="message_1")
     message_2 = Message.objects.create(message_id=8,
                                        chat=mock_user.chat,
                                        date=now,
                                        text="message_2")
     CallbackQuery.objects.create(callback_query_id="22",
                                  bot=self.bot,
                                  from_user=mock_user.user,
                                  message=message_1)
     CallbackQuery.objects.create(callback_query_id="23",
                                  bot=self.bot,
                                  from_user=mock_user.user,
                                  message=message_2)
     self.assertEqual(mock_user._next_callback_query_id(), "24")
Beispiel #8
0
    def test_init(self, mocked_next_user_id: Mock, mocked_dispatcher: Mock):
        mocked_next_user_id.return_value = 100
        test_user = MockUser(bot=self.bot)

        self.assertEqual(test_user.user_id, 100)
        self.assertEqual(test_user.username, "test_user_100")
        self.assertEqual(test_user.bot, self.bot)
        user = User.objects.first()
        self.assertEqual(test_user.user, user)
        self.assertEqual(test_user.user.user_id, test_user.user_id)
        self.assertEqual(test_user.user.username, test_user.username)
        self.assertEqual(test_user.user.first_name, test_user.username)
        self.assertEqual(test_user.user.last_name, test_user.username)
Beispiel #9
0
    def test_send_message__with_commands(self, mocked_next_update_id: Mock,
                                         mocked_next_message_id: Mock,
                                         mocked_now: Mock,
                                         mocked_dispatcher: Mock):
        now = timezone.datetime(2000, 1, 1, tzinfo=timezone.utc)
        mocked_now.return_value = now
        mocked_next_message_id.return_value = 42
        mocked_next_update_id.return_value = 142
        test_user = MockUser(bot=self.bot, username='******')

        test_user.send_message("/start /help")

        user = types.User(id=START_USER_ID,
                          is_bot=False,
                          username='******',
                          first_name='u',
                          last_name='u')
        chat = types.Chat(id=START_USER_ID,
                          type='private',
                          username='******',
                          first_name='u',
                          last_name='u')
        message = types.Message(message_id=42,
                                date=now,
                                chat=chat,
                                from_user=user,
                                text='/start /help',
                                entities=[
                                    types.MessageEntity(type='bot_command',
                                                        offset=0,
                                                        length=6),
                                    types.MessageEntity(type='bot_command',
                                                        offset=7,
                                                        length=5),
                                ])
        update = types.Update(update_id=142, message=message)
        update_data = update.to_dict(date_as_timestamp=True)
        test_user.dispatcher.dispatch.assert_called_with(
            update_data=update_data)
class CommandsTestCase(TestCase):
    def setUp(self) -> None:
        super().setUp()
        self.bot = Bot.objects.create(
            name='bot', token='token',
            root_handlerconf='testapp.handlers'
        )
        self.user = MockUser(bot=self.bot)

    def test_default_handler(self):
        self.assertEqual(self.mock_chatbot.last_message(), None)
        self.user.send_message('help me')
        self.assertIn(
            "I don't understand you",
            self.mock_chatbot.last_message()['text'],
        )
        self.assertEqual('help me', self.user.messages()[1].text)
        self.assertIn(
            "I don't understand you",
            self.user.messages()[0].text
        )

    def test_help_command(self):
        self.assertEqual(self.mock_chatbot.last_message(), None)
        self.user.send_message('/help')
        self.assertIn(
            "/add - add note",
            self.mock_chatbot.last_message()['text'],
        )
        self.user.send_message('/start')
        self.assertIn(
            "/add - add note",
            self.mock_chatbot.last_message()['text'],
        )
        self.user.send_message('/cancel')
        self.assertIn(
            "/add - add note",
            self.mock_chatbot.last_message()['text'],
        )
 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)
Beispiel #12
0
    def test_extract_entities(self, mocked_dispatcher: Mock):
        entities = MockUser._extract_entities(text='/start /help')

        expected_entities = [
            {
                'offset': 0,
                'length': 6,
                'type': 'bot_command'
            },
            {
                'offset': 7,
                'length': 5,
                'type': 'bot_command'
            },
        ]
        self.assertEqual(expected_entities, entities)