2022-05-17 23:14:52 +02:00
|
|
|
from logging import DEBUG
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2022-12-11 14:15:04 +01:00
|
|
|
from mobilizon_reshare.dataclasses import EventPublicationStatus
|
|
|
|
from mobilizon_reshare.dataclasses import MobilizonEvent
|
2022-05-17 23:14:52 +02:00
|
|
|
from mobilizon_reshare.main.publish import select_and_publish, publish_event
|
|
|
|
from mobilizon_reshare.models.event import Event
|
|
|
|
from mobilizon_reshare.models.publication import PublicationStatus
|
|
|
|
from mobilizon_reshare.storage.query.read import get_all_publications
|
|
|
|
from tests.conftest import event_0, event_1
|
|
|
|
|
|
|
|
one_unpublished_event_specification = {
|
|
|
|
"event": 1,
|
|
|
|
"publisher": ["telegram", "twitter", "mastodon", "zulip"],
|
|
|
|
}
|
|
|
|
three_event_specification = {
|
|
|
|
"event": 3,
|
|
|
|
"publications": [
|
|
|
|
{"event_idx": 0, "publisher_idx": 0, "status": PublicationStatus.COMPLETED}
|
|
|
|
],
|
|
|
|
"publisher": ["telegram", "twitter", "mastodon", "zulip"],
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2022-07-12 07:39:56 +02:00
|
|
|
async def test_publish_no_event(caplog, command_config):
|
2022-05-17 23:14:52 +02:00
|
|
|
with caplog.at_level(DEBUG):
|
2022-07-12 07:39:56 +02:00
|
|
|
assert await select_and_publish(command_config) is None
|
2022-05-17 23:14:52 +02:00
|
|
|
assert "No event to publish found" in caplog.text
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"publisher_class", [pytest.lazy_fixture("mock_publisher_class")]
|
|
|
|
)
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"specification,expected_event",
|
|
|
|
[
|
|
|
|
[one_unpublished_event_specification, event_0],
|
|
|
|
[three_event_specification, event_1],
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_select_and_publish_new_event(
|
|
|
|
generate_models,
|
|
|
|
caplog,
|
|
|
|
mock_publisher_config,
|
|
|
|
message_collector,
|
|
|
|
specification,
|
|
|
|
expected_event,
|
2022-07-12 07:39:56 +02:00
|
|
|
command_config,
|
2022-05-17 23:14:52 +02:00
|
|
|
):
|
|
|
|
await generate_models(specification)
|
|
|
|
with caplog.at_level(DEBUG):
|
|
|
|
# calling the publish command without arguments
|
2022-07-12 07:39:56 +02:00
|
|
|
assert await select_and_publish(command_config) is not None
|
2022-05-17 23:14:52 +02:00
|
|
|
|
|
|
|
assert "Event to publish found" in caplog.text
|
|
|
|
assert message_collector == [
|
|
|
|
f"{expected_event.name}|{expected_event.description}",
|
|
|
|
]
|
|
|
|
|
|
|
|
event = (
|
|
|
|
await Event.filter(mobilizon_id=expected_event.mobilizon_id)
|
|
|
|
.prefetch_related("publications")
|
|
|
|
.prefetch_related("publications__publisher")
|
|
|
|
)[0]
|
|
|
|
|
|
|
|
# it should create a publication for each publisher
|
|
|
|
publications = event.publications
|
|
|
|
assert len(publications) == 1, publications
|
|
|
|
|
|
|
|
# all the publications for the first event should be saved as COMPLETED
|
|
|
|
for p in publications:
|
|
|
|
assert p.status == PublicationStatus.COMPLETED
|
|
|
|
|
|
|
|
# the derived status for the event should be COMPLETED
|
2022-12-07 21:46:57 +01:00
|
|
|
assert (
|
|
|
|
MobilizonEvent.from_model(event).status == EventPublicationStatus.COMPLETED
|
|
|
|
)
|
2022-05-17 23:14:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"publisher_class,publishers,expected",
|
|
|
|
[
|
|
|
|
[pytest.lazy_fixture("mock_publisher_class"), None, {"mock"}],
|
|
|
|
[pytest.lazy_fixture("mock_publisher_class"), [], {"mock"}],
|
|
|
|
[pytest.lazy_fixture("mock_publisher_class"), [None, None], {"mock"}],
|
|
|
|
[pytest.lazy_fixture("mock_zulip_publisher_class"), ["zulip"], {"zulip"}],
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_publish_event(
|
|
|
|
generate_models,
|
|
|
|
caplog,
|
|
|
|
mock_publisher_config,
|
|
|
|
message_collector,
|
|
|
|
publishers,
|
|
|
|
expected,
|
2022-07-12 07:39:56 +02:00
|
|
|
command_config,
|
2022-05-17 23:14:52 +02:00
|
|
|
):
|
|
|
|
await generate_models(one_unpublished_event_specification)
|
|
|
|
with caplog.at_level(DEBUG):
|
|
|
|
# calling mobilizon-reshare publish -E <UUID> -p <platform>
|
2022-07-12 07:39:56 +02:00
|
|
|
report = await publish_event(event_0, command_config, publishers)
|
2022-05-17 23:14:52 +02:00
|
|
|
assert report is not None
|
|
|
|
assert report.successful
|
|
|
|
|
|
|
|
# We test whether we published only to the expected platforms
|
|
|
|
assert {pub.publication.publisher.name for pub in report.reports} == expected
|
|
|
|
publications = list(await get_all_publications())
|
|
|
|
assert len(publications) == len(expected)
|
|
|
|
assert all(p.status == PublicationStatus.COMPLETED for p in publications)
|
|
|
|
assert {p.publisher.name for p in publications} == expected
|