Ejemplo n.º 1
0
    def test_next_update_id(self, mocked_dispatcher: Mock):
        """update_id must be unique"""
        now = timezone.now()
        mock_user = ClientUser(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)
Ejemplo n.º 2
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 = ClientUser(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)
Ejemplo n.º 3
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 = ClientUser(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)
Ejemplo n.º 4
0
    def test_callback_queries(self, mocked_dispatcher: Mock):
        mock_user = ClientUser(bot=self.bot)
        another_user = ClientUser(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,
        )
Ejemplo n.º 5
0
    def test_init(self, mocked_next_user_id: Mock, mocked_dispatcher: Mock):
        mocked_next_user_id.return_value = 100
        test_user = ClientUser(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)
Ejemplo n.º 6
0
 def test_next_callback_query(self, mocked_dispatcher: Mock):
     mock_user = ClientUser(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")
Ejemplo n.º 7
0
    def test_extract_entities(self, mocked_dispatcher: Mock):
        entities = ClientUser._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)