microblog/tests/utils.py

82 lines
2.1 KiB
Python
Raw Normal View History

2022-07-26 18:26:34 +00:00
import asyncio
2022-06-22 18:11:22 +00:00
from contextlib import contextmanager
2022-06-22 19:15:07 +00:00
from typing import Any
2022-07-26 18:26:34 +00:00
from uuid import uuid4
2022-06-22 18:11:22 +00:00
import fastapi
2022-07-26 18:26:34 +00:00
import httpx
import respx
2022-06-22 18:11:22 +00:00
from app import actor
from app import httpsig
2022-07-26 18:26:34 +00:00
from app import models
from app.actor import LOCAL_ACTOR
from app.ap_object import RemoteObject
2022-06-22 18:11:22 +00:00
from app.config import session_serializer
2022-07-26 18:26:34 +00:00
from app.database import async_session
2022-06-22 18:11:22 +00:00
from app.main import app
2022-07-26 18:26:34 +00:00
from tests import factories
2022-06-22 18:11:22 +00:00
@contextmanager
def mock_httpsig_checker(ra: actor.RemoteActor):
async def httpsig_checker(
request: fastapi.Request,
) -> httpsig.HTTPSigInfo:
return httpsig.HTTPSigInfo(
has_valid_signature=True,
signed_by_ap_actor_id=ra.ap_id,
)
app.dependency_overrides[httpsig.httpsig_checker] = httpsig_checker
try:
yield
finally:
del app.dependency_overrides[httpsig.httpsig_checker]
2022-06-22 19:15:07 +00:00
def generate_admin_session_cookies() -> dict[str, Any]:
2022-06-22 18:11:22 +00:00
return {"session": session_serializer.dumps({"is_logged_in": True})}
2022-07-26 18:26:34 +00:00
def setup_remote_actor(respx_mock: respx.MockRouter) -> actor.RemoteActor:
ra = factories.RemoteActorFactory(
base_url="https://example.com",
username="toto",
public_key="pk",
)
respx_mock.get(ra.ap_id).mock(return_value=httpx.Response(200, json=ra.ap_actor))
return ra
def setup_remote_actor_as_follower(ra: actor.RemoteActor) -> models.Follower:
actor = factories.ActorFactory.from_remote_actor(ra)
follow_id = uuid4().hex
follow_from_inbox = RemoteObject(
factories.build_follow_activity(
from_remote_actor=ra,
for_remote_actor=LOCAL_ACTOR,
outbox_public_id=follow_id,
),
ra,
)
inbox_object = factories.InboxObjectFactory.from_remote_object(
follow_from_inbox, actor
)
follower = factories.FollowerFactory(
inbox_object_id=inbox_object.id,
actor_id=actor.id,
ap_actor_id=actor.ap_id,
)
return follower
def run_async(func, *args, **kwargs):
async def _func():
async with async_session() as db:
return await func(db, *args, **kwargs)
asyncio.run(_func())