2026-06-13 14:32:33 +00:00
|
|
|
# retoor <retoor@molodetz.nl>
|
|
|
|
|
|
2026-09-08 03:44:30 +02:00
|
|
|
import time
|
|
|
|
|
|
2026-05-23 03:44:04 +00:00
|
|
|
import requests
|
2026-09-08 03:44:30 +02:00
|
|
|
|
|
|
|
|
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
|
2026-05-23 03:44:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_404_renders_error_page(app_server):
|
2026-09-08 03:44:30 +02:00
|
|
|
_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")
|
2026-05-23 03:44:04 +00:00
|
|
|
assert r.status_code == 404
|