2021-09-26 20:12:06 +02:00
|
|
|
import pytest
|
2021-10-05 15:32:07 +02:00
|
|
|
import requests
|
2021-09-26 20:12:06 +02:00
|
|
|
import responses
|
|
|
|
|
|
|
|
from mobilizon_reshare.config.config import get_settings
|
|
|
|
from mobilizon_reshare.models.publication import PublicationStatus
|
2021-11-11 15:18:04 +01:00
|
|
|
from mobilizon_reshare.models.publisher import Publisher
|
2021-09-26 20:12:06 +02:00
|
|
|
from mobilizon_reshare.publishers.coordinator import PublisherCoordinator
|
2021-10-05 15:32:07 +02:00
|
|
|
from mobilizon_reshare.publishers.exceptions import (
|
|
|
|
InvalidEvent,
|
|
|
|
InvalidResponse,
|
2021-10-24 21:32:28 +02:00
|
|
|
InvalidMessage,
|
2021-11-28 17:25:43 +01:00
|
|
|
HTTPResponseError,
|
2021-10-05 15:32:07 +02:00
|
|
|
)
|
|
|
|
from mobilizon_reshare.publishers.platforms.zulip import ZulipFormatter, ZulipPublisher
|
2022-02-23 17:26:13 +01:00
|
|
|
from mobilizon_reshare.storage.query.converter import event_to_model
|
2021-11-24 23:58:06 +01:00
|
|
|
from mobilizon_reshare.storage.query.read import build_publications
|
2021-09-26 20:12:06 +02:00
|
|
|
|
|
|
|
api_uri = "https://zulip.twc-italia.org/api/v1/"
|
|
|
|
users_me = {
|
|
|
|
"result": "success",
|
|
|
|
"msg": "",
|
|
|
|
"email": "giacomotest2-bot@zulip.twc-italia.org",
|
|
|
|
"user_id": 217,
|
|
|
|
"avatar_version": 1,
|
|
|
|
"is_admin": False,
|
|
|
|
"is_owner": False,
|
|
|
|
"is_guest": False,
|
|
|
|
"is_bot": True,
|
|
|
|
"full_name": "Bot test Giacomo2",
|
|
|
|
"timezone": "",
|
|
|
|
"is_active": True,
|
|
|
|
"date_joined": "2021-09-13T19:36:45.857782+00:00",
|
|
|
|
"avatar_url": "https://secure.gravatar.com/avatar/d2d9a932bf9ff69d4e3cdf2203271500",
|
|
|
|
"bot_type": 1,
|
|
|
|
"bot_owner_id": 14,
|
|
|
|
"max_message_id": 8048,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def mocked_valid_response():
|
|
|
|
with responses.RequestsMock() as rsps:
|
|
|
|
rsps.add(
|
2022-02-14 21:10:27 +01:00
|
|
|
responses.GET, api_uri + "users/me", json=users_me, status=200,
|
2021-09-26 20:12:06 +02:00
|
|
|
)
|
|
|
|
rsps.add(
|
|
|
|
responses.POST,
|
|
|
|
api_uri + "messages",
|
|
|
|
json={"result": "success", "msg": "", "id": 8049},
|
|
|
|
status=200,
|
|
|
|
)
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def mocked_credential_error_response():
|
|
|
|
with responses.RequestsMock() as rsps:
|
|
|
|
rsps.add(
|
|
|
|
responses.GET,
|
|
|
|
api_uri + "users/me",
|
|
|
|
json={"result": "error", "msg": "Your credentials are not valid!"},
|
|
|
|
status=403,
|
|
|
|
)
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def mocked_client_error_response():
|
|
|
|
with responses.RequestsMock() as rsps:
|
|
|
|
rsps.add(
|
2022-02-14 21:10:27 +01:00
|
|
|
responses.GET, api_uri + "users/me", json=users_me, status=200,
|
2021-09-26 20:12:06 +02:00
|
|
|
)
|
|
|
|
rsps.add(
|
|
|
|
responses.POST,
|
|
|
|
api_uri + "messages",
|
|
|
|
json={"result": "error", "msg": "Invalid request"},
|
|
|
|
status=400,
|
|
|
|
)
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
@pytest.mark.asyncio
|
2021-11-24 23:58:06 +01:00
|
|
|
async def setup_db(
|
|
|
|
mock_active_publishers_config, event_model_generator, publication_model_generator
|
|
|
|
):
|
2021-09-26 20:12:06 +02:00
|
|
|
settings = get_settings()
|
|
|
|
settings["publisher"]["zulip"][
|
|
|
|
"bot_email"
|
|
|
|
] = "giacomotest2-bot@zulip.twc-italia.org"
|
2021-11-24 23:58:06 +01:00
|
|
|
settings["publisher"]["zulip"]["instance"] = "https://zulip.twc-italia.org"
|
2021-09-26 20:12:06 +02:00
|
|
|
|
2021-11-11 15:18:04 +01:00
|
|
|
publisher = await Publisher.filter(name="zulip").first()
|
2021-09-26 20:12:06 +02:00
|
|
|
event = event_model_generator()
|
|
|
|
await event.save()
|
|
|
|
publication = publication_model_generator(
|
|
|
|
event_id=event.id, publisher_id=publisher.id
|
|
|
|
)
|
|
|
|
await publication.save()
|
|
|
|
|
|
|
|
|
2021-11-11 15:18:04 +01:00
|
|
|
@pytest.fixture
|
2021-09-26 20:12:06 +02:00
|
|
|
@pytest.mark.asyncio
|
2021-11-24 23:58:06 +01:00
|
|
|
async def unsaved_publications(event):
|
2022-02-23 17:26:13 +01:00
|
|
|
await event_to_model(event).save()
|
2021-11-24 23:58:06 +01:00
|
|
|
return await build_publications(event)
|
2021-11-11 15:18:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2021-11-24 23:58:06 +01:00
|
|
|
async def test_zulip_publisher(mocked_valid_response, setup_db, unsaved_publications):
|
2021-11-11 15:18:04 +01:00
|
|
|
|
2021-11-24 23:58:06 +01:00
|
|
|
report = PublisherCoordinator(unsaved_publications).run()
|
2021-09-26 20:12:06 +02:00
|
|
|
|
2021-10-16 01:25:45 +02:00
|
|
|
assert report.reports[0].status == PublicationStatus.COMPLETED
|
2021-09-26 20:12:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2022-02-09 00:54:56 +01:00
|
|
|
async def test_zulip_publisher_failure_invalid_credentials(
|
2021-11-24 23:58:06 +01:00
|
|
|
mocked_credential_error_response, setup_db, unsaved_publications
|
2021-09-26 20:12:06 +02:00
|
|
|
):
|
2021-11-24 23:58:06 +01:00
|
|
|
report = PublisherCoordinator(unsaved_publications).run()
|
2021-10-16 01:25:45 +02:00
|
|
|
assert report.reports[0].status == PublicationStatus.FAILED
|
2021-11-28 17:25:43 +01:00
|
|
|
assert report.reports[0].reason.startswith("403 Client Error: Forbidden for url: ")
|
2021-09-26 20:12:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2021-10-03 13:19:37 +02:00
|
|
|
async def test_zulip_publisher_failure_client_error(
|
2021-11-24 23:58:06 +01:00
|
|
|
mocked_client_error_response, setup_db, unsaved_publications
|
2021-09-26 20:12:06 +02:00
|
|
|
):
|
2021-11-24 23:58:06 +01:00
|
|
|
report = PublisherCoordinator(unsaved_publications).run()
|
2021-10-16 01:25:45 +02:00
|
|
|
assert report.reports[0].status == PublicationStatus.FAILED
|
2021-11-28 17:25:43 +01:00
|
|
|
assert report.reports[0].reason.startswith("400 Client Error: Bad Request for url:")
|
2021-10-03 13:19:37 +02:00
|
|
|
|
|
|
|
|
2021-10-05 15:32:07 +02:00
|
|
|
def test_event_validation(event):
|
|
|
|
event.description = None
|
|
|
|
with pytest.raises(InvalidEvent):
|
|
|
|
ZulipFormatter().validate_event(event)
|
|
|
|
|
|
|
|
|
2021-10-03 13:19:37 +02:00
|
|
|
def test_message_length_success(event):
|
|
|
|
message = "a" * 500
|
|
|
|
event.description = message
|
2021-11-27 23:31:44 +01:00
|
|
|
assert ZulipFormatter().validate_event(event) is None
|
2021-10-03 13:19:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_message_length_failure(event):
|
|
|
|
message = "a" * 10000
|
|
|
|
event.description = message
|
2021-10-24 21:32:28 +02:00
|
|
|
with pytest.raises(InvalidMessage):
|
2021-11-27 23:31:44 +01:00
|
|
|
ZulipFormatter().validate_event(event)
|
2021-10-05 15:32:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_validate_response():
|
|
|
|
response = requests.Response()
|
|
|
|
response.status_code = 200
|
|
|
|
response._content = b"""{"result":"ok"}"""
|
|
|
|
ZulipPublisher()._validate_response(response)
|
|
|
|
|
|
|
|
|
|
|
|
def test_validate_response_invalid_json():
|
|
|
|
response = requests.Response()
|
|
|
|
response.status_code = 200
|
|
|
|
response._content = b"""{"osxsa"""
|
|
|
|
with pytest.raises(InvalidResponse) as e:
|
|
|
|
ZulipPublisher()._validate_response(response)
|
|
|
|
|
|
|
|
e.match("json")
|
|
|
|
|
|
|
|
|
|
|
|
def test_validate_response_invalid_request():
|
|
|
|
response = requests.Response()
|
|
|
|
response.status_code = 400
|
|
|
|
response._content = b"""{"result":"error", "msg":"wrong request"}"""
|
2021-11-28 17:25:43 +01:00
|
|
|
with pytest.raises(HTTPResponseError) as e:
|
2021-10-05 15:32:07 +02:00
|
|
|
|
|
|
|
ZulipPublisher()._validate_response(response)
|
|
|
|
|
2021-11-28 17:25:43 +01:00
|
|
|
e.match("400 Client Error: None for url:")
|