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-11-27 23:31:44 +01:00
|
|
|
assert TelegramFormatter().validate_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):
|
2021-11-27 23:31:44 +01:00
|
|
|
TelegramFormatter().validate_event(event)
|
2021-10-05 15:32:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
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")
|