foxhole/tests/test_inbox.py

71 lines
2 KiB
Python

#!/usr/bin/env python3
import pytest
import fastapi
import respx
import httpx
from uuid import uuid4
from tests import factories # type: ignore
from unittest import mock
from app.main import app
from app.utils import precheck
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session
from app import activitypub as ap, httpsig
from app.config import AP_CONTENT_TYPE
from app import models
from sqlalchemy import select
def test_inbox_follow_request(
db: Session,
client: TestClient,
respx_mock: respx.MockRouter,
) -> None:
async def inbox_prechecker(
request: fastapi.Request,
) -> bool:
return True
# build test actor
ra = factories.RemoteActorFactory(
base_url="https://example.com",
username="test",
public_key="pk",
)
ap_id = ra.ap_id # type: ignore
app.dependency_overrides[precheck.inbox_prechecker] = inbox_prechecker
# mock request
respx_mock.get(ap_id).mock(return_value=httpx.Response(200,json=ra.ap_actor))
respx_mock.post(ap_id + "/inbox").mock(return_value=httpx.Response(202))
# send follower request
with mock.patch("app.boxes.MANUALLY_APPROVES_FOLLOWERS", False):
response = client.post(
"/inbox",
headers={"Content-Type": AP_CONTENT_TYPE},
json={
"@context": ap.AS_CTX,
"type": "Follow",
"id": ap_id + "/follow/" + (uuid4().hex),
"actor": ap_id,
"object": ap.ME["id"],
},
)
assert response.status_code == 202
# actor was saved in actor table
saved_actor = db.execute(select(models.Actor)).scalar_one()
assert saved_actor.ap_id == ap_id
# follower request was saved in outbox table
outbox_object = db.execute(select(models.OutboxObject)).scalar_one()
assert outbox_object.ap_type == "Accept"
# follower was saved in follower table
follower_actor = db.execute(select(models.Follower)).scalar_one()
assert follower_actor.ap_actor_id == ap_id