Ejemplo n.º 1
0
    def test_init_form(self):
        self.mock_user.send_message("hey, form")

        self.assertEqual(self.mock_user.messages()[0].text, 'An apple?')
        self.assertEqual(
            self.mock_user.messages()[0].reply_markup,
            InlineKeyboardMarkup(inline_keyboard=[[
                InlineKeyboardButton("Yes", callback_data="yes")
            ], [InlineKeyboardButton("No", callback_data="no")]]))
        self.assertEqual(self.mock_user.messages()[1].text, 'hey, form')
Ejemplo n.º 2
0
    def test_edit(self, mocked_edit_message_text: Mock):
        old_text = "old text"
        new_text = "new text"
        old_markup = InlineKeyboardMarkup(inline_keyboard=[[
            InlineKeyboardButton("Yes", callback_data="yes"),
            InlineKeyboardButton("No", callback_data="no"),
        ]])
        new_markup = InlineKeyboardMarkup(inline_keyboard=[[
            InlineKeyboardButton("Yes and yes", callback_data="yes"),
        ]])
        bot = Bot.objects.create(
            name="bot",
            token="token",
        )
        chat = Chat.objects.create(
            bot=bot,
            chat_id=142,
            type="private",
        )
        message = Message.objects.create(
            message_id=42,
            date=timezone.datetime(1999, 12, 31, tzinfo=timezone.utc),
            chat=chat,
            text=old_text,
            _reply_markup=old_markup.to_dict(),
        )
        mocked_edit_message_text.return_value = TelegramMessage(
            message_id=42,
            date=timezone.datetime(1999, 12, 31, tzinfo=timezone.utc),
            chat=TelegramChat(id=142, type="private"),
            text=new_text,
            reply_markup=new_markup,
        )

        returned = message.edit(
            text=new_text,
            reply_markup=new_markup,
        )
        mocked_edit_message_text.assert_called_with(
            text=new_text,
            chat_id=chat.chat_id,
            message_id=message.message_id,
            parse_mode=None,
            entities=None,
            disable_web_page_preview=None,
            reply_markup=new_markup,
        )

        message.refresh_from_db()

        self.assertEqual(message.reply_markup, new_markup)
        self.assertEqual(message.text, new_text)
        self.assertEqual(message, returned)
Ejemplo n.º 3
0
    def test_apple_field(self):
        self.mock_user.send_message("hey, form")
        self.mock_user.send_callback_query(data='yes')

        self.assertEqual(self.mock_user.messages()[0].text, 'An orange?')
        self.assertEqual(
            self.mock_user.messages()[0].reply_markup,
            InlineKeyboardMarkup(inline_keyboard=[[
                InlineKeyboardButton("Yes", callback_data="yes")
            ], [InlineKeyboardButton("No", callback_data="no")]]))
        self.assertEqual(self.mock_user.messages()[1].text, 'An apple?')
        self.assertEqual(self.mock_user.callback_queries()[0].data, 'yes')
Ejemplo n.º 4
0
 class Form(forms.Form):
     apple_field = forms.Field(
         prompt="An apple?",
         inline_keyboard=[[
             InlineKeyboardButton("Yes", callback_data="yes")
         ], [InlineKeyboardButton("No", callback_data="no")]],
     )
     orange_field = forms.Field(
         prompt="An orange?",
         inline_keyboard=[[
             InlineKeyboardButton("Yes", callback_data="yes")
         ], [InlineKeyboardButton("No", callback_data="no")]],
     )
Ejemplo n.º 5
0
    def test_to_prompt_message__text(self):
        field = forms.Field(
            name='field',
            prompt="Input:",
            inline_keyboard=[[
                InlineKeyboardButton("Yes", callback_data="yes")
            ], [InlineKeyboardButton("No", callback_data="No")]],
        )

        message = field.get_prompt_message_params(Mock(), Mock())

        self.assertEqual(message.reply_markup.inline_keyboard,
                         [[InlineKeyboardButton("Yes", callback_data="yes")],
                          [InlineKeyboardButton("No", callback_data="No")]])
        self.assertEqual(message.text, "Input:")
Ejemplo n.º 6
0
    def test_edit_reply_markup(self, mocked_edit_message_reply_markup: Mock):
        old_markup = InlineKeyboardMarkup(inline_keyboard=[[
            InlineKeyboardButton("Yes", callback_data="yes"),
            InlineKeyboardButton("No", callback_data="no"),
        ]])
        new_markup = InlineKeyboardMarkup(inline_keyboard=[[
            InlineKeyboardButton("Yes and yes", callback_data="yes"),
        ]])
        bot = Bot.objects.create(
            name="bot",
            token="token",
        )
        chat = Chat.objects.create(
            bot=bot,
            chat_id=142,
            type="private",
        )
        message = Message.objects.create(
            message_id=42,
            date=timezone.datetime(1999, 12, 31, tzinfo=timezone.utc),
            chat=chat,
            _reply_markup=old_markup.to_dict(),
        )
        mocked_edit_message_reply_markup.return_value = TelegramMessage(
            message_id=42,
            date=timezone.datetime(1999, 12, 31, tzinfo=timezone.utc),
            chat=TelegramChat(id=142, type="private"),
            reply_markup=new_markup,
        )

        returned = message.edit_reply_markup(reply_markup=new_markup, )

        mocked_edit_message_reply_markup.assert_called_with(
            chat_id=chat.chat_id,
            message_id=message.message_id,
            reply_markup=new_markup)

        message.refresh_from_db()

        self.assertEqual(message.reply_markup, new_markup)
        self.assertEqual(message, returned)
Ejemplo n.º 7
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())