2021-12-05 16:43:23 +01:00
|
|
|
import uuid
|
|
|
|
from logging import DEBUG, INFO
|
2021-11-11 15:18:04 +01:00
|
|
|
|
2021-12-05 16:43:23 +01:00
|
|
|
import arrow
|
2021-11-11 15:18:04 +01:00
|
|
|
import pytest
|
|
|
|
|
2021-12-05 16:43:23 +01:00
|
|
|
from mobilizon_reshare.storage.query.read import get_all_events
|
2021-11-11 16:20:50 +01:00
|
|
|
from tests.commands.conftest import simple_event_element
|
2021-11-11 15:18:04 +01:00
|
|
|
from mobilizon_reshare.event.event import MobilizonEvent, EventPublicationStatus
|
|
|
|
from mobilizon_reshare.main.start import start
|
|
|
|
from mobilizon_reshare.models.event import Event
|
|
|
|
from mobilizon_reshare.models.publication import PublicationStatus
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
@pytest.mark.parametrize(
|
2022-02-05 18:46:48 +01:00
|
|
|
"elements",
|
|
|
|
[[]],
|
2021-11-11 15:18:04 +01:00
|
|
|
)
|
2021-11-11 16:20:50 +01:00
|
|
|
async def test_start_no_event(
|
|
|
|
mock_mobilizon_success_answer, mobilizon_answer, caplog, elements
|
|
|
|
):
|
2021-11-11 15:18:04 +01:00
|
|
|
with caplog.at_level(DEBUG):
|
|
|
|
assert await start() is None
|
|
|
|
assert "No event to publish found" in caplog.text
|
|
|
|
|
|
|
|
|
2021-11-11 16:20:50 +01:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"publisher_class", [pytest.lazy_fixture("mock_publisher_class")]
|
|
|
|
)
|
2021-11-11 15:18:04 +01:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
@pytest.mark.parametrize(
|
2021-11-11 16:20:50 +01:00
|
|
|
"elements",
|
|
|
|
[[simple_event_element()], [simple_event_element(), simple_event_element()]],
|
2021-11-11 15:18:04 +01:00
|
|
|
)
|
|
|
|
async def test_start_new_event(
|
|
|
|
mock_mobilizon_success_answer,
|
|
|
|
mobilizon_answer,
|
|
|
|
caplog,
|
|
|
|
mock_publisher_config,
|
|
|
|
message_collector,
|
2021-12-05 16:43:23 +01:00
|
|
|
elements,
|
2021-11-11 15:18:04 +01:00
|
|
|
):
|
|
|
|
with caplog.at_level(DEBUG):
|
2021-11-11 16:20:50 +01:00
|
|
|
# calling the start command
|
2021-11-11 15:18:04 +01:00
|
|
|
assert await start() is None
|
|
|
|
|
2021-11-11 16:20:50 +01:00
|
|
|
# since the mobilizon_answer contains at least one result, one event to publish must be found and published
|
|
|
|
# by the publisher coordinator
|
2021-11-11 15:18:04 +01:00
|
|
|
assert "Event to publish found" in caplog.text
|
2021-11-11 16:20:50 +01:00
|
|
|
assert message_collector == [
|
|
|
|
"test event|Some description",
|
|
|
|
]
|
2021-11-11 15:18:04 +01:00
|
|
|
|
|
|
|
all_events = (
|
|
|
|
await Event.all()
|
|
|
|
.prefetch_related("publications")
|
|
|
|
.prefetch_related("publications__publisher")
|
|
|
|
)
|
|
|
|
|
2021-11-11 16:20:50 +01:00
|
|
|
# the start command should save all the events in the database
|
2021-12-05 16:43:23 +01:00
|
|
|
assert len(all_events) == len(elements), all_events
|
2021-11-11 15:18:04 +01:00
|
|
|
|
2021-11-11 16:20:50 +01:00
|
|
|
# it should create a publication for each publisher
|
2021-11-11 15:18:04 +01:00
|
|
|
publications = all_events[0].publications
|
2021-11-24 23:58:06 +01:00
|
|
|
assert len(publications) == 1, publications
|
2021-11-11 16:20:50 +01:00
|
|
|
|
|
|
|
# all the other events should have no publication
|
|
|
|
for e in all_events[1:]:
|
|
|
|
assert len(e.publications) == 0, e.publications
|
2021-11-11 15:18:04 +01:00
|
|
|
|
2021-11-11 16:20:50 +01:00
|
|
|
# all the publications for the first event should be saved as COMPLETED
|
|
|
|
for p in publications[1:]:
|
|
|
|
assert p.status == PublicationStatus.COMPLETED
|
|
|
|
|
|
|
|
# the derived status for the event should be COMPLETED
|
2021-11-11 15:18:04 +01:00
|
|
|
assert (
|
|
|
|
MobilizonEvent.from_model(all_events[0]).status
|
|
|
|
== EventPublicationStatus.COMPLETED
|
|
|
|
)
|
2021-11-11 16:20:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"publisher_class", [pytest.lazy_fixture("mock_publisher_class")]
|
|
|
|
)
|
|
|
|
@pytest.mark.parametrize(
|
2022-02-05 18:46:48 +01:00
|
|
|
"elements",
|
|
|
|
[[]],
|
2021-11-11 16:20:50 +01:00
|
|
|
)
|
|
|
|
async def test_start_event_from_db(
|
|
|
|
mock_mobilizon_success_answer,
|
|
|
|
mobilizon_answer,
|
|
|
|
caplog,
|
|
|
|
mock_publisher_config,
|
|
|
|
message_collector,
|
|
|
|
event_generator,
|
|
|
|
):
|
|
|
|
event = event_generator()
|
|
|
|
event_model = event.to_model()
|
|
|
|
await event_model.save()
|
|
|
|
|
|
|
|
with caplog.at_level(DEBUG):
|
|
|
|
# calling the start command
|
|
|
|
assert await start() is None
|
|
|
|
|
|
|
|
# since the db contains at least one event, this has to be picked and published
|
|
|
|
assert "Event to publish found" in caplog.text
|
|
|
|
assert message_collector == [
|
|
|
|
"test event|description of the event",
|
|
|
|
]
|
|
|
|
|
|
|
|
await event_model.fetch_related("publications", "publications__publisher")
|
|
|
|
# it should create a publication for each publisher
|
|
|
|
publications = event_model.publications
|
2021-11-24 23:58:06 +01:00
|
|
|
assert len(publications) == 1, publications
|
2021-11-11 16:20:50 +01:00
|
|
|
|
|
|
|
# all the publications for the first event should be saved as COMPLETED
|
2021-11-24 23:58:06 +01:00
|
|
|
for p in publications:
|
2021-11-11 16:20:50 +01:00
|
|
|
assert p.status == PublicationStatus.COMPLETED
|
|
|
|
|
|
|
|
# the derived status for the event should be COMPLETED
|
|
|
|
assert (
|
|
|
|
MobilizonEvent.from_model(event_model).status
|
|
|
|
== EventPublicationStatus.COMPLETED
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"publisher_class", [pytest.lazy_fixture("mock_publisher_invalid_class")]
|
|
|
|
)
|
|
|
|
@pytest.mark.parametrize(
|
2022-02-05 18:46:48 +01:00
|
|
|
"elements",
|
|
|
|
[[]],
|
2021-11-11 16:20:50 +01:00
|
|
|
)
|
|
|
|
async def test_start_publisher_failure(
|
|
|
|
mock_mobilizon_success_answer,
|
|
|
|
mobilizon_answer,
|
|
|
|
caplog,
|
|
|
|
mock_publisher_config,
|
|
|
|
message_collector,
|
|
|
|
event_generator,
|
|
|
|
mock_notifier_config,
|
|
|
|
):
|
|
|
|
event = event_generator()
|
|
|
|
event_model = event.to_model()
|
|
|
|
await event_model.save()
|
|
|
|
|
|
|
|
with caplog.at_level(DEBUG):
|
|
|
|
# calling the start command
|
|
|
|
assert await start() is None
|
|
|
|
|
|
|
|
# since the db contains at least one event, this has to be picked and published
|
|
|
|
|
|
|
|
await event_model.fetch_related("publications", "publications__publisher")
|
|
|
|
# it should create a publication for each publisher
|
|
|
|
publications = event_model.publications
|
2021-11-24 23:58:06 +01:00
|
|
|
assert len(publications) == 1, publications
|
2021-11-11 16:20:50 +01:00
|
|
|
|
|
|
|
# all the publications for event should be saved as FAILED
|
|
|
|
for p in publications:
|
|
|
|
assert p.status == PublicationStatus.FAILED
|
|
|
|
assert p.reason == "credentials error"
|
|
|
|
|
|
|
|
assert "Event to publish found" in caplog.text
|
|
|
|
assert message_collector == [
|
2021-11-24 23:58:06 +01:00
|
|
|
f"Publication {p.id} failed with status: 0."
|
2022-02-05 18:46:48 +01:00
|
|
|
f"\nReason: credentials error\nPublisher: mock\nEvent: test event"
|
2021-11-11 16:20:50 +01:00
|
|
|
for p in publications
|
|
|
|
for _ in range(2)
|
|
|
|
] # 2 publications failed * 2 notifiers
|
|
|
|
# the derived status for the event should be FAILED
|
|
|
|
assert (
|
|
|
|
MobilizonEvent.from_model(event_model).status
|
|
|
|
== EventPublicationStatus.FAILED
|
|
|
|
)
|
2021-12-05 16:43:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
async def published_event(event_generator):
|
|
|
|
|
|
|
|
event = event_generator()
|
|
|
|
event_model = event.to_model()
|
|
|
|
await event_model.save()
|
|
|
|
assert await start() is None
|
|
|
|
await event_model.refresh_from_db()
|
|
|
|
await event_model.fetch_related("publications")
|
|
|
|
for pub in event_model.publications:
|
|
|
|
pub.timestamp = arrow.now().shift(days=-2).datetime
|
|
|
|
await pub.save()
|
|
|
|
return event_model
|
|
|
|
|
|
|
|
|
|
|
|
def second_event_element():
|
|
|
|
return {
|
|
|
|
"beginsOn": "2021-05-23T12:15:00Z",
|
|
|
|
"description": "description of the second event",
|
|
|
|
"endsOn": "2021-05-23T15:15:00Z",
|
|
|
|
"onlineAddress": None,
|
|
|
|
"options": {"showEndTime": True, "showStartTime": True},
|
|
|
|
"physicalAddress": None,
|
|
|
|
"picture": None,
|
|
|
|
"title": "test event",
|
|
|
|
"url": "https://some_mobilizon/events/1e2e5943-4a5c-497a-b65d-90457b715d7b",
|
|
|
|
"uuid": str(uuid.uuid4()),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"publisher_class", [pytest.lazy_fixture("mock_publisher_class")]
|
|
|
|
)
|
|
|
|
@pytest.mark.parametrize(
|
2022-02-05 18:46:48 +01:00
|
|
|
"elements",
|
|
|
|
[[second_event_element()]],
|
2021-12-05 16:43:23 +01:00
|
|
|
)
|
|
|
|
async def test_start_second_execution(
|
|
|
|
mock_mobilizon_success_answer,
|
|
|
|
mobilizon_answer,
|
|
|
|
caplog,
|
|
|
|
mock_publisher_config,
|
|
|
|
message_collector,
|
|
|
|
event_generator,
|
|
|
|
published_event,
|
|
|
|
):
|
|
|
|
# the fixture published_event provides an existing event in the db
|
|
|
|
|
|
|
|
# I clean the message collector
|
|
|
|
message_collector.data = []
|
|
|
|
|
|
|
|
with caplog.at_level(INFO):
|
|
|
|
# calling the start command
|
|
|
|
assert await start() is None
|
|
|
|
|
|
|
|
# verify that the second event gets published
|
|
|
|
assert "Event to publish found" in caplog.text
|
|
|
|
assert message_collector == [
|
|
|
|
"test event|description of the second event",
|
|
|
|
]
|
|
|
|
# I verify that the db event and the new event coming from mobilizon are both in the db
|
|
|
|
assert len(list(await get_all_events())) == 2
|