microblog.pub/tests/test_public.py

94 lines
3.4 KiB
Python
Raw Permalink Normal View History

from unittest import mock
2022-06-22 18:11:22 +00:00
import pytest
from fastapi.testclient import TestClient
2022-06-29 18:43:17 +00:00
from sqlalchemy.orm import Session
2022-06-22 18:11:22 +00:00
2022-06-22 20:17:07 +00:00
from app import activitypub as ap
from app.actor import LOCAL_ACTOR
2022-06-22 18:11:22 +00: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"',
]
2022-06-22 20:17:07 +00:00
def test_index__html(db: Session, client: TestClient):
2022-06-22 18:11:22 +00:00
response = client.get("/")
assert response.status_code == 200
2022-06-22 20:17:07 +00:00
assert response.headers["content-type"].startswith("text/html")
2022-06-22 18:11:22 +00:00
@pytest.mark.parametrize("accept", _ACCEPTED_AP_HEADERS)
2022-06-22 20:17:07 +00:00
def test_index__ap(db: Session, client: TestClient, accept: str):
response = client.get("/", headers={"Accept": accept})
2022-06-22 18:11:22 +00:00
assert response.status_code == 200
2022-06-22 20:17:07 +00:00
assert response.headers["content-type"] == ap.AP_CONTENT_TYPE
assert response.json() == LOCAL_ACTOR.ap_actor
def test_followers__ap(client, db) -> None:
response = client.get("/followers", headers={"Accept": ap.AP_CONTENT_TYPE})
assert response.status_code == 200
assert response.headers["content-type"] == ap.AP_CONTENT_TYPE
json_resp = response.json()
assert json_resp["id"].endswith("/followers")
assert "first" in json_resp
def test_followers__ap_hides_followers(client, db) -> None:
with mock.patch("app.main.config.HIDES_FOLLOWERS", True):
response = client.get("/followers", headers={"Accept": ap.AP_CONTENT_TYPE})
assert response.status_code == 200
assert response.headers["content-type"] == ap.AP_CONTENT_TYPE
json_resp = response.json()
assert json_resp["id"].endswith("/followers")
assert "first" not in json_resp
2022-06-22 18:11:22 +00:00
2022-06-22 20:17:07 +00:00
def test_followers__html(client, db) -> None:
response = client.get("/followers")
assert response.status_code == 200
assert response.headers["content-type"].startswith("text/html")
def test_followers__html_hides_followers(client, db) -> None:
with mock.patch("app.main.config.HIDES_FOLLOWERS", True):
response = client.get("/followers", headers={"Accept": "text/html"})
assert response.status_code == 404
assert response.headers["content-type"].startswith("text/html")
2022-06-22 20:17:07 +00:00
def test_following__ap(client, db) -> None:
response = client.get("/following", headers={"Accept": ap.AP_CONTENT_TYPE})
assert response.status_code == 200
assert response.headers["content-type"] == ap.AP_CONTENT_TYPE
json_resp = response.json()
assert json_resp["id"].endswith("/following")
assert "first" in json_resp
def test_following__ap_hides_following(client, db) -> None:
with mock.patch("app.main.config.HIDES_FOLLOWING", True):
response = client.get("/following", headers={"Accept": ap.AP_CONTENT_TYPE})
assert response.status_code == 200
assert response.headers["content-type"] == ap.AP_CONTENT_TYPE
json_resp = response.json()
assert json_resp["id"].endswith("/following")
assert "first" not in json_resp
2022-06-22 20:17:07 +00:00
def test_following__html(client, db) -> None:
response = client.get("/following")
2022-06-22 18:11:22 +00:00
assert response.status_code == 200
2022-06-22 20:17:07 +00:00
assert response.headers["content-type"].startswith("text/html")
def test_following__html_hides_following(client, db) -> None:
with mock.patch("app.main.config.HIDES_FOLLOWING", True):
response = client.get("/following", headers={"Accept": "text/html"})
assert response.status_code == 404
assert response.headers["content-type"].startswith("text/html")