From bc212e78011b4d7cc4bee1f4193581f09b1bf824 Mon Sep 17 00:00:00 2001 From: Simone Robutti Date: Sun, 3 Oct 2021 13:19:37 +0200 Subject: [PATCH] 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 --- mobilizon_reshare/.secrets.toml | 33 +++++++++++++++++++ .../publishers/platforms/telegram.py | 5 +-- .../publishers/platforms/zulip.py | 7 ++-- tests/publishers/test_telegram.py | 13 ++++++++ tests/publishers/test_zulip.py | 15 ++++++++- 5 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 mobilizon_reshare/.secrets.toml create mode 100644 tests/publishers/test_telegram.py diff --git a/mobilizon_reshare/.secrets.toml b/mobilizon_reshare/.secrets.toml new file mode 100644 index 0000000..d8581ca --- /dev/null +++ b/mobilizon_reshare/.secrets.toml @@ -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 diff --git a/mobilizon_reshare/publishers/platforms/telegram.py b/mobilizon_reshare/publishers/platforms/telegram.py index 2cfb979..fd91440 100644 --- a/mobilizon_reshare/publishers/platforms/telegram.py +++ b/mobilizon_reshare/publishers/platforms/telegram.py @@ -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) diff --git a/mobilizon_reshare/publishers/platforms/zulip.py b/mobilizon_reshare/publishers/platforms/zulip.py index 81ddd36..8ac1bcc 100644 --- a/mobilizon_reshare/publishers/platforms/zulip.py +++ b/mobilizon_reshare/publishers/platforms/zulip.py @@ -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: diff --git a/tests/publishers/test_telegram.py b/tests/publishers/test_telegram.py new file mode 100644 index 0000000..c59f23c --- /dev/null +++ b/tests/publishers/test_telegram.py @@ -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) diff --git a/tests/publishers/test_zulip.py b/tests/publishers/test_zulip.py index 71c56d9..f3e2c88 100644 --- a/tests/publishers/test_zulip.py +++ b/tests/publishers/test_zulip.py @@ -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)