Cleanup utils

This commit is contained in:
Thomas Sileo 2022-06-22 21:15:07 +02:00
parent b7b04cad03
commit f9f9e62e13
13 changed files with 21 additions and 13 deletions

View file

@ -1,5 +1,6 @@
import typing import typing
from dataclasses import dataclass from dataclasses import dataclass
from typing import Union
from urllib.parse import urlparse from urllib.parse import urlparse
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
@ -148,7 +149,7 @@ ActorsMetadata = dict[str, ActorMetadata]
def get_actors_metadata( def get_actors_metadata(
db: Session, db: Session,
actors: list["ActorModel"], actors: list[Union["ActorModel", "RemoteActor"]],
) -> ActorsMetadata: ) -> ActorsMetadata:
from app import models from app import models
@ -179,6 +180,8 @@ def get_actors_metadata(
} }
idx: ActorsMetadata = {} idx: ActorsMetadata = {}
for actor in actors: for actor in actors:
if not actor.ap_id:
raise ValueError("Should never happen")
idx[actor.ap_id] = ActorMetadata( idx[actor.ap_id] = ActorMetadata(
ap_actor_id=actor.ap_id, ap_actor_id=actor.ap_id,
is_following=actor.ap_id in following, is_following=actor.ap_id in following,

View file

@ -72,9 +72,9 @@ def get_lookup(
if query: if query:
ap_object = lookup(db, query) ap_object = lookup(db, query)
if ap_object.ap_type in ap.ACTOR_TYPES: 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: else:
actors_metadata = get_actors_metadata(db, [ap_object.actor]) actors_metadata = get_actors_metadata(db, [ap_object.actor]) # type: ignore
print(ap_object) print(ap_object)
return templates.render_template( return templates.render_template(
db, db,

View file

@ -7,10 +7,10 @@ from dateutil.parser import isoparse
from markdown import markdown from markdown import markdown
from app import activitypub as ap from app import activitypub as ap
from app import opengraph
from app.actor import LOCAL_ACTOR from app.actor import LOCAL_ACTOR
from app.actor import Actor from app.actor import Actor
from app.actor import RemoteActor from app.actor import RemoteActor
from app.utils import opengraph
class Object: class Object:

View file

@ -23,8 +23,8 @@ from app.config import VERSION
from app.config import generate_csrf_token from app.config import generate_csrf_token
from app.config import session_serializer from app.config import session_serializer
from app.database import now from app.database import now
from app.highlight import HIGHLIGHT_CSS from app.utils.highlight import HIGHLIGHT_CSS
from app.highlight import highlight from app.utils.highlight import highlight
_templates = Jinja2Templates(directory="app/templates") _templates = Jinja2Templates(directory="app/templates")

0
app/utils/__init__.py Normal file
View file

View file

@ -8,7 +8,7 @@ from pydantic import BaseModel
from app import activitypub as ap from app import activitypub as ap
from app import config from app import config
from app.urlutils import is_url_valid from app.utils.url import is_url_valid
class OpenGraphMeta(BaseModel): class OpenGraphMeta(BaseModel):

View file

@ -1,16 +1,20 @@
import typing
import starlette
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from app.main import app from app.main import app
def test_admin_endpoints_are_authenticated(client: TestClient): def test_admin_endpoints_are_authenticated(client: TestClient) -> None:
routes_tested = [] routes_tested = []
for route in app.routes: for route in app.routes:
route = typing.cast(starlette.routing.Route, route)
if not route.path.startswith("/admin") or route.path == "/admin/login": if not route.path.startswith("/admin") or route.path == "/admin/login":
continue continue
for method in route.methods: for method in route.methods: # type: ignore
resp = client.request(method, route.path) resp = client.request(method, route.path)
# Admin routes should redirect to the login page # Admin routes should redirect to the login page

View file

@ -55,7 +55,6 @@ def test_enforce_httpsig__no_signature(
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_enforce_httpsig__with_valid_signature( async def test_enforce_httpsig__with_valid_signature(
client: TestClient,
respx_mock: respx.MockRouter, respx_mock: respx.MockRouter,
) -> None: ) -> None:
# Given a remote actor # Given a remote actor
@ -105,7 +104,6 @@ def test_httpsig_checker__no_signature(
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_httpsig_checker__with_valid_signature( async def test_httpsig_checker__with_valid_signature(
client: TestClient,
respx_mock: respx.MockRouter, respx_mock: respx.MockRouter,
) -> None: ) -> None:
# Given a remote actor # Given a remote actor
@ -138,7 +136,6 @@ async def test_httpsig_checker__with_valid_signature(
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_httpsig_checker__with_invvalid_signature( async def test_httpsig_checker__with_invvalid_signature(
client: TestClient,
respx_mock: respx.MockRouter, respx_mock: respx.MockRouter,
) -> None: ) -> None:
# Given a remote actor # Given a remote actor

View file

@ -44,6 +44,9 @@ def test_new_outgoing_activity(
outbox_object = _setup_outbox_object() outbox_object = _setup_outbox_object()
inbox_url = "https://example.com/inbox" inbox_url = "https://example.com/inbox"
if not outbox_object.id:
raise ValueError("Should never happen")
# When queuing the activity # When queuing the activity
outgoing_activity = new_outgoing_activity(db, inbox_url, outbox_object.id) outgoing_activity = new_outgoing_activity(db, inbox_url, outbox_object.id)

View file

@ -1,4 +1,5 @@
from contextlib import contextmanager from contextlib import contextmanager
from typing import Any
import fastapi import fastapi
@ -25,5 +26,5 @@ def mock_httpsig_checker(ra: actor.RemoteActor):
del app.dependency_overrides[httpsig.httpsig_checker] 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})} return {"session": session_serializer.dumps({"is_logged_in": True})}