import logging
import uuid
from types import ModuleType
from unittest.mock import Mock, patch
import httpx
import pytest
from flask import Flask
from webmentions_ssg import DATABASE as db
from webmentions_ssg.models import ReceivedWebmention
from webmentions_ssg.url_security import AddressResolutionError
SOURCE_URL = "https://source.example/article"
TARGET_URL = "https://dennisfink.me/blog/example/"
@pytest.fixture
def receiver(app: Flask) -> ModuleType:
from webmentions_ssg.tasks import receiver
return receiver
def create_webmention(
app: Flask,
*,
source: str = SOURCE_URL,
target: str = TARGET_URL,
status: str = "received",
failure_reason: str | None = None,
) -> uuid.UUID:
identifier = uuid.uuid7()
with app.app_context():
webmention = ReceivedWebmention(
uuid=identifier,
source=source,
target=target,
status=status,
failure_reason=failure_reason,
)
db.session.add(webmention)
db.session.commit()
return identifier
def get_webmention_state(app: Flask, identifier: uuid.UUID) -> tuple[str, str | None]:
with app.app_context():
webmention = db.session.get(ReceivedWebmention, identifier)
assert webmention is not None
return (webmention.status, webmention.failure_reason)
def set_stream_response(httpx_client: Mock, response: httpx.Response) -> Mock:
client = httpx_client.return_value.__enter__.return_value
client.stream.return_value.__enter__.return_value = response
return client
@pytest.mark.parametrize(
("body", "expected"),
[
pytest.param(f'Reply', True, id="a-href"),
pytest.param(f'', True, id="area-href"),
pytest.param(
f'', True, id="link-href"
),
pytest.param(f'
', True, id="img-src"),
pytest.param(f'', True, id="audio-src"),
pytest.param(f'', True, id="video-src"),
pytest.param(
f'', True, id="audio-source-src"
),
pytest.param(
f'', True, id="video-source-src"
),
pytest.param(f'', True, id="iframe-src"),
pytest.param(f'