Beispiel #1
0
    def test_from_telegram(self):
        telegram_callback_query = TelegramCallbackQuery(
            id="4382bfdwdsb323b2d9",
            data="Data from button callback",
            chat_instance="42a",
            from_user=TelegramUser(
                id=1111111,
                is_bot=False,
                username="******",
                first_name="Test Firstname",
                last_name="Test Lastname",
            ),
            message=TelegramMessage(
                message_id=42,
                date=timezone.datetime(2000, 1, 1, tzinfo=timezone.utc),
                chat=TelegramChat(id=42, type="private"),
                from_user=TelegramUser(id=40, is_bot=False),
            )
        )

        CallbackQuery.objects.from_telegram(
            bot=self.bot,
            telegram_callback_query=telegram_callback_query,
        )

        callback_query = CallbackQuery.objects.first()
        user = User.objects.get(user_id=1111111)
        message = Message.objects.first()
        self.assertEqual(
            callback_query.callback_query_id,
            telegram_callback_query.id)
        self.assertEqual(callback_query.from_user, user)
        self.assertEqual(callback_query.message, message)
        self.assertEqual(callback_query.chat.chat_id, 42)
        self.assertEqual(callback_query.text, "Data from button callback")
Beispiel #2
0
    def test_from_telegram(self):
        animation = Animation(
            file_id="1",
            file_unique_id="1",
            width=1,
            height=1,
            duration=1,
        )
        reply_markup = InlineKeyboardMarkup(
            inline_keyboard=[[InlineKeyboardButton(text="button")]]
        )
        telegram_message = TelegramMessage(
            message_id=42,
            date=timezone.datetime(2000, 1, 1, tzinfo=timezone.utc),
            chat=TelegramChat(id=42, type="private"),
            from_user=TelegramUser(id=40, is_bot=False),
            animation=animation,
            reply_markup=reply_markup,
            left_chat_member=TelegramUser(id=41, is_bot=False),
            new_chat_members=[
                TelegramUser(id=42, is_bot=False),
                TelegramUser(id=43, is_bot=False),
            ]
        )
        direction = Message.DIRECTION_OUT

        Message.objects.from_telegram(
            bot=self.bot,
            telegram_message=telegram_message,
            direction=direction
        )

        chat = Chat.objects.first()
        self.assertEqual(chat.chat_id, 42)
        self.assertEqual(chat.type, "private")
        user = User.objects.get(user_id=40)
        self.assertEqual(user.is_bot, False)
        message = Message.objects.first()
        self.assertEqual(message.direction, Message.DIRECTION_OUT)
        self.assertEqual(message.message_id, 42)
        self.assertEqual(message.chat, chat)
        self.assertEqual(message.from_user, user)
        self.assertEqual(message.animation, animation)
        self.assertEqual(message.reply_markup, reply_markup)
        self.assertEqual(message.left_chat_member.user_id, 41)
        new_chat_member_1 = User.objects.get(user_id=42)
        new_chat_member_2 = User.objects.get(user_id=43)
        self.assertIn(new_chat_member_1, message.new_chat_members.all())
        self.assertIn(new_chat_member_2, message.new_chat_members.all())
Beispiel #3
0
    def test_get_me__successfull(self, mocked_now, mocked_api):
        now = timezone.datetime(2000, 1, 1, tzinfo=timezone.utc)
        mocked_now.return_value = now
        telegram_user = TelegramUser(
            id=7,
            is_bot=True,
            first_name="first_name",
            username="******",
        )
        mocked_api.return_value.get_me.return_value = telegram_user

        me = self.bot.get_me()

        mocked_api.assert_called_with(token="bot-token")
        self.assertEqual(me, telegram_user)
        self.bot.refresh_from_db()
        self.assertEqual(
            self.bot._me,
            {
                'id': 7,
                'is_bot': True,
                'first_name': 'first_name',
                'username': '******'
            }
        )
        self.bot.refresh_from_db()
        self.assertEqual(self.bot.update_successful, True)
        self.assertEqual(self.bot.me_update_datetime, now)
Beispiel #4
0
    def test_from_telegram__callback_query(self):
        telegram_update = TelegramUpdate(
            update_id=10000,
            callback_query=TelegramCallbackQuery(
                id="4382bfdwdsb323b2d9",
                data="Data from button callback",
                inline_message_id="1234csdbsk4839",
                chat_instance="42a",
                from_user=TelegramUser(
                    id=1111111,
                    is_bot=False,
                    username="******",
                    first_name="Test Firstname",
                    last_name="Test Lastname",
                )
            )
        )

        Update.objects.from_telegram(
            bot=self.bot,
            telegram_update=telegram_update,
        )

        update = Update.objects.first()
        self.assertEqual(update.update_id, telegram_update.update_id)
Beispiel #5
0
    def test_from_telegram__existing(self):
        telegram_user = TelegramUser(
            id=42,
            is_bot=False,
            first_name="first_name",
            last_name="last_name",
            username="******",
            language_code="en",
            can_join_groups=True,
            can_read_all_group_messages=True,
            supports_inline_queries=True,
        )
        User.objects.create(
            user_id=42,
            first_name="old_first_name",
            last_name="old_last_name",
            username="******",
            language_code="en",
        )

        User.objects.from_telegram(telegram_user)

        self.assertEqual(User.objects.count(), 1)
        user = User.objects.first()
        self.assertEqual(user.user_id, 42)
        self.assertEqual(user.is_bot, False)
        self.assertEqual(user.first_name, "first_name")
        self.assertEqual(user.last_name, "last_name")
        self.assertEqual(user.username, "username")
Beispiel #6
0
    def test_reply(self, mocked_send_message: Mock):
        bot = Bot.objects.create(
            name="bot",
            token="token",
        )
        chat = Chat.objects.create(
            bot=bot,
            chat_id=142,
            type='private',
        )
        incoming_message = Message.objects.create(
            message_id=42,
            date=timezone.datetime(2000, 1, 1, tzinfo=timezone.utc),
            chat=chat,
        )

        mocked_send_message.return_value = TelegramMessage(
            message_id=43,
            date=timezone.datetime(1999, 12, 31, tzinfo=timezone.utc),
            from_user=TelegramUser(id=1, is_bot=True),
            chat=TelegramChat(id=142, type="private"),
            text='Reply',
            reply_to_message=TelegramMessage(
                message_id=42,
                chat=TelegramChat(id=142, type="private"),
                date=timezone.datetime(2000, 1, 1, tzinfo=timezone.utc),
            ),
        )

        incoming_message.reply(
            text="Reply",
            reply=True,
        )

        mocked_send_message.assert_called_with(
            chat_id=chat.chat_id,
            text="Reply",
            parse_mode=None,
            reply_to_message_id=42,

        )

        user = User.objects.first()
        message = Message.objects.last()

        self.assertEqual(message.direction, Message.DIRECTION_OUT)
        self.assertEqual(message.message_id, 43)
        self.assertEqual(message.date,
                         timezone.datetime(1999, 12, 31, tzinfo=timezone.utc))
        self.assertEqual(message.chat, chat)
        self.assertEqual(message.from_user, user)
        self.assertEqual(message.reply_to_message, incoming_message)
        self.assertEqual(incoming_message.reply_message, message)
Beispiel #7
0
    def test_me(self):
        bot = Bot(
            _me={
                "id": 7,
                "is_bot": True,
                "first_name": "first_name",
                "username": "******",
            })

        self.assertEqual(
            bot.me,
            TelegramUser(
                id=7,
                is_bot=True,
                first_name="first_name",
                username="******",
            ),
        )
Beispiel #8
0
    def test_me(self):
        bot = Bot(
            _me={
                'id': 7,
                'is_bot': True,
                'first_name': 'first_name',
                'username': '******'
            }
        )

        self.assertEqual(
            bot.me,
            TelegramUser(
                id=7,
                is_bot=True,
                first_name='first_name',
                username='******',
            )
        )