Files
devplacepy/tests/api/error/pages.py
T

81 lines
2.3 KiB
Python
Raw Normal View History

# retoor <retoor@molodetz.nl>
import time
import requests
from devplacepy.database import set_setting
from tests.conftest import BASE_URL, CACHE_VERSION_PROPAGATION_SECONDS
JSON = {"Accept": "application/json"}
def _set_happy_404(enabled: bool) -> None:
set_setting("happy_404_enabled", "1" if enabled else "0")
time.sleep(CACHE_VERSION_PROPAGATION_SECONDS)
def _login(seeded_db, who="alice"):
creds = seeded_db[who]
session = requests.Session()
session.post(
f"{BASE_URL}/auth/login",
data={"email": creds["email"], "password": creds["password"]},
)
return session
def test_404_renders_error_page(app_server):
_set_happy_404(False)
try:
r = requests.get(f"{BASE_URL}/this-page-does-not-exist-xyz")
assert r.status_code == 404
assert "404" in r.text or "not found" in r.text.lower()
finally:
_set_happy_404(True)
def test_happy_404_renders_a_real_post(app_server, seeded_db):
session = _login(seeded_db)
title = f"Happy404 Marker {int(time.time() * 1000)}"
created = session.post(
f"{BASE_URL}/posts/create",
headers=JSON,
data={
"title": title,
"content": "content seeded so the happy 404 pool has something to pick.",
"topic": "devlog",
},
).json()["data"]
assert created["uid"]
_set_happy_404(True)
r = requests.get(f"{BASE_URL}/this-path-was-never-registered-anywhere")
assert r.status_code == 200
assert 'class="post-detail"' in r.text
assert "Page not found" not in r.text
assert 'name="robots" content="noindex,nofollow"' in r.text
def test_happy_404_disabled_falls_back_to_error_page(app_server):
_set_happy_404(False)
try:
r = requests.get(f"{BASE_URL}/another-path-that-does-not-exist")
assert r.status_code == 404
assert "Page not found" in r.text
finally:
_set_happy_404(True)
def test_happy_404_never_applies_to_json_requests():
_set_happy_404(True)
r = requests.get(f"{BASE_URL}/yet-another-missing-path", headers=JSON)
assert r.status_code == 404
assert r.json()["error"]["status"] == 404
def test_happy_404_never_applies_to_api_paths():
_set_happy_404(True)
r = requests.get(f"{BASE_URL}/api/this-devrant-route-does-not-exist")
assert r.status_code == 404