2021-10-05 15:32:07 +02:00
|
|
|
import pytest
|
|
|
|
import requests
|
|
|
|
|
2021-10-24 21:32:28 +02:00
|
|
|
from mobilizon_reshare.publishers.exceptions import (
|
|
|
|
InvalidEvent,
|
|
|
|
InvalidResponse,
|
|
|
|
InvalidMessage,
|
|
|
|
)
|
2021-10-05 15:32:07 +02:00
|
|
|
from mobilizon_reshare.publishers.platforms.telegram import (
|
|
|
|
TelegramFormatter,
|
|
|
|
TelegramPublisher,
|
|
|
|
)
|
2021-10-03 13:19:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_message_length_success(event):
|
|
|
|
message = "a" * 500
|
|
|
|
event.description = message
|
2021-10-24 21:32:28 +02:00
|
|
|
assert (
|
|
|
|
TelegramFormatter().validate_message(
|
|
|
|
TelegramFormatter().get_message_from_event(event)
|
|
|
|
)
|
|
|
|
is None
|
|
|
|
)
|
2021-10-03 13:19:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_message_length_failure(event):
|
|
|
|
message = "a" * 10000
|
|
|
|
event.description = message
|
2021-10-24 21:32:28 +02:00
|
|
|
|
|
|
|
with pytest.raises(InvalidMessage):
|
|
|
|
TelegramFormatter().validate_message(
|
|
|
|
TelegramFormatter().get_message_from_event(event)
|
|
|
|
)
|
2021-10-05 15:32:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"message, result",
|
|
|
|
[
|
|
|
|
["", ""],
|
|
|
|
["a#b", "ab"],
|
|
|
|
["-", "\\-"],
|
|
|
|
["(", "\\("],
|
|
|
|
["!", "\\!"],
|
|
|
|
[")", "\\)"],
|
|
|
|
[")!", "\\)\\!"],
|
2021-10-19 07:35:18 +02:00
|
|
|
["[link](https://link.com)", "[link](https://link.com)"],
|
|
|
|
[
|
|
|
|
"[link](https://link.com) [link2](https://link.com)",
|
|
|
|
"[link](https://link.com) [link2](https://link.com)",
|
|
|
|
],
|
2021-10-05 15:32:07 +02:00
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_escape_message(message, result):
|
|
|
|
assert TelegramFormatter().escape_message(message) == result
|
|
|
|
|
|
|
|
|
|
|
|
def test_event_validation(event):
|
|
|
|
event.description = None
|
|
|
|
with pytest.raises(InvalidEvent):
|
|
|
|
TelegramFormatter().validate_event(event)
|
|
|
|
|
|
|
|
|
|
|
|
def test_validate_response():
|
|
|
|
response = requests.Response()
|
|
|
|
response.status_code = 200
|
|
|
|
response._content = b"""{"ok":true}"""
|
|
|
|
TelegramPublisher()._validate_response(response)
|
|
|
|
|
|
|
|
|
|
|
|
def test_validate_response_invalid_json():
|
|
|
|
response = requests.Response()
|
|
|
|
response.status_code = 200
|
|
|
|
response._content = b"""{"osxsa"""
|
|
|
|
with pytest.raises(InvalidResponse) as e:
|
|
|
|
TelegramPublisher()._validate_response(response)
|
|
|
|
|
|
|
|
e.match("json")
|
|
|
|
|
|
|
|
|
|
|
|
def test_validate_response_invalid_request():
|
|
|
|
response = requests.Response()
|
|
|
|
response.status_code = 400
|
|
|
|
response._content = b"""{"error":true}"""
|
2021-10-24 21:32:28 +02:00
|
|
|
with pytest.raises(InvalidResponse) as e:
|
2021-10-05 15:32:07 +02:00
|
|
|
|
|
|
|
TelegramPublisher()._validate_response(response)
|
|
|
|
|
2021-10-20 00:08:58 +02:00
|
|
|
e.match("400 Client Error")
|
2021-10-05 15:32:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_validate_response_invalid_response():
|
|
|
|
response = requests.Response()
|
|
|
|
response.status_code = 200
|
|
|
|
response._content = b"""{"error":true}"""
|
|
|
|
with pytest.raises(InvalidResponse) as e:
|
|
|
|
|
|
|
|
TelegramPublisher()._validate_response(response)
|
|
|
|
|
|
|
|
e.match("Invalid request")
|