94 lines
3.5 KiB
Python
94 lines
3.5 KiB
Python
# retoor <retoor@molodetz.nl>
|
|
import re
|
|
|
|
import pytest
|
|
from defusedxml import ElementTree
|
|
|
|
PUBLIC_PAGES = ["/", "/rol", "/standaard", "/flyers", "/memes", "/mensen", "/mensen/retoor", "/binnen", "/docs", "/docs/welkom", "/docs/api/inhoud", "/docs/search?q=markdown", "/voorwaarden", "/privacy", "/auth/login"]
|
|
JSON_PAGES = ["/", "/rol", "/standaard", "/flyers", "/memes", "/mensen", "/binnen"]
|
|
|
|
|
|
@pytest.mark.parametrize("path", PUBLIC_PAGES)
|
|
def test_public_pages_return_200(anon, path):
|
|
response = anon.get(path)
|
|
assert response.status_code == 200, path
|
|
assert "text/html" in response.headers["content-type"]
|
|
|
|
|
|
@pytest.mark.parametrize("path", JSON_PAGES)
|
|
def test_json_faces(anon, path):
|
|
response = anon.get(path, headers={"Accept": "application/json"})
|
|
assert response.status_code == 200
|
|
assert response.headers["content-type"].startswith("application/json")
|
|
assert isinstance(response.json(), dict)
|
|
|
|
|
|
def test_home_is_molodetz(anon):
|
|
html = anon.get("/").text
|
|
assert "Molodetz" in html
|
|
for label in ("Rol", "Standaard", "Flyers", "Memes", "Mensen", "Binnen"):
|
|
assert f">{label}</a>" in html
|
|
assert "wa.me" not in html and "whatsapp" not in html.lower()
|
|
|
|
|
|
def test_no_bare_static_urls_in_rendered_html(anon):
|
|
for path in ("/", "/flyers", "/memes", "/rol"):
|
|
html = anon.get(path).text
|
|
assert not re.search(r"""(src|href)="/static/(?!v|uploads/)""", html), path
|
|
|
|
|
|
def test_versioned_static_asset(anon):
|
|
html = anon.get("/").text
|
|
match = re.search(r'href="(/static/v[^"]+/css/base\.css)"', html)
|
|
assert match
|
|
assert anon.get(match.group(1)).status_code == 200
|
|
|
|
|
|
def test_seeded_posts_and_placeholders(anon):
|
|
rol = anon.get("/rol", headers={"Accept": "application/json"}).json()["posts"]
|
|
seeds = {"Een zes is geen ambitie.", "Af is een eigenschap.", "De assistent is geen senior.", "Geen cursus."}
|
|
assert seeds <= {post["title"] for post in rol}
|
|
assert all(post["is_placeholder"] for post in rol if post["title"] in seeds)
|
|
standaard = anon.get("/standaard", headers={"Accept": "application/json"}).json()["posts"]
|
|
keuzes = next(post for post in standaard if post["title"] == "Eenendertig keuzes. Jij hebt er nul.")
|
|
assert keuzes["is_placeholder"] is False
|
|
|
|
|
|
def test_post_page(anon):
|
|
posts = anon.get("/rol", headers={"Accept": "application/json"}).json()["posts"]
|
|
post = next(post for post in posts if post["title"] == "Geen cursus.")
|
|
response = anon.get(f"/posts/{post['slug']}")
|
|
assert response.status_code == 200
|
|
assert "Placeholder" in response.text
|
|
|
|
|
|
def test_galleries_have_images(anon):
|
|
for path, minimum in (("/flyers", 4), ("/memes", 10)):
|
|
html = anon.get(path).text
|
|
assert html.count("data-lightbox") >= minimum, path
|
|
|
|
|
|
def test_mensen_lists_real_accounts_only(anon):
|
|
people = anon.get("/mensen", headers={"Accept": "application/json"}).json()
|
|
names = [person["username"] for person in people.get("people", [])]
|
|
assert names[0] == "retoor"
|
|
assert set(names) <= {"retoor", "lezer"}
|
|
|
|
|
|
def test_unknown_page_is_404(anon):
|
|
response = anon.get("/bestaat-niet-xyz")
|
|
assert response.status_code == 404
|
|
assert anon.get("/bestaat-niet-xyz", headers={"Accept": "application/json"}).json()["error"]
|
|
|
|
|
|
def test_robots_and_sitemap(anon):
|
|
assert "Sitemap:" in anon.get("/robots.txt").text
|
|
sitemap = anon.get("/sitemap.xml")
|
|
root = ElementTree.fromstring(sitemap.content)
|
|
assert root.tag.endswith("urlset")
|
|
assert len(list(root)) >= 5
|
|
|
|
|
|
def test_health(anon):
|
|
assert anon.get("/health").json()["status"] == "ok"
|