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
|
|
|
|
|
2022-12-11 14:15:04 +01:00
|
|
|
from mobilizon_reshare.dataclasses import MobilizonEvent
|
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),
|
2022-02-09 00:54:56 +01:00
|
|
|
"last_update_time": now,
|
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()
|
2023-05-22 13:00:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def mock_zulip_publisher_invalid_response(message_collector):
|
|
|
|
class MockPublisher(AbstractPlatform):
|
|
|
|
|
|
|
|
name = "zulip"
|
|
|
|
|
|
|
|
def _send(self, message, event):
|
|
|
|
message_collector.append(message)
|
|
|
|
|
|
|
|
def _validate_response(self, response):
|
|
|
|
raise InvalidResponse("Invalid response")
|
|
|
|
|
|
|
|
def validate_credentials(self) -> None:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return MockPublisher()
|