Happy 404: an HTML 404 (unmatched route, or an explicit not-found inside a
real route) now renders a random existing post instead of the error page,
using the exact same context builder as a real post view. Toggle is the
happy_404_enabled site setting (default on, /admin/settings); JSON/API
requests and a handful of excluded prefixes are never affected. The pool of
candidate slugs is cached in-process and resampled periodically so it stays
fast and eventually cycles the whole posts table; on any internal failure it
falls straight through to the real 404 page.
Applying this everywhere surfaced ~60 existing tests that asserted a literal
404 for a legitimate resource-not-found flow (deleted post, unknown
container, wrong project slug, etc.) - each now disables the setting for the
duration of that specific check and restores it after, so the underlying
not-found behavior stays covered independently of the new feature.
Post page also gained, all built on the same shared post_page_context() so
they render identically on both a real post and a happy-404 page:
- A left sidebar (three separate cards, matching /feed's sidebar-card
convention) for "Gists from {author}", "Projects from {author}" (private
projects filtered through the normal visibility check), and "Related
Discussions" - each cached per author and invalidated on create/edit/
delete so new content shows up immediately.
- A right column reusing /feed's exact Daily Topic widget class for up to
three "Featured" articles (the existing but previously-unused `featured`
news flag), cached as a pool with per-request random sampling.
- A "Next post -> " link beside "Back to Feed", pointing at the next older
post site-wide (blocked authors skipped). Wired through the same next_url
mechanism already used for listing pagination, so it emits a real
backend-rendered <link rel="next"> tag for SEO, not just a visible link.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BWJy6PrMMt5hwWxQwia2rd
128 lines
3.9 KiB
Python
128 lines
3.9 KiB
Python
# retoor <retoor@molodetz.nl>
|
|
|
|
import io
|
|
import time
|
|
import pytest
|
|
import requests
|
|
from datetime import datetime, timezone
|
|
from PIL import Image
|
|
from tests.conftest import BASE_URL, CACHE_VERSION_PROPAGATION_SECONDS
|
|
from devplacepy.attachments import store_attachment
|
|
from devplacepy.database import get_table, set_setting
|
|
from devplacepy.utils import generate_uid, make_combined_slug
|
|
|
|
|
|
@pytest.fixture(scope="module", autouse=True)
|
|
def _disable_happy_404(app_server):
|
|
set_setting("happy_404_enabled", "0")
|
|
time.sleep(CACHE_VERSION_PROPAGATION_SECONDS)
|
|
yield
|
|
set_setting("happy_404_enabled", "1")
|
|
time.sleep(CACHE_VERSION_PROPAGATION_SECONDS)
|
|
|
|
|
|
def _png():
|
|
buf = io.BytesIO()
|
|
Image.new("RGBA", (32, 32), (20, 40, 60, 255)).save(buf, format="PNG")
|
|
return buf.getvalue()
|
|
|
|
|
|
def _seed_published(receiver_uid, giver_uid, description="Published award"):
|
|
uid = generate_uid()
|
|
slug = make_combined_slug(description, uid)
|
|
now = datetime.now(timezone.utc).isoformat()
|
|
att = store_attachment(_png(), "award-256.png", receiver_uid)
|
|
get_table("awards").insert(
|
|
{
|
|
"uid": uid,
|
|
"slug": slug,
|
|
"description": description,
|
|
"giver_uid": giver_uid,
|
|
"receiver_uid": receiver_uid,
|
|
"attachment_uid_512": att["uid"],
|
|
"attachment_uid_256": att["uid"],
|
|
"attachment_uid_64": att["uid"],
|
|
"generated_at": now,
|
|
"created_at": now,
|
|
"job_uid": "",
|
|
"deleted_at": None,
|
|
"deleted_by": None,
|
|
}
|
|
)
|
|
return uid, slug, att["url"]
|
|
|
|
|
|
def _user():
|
|
uid = generate_uid()
|
|
get_table("users").insert(
|
|
{
|
|
"uid": uid,
|
|
"username": f"aw_{uid[:8]}",
|
|
"terms_version": "1",
|
|
"email": f"{uid[:8]}@t.dev",
|
|
"created_at": datetime.now(timezone.utc).isoformat(),
|
|
}
|
|
)
|
|
return uid
|
|
|
|
|
|
def test_published_award_redirects(app_server):
|
|
giver = _user()
|
|
receiver = _user()
|
|
_, slug, url = _seed_published(receiver, giver)
|
|
r = requests.get(f"{BASE_URL}/awards/{slug}/256", allow_redirects=False)
|
|
assert r.status_code == 302
|
|
assert r.headers["Location"] == url
|
|
assert "immutable" in r.headers.get("Cache-Control", "")
|
|
|
|
|
|
def test_pending_award_returns_404(app_server):
|
|
uid = generate_uid()
|
|
slug = make_combined_slug("pending", uid)
|
|
now = datetime.now(timezone.utc).isoformat()
|
|
get_table("awards").insert(
|
|
{
|
|
"uid": uid,
|
|
"slug": slug,
|
|
"description": "pending",
|
|
"giver_uid": _user(),
|
|
"receiver_uid": _user(),
|
|
"attachment_uid_512": "",
|
|
"attachment_uid_256": "",
|
|
"attachment_uid_64": "",
|
|
"generated_at": None,
|
|
"created_at": now,
|
|
"job_uid": "",
|
|
"deleted_at": None,
|
|
"deleted_by": None,
|
|
}
|
|
)
|
|
assert requests.get(f"{BASE_URL}/awards/{slug}/256").status_code == 404
|
|
|
|
|
|
def test_revoked_award_returns_404(app_server):
|
|
giver = _user()
|
|
receiver = _user()
|
|
uid, slug, _ = _seed_published(receiver, giver, "revoked")
|
|
get_table("awards").update(
|
|
{"uid": uid, "deleted_at": datetime.now(timezone.utc).isoformat(), "deleted_by": giver},
|
|
["uid"],
|
|
)
|
|
assert requests.get(f"{BASE_URL}/awards/{slug}/64").status_code == 404
|
|
|
|
|
|
def test_lookup_by_bare_uid_works(app_server):
|
|
giver = _user()
|
|
receiver = _user()
|
|
uid, slug, url = _seed_published(receiver, giver, "uid lookup")
|
|
r = requests.get(f"{BASE_URL}/awards/{uid}/256", allow_redirects=False)
|
|
assert r.status_code == 302
|
|
assert r.headers["Location"] == url
|
|
assert slug
|
|
|
|
|
|
def test_invalid_size_returns_404(app_server):
|
|
giver = _user()
|
|
receiver = _user()
|
|
_, slug, _ = _seed_published(receiver, giver)
|
|
assert requests.get(f"{BASE_URL}/awards/{slug}/128").status_code == 404 |