From f9f9e62e139279f9556ced018ff04db68b369db0 Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Wed, 22 Jun 2022 21:15:07 +0200 Subject: [PATCH] Cleanup utils --- app/actor.py | 5 ++++- app/admin.py | 4 ++-- app/ap_object.py | 2 +- {scss => app/scss}/main.scss | 0 app/templates.py | 4 ++-- app/utils/__init__.py | 0 app/{ => utils}/highlight.py | 0 app/{ => utils}/opengraph.py | 2 +- app/{urlutils.py => utils/url.py} | 0 tests/test_admin.py | 8 ++++++-- tests/test_httpsig.py | 3 --- tests/test_process_outgoing_activities.py | 3 +++ tests/utils.py | 3 ++- 13 files changed, 21 insertions(+), 13 deletions(-) rename {scss => app/scss}/main.scss (100%) create mode 100644 app/utils/__init__.py rename app/{ => utils}/highlight.py (100%) rename app/{ => utils}/opengraph.py (98%) rename app/{urlutils.py => utils/url.py} (100%) diff --git a/app/actor.py b/app/actor.py index 08d188e..c11bb9f 100644 --- a/app/actor.py +++ b/app/actor.py @@ -1,5 +1,6 @@ import typing from dataclasses import dataclass +from typing import Union from urllib.parse import urlparse from sqlalchemy.orm import Session @@ -148,7 +149,7 @@ ActorsMetadata = dict[str, ActorMetadata] def get_actors_metadata( db: Session, - actors: list["ActorModel"], + actors: list[Union["ActorModel", "RemoteActor"]], ) -> ActorsMetadata: from app import models @@ -179,6 +180,8 @@ def get_actors_metadata( } idx: ActorsMetadata = {} for actor in actors: + if not actor.ap_id: + raise ValueError("Should never happen") idx[actor.ap_id] = ActorMetadata( ap_actor_id=actor.ap_id, is_following=actor.ap_id in following, diff --git a/app/admin.py b/app/admin.py index b58f87d..394a774 100644 --- a/app/admin.py +++ b/app/admin.py @@ -72,9 +72,9 @@ def get_lookup( if query: ap_object = lookup(db, query) if ap_object.ap_type in ap.ACTOR_TYPES: - actors_metadata = get_actors_metadata(db, [ap_object]) + actors_metadata = get_actors_metadata(db, [ap_object]) # type: ignore else: - actors_metadata = get_actors_metadata(db, [ap_object.actor]) + actors_metadata = get_actors_metadata(db, [ap_object.actor]) # type: ignore print(ap_object) return templates.render_template( db, diff --git a/app/ap_object.py b/app/ap_object.py index dc32dd8..5c9c613 100644 --- a/app/ap_object.py +++ b/app/ap_object.py @@ -7,10 +7,10 @@ from dateutil.parser import isoparse from markdown import markdown from app import activitypub as ap -from app import opengraph from app.actor import LOCAL_ACTOR from app.actor import Actor from app.actor import RemoteActor +from app.utils import opengraph class Object: diff --git a/scss/main.scss b/app/scss/main.scss similarity index 100% rename from scss/main.scss rename to app/scss/main.scss diff --git a/app/templates.py b/app/templates.py index 2ca7c14..a651d3b 100644 --- a/app/templates.py +++ b/app/templates.py @@ -23,8 +23,8 @@ from app.config import VERSION from app.config import generate_csrf_token from app.config import session_serializer from app.database import now -from app.highlight import HIGHLIGHT_CSS -from app.highlight import highlight +from app.utils.highlight import HIGHLIGHT_CSS +from app.utils.highlight import highlight _templates = Jinja2Templates(directory="app/templates") diff --git a/app/utils/__init__.py b/app/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/highlight.py b/app/utils/highlight.py similarity index 100% rename from app/highlight.py rename to app/utils/highlight.py diff --git a/app/opengraph.py b/app/utils/opengraph.py similarity index 98% rename from app/opengraph.py rename to app/utils/opengraph.py index e28c79b..1e29919 100644 --- a/app/opengraph.py +++ b/app/utils/opengraph.py @@ -8,7 +8,7 @@ from pydantic import BaseModel from app import activitypub as ap from app import config -from app.urlutils import is_url_valid +from app.utils.url import is_url_valid class OpenGraphMeta(BaseModel): diff --git a/app/urlutils.py b/app/utils/url.py similarity index 100% rename from app/urlutils.py rename to app/utils/url.py diff --git a/tests/test_admin.py b/tests/test_admin.py index 3eb795f..5ccbaa2 100644 --- a/tests/test_admin.py +++ b/tests/test_admin.py @@ -1,16 +1,20 @@ +import typing + +import starlette from fastapi.testclient import TestClient from app.main import app -def test_admin_endpoints_are_authenticated(client: TestClient): +def test_admin_endpoints_are_authenticated(client: TestClient) -> None: routes_tested = [] for route in app.routes: + route = typing.cast(starlette.routing.Route, route) if not route.path.startswith("/admin") or route.path == "/admin/login": continue - for method in route.methods: + for method in route.methods: # type: ignore resp = client.request(method, route.path) # Admin routes should redirect to the login page diff --git a/tests/test_httpsig.py b/tests/test_httpsig.py index 97a8d24..1538ce1 100644 --- a/tests/test_httpsig.py +++ b/tests/test_httpsig.py @@ -55,7 +55,6 @@ def test_enforce_httpsig__no_signature( @pytest.mark.asyncio async def test_enforce_httpsig__with_valid_signature( - client: TestClient, respx_mock: respx.MockRouter, ) -> None: # Given a remote actor @@ -105,7 +104,6 @@ def test_httpsig_checker__no_signature( @pytest.mark.asyncio async def test_httpsig_checker__with_valid_signature( - client: TestClient, respx_mock: respx.MockRouter, ) -> None: # Given a remote actor @@ -138,7 +136,6 @@ async def test_httpsig_checker__with_valid_signature( @pytest.mark.asyncio async def test_httpsig_checker__with_invvalid_signature( - client: TestClient, respx_mock: respx.MockRouter, ) -> None: # Given a remote actor diff --git a/tests/test_process_outgoing_activities.py b/tests/test_process_outgoing_activities.py index e2bd43f..2d2b1ad 100644 --- a/tests/test_process_outgoing_activities.py +++ b/tests/test_process_outgoing_activities.py @@ -44,6 +44,9 @@ def test_new_outgoing_activity( outbox_object = _setup_outbox_object() inbox_url = "https://example.com/inbox" + if not outbox_object.id: + raise ValueError("Should never happen") + # When queuing the activity outgoing_activity = new_outgoing_activity(db, inbox_url, outbox_object.id) diff --git a/tests/utils.py b/tests/utils.py index cff33b2..cd09df3 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,4 +1,5 @@ from contextlib import contextmanager +from typing import Any import fastapi @@ -25,5 +26,5 @@ def mock_httpsig_checker(ra: actor.RemoteActor): del app.dependency_overrides[httpsig.httpsig_checker] -def generate_admin_session_cookies() -> dict[str, str]: +def generate_admin_session_cookies() -> dict[str, Any]: return {"session": session_serializer.dumps({"is_logged_in": True})}