foxhole/tests/test_main.py

62 lines
1.7 KiB
Python
Raw Normal View History

2023-04-03 03:50:59 +02:00
#!/usr/bin/env python3
2023-04-03 09:16:46 +02:00
import pytest
import httpx
import respx
2023-04-03 03:50:59 +02:00
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session
2023-04-03 09:16:46 +02:00
from app import activitypub as ap
from app.config import AP_CONTENT_TYPE
from tests.utils import build_remote_actor
2023-04-03 03:50:59 +02:00
2023-04-03 09:16:46 +02:00
_ACCEPTED_AP_HEADERS = [
"application/activity+json",
"application/activity+json; charset=utf-8",
"application/ld+json",
'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
]
2023-04-03 03:50:59 +02:00
2023-07-30 14:48:50 +02:00
2023-04-03 03:50:59 +02:00
def test_index__html(client: TestClient):
response = client.get("/")
assert response.status_code == 200
2023-07-30 14:48:50 +02:00
assert response.headers["content-type"].startswith("text/html")
2023-04-03 09:16:46 +02:00
@pytest.mark.parametrize("accept", _ACCEPTED_AP_HEADERS)
def test_index__ap(db: Session, client: TestClient, accept: str):
response = client.get("/", headers={"Accept": accept})
assert response.status_code == 200
assert response.headers["content-type"] == AP_CONTENT_TYPE
assert response.json() == ap.ME
@pytest.mark.asyncio
async def test_follow_only_status(
client: TestClient,
respx_mock: respx.MockRouter,
) -> None:
# build test actor
ra = build_remote_actor()
remote_ap_id = ra.ap_id # type: ignore
# mock request
respx_mock.get(remote_ap_id).mock(
return_value=httpx.Response(200,json=ra.ap_actor))
respx_mock.post(remote_ap_id + "/inbox").mock(
return_value=httpx.Response(202))
response = client.post(
"/outbox",
headers={"Authorization": "Basic test-token"},
content='{"visibility": "followers-only","content": "note content"}'
)
assert response.status_code == 200
# And the Note was not show in the index
indext_content = client.get("/").text
assert "note content" not in indext_content