2016-04-27 00:08:35 +02:00
|
|
|
#!/usr/bin/env python
|
2016-04-26 23:42:50 +02:00
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2018-01-04 16:16:06 +01:00
|
|
|
# Copyright (C) 2015-2018
|
2016-04-26 23:42:50 +02:00
|
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2017-08-11 23:58:41 +02:00
|
|
|
# it under the terms of the GNU Lesser Public License as published by
|
2016-04-26 23:42:50 +02:00
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2017-08-11 23:58:41 +02:00
|
|
|
# GNU Lesser Public License for more details.
|
2016-04-26 23:42:50 +02:00
|
|
|
#
|
2017-08-11 23:58:41 +02:00
|
|
|
# You should have received a copy of the GNU Lesser Public License
|
2016-04-26 23:42:50 +02:00
|
|
|
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
2017-08-11 23:58:41 +02:00
|
|
|
import datetime
|
2016-04-26 23:42:50 +02:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
import pytest
|
2016-04-26 23:42:50 +02:00
|
|
|
|
2019-02-13 12:07:25 +01:00
|
|
|
from telegram import Message, User, Chat, MessageEntity, Document, Update
|
2016-10-15 22:59:41 +02:00
|
|
|
from telegram.ext import Filters, BaseFilter
|
2018-03-15 05:59:27 +01:00
|
|
|
import re
|
2017-04-23 23:22:05 +02:00
|
|
|
|
2017-06-20 09:49:01 +02:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
@pytest.fixture(scope='function')
|
2019-02-13 12:07:25 +01:00
|
|
|
def update():
|
2019-11-15 21:51:22 +01:00
|
|
|
return Update(0, Message(0, User(0, 'Testuser', False), datetime.datetime.utcnow(),
|
2019-02-13 12:07:25 +01:00
|
|
|
Chat(0, 'private')))
|
2017-08-11 23:58:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function',
|
|
|
|
params=MessageEntity.ALL_TYPES)
|
|
|
|
def message_entity(request):
|
|
|
|
return MessageEntity(request.param, 0, 0, url='', user='')
|
|
|
|
|
|
|
|
|
|
|
|
class TestFilters(object):
|
2019-02-13 12:07:25 +01:00
|
|
|
def test_filters_all(self, update):
|
|
|
|
assert Filters.all(update)
|
|
|
|
|
|
|
|
def test_filters_text(self, update):
|
|
|
|
update.message.text = 'test'
|
|
|
|
assert Filters.text(update)
|
|
|
|
update.message.text = '/test'
|
|
|
|
assert not Filters.text(update)
|
|
|
|
|
|
|
|
def test_filters_command(self, update):
|
|
|
|
update.message.text = 'test'
|
|
|
|
assert not Filters.command(update)
|
|
|
|
update.message.text = '/test'
|
|
|
|
assert Filters.command(update)
|
|
|
|
|
|
|
|
def test_filters_regex(self, update):
|
|
|
|
SRE_TYPE = type(re.match("", ""))
|
|
|
|
update.message.text = '/start deep-linked param'
|
|
|
|
result = Filters.regex(r'deep-linked param')(update)
|
|
|
|
assert result
|
|
|
|
assert isinstance(result, dict)
|
|
|
|
matches = result['matches']
|
|
|
|
assert isinstance(matches, list)
|
|
|
|
assert type(matches[0]) is SRE_TYPE
|
|
|
|
update.message.text = '/help'
|
|
|
|
assert Filters.regex(r'help')(update)
|
|
|
|
|
|
|
|
update.message.text = 'test'
|
|
|
|
assert not Filters.regex(r'fail')(update)
|
|
|
|
assert Filters.regex(r'test')(update)
|
|
|
|
assert Filters.regex(re.compile(r'test'))(update)
|
|
|
|
assert Filters.regex(re.compile(r'TEST', re.IGNORECASE))(update)
|
|
|
|
|
|
|
|
update.message.text = 'i love python'
|
|
|
|
assert Filters.regex(r'.\b[lo]{2}ve python')(update)
|
|
|
|
|
|
|
|
update.message.text = None
|
|
|
|
assert not Filters.regex(r'fail')(update)
|
|
|
|
|
|
|
|
def test_filters_regex_multiple(self, update):
|
|
|
|
SRE_TYPE = type(re.match("", ""))
|
|
|
|
update.message.text = '/start deep-linked param'
|
|
|
|
result = (Filters.regex('deep') & Filters.regex(r'linked param'))(update)
|
|
|
|
assert result
|
|
|
|
assert isinstance(result, dict)
|
|
|
|
matches = result['matches']
|
|
|
|
assert isinstance(matches, list)
|
|
|
|
assert all([type(res) == SRE_TYPE for res in matches])
|
|
|
|
result = (Filters.regex('deep') | Filters.regex(r'linked param'))(update)
|
|
|
|
assert result
|
|
|
|
assert isinstance(result, dict)
|
|
|
|
matches = result['matches']
|
|
|
|
assert isinstance(matches, list)
|
|
|
|
assert all([type(res) == SRE_TYPE for res in matches])
|
|
|
|
result = (Filters.regex('not int') | Filters.regex(r'linked param'))(update)
|
|
|
|
assert result
|
|
|
|
assert isinstance(result, dict)
|
|
|
|
matches = result['matches']
|
|
|
|
assert isinstance(matches, list)
|
|
|
|
assert all([type(res) == SRE_TYPE for res in matches])
|
|
|
|
result = (Filters.regex('not int') & Filters.regex(r'linked param'))(update)
|
|
|
|
assert not result
|
|
|
|
|
|
|
|
def test_filters_merged_with_regex(self, update):
|
|
|
|
SRE_TYPE = type(re.match("", ""))
|
|
|
|
update.message.text = '/start deep-linked param'
|
|
|
|
result = (Filters.command & Filters.regex(r'linked param'))(update)
|
|
|
|
assert result
|
|
|
|
assert isinstance(result, dict)
|
|
|
|
matches = result['matches']
|
|
|
|
assert isinstance(matches, list)
|
|
|
|
assert all([type(res) == SRE_TYPE for res in matches])
|
|
|
|
result = (Filters.regex(r'linked param') & Filters.command)(update)
|
|
|
|
assert result
|
|
|
|
assert isinstance(result, dict)
|
|
|
|
matches = result['matches']
|
|
|
|
assert isinstance(matches, list)
|
|
|
|
assert all([type(res) == SRE_TYPE for res in matches])
|
|
|
|
result = (Filters.regex(r'linked param') | Filters.command)(update)
|
|
|
|
assert result
|
|
|
|
assert isinstance(result, dict)
|
|
|
|
matches = result['matches']
|
|
|
|
assert isinstance(matches, list)
|
|
|
|
assert all([type(res) == SRE_TYPE for res in matches])
|
2019-03-14 09:03:21 +01:00
|
|
|
# Should not give a match since it's a or filter and it short circuits
|
2019-02-13 12:07:25 +01:00
|
|
|
result = (Filters.command | Filters.regex(r'linked param'))(update)
|
2019-03-14 09:03:21 +01:00
|
|
|
assert result is True
|
2019-02-13 12:07:25 +01:00
|
|
|
|
|
|
|
def test_regex_complex_merges(self, update):
|
|
|
|
SRE_TYPE = type(re.match("", ""))
|
|
|
|
update.message.text = 'test it out'
|
|
|
|
filter = (Filters.regex('test')
|
|
|
|
& ((Filters.status_update | Filters.forwarded) | Filters.regex('out')))
|
|
|
|
result = filter(update)
|
|
|
|
assert result
|
|
|
|
assert isinstance(result, dict)
|
|
|
|
matches = result['matches']
|
|
|
|
assert isinstance(matches, list)
|
|
|
|
assert len(matches) == 2
|
|
|
|
assert all([type(res) == SRE_TYPE for res in matches])
|
2019-11-15 21:51:22 +01:00
|
|
|
update.message.forward_date = datetime.datetime.utcnow()
|
2019-02-13 12:07:25 +01:00
|
|
|
result = filter(update)
|
|
|
|
assert result
|
|
|
|
assert isinstance(result, dict)
|
|
|
|
matches = result['matches']
|
|
|
|
assert isinstance(matches, list)
|
|
|
|
assert all([type(res) == SRE_TYPE for res in matches])
|
|
|
|
update.message.text = 'test it'
|
|
|
|
result = filter(update)
|
|
|
|
assert result
|
|
|
|
assert isinstance(result, dict)
|
|
|
|
matches = result['matches']
|
|
|
|
assert isinstance(matches, list)
|
|
|
|
assert all([type(res) == SRE_TYPE for res in matches])
|
|
|
|
update.message.forward_date = False
|
|
|
|
result = filter(update)
|
|
|
|
assert not result
|
|
|
|
update.message.text = 'test it out'
|
|
|
|
result = filter(update)
|
|
|
|
assert result
|
|
|
|
assert isinstance(result, dict)
|
|
|
|
matches = result['matches']
|
|
|
|
assert isinstance(matches, list)
|
|
|
|
assert all([type(res) == SRE_TYPE for res in matches])
|
|
|
|
update.message.pinned_message = True
|
|
|
|
result = filter(update)
|
|
|
|
assert result
|
|
|
|
assert isinstance(result, dict)
|
|
|
|
matches = result['matches']
|
|
|
|
assert isinstance(matches, list)
|
|
|
|
assert all([type(res) == SRE_TYPE for res in matches])
|
|
|
|
update.message.text = 'it out'
|
|
|
|
result = filter(update)
|
|
|
|
assert not result
|
|
|
|
|
|
|
|
update.message.text = 'test it out'
|
|
|
|
update.message.forward_date = None
|
|
|
|
update.message.pinned_message = None
|
|
|
|
filter = ((Filters.regex('test') | Filters.command)
|
|
|
|
& (Filters.regex('it') | Filters.status_update))
|
|
|
|
result = filter(update)
|
|
|
|
assert result
|
|
|
|
assert isinstance(result, dict)
|
|
|
|
matches = result['matches']
|
|
|
|
assert isinstance(matches, list)
|
|
|
|
assert len(matches) == 2
|
|
|
|
assert all([type(res) == SRE_TYPE for res in matches])
|
|
|
|
update.message.text = 'test'
|
|
|
|
result = filter(update)
|
|
|
|
assert not result
|
|
|
|
update.message.pinned_message = True
|
|
|
|
result = filter(update)
|
|
|
|
assert result
|
|
|
|
assert isinstance(result, dict)
|
|
|
|
matches = result['matches']
|
|
|
|
assert isinstance(matches, list)
|
|
|
|
assert len(matches) == 1
|
|
|
|
assert all([type(res) == SRE_TYPE for res in matches])
|
|
|
|
update.message.text = 'nothing'
|
|
|
|
result = filter(update)
|
|
|
|
assert not result
|
|
|
|
update.message.text = '/start'
|
|
|
|
result = filter(update)
|
|
|
|
assert result
|
|
|
|
assert isinstance(result, bool)
|
|
|
|
update.message.text = '/start it'
|
|
|
|
result = filter(update)
|
|
|
|
assert result
|
|
|
|
assert isinstance(result, dict)
|
|
|
|
matches = result['matches']
|
|
|
|
assert isinstance(matches, list)
|
|
|
|
assert len(matches) == 1
|
|
|
|
assert all([type(res) == SRE_TYPE for res in matches])
|
|
|
|
|
|
|
|
def test_regex_inverted(self, update):
|
|
|
|
update.message.text = '/start deep-linked param'
|
|
|
|
filter = ~Filters.regex(r'deep-linked param')
|
|
|
|
result = filter(update)
|
|
|
|
assert not result
|
|
|
|
update.message.text = 'not it'
|
|
|
|
result = filter(update)
|
|
|
|
assert result
|
|
|
|
assert isinstance(result, bool)
|
|
|
|
|
|
|
|
filter = (~Filters.regex('linked') & Filters.command)
|
|
|
|
update.message.text = "it's linked"
|
|
|
|
result = filter(update)
|
|
|
|
assert not result
|
|
|
|
update.message.text = '/start'
|
|
|
|
result = filter(update)
|
|
|
|
assert result
|
|
|
|
update.message.text = '/linked'
|
|
|
|
result = filter(update)
|
|
|
|
assert not result
|
|
|
|
|
|
|
|
filter = (~Filters.regex('linked') | Filters.command)
|
|
|
|
update.message.text = "it's linked"
|
|
|
|
result = filter(update)
|
|
|
|
assert not result
|
|
|
|
update.message.text = '/start linked'
|
|
|
|
result = filter(update)
|
|
|
|
assert result
|
|
|
|
update.message.text = '/start'
|
|
|
|
result = filter(update)
|
|
|
|
assert result
|
|
|
|
update.message.text = 'nothig'
|
|
|
|
result = filter(update)
|
|
|
|
assert result
|
|
|
|
|
|
|
|
def test_filters_reply(self, update):
|
2019-11-15 21:51:22 +01:00
|
|
|
another_message = Message(1, User(1, 'TestOther', False), datetime.datetime.utcnow(),
|
2017-08-11 23:58:41 +02:00
|
|
|
Chat(0, 'private'))
|
2019-02-13 12:07:25 +01:00
|
|
|
update.message.text = 'test'
|
|
|
|
assert not Filters.reply(update)
|
|
|
|
update.message.reply_to_message = another_message
|
|
|
|
assert Filters.reply(update)
|
|
|
|
|
|
|
|
def test_filters_audio(self, update):
|
|
|
|
assert not Filters.audio(update)
|
|
|
|
update.message.audio = 'test'
|
|
|
|
assert Filters.audio(update)
|
|
|
|
|
|
|
|
def test_filters_document(self, update):
|
|
|
|
assert not Filters.document(update)
|
|
|
|
update.message.document = 'test'
|
|
|
|
assert Filters.document(update)
|
|
|
|
|
|
|
|
def test_filters_document_type(self, update):
|
|
|
|
update.message.document = Document("file_id",
|
|
|
|
mime_type="application/vnd.android.package-archive")
|
|
|
|
assert Filters.document.apk(update)
|
|
|
|
assert Filters.document.application(update)
|
|
|
|
assert not Filters.document.doc(update)
|
|
|
|
assert not Filters.document.audio(update)
|
|
|
|
|
|
|
|
update.message.document.mime_type = "application/msword"
|
|
|
|
assert Filters.document.doc(update)
|
|
|
|
assert Filters.document.application(update)
|
|
|
|
assert not Filters.document.docx(update)
|
|
|
|
assert not Filters.document.audio(update)
|
|
|
|
|
|
|
|
update.message.document.mime_type = "application/vnd.openxmlformats-officedocument." \
|
|
|
|
"wordprocessingml.document"
|
|
|
|
assert Filters.document.docx(update)
|
|
|
|
assert Filters.document.application(update)
|
|
|
|
assert not Filters.document.exe(update)
|
|
|
|
assert not Filters.document.audio(update)
|
|
|
|
|
|
|
|
update.message.document.mime_type = "application/x-ms-dos-executable"
|
|
|
|
assert Filters.document.exe(update)
|
|
|
|
assert Filters.document.application(update)
|
|
|
|
assert not Filters.document.docx(update)
|
|
|
|
assert not Filters.document.audio(update)
|
|
|
|
|
|
|
|
update.message.document.mime_type = "video/mp4"
|
|
|
|
assert Filters.document.gif(update)
|
|
|
|
assert Filters.document.video(update)
|
|
|
|
assert not Filters.document.jpg(update)
|
|
|
|
assert not Filters.document.text(update)
|
|
|
|
|
|
|
|
update.message.document.mime_type = "image/jpeg"
|
|
|
|
assert Filters.document.jpg(update)
|
|
|
|
assert Filters.document.image(update)
|
|
|
|
assert not Filters.document.mp3(update)
|
|
|
|
assert not Filters.document.video(update)
|
|
|
|
|
|
|
|
update.message.document.mime_type = "audio/mpeg"
|
|
|
|
assert Filters.document.mp3(update)
|
|
|
|
assert Filters.document.audio(update)
|
|
|
|
assert not Filters.document.pdf(update)
|
|
|
|
assert not Filters.document.image(update)
|
|
|
|
|
|
|
|
update.message.document.mime_type = "application/pdf"
|
|
|
|
assert Filters.document.pdf(update)
|
|
|
|
assert Filters.document.application(update)
|
|
|
|
assert not Filters.document.py(update)
|
|
|
|
assert not Filters.document.audio(update)
|
|
|
|
|
|
|
|
update.message.document.mime_type = "text/x-python"
|
|
|
|
assert Filters.document.py(update)
|
|
|
|
assert Filters.document.text(update)
|
|
|
|
assert not Filters.document.svg(update)
|
|
|
|
assert not Filters.document.application(update)
|
|
|
|
|
|
|
|
update.message.document.mime_type = "image/svg+xml"
|
|
|
|
assert Filters.document.svg(update)
|
|
|
|
assert Filters.document.image(update)
|
|
|
|
assert not Filters.document.txt(update)
|
|
|
|
assert not Filters.document.video(update)
|
|
|
|
|
|
|
|
update.message.document.mime_type = "text/plain"
|
|
|
|
assert Filters.document.txt(update)
|
|
|
|
assert Filters.document.text(update)
|
|
|
|
assert not Filters.document.targz(update)
|
|
|
|
assert not Filters.document.application(update)
|
|
|
|
|
|
|
|
update.message.document.mime_type = "application/x-compressed-tar"
|
|
|
|
assert Filters.document.targz(update)
|
|
|
|
assert Filters.document.application(update)
|
|
|
|
assert not Filters.document.wav(update)
|
|
|
|
assert not Filters.document.audio(update)
|
|
|
|
|
|
|
|
update.message.document.mime_type = "audio/x-wav"
|
|
|
|
assert Filters.document.wav(update)
|
|
|
|
assert Filters.document.audio(update)
|
|
|
|
assert not Filters.document.xml(update)
|
|
|
|
assert not Filters.document.image(update)
|
|
|
|
|
|
|
|
update.message.document.mime_type = "application/xml"
|
|
|
|
assert Filters.document.xml(update)
|
|
|
|
assert Filters.document.application(update)
|
|
|
|
assert not Filters.document.zip(update)
|
|
|
|
assert not Filters.document.audio(update)
|
|
|
|
|
|
|
|
update.message.document.mime_type = "application/zip"
|
|
|
|
assert Filters.document.zip(update)
|
|
|
|
assert Filters.document.application(update)
|
|
|
|
assert not Filters.document.apk(update)
|
|
|
|
assert not Filters.document.audio(update)
|
|
|
|
|
|
|
|
update.message.document.mime_type = "image/x-rgb"
|
|
|
|
assert not Filters.document.category("application/")(update)
|
|
|
|
assert not Filters.document.mime_type("application/x-sh")(update)
|
|
|
|
update.message.document.mime_type = "application/x-sh"
|
|
|
|
assert Filters.document.category("application/")(update)
|
|
|
|
assert Filters.document.mime_type("application/x-sh")(update)
|
|
|
|
|
|
|
|
def test_filters_animation(self, update):
|
|
|
|
assert not Filters.animation(update)
|
|
|
|
update.message.animation = 'test'
|
|
|
|
assert Filters.animation(update)
|
|
|
|
|
|
|
|
def test_filters_photo(self, update):
|
|
|
|
assert not Filters.photo(update)
|
|
|
|
update.message.photo = 'test'
|
|
|
|
assert Filters.photo(update)
|
|
|
|
|
|
|
|
def test_filters_sticker(self, update):
|
|
|
|
assert not Filters.sticker(update)
|
|
|
|
update.message.sticker = 'test'
|
|
|
|
assert Filters.sticker(update)
|
|
|
|
|
|
|
|
def test_filters_video(self, update):
|
|
|
|
assert not Filters.video(update)
|
|
|
|
update.message.video = 'test'
|
|
|
|
assert Filters.video(update)
|
|
|
|
|
|
|
|
def test_filters_voice(self, update):
|
|
|
|
assert not Filters.voice(update)
|
|
|
|
update.message.voice = 'test'
|
|
|
|
assert Filters.voice(update)
|
|
|
|
|
|
|
|
def test_filters_video_note(self, update):
|
|
|
|
assert not Filters.video_note(update)
|
|
|
|
update.message.video_note = 'test'
|
|
|
|
assert Filters.video_note(update)
|
|
|
|
|
|
|
|
def test_filters_contact(self, update):
|
|
|
|
assert not Filters.contact(update)
|
|
|
|
update.message.contact = 'test'
|
|
|
|
assert Filters.contact(update)
|
|
|
|
|
|
|
|
def test_filters_location(self, update):
|
|
|
|
assert not Filters.location(update)
|
|
|
|
update.message.location = 'test'
|
|
|
|
assert Filters.location(update)
|
|
|
|
|
|
|
|
def test_filters_venue(self, update):
|
|
|
|
assert not Filters.venue(update)
|
|
|
|
update.message.venue = 'test'
|
|
|
|
assert Filters.venue(update)
|
|
|
|
|
|
|
|
def test_filters_status_update(self, update):
|
|
|
|
assert not Filters.status_update(update)
|
|
|
|
|
|
|
|
update.message.new_chat_members = ['test']
|
|
|
|
assert Filters.status_update(update)
|
|
|
|
assert Filters.status_update.new_chat_members(update)
|
|
|
|
update.message.new_chat_members = None
|
|
|
|
|
|
|
|
update.message.left_chat_member = 'test'
|
|
|
|
assert Filters.status_update(update)
|
|
|
|
assert Filters.status_update.left_chat_member(update)
|
|
|
|
update.message.left_chat_member = None
|
|
|
|
|
|
|
|
update.message.new_chat_title = 'test'
|
|
|
|
assert Filters.status_update(update)
|
|
|
|
assert Filters.status_update.new_chat_title(update)
|
|
|
|
update.message.new_chat_title = ''
|
|
|
|
|
|
|
|
update.message.new_chat_photo = 'test'
|
|
|
|
assert Filters.status_update(update)
|
|
|
|
assert Filters.status_update.new_chat_photo(update)
|
|
|
|
update.message.new_chat_photo = None
|
|
|
|
|
|
|
|
update.message.delete_chat_photo = True
|
|
|
|
assert Filters.status_update(update)
|
|
|
|
assert Filters.status_update.delete_chat_photo(update)
|
|
|
|
update.message.delete_chat_photo = False
|
|
|
|
|
|
|
|
update.message.group_chat_created = True
|
|
|
|
assert Filters.status_update(update)
|
|
|
|
assert Filters.status_update.chat_created(update)
|
|
|
|
update.message.group_chat_created = False
|
|
|
|
|
|
|
|
update.message.supergroup_chat_created = True
|
|
|
|
assert Filters.status_update(update)
|
|
|
|
assert Filters.status_update.chat_created(update)
|
|
|
|
update.message.supergroup_chat_created = False
|
|
|
|
|
|
|
|
update.message.channel_chat_created = True
|
|
|
|
assert Filters.status_update(update)
|
|
|
|
assert Filters.status_update.chat_created(update)
|
|
|
|
update.message.channel_chat_created = False
|
|
|
|
|
|
|
|
update.message.migrate_to_chat_id = 100
|
|
|
|
assert Filters.status_update(update)
|
|
|
|
assert Filters.status_update.migrate(update)
|
|
|
|
update.message.migrate_to_chat_id = 0
|
|
|
|
|
|
|
|
update.message.migrate_from_chat_id = 100
|
|
|
|
assert Filters.status_update(update)
|
|
|
|
assert Filters.status_update.migrate(update)
|
|
|
|
update.message.migrate_from_chat_id = 0
|
|
|
|
|
|
|
|
update.message.pinned_message = 'test'
|
|
|
|
assert Filters.status_update(update)
|
|
|
|
assert Filters.status_update.pinned_message(update)
|
|
|
|
update.message.pinned_message = None
|
|
|
|
|
|
|
|
update.message.connected_website = 'http://example.com/'
|
|
|
|
assert Filters.status_update(update)
|
|
|
|
assert Filters.status_update.connected_website(update)
|
|
|
|
update.message.connected_website = None
|
|
|
|
|
|
|
|
def test_filters_forwarded(self, update):
|
|
|
|
assert not Filters.forwarded(update)
|
2019-11-15 21:51:22 +01:00
|
|
|
update.message.forward_date = datetime.datetime.utcnow()
|
2019-02-13 12:07:25 +01:00
|
|
|
assert Filters.forwarded(update)
|
|
|
|
|
|
|
|
def test_filters_game(self, update):
|
|
|
|
assert not Filters.game(update)
|
|
|
|
update.message.game = 'test'
|
|
|
|
assert Filters.game(update)
|
|
|
|
|
|
|
|
def test_entities_filter(self, update, message_entity):
|
|
|
|
update.message.entities = [message_entity]
|
|
|
|
assert Filters.entity(message_entity.type)(update)
|
|
|
|
|
|
|
|
update.message.entities = []
|
|
|
|
assert not Filters.entity(MessageEntity.MENTION)(update)
|
2017-08-11 23:58:41 +02:00
|
|
|
|
|
|
|
second = message_entity.to_dict()
|
|
|
|
second['type'] = 'bold'
|
|
|
|
second = MessageEntity.de_json(second, None)
|
2019-02-13 12:07:25 +01:00
|
|
|
update.message.entities = [message_entity, second]
|
|
|
|
assert Filters.entity(message_entity.type)(update)
|
|
|
|
assert not Filters.caption_entity(message_entity.type)(update)
|
2018-04-20 13:24:40 +02:00
|
|
|
|
2019-02-13 12:07:25 +01:00
|
|
|
def test_caption_entities_filter(self, update, message_entity):
|
|
|
|
update.message.caption_entities = [message_entity]
|
|
|
|
assert Filters.caption_entity(message_entity.type)(update)
|
2018-04-20 13:24:40 +02:00
|
|
|
|
2019-02-13 12:07:25 +01:00
|
|
|
update.message.caption_entities = []
|
|
|
|
assert not Filters.caption_entity(MessageEntity.MENTION)(update)
|
2018-04-20 13:24:40 +02:00
|
|
|
|
|
|
|
second = message_entity.to_dict()
|
|
|
|
second['type'] = 'bold'
|
|
|
|
second = MessageEntity.de_json(second, None)
|
2019-02-13 12:07:25 +01:00
|
|
|
update.message.caption_entities = [message_entity, second]
|
|
|
|
assert Filters.caption_entity(message_entity.type)(update)
|
|
|
|
assert not Filters.entity(message_entity.type)(update)
|
|
|
|
|
|
|
|
def test_private_filter(self, update):
|
|
|
|
assert Filters.private(update)
|
|
|
|
update.message.chat.type = 'group'
|
|
|
|
assert not Filters.private(update)
|
|
|
|
|
|
|
|
def test_group_filter(self, update):
|
|
|
|
assert not Filters.group(update)
|
|
|
|
update.message.chat.type = 'group'
|
|
|
|
assert Filters.group(update)
|
|
|
|
update.message.chat.type = 'supergroup'
|
|
|
|
assert Filters.group(update)
|
2017-06-19 19:53:44 +02:00
|
|
|
|
|
|
|
def test_filters_user(self):
|
2017-08-11 23:58:41 +02:00
|
|
|
with pytest.raises(ValueError, match='user_id or username'):
|
2017-06-22 12:20:11 +02:00
|
|
|
Filters.user(user_id=1, username='user')
|
2017-08-11 23:58:41 +02:00
|
|
|
with pytest.raises(ValueError, match='user_id or username'):
|
2017-06-20 09:49:01 +02:00
|
|
|
Filters.user()
|
|
|
|
|
2019-02-13 12:07:25 +01:00
|
|
|
def test_filters_user_id(self, update):
|
|
|
|
assert not Filters.user(user_id=1)(update)
|
|
|
|
update.message.from_user.id = 1
|
|
|
|
assert Filters.user(user_id=1)(update)
|
|
|
|
update.message.from_user.id = 2
|
|
|
|
assert Filters.user(user_id=[1, 2])(update)
|
|
|
|
assert not Filters.user(user_id=[3, 4])(update)
|
|
|
|
|
|
|
|
def test_filters_username(self, update):
|
|
|
|
assert not Filters.user(username='user')(update)
|
|
|
|
assert not Filters.user(username='Testuser')(update)
|
|
|
|
update.message.from_user.username = 'user'
|
|
|
|
assert Filters.user(username='@user')(update)
|
|
|
|
assert Filters.user(username='user')(update)
|
|
|
|
assert Filters.user(username=['user1', 'user', 'user2'])(update)
|
|
|
|
assert not Filters.user(username=['@username', '@user_2'])(update)
|
2016-10-15 22:59:41 +02:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
def test_filters_chat(self):
|
|
|
|
with pytest.raises(ValueError, match='chat_id or username'):
|
|
|
|
Filters.chat(chat_id=-1, username='chat')
|
|
|
|
with pytest.raises(ValueError, match='chat_id or username'):
|
|
|
|
Filters.chat()
|
|
|
|
|
2019-02-13 12:07:25 +01:00
|
|
|
def test_filters_chat_id(self, update):
|
|
|
|
assert not Filters.chat(chat_id=-1)(update)
|
|
|
|
update.message.chat.id = -1
|
|
|
|
assert Filters.chat(chat_id=-1)(update)
|
|
|
|
update.message.chat.id = -2
|
|
|
|
assert Filters.chat(chat_id=[-1, -2])(update)
|
|
|
|
assert not Filters.chat(chat_id=[-3, -4])(update)
|
|
|
|
|
|
|
|
def test_filters_chat_username(self, update):
|
|
|
|
assert not Filters.chat(username='chat')(update)
|
|
|
|
update.message.chat.username = 'chat'
|
|
|
|
assert Filters.chat(username='@chat')(update)
|
|
|
|
assert Filters.chat(username='chat')(update)
|
|
|
|
assert Filters.chat(username=['chat1', 'chat', 'chat2'])(update)
|
|
|
|
assert not Filters.chat(username=['@chat1', 'chat_2'])(update)
|
|
|
|
|
|
|
|
def test_filters_invoice(self, update):
|
|
|
|
assert not Filters.invoice(update)
|
|
|
|
update.message.invoice = 'test'
|
|
|
|
assert Filters.invoice(update)
|
|
|
|
|
|
|
|
def test_filters_successful_payment(self, update):
|
|
|
|
assert not Filters.successful_payment(update)
|
|
|
|
update.message.successful_payment = 'test'
|
|
|
|
assert Filters.successful_payment(update)
|
|
|
|
|
|
|
|
def test_filters_passport_data(self, update):
|
|
|
|
assert not Filters.passport_data(update)
|
|
|
|
update.message.passport_data = 'test'
|
|
|
|
assert Filters.passport_data(update)
|
|
|
|
|
|
|
|
def test_language_filter_single(self, update):
|
|
|
|
update.message.from_user.language_code = 'en_US'
|
|
|
|
assert (Filters.language('en_US'))(update)
|
|
|
|
assert (Filters.language('en'))(update)
|
|
|
|
assert not (Filters.language('en_GB'))(update)
|
|
|
|
assert not (Filters.language('da'))(update)
|
|
|
|
update.message.from_user.language_code = 'da'
|
|
|
|
assert not (Filters.language('en_US'))(update)
|
|
|
|
assert not (Filters.language('en'))(update)
|
|
|
|
assert not (Filters.language('en_GB'))(update)
|
|
|
|
assert (Filters.language('da'))(update)
|
|
|
|
|
|
|
|
def test_language_filter_multiple(self, update):
|
2017-08-11 23:58:41 +02:00
|
|
|
f = Filters.language(['en_US', 'da'])
|
2019-02-13 12:07:25 +01:00
|
|
|
update.message.from_user.language_code = 'en_US'
|
|
|
|
assert f(update)
|
|
|
|
update.message.from_user.language_code = 'en_GB'
|
|
|
|
assert not f(update)
|
|
|
|
update.message.from_user.language_code = 'da'
|
|
|
|
assert f(update)
|
|
|
|
|
2019-10-27 00:12:54 +02:00
|
|
|
def test_msg_in_filter(self, update):
|
|
|
|
update.message.text = 'test'
|
|
|
|
update.message.caption = 'caption'
|
|
|
|
|
|
|
|
assert Filters.msg_in(['test'])(update)
|
|
|
|
assert Filters.msg_in(['caption'], caption=True)(update)
|
|
|
|
|
|
|
|
assert not Filters.msg_in(['test'], caption=True)(update)
|
|
|
|
assert not Filters.msg_in(['caption'])(update)
|
|
|
|
|
2019-02-13 12:07:25 +01:00
|
|
|
def test_and_filters(self, update):
|
|
|
|
update.message.text = 'test'
|
2019-11-15 21:51:22 +01:00
|
|
|
update.message.forward_date = datetime.datetime.utcnow()
|
2019-02-13 12:07:25 +01:00
|
|
|
assert (Filters.text & Filters.forwarded)(update)
|
|
|
|
update.message.text = '/test'
|
|
|
|
assert not (Filters.text & Filters.forwarded)(update)
|
|
|
|
update.message.text = 'test'
|
|
|
|
update.message.forward_date = None
|
|
|
|
assert not (Filters.text & Filters.forwarded)(update)
|
|
|
|
|
|
|
|
update.message.text = 'test'
|
2019-11-15 21:51:22 +01:00
|
|
|
update.message.forward_date = datetime.datetime.utcnow()
|
2019-02-13 12:07:25 +01:00
|
|
|
assert (Filters.text & Filters.forwarded & Filters.private)(update)
|
|
|
|
|
|
|
|
def test_or_filters(self, update):
|
|
|
|
update.message.text = 'test'
|
|
|
|
assert (Filters.text | Filters.status_update)(update)
|
|
|
|
update.message.group_chat_created = True
|
|
|
|
assert (Filters.text | Filters.status_update)(update)
|
|
|
|
update.message.text = None
|
|
|
|
assert (Filters.text | Filters.status_update)(update)
|
|
|
|
update.message.group_chat_created = False
|
|
|
|
assert not (Filters.text | Filters.status_update)(update)
|
|
|
|
|
|
|
|
def test_and_or_filters(self, update):
|
|
|
|
update.message.text = 'test'
|
2019-11-15 21:51:22 +01:00
|
|
|
update.message.forward_date = datetime.datetime.utcnow()
|
2019-02-13 12:07:25 +01:00
|
|
|
assert (Filters.text & (Filters.status_update | Filters.forwarded))(update)
|
|
|
|
update.message.forward_date = False
|
|
|
|
assert not (Filters.text & (Filters.forwarded | Filters.status_update))(update)
|
|
|
|
update.message.pinned_message = True
|
|
|
|
assert (Filters.text & (Filters.forwarded | Filters.status_update)(update))
|
2017-08-11 23:58:41 +02:00
|
|
|
|
|
|
|
assert str((Filters.text & (Filters.forwarded | Filters.entity(
|
|
|
|
MessageEntity.MENTION)))) == '<Filters.text and <Filters.forwarded or ' \
|
|
|
|
'Filters.entity(mention)>>'
|
|
|
|
|
2019-02-13 12:07:25 +01:00
|
|
|
def test_inverted_filters(self, update):
|
|
|
|
update.message.text = '/test'
|
|
|
|
assert Filters.command(update)
|
|
|
|
assert not (~Filters.command)(update)
|
|
|
|
update.message.text = 'test'
|
|
|
|
assert not Filters.command(update)
|
|
|
|
assert (~Filters.command)(update)
|
|
|
|
|
|
|
|
def test_inverted_and_filters(self, update):
|
|
|
|
update.message.text = '/test'
|
|
|
|
update.message.forward_date = 1
|
|
|
|
assert (Filters.forwarded & Filters.command)(update)
|
|
|
|
assert not (~Filters.forwarded & Filters.command)(update)
|
|
|
|
assert not (Filters.forwarded & ~Filters.command)(update)
|
|
|
|
assert not (~(Filters.forwarded & Filters.command))(update)
|
|
|
|
update.message.forward_date = None
|
|
|
|
assert not (Filters.forwarded & Filters.command)(update)
|
|
|
|
assert (~Filters.forwarded & Filters.command)(update)
|
|
|
|
assert not (Filters.forwarded & ~Filters.command)(update)
|
|
|
|
assert (~(Filters.forwarded & Filters.command))(update)
|
|
|
|
update.message.text = 'test'
|
|
|
|
assert not (Filters.forwarded & Filters.command)(update)
|
|
|
|
assert not (~Filters.forwarded & Filters.command)(update)
|
|
|
|
assert not (Filters.forwarded & ~Filters.command)(update)
|
|
|
|
assert (~(Filters.forwarded & Filters.command))(update)
|
|
|
|
|
|
|
|
def test_faulty_custom_filter(self, update):
|
2016-10-15 22:59:41 +02:00
|
|
|
class _CustomFilter(BaseFilter):
|
|
|
|
pass
|
|
|
|
|
|
|
|
custom = _CustomFilter()
|
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
with pytest.raises(NotImplementedError):
|
2019-02-13 12:07:25 +01:00
|
|
|
(custom & Filters.text)(update)
|
2017-08-11 23:58:41 +02:00
|
|
|
|
2019-02-13 12:07:25 +01:00
|
|
|
def test_custom_unnamed_filter(self, update):
|
2017-06-19 21:49:42 +02:00
|
|
|
class Unnamed(BaseFilter):
|
2017-08-11 23:58:41 +02:00
|
|
|
def filter(self, mes):
|
2017-06-19 21:49:42 +02:00
|
|
|
return True
|
|
|
|
|
|
|
|
unnamed = Unnamed()
|
2017-08-11 23:58:41 +02:00
|
|
|
assert str(unnamed) == Unnamed.__name__
|
2019-02-13 12:07:25 +01:00
|
|
|
|
|
|
|
def test_update_type_message(self, update):
|
|
|
|
assert Filters.update.message(update)
|
|
|
|
assert not Filters.update.edited_message(update)
|
|
|
|
assert Filters.update.messages(update)
|
|
|
|
assert not Filters.update.channel_post(update)
|
|
|
|
assert not Filters.update.edited_channel_post(update)
|
|
|
|
assert not Filters.update.channel_posts(update)
|
|
|
|
assert Filters.update(update)
|
|
|
|
|
|
|
|
def test_update_type_edited_message(self, update):
|
|
|
|
update.edited_message, update.message = update.message, update.edited_message
|
|
|
|
assert not Filters.update.message(update)
|
|
|
|
assert Filters.update.edited_message(update)
|
|
|
|
assert Filters.update.messages(update)
|
|
|
|
assert not Filters.update.channel_post(update)
|
|
|
|
assert not Filters.update.edited_channel_post(update)
|
|
|
|
assert not Filters.update.channel_posts(update)
|
|
|
|
assert Filters.update(update)
|
|
|
|
|
|
|
|
def test_update_type_channel_post(self, update):
|
|
|
|
update.channel_post, update.message = update.message, update.edited_message
|
|
|
|
assert not Filters.update.message(update)
|
|
|
|
assert not Filters.update.edited_message(update)
|
|
|
|
assert not Filters.update.messages(update)
|
|
|
|
assert Filters.update.channel_post(update)
|
|
|
|
assert not Filters.update.edited_channel_post(update)
|
|
|
|
assert Filters.update.channel_posts(update)
|
|
|
|
assert Filters.update(update)
|
|
|
|
|
|
|
|
def test_update_type_edited_channel_post(self, update):
|
|
|
|
update.edited_channel_post, update.message = update.message, update.edited_message
|
|
|
|
assert not Filters.update.message(update)
|
|
|
|
assert not Filters.update.edited_message(update)
|
|
|
|
assert not Filters.update.messages(update)
|
|
|
|
assert not Filters.update.channel_post(update)
|
|
|
|
assert Filters.update.edited_channel_post(update)
|
|
|
|
assert Filters.update.channel_posts(update)
|
|
|
|
assert Filters.update(update)
|
2019-03-14 09:03:21 +01:00
|
|
|
|
|
|
|
def test_merged_short_circuit_and(self, update):
|
|
|
|
update.message.text = '/test'
|
|
|
|
|
|
|
|
class TestException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class RaisingFilter(BaseFilter):
|
|
|
|
def filter(self, _):
|
|
|
|
raise TestException
|
|
|
|
|
|
|
|
raising_filter = RaisingFilter()
|
|
|
|
|
|
|
|
with pytest.raises(TestException):
|
|
|
|
(Filters.command & raising_filter)(update)
|
|
|
|
|
|
|
|
update.message.text = 'test'
|
|
|
|
(Filters.command & raising_filter)(update)
|
|
|
|
|
|
|
|
def test_merged_short_circuit_or(self, update):
|
|
|
|
update.message.text = 'test'
|
|
|
|
|
|
|
|
class TestException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class RaisingFilter(BaseFilter):
|
|
|
|
def filter(self, _):
|
|
|
|
raise TestException
|
|
|
|
|
|
|
|
raising_filter = RaisingFilter()
|
|
|
|
|
|
|
|
with pytest.raises(TestException):
|
|
|
|
(Filters.command | raising_filter)(update)
|
|
|
|
|
|
|
|
update.message.text = '/test'
|
|
|
|
(Filters.command | raising_filter)(update)
|
|
|
|
|
|
|
|
def test_merged_data_merging_and(self, update):
|
|
|
|
update.message.text = '/test'
|
|
|
|
|
|
|
|
class DataFilter(BaseFilter):
|
|
|
|
data_filter = True
|
|
|
|
|
|
|
|
def __init__(self, data):
|
|
|
|
self.data = data
|
|
|
|
|
|
|
|
def filter(self, _):
|
|
|
|
return {'test': [self.data]}
|
|
|
|
|
|
|
|
result = (Filters.command & DataFilter('blah'))(update)
|
|
|
|
assert result['test'] == ['blah']
|
|
|
|
|
|
|
|
result = (DataFilter('blah1') & DataFilter('blah2'))(update)
|
|
|
|
assert result['test'] == ['blah1', 'blah2']
|
|
|
|
|
|
|
|
update.message.text = 'test'
|
|
|
|
result = (Filters.command & DataFilter('blah'))(update)
|
|
|
|
assert not result
|
|
|
|
|
|
|
|
def test_merged_data_merging_or(self, update):
|
|
|
|
update.message.text = '/test'
|
|
|
|
|
|
|
|
class DataFilter(BaseFilter):
|
|
|
|
data_filter = True
|
|
|
|
|
|
|
|
def __init__(self, data):
|
|
|
|
self.data = data
|
|
|
|
|
|
|
|
def filter(self, _):
|
|
|
|
return {'test': [self.data]}
|
|
|
|
|
|
|
|
result = (Filters.command | DataFilter('blah'))(update)
|
|
|
|
assert result
|
|
|
|
|
|
|
|
result = (DataFilter('blah1') | DataFilter('blah2'))(update)
|
|
|
|
assert result['test'] == ['blah1']
|
|
|
|
|
|
|
|
update.message.text = 'test'
|
|
|
|
result = (Filters.command | DataFilter('blah'))(update)
|
|
|
|
assert result['test'] == ['blah']
|