Check length (#66)

* decoupled notifiers from event

* stub

* publishers working

* fixed format CLI

* fixed unit tests

* renamed abstractnotifier

* added another excluded character

* restored bundled secrets file
This commit is contained in:
Simone Robutti 2021-10-03 13:19:37 +02:00 committed by GitHub
parent b6b2402767
commit bc212e7801
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 66 additions and 7 deletions

View File

@ -0,0 +1,33 @@
[default.publisher.telegram]
active=true
chat_id="xxx"
token="xxx"
username="xxx"
[default.publisher.facebook]
active=false
[default.publisher.zulip]
active=true
chat_id="xxx"
subject="xxx"
bot_token="xxx"
bot_email="xxx"
[default.publisher.twitter]
active=false
[default.publisher.mastodon]
active=false
[default.notifier.telegram]
active=true
chat_id="xxx"
token="xxx"
username="xxx"
[default.notifier.zulip]
active=true
chat_id="xxx"
subject="xxx"
bot_token="xxx"
bot_email="xxx"
[default.notifier.twitter]
active=false
[default.notifier.mastodon]
active=false

View File

@ -13,6 +13,7 @@ from mobilizon_reshare.publishers.exceptions import (
InvalidCredentials,
InvalidEvent,
InvalidResponse,
PublisherError,
)
@ -44,8 +45,8 @@ class TelegramFormatter(AbstractEventFormatter):
return super(TelegramFormatter, self).get_message_from_event(event)
def validate_message(self, message: str) -> None:
# TODO implement
pass
if len(message) >= 4096:
raise PublisherError("Message is too long")
def _preprocess_event(self, event: MobilizonEvent):
event.description = html_to_markdown(event.description)

View File

@ -15,6 +15,7 @@ from mobilizon_reshare.publishers.exceptions import (
InvalidEvent,
InvalidResponse,
ZulipError,
PublisherError,
)
@ -31,7 +32,8 @@ class ZulipFormatter(AbstractEventFormatter):
self._log_error("No text was found", raise_error=InvalidEvent)
def validate_message(self, message) -> None:
pass
if len(message.encode("utf-8")) >= 10000:
raise PublisherError("Message is too long")
def _preprocess_event(self, event: MobilizonEvent):
event.description = html_to_markdown(event.description)
@ -111,9 +113,6 @@ class ZulipPlatform(AbstractPlatform):
raise_error=InvalidBot,
)
def validate_message(self) -> None:
pass
def _validate_response(self, res) -> dict:
# See https://zulip.com/api/rest-error-handling
try:

View File

@ -0,0 +1,13 @@
from mobilizon_reshare.publishers.platforms.telegram import TelegramFormatter
def test_message_length_success(event):
message = "a" * 500
event.description = message
assert TelegramFormatter().is_message_valid(event)
def test_message_length_failure(event):
message = "a" * 10000
event.description = message
assert not TelegramFormatter().is_message_valid(event)

View File

@ -8,6 +8,7 @@ from mobilizon_reshare.models.publication import PublicationStatus
from mobilizon_reshare.publishers import get_active_publishers
from mobilizon_reshare.publishers.abstract import EventPublication
from mobilizon_reshare.publishers.coordinator import PublisherCoordinator
from mobilizon_reshare.publishers.platforms.zulip import ZulipFormatter
from mobilizon_reshare.storage.query import (
get_publishers,
update_publishers,
@ -136,7 +137,7 @@ async def test_zulip_publishr_failure_invalid_credentials(
@pytest.mark.asyncio
async def test_zulip_publishr_failure_client_error(
async def test_zulip_publisher_failure_client_error(
mocked_client_error_response, setup_db, event
):
publication_models = await publications_with_status(
@ -152,3 +153,15 @@ async def test_zulip_publishr_failure_client_error(
).run()
assert list(report.reports.values())[0].status == PublicationStatus.FAILED
assert list(report.reports.values())[0].reason == "400 Error - Invalid request"
def test_message_length_success(event):
message = "a" * 500
event.description = message
assert ZulipFormatter().is_message_valid(event)
def test_message_length_failure(event):
message = "a" * 10000
event.description = message
assert not ZulipFormatter().is_message_valid(event)