2021-10-24 21:43:09 +02:00
|
|
|
import logging
|
2021-12-01 01:08:37 +01:00
|
|
|
from datetime import timedelta
|
2021-08-27 23:45:24 +02:00
|
|
|
from uuid import UUID
|
2022-01-03 20:14:31 +01:00
|
|
|
from unittest.mock import MagicMock
|
2021-08-27 23:45:24 +02:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from mobilizon_reshare.event.event import MobilizonEvent
|
2021-10-02 18:09:03 +02:00
|
|
|
from mobilizon_reshare.models.publication import (
|
|
|
|
PublicationStatus,
|
|
|
|
Publication as PublicationModel,
|
|
|
|
)
|
2021-08-27 23:45:24 +02:00
|
|
|
from mobilizon_reshare.models.publisher import Publisher
|
2021-10-16 01:25:45 +02:00
|
|
|
from mobilizon_reshare.publishers.abstract import EventPublication, RecapPublication
|
2021-08-27 23:45:24 +02:00
|
|
|
from mobilizon_reshare.publishers.coordinator import (
|
|
|
|
PublisherCoordinatorReport,
|
2021-10-16 01:25:45 +02:00
|
|
|
EventPublicationReport,
|
2021-08-27 23:45:24 +02:00
|
|
|
PublisherCoordinator,
|
|
|
|
PublicationFailureNotifiersCoordinator,
|
2021-10-16 01:25:45 +02:00
|
|
|
RecapCoordinator,
|
2021-08-27 23:45:24 +02:00
|
|
|
)
|
2022-02-14 21:10:27 +01:00
|
|
|
from mobilizon_reshare.storage.query.event_converter import to_model
|
2021-12-01 01:08:37 +01:00
|
|
|
from tests import today
|
2021-08-27 23:45:24 +02:00
|
|
|
|
|
|
|
|
2021-10-24 21:43:09 +02:00
|
|
|
@pytest.fixture()
|
2022-02-05 18:46:48 +01:00
|
|
|
def failure_report(mock_publisher_invalid, event):
|
2021-10-24 21:43:09 +02:00
|
|
|
return EventPublicationReport(
|
|
|
|
status=PublicationStatus.FAILED,
|
|
|
|
reason="some failure",
|
|
|
|
publication=EventPublication(
|
2022-02-05 18:46:48 +01:00
|
|
|
publisher=mock_publisher_invalid,
|
|
|
|
formatter=None,
|
|
|
|
event=event,
|
|
|
|
id=UUID(int=1),
|
2021-10-24 21:43:09 +02:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-08-27 23:45:24 +02:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"statuses, successful",
|
|
|
|
[
|
|
|
|
[[PublicationStatus.COMPLETED, PublicationStatus.COMPLETED], True],
|
|
|
|
[[PublicationStatus.COMPLETED, PublicationStatus.FAILED], False],
|
|
|
|
[[], True],
|
|
|
|
[[PublicationStatus.COMPLETED], True],
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_publication_report_successful(statuses, successful):
|
2021-10-16 01:25:45 +02:00
|
|
|
reports = []
|
2021-08-27 23:45:24 +02:00
|
|
|
for i, status in enumerate(statuses):
|
2021-10-16 01:25:45 +02:00
|
|
|
reports.append(
|
2021-10-24 21:32:28 +02:00
|
|
|
EventPublicationReport(reason=None, publication=None, status=status)
|
2021-08-27 23:45:24 +02:00
|
|
|
)
|
2021-10-16 01:25:45 +02:00
|
|
|
assert (
|
|
|
|
PublisherCoordinatorReport(publications=[], reports=reports).successful
|
|
|
|
== successful
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def mock_recap_publications(
|
|
|
|
num_publications: int,
|
|
|
|
test_event: MobilizonEvent,
|
|
|
|
mock_publisher_valid,
|
|
|
|
mock_formatter_valid,
|
|
|
|
):
|
|
|
|
result = []
|
|
|
|
for _ in range(num_publications):
|
|
|
|
result.append(
|
|
|
|
RecapPublication(
|
|
|
|
publisher=mock_publisher_valid,
|
|
|
|
formatter=mock_formatter_valid,
|
|
|
|
events=[test_event, test_event],
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return result
|
2021-08-27 23:45:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
@pytest.mark.asyncio
|
2021-10-02 18:09:03 +02:00
|
|
|
async def mock_publications(
|
|
|
|
num_publications: int,
|
2021-09-26 20:12:06 +02:00
|
|
|
test_event: MobilizonEvent,
|
2021-10-02 18:09:03 +02:00
|
|
|
mock_publisher_valid,
|
|
|
|
mock_formatter_valid,
|
2021-09-26 20:12:06 +02:00
|
|
|
):
|
2021-10-02 18:09:03 +02:00
|
|
|
result = []
|
|
|
|
for i in range(num_publications):
|
2022-02-09 00:54:56 +01:00
|
|
|
event = to_model(test_event)
|
2021-10-02 18:09:03 +02:00
|
|
|
await event.save()
|
|
|
|
publisher = Publisher(name="telegram")
|
|
|
|
await publisher.save()
|
|
|
|
publication = PublicationModel(
|
|
|
|
id=UUID(int=i + 1),
|
|
|
|
event=event,
|
|
|
|
publisher=publisher,
|
2021-12-01 01:08:37 +01:00
|
|
|
timestamp=today + timedelta(hours=i),
|
2021-10-02 18:09:03 +02:00
|
|
|
reason=None,
|
|
|
|
)
|
|
|
|
publication = EventPublication.from_orm(publication, test_event)
|
|
|
|
publication.publisher = mock_publisher_valid
|
|
|
|
publication.formatter = mock_formatter_valid
|
|
|
|
result.append(publication)
|
|
|
|
return result
|
2021-08-27 23:45:24 +02:00
|
|
|
|
|
|
|
|
2021-10-02 18:09:03 +02:00
|
|
|
@pytest.mark.parametrize("num_publications", [2])
|
2021-08-27 23:45:24 +02:00
|
|
|
@pytest.mark.asyncio
|
2022-02-14 21:10:27 +01:00
|
|
|
async def test_publication_coordinator_run_success(mock_publications,):
|
|
|
|
coordinator = PublisherCoordinator(publications=mock_publications,)
|
2021-08-27 23:45:24 +02:00
|
|
|
report = coordinator.run()
|
|
|
|
assert len(report.reports) == 2
|
2021-10-16 01:25:45 +02:00
|
|
|
assert report.successful, "\n".join(map(lambda rep: rep.reason, report.reports))
|
2021-08-27 23:45:24 +02:00
|
|
|
|
|
|
|
|
2021-10-02 18:09:03 +02:00
|
|
|
@pytest.mark.parametrize("num_publications", [1])
|
2021-08-27 23:45:24 +02:00
|
|
|
@pytest.mark.asyncio
|
2021-10-16 01:25:45 +02:00
|
|
|
async def test_publication_coordinator_run_failure(
|
2021-10-02 18:09:03 +02:00
|
|
|
mock_publications, mock_publisher_invalid, mock_formatter_invalid
|
2021-08-27 23:45:24 +02:00
|
|
|
):
|
2021-10-02 18:09:03 +02:00
|
|
|
for pub in mock_publications:
|
|
|
|
pub.publisher = mock_publisher_invalid
|
|
|
|
pub.formatter = mock_formatter_invalid
|
|
|
|
coordinator = PublisherCoordinator(mock_publications)
|
2021-08-27 23:45:24 +02:00
|
|
|
|
|
|
|
report = coordinator.run()
|
|
|
|
assert len(report.reports) == 1
|
|
|
|
assert not report.successful
|
2021-11-27 23:31:44 +01:00
|
|
|
assert list(report.reports)[0].reason == "credentials error, Invalid event error"
|
2021-08-27 23:45:24 +02:00
|
|
|
|
|
|
|
|
2021-10-02 18:09:03 +02:00
|
|
|
@pytest.mark.parametrize("num_publications", [1])
|
2021-08-27 23:45:24 +02:00
|
|
|
@pytest.mark.asyncio
|
2021-10-16 01:25:45 +02:00
|
|
|
async def test_publication_coordinator_run_failure_response(
|
2021-10-02 18:09:03 +02:00
|
|
|
mock_publications, mock_publisher_invalid_response
|
2021-08-27 23:45:24 +02:00
|
|
|
):
|
|
|
|
|
2021-10-02 18:09:03 +02:00
|
|
|
for pub in mock_publications:
|
|
|
|
pub.publisher = mock_publisher_invalid_response
|
|
|
|
coordinator = PublisherCoordinator(publications=mock_publications)
|
2021-08-27 23:45:24 +02:00
|
|
|
report = coordinator.run()
|
|
|
|
assert len(report.reports) == 1
|
|
|
|
assert not report.successful
|
2021-10-16 01:25:45 +02:00
|
|
|
assert list(report.reports)[0].reason == "Invalid response"
|
2021-08-27 23:45:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2021-10-24 21:43:09 +02:00
|
|
|
async def test_notifier_coordinator_publication_failed(
|
|
|
|
mock_publisher_valid, failure_report
|
|
|
|
):
|
2021-08-27 23:45:24 +02:00
|
|
|
mock_send = MagicMock()
|
|
|
|
mock_publisher_valid._send = mock_send
|
2021-10-02 18:09:03 +02:00
|
|
|
coordinator = PublicationFailureNotifiersCoordinator(
|
2021-10-24 21:43:09 +02:00
|
|
|
failure_report, [mock_publisher_valid, mock_publisher_valid]
|
2021-08-27 23:45:24 +02:00
|
|
|
)
|
2021-10-02 18:09:03 +02:00
|
|
|
coordinator.notify_failure()
|
2021-08-27 23:45:24 +02:00
|
|
|
|
|
|
|
# 4 = 2 reports * 2 notifiers
|
2021-10-02 18:09:03 +02:00
|
|
|
assert mock_send.call_count == 2
|
2021-10-16 01:25:45 +02:00
|
|
|
|
|
|
|
|
2021-10-24 21:43:09 +02:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_notifier_coordinator_error(
|
|
|
|
failure_report, mock_publisher_invalid_response, caplog
|
|
|
|
):
|
|
|
|
mock_send = MagicMock()
|
|
|
|
mock_publisher_invalid_response._send = mock_send
|
|
|
|
|
|
|
|
coordinator = PublicationFailureNotifiersCoordinator(
|
|
|
|
failure_report,
|
|
|
|
[mock_publisher_invalid_response, mock_publisher_invalid_response],
|
|
|
|
)
|
|
|
|
with caplog.at_level(logging.CRITICAL):
|
|
|
|
coordinator.notify_failure()
|
|
|
|
assert "Notifier failed to send" in caplog.text
|
|
|
|
assert failure_report.get_failure_message() in caplog.text
|
|
|
|
# 4 = 2 reports * 2 notifiers
|
|
|
|
assert mock_send.call_count == 2
|
|
|
|
|
|
|
|
|
2021-10-16 01:25:45 +02:00
|
|
|
@pytest.mark.parametrize("num_publications", [2])
|
|
|
|
@pytest.mark.asyncio
|
2021-10-17 14:09:24 +02:00
|
|
|
async def test_recap_coordinator_run_success(
|
|
|
|
mock_recap_publications, message_collector
|
|
|
|
):
|
2021-10-16 01:25:45 +02:00
|
|
|
coordinator = RecapCoordinator(recap_publications=mock_recap_publications)
|
|
|
|
report = coordinator.run()
|
2021-10-17 14:09:24 +02:00
|
|
|
|
|
|
|
# one recap per publication
|
|
|
|
assert len(message_collector) == 2
|
|
|
|
|
|
|
|
# check that header is in all messages
|
|
|
|
assert all(("Upcoming" in message) for message in message_collector)
|
|
|
|
|
2021-10-16 01:25:45 +02:00
|
|
|
assert len(report.reports) == 2
|
|
|
|
assert report.successful, "\n".join(map(lambda rep: rep.reason, report.reports))
|