2021-09-27 09:20:01 +02:00
|
|
|
from datetime import timedelta
|
2021-11-20 15:40:10 +01:00
|
|
|
from typing import Optional
|
2021-08-29 14:51:52 +02:00
|
|
|
from uuid import UUID
|
2021-08-27 23:45:24 +02:00
|
|
|
|
2021-09-27 09:20:01 +02:00
|
|
|
import arrow
|
2021-08-27 23:45:24 +02:00
|
|
|
import pytest
|
|
|
|
|
2021-11-24 23:58:06 +01:00
|
|
|
import mobilizon_reshare.storage.query.read
|
2021-08-27 23:45:24 +02:00
|
|
|
from mobilizon_reshare.event.event import MobilizonEvent
|
2021-11-24 23:58:06 +01:00
|
|
|
from mobilizon_reshare.models.publisher import Publisher
|
2021-10-02 18:09:03 +02:00
|
|
|
from mobilizon_reshare.publishers.abstract import (
|
|
|
|
AbstractPlatform,
|
|
|
|
AbstractEventFormatter,
|
|
|
|
)
|
2021-08-27 23:45:24 +02:00
|
|
|
from mobilizon_reshare.publishers.exceptions import PublisherError, InvalidResponse
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def test_event():
|
2021-09-27 09:20:01 +02:00
|
|
|
now = arrow.now()
|
2021-08-27 23:45:24 +02:00
|
|
|
return MobilizonEvent(
|
|
|
|
**{
|
|
|
|
"name": "TestName",
|
|
|
|
"description": "TestDescr",
|
|
|
|
"begin_datetime": now,
|
|
|
|
"end_datetime": now + timedelta(hours=1),
|
|
|
|
"mobilizon_link": "",
|
2021-08-29 14:51:52 +02:00
|
|
|
"mobilizon_id": UUID(int=0),
|
2021-08-27 23:45:24 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2021-10-02 18:09:03 +02:00
|
|
|
def mock_formatter_invalid():
|
|
|
|
class MockFormatter(AbstractEventFormatter):
|
|
|
|
def validate_event(self, event) -> None:
|
2021-10-24 21:32:28 +02:00
|
|
|
raise PublisherError("Invalid event error")
|
2021-08-27 23:45:24 +02:00
|
|
|
|
2021-10-02 18:09:03 +02:00
|
|
|
def get_message_from_event(self, event) -> str:
|
2021-08-27 23:45:24 +02:00
|
|
|
return ""
|
|
|
|
|
2021-10-02 18:09:03 +02:00
|
|
|
def validate_message(self, event) -> None:
|
2021-10-24 21:32:28 +02:00
|
|
|
raise PublisherError("Invalid message error")
|
2021-08-27 23:45:24 +02:00
|
|
|
|
2021-10-02 18:09:03 +02:00
|
|
|
return MockFormatter()
|
|
|
|
|
|
|
|
|
2021-08-27 23:45:24 +02:00
|
|
|
@pytest.fixture
|
2021-10-17 14:09:24 +02:00
|
|
|
def mock_publisher_invalid(message_collector):
|
2021-10-02 18:09:03 +02:00
|
|
|
class MockPublisher(AbstractPlatform):
|
2021-10-17 14:09:24 +02:00
|
|
|
|
2021-10-24 21:32:28 +02:00
|
|
|
name = "mock"
|
|
|
|
|
2021-11-20 15:40:10 +01:00
|
|
|
def _send(self, message: str, event: Optional[MobilizonEvent] = None):
|
2021-10-24 21:32:28 +02:00
|
|
|
message_collector.append(message)
|
2021-08-27 23:45:24 +02:00
|
|
|
|
2021-10-02 18:09:03 +02:00
|
|
|
def _validate_response(self, response):
|
2021-10-24 21:32:28 +02:00
|
|
|
return InvalidResponse("response error")
|
2021-10-02 18:09:03 +02:00
|
|
|
|
2021-08-27 23:45:24 +02:00
|
|
|
def validate_credentials(self) -> None:
|
2021-10-24 21:32:28 +02:00
|
|
|
raise PublisherError("credentials error")
|
2021-10-02 18:09:03 +02:00
|
|
|
|
|
|
|
return MockPublisher()
|
2021-08-27 23:45:24 +02:00
|
|
|
|
2021-10-02 18:09:03 +02:00
|
|
|
|
|
|
|
@pytest.fixture
|
2021-10-17 14:09:24 +02:00
|
|
|
def mock_publisher_invalid_response(message_collector):
|
2021-10-02 18:09:03 +02:00
|
|
|
class MockPublisher(AbstractPlatform):
|
2021-10-24 21:32:28 +02:00
|
|
|
|
|
|
|
name = "mock"
|
|
|
|
|
2021-11-20 15:40:10 +01:00
|
|
|
def _send(self, message, event):
|
2021-10-24 21:32:28 +02:00
|
|
|
message_collector.append(message)
|
2021-08-27 23:45:24 +02:00
|
|
|
|
2021-10-02 18:09:03 +02:00
|
|
|
def _validate_response(self, response):
|
2021-08-27 23:45:24 +02:00
|
|
|
raise InvalidResponse("Invalid response")
|
|
|
|
|
2021-10-02 18:09:03 +02:00
|
|
|
def validate_credentials(self) -> None:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return MockPublisher()
|
2021-11-24 23:58:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
async def mock_active_publishers_config(monkeypatch):
|
|
|
|
p = Publisher(name="zulip")
|
|
|
|
await p.save()
|
|
|
|
|
|
|
|
def _mock_active_pub():
|
|
|
|
return ["zulip"]
|
|
|
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
mobilizon_reshare.storage.query.read,
|
|
|
|
"get_active_publishers",
|
|
|
|
_mock_active_pub
|
|
|
|
)
|
|
|
|
|
|
|
|
return p
|