**A signed-in member sees the members area from anywhere.** A slim bar above the
public header, with the same five sections /comunidad/ has. Those pages are
files on disk and get cached, so it is drawn in the browser: memberbar.js asks
/comunidad/sesion.json who is reading, and that endpoint is the only thing that
decides — a visitor is told `{"signed_in": false}` and the answer is never
cached. Sign-in also sets vl_sesion=1, a cookie holding one digit that scripts
can read, and without it the script makes no request at all: a stranger reading
one article costs the app nothing. It is a hint, never a credential — the
session cookie stays HttpOnly and signed, and a forged hint earns a reply saying
you are not signed in. Comunidad is now in the public menu in all three
languages, signed in or not.
**The public menu is built from the pages.** It was three names per language in
config.yaml, so a page published through Contenido never appeared on the site at
all and the German and Portuguese menus had to be kept in step by hand. Pages
carry `parent` (another page's basename — the one translate.py already uses to
pair siblings, so it holds in all three languages) and `weight`, both chosen in
the editor, and header.html builds two levels from them. URLs do not change: a
subpage is still /page/<slug>/, only the menu nests, so nothing in the Caddyfile
or the pipeline learns about folders. The dropdown is the theme's own, ported
from WordPress — hover, focus-within, inline on a phone, no script. A parent that
has been deleted is kept and labelled rather than silently dropped on the next
save.
**Sharing and the rest of SEO/GEO.** The groundwork was already here — hreflang,
Open Graph, Twitter card, BlogPosting, llms.txt, robots.txt naming the AI
crawlers — so this closes the gaps. Static pages had *none* of it and shared as
bare links; they now carry WebPage schema and the full set of tags. Both branches
gain og:image with an alt and a twitter:image, falling back to params.defaultImage
(empty until there is a logo, which is the one line to fill in then). The home
page gains Organization with areaServed Wien and a sameAs list; posts and pages
gain BreadcrumbList, and an article's structured data now carries the author's
profile URL. An optional FAQ — `pregunta|respuesta` per line — renders at the
foot of the page and as FAQPage schema, because current guidance for generative
engines puts exactly that shape at the top of what gets quoted, and schema
describing something invisible is what search engines penalise. translate.py
carries both new fields: image_alt joins the translated keys, and faq is
translated half by half so the separator survives.
**The writing boxes.** A markdown toolbar and a Vista previa on every box where
somebody writes prose, opted in with data-markdown. The preview POSTs to
/comunidad/previsualizar and renders with render.to_html — the same function the
board and the site use, so it cannot promise something publishing will not
deliver; a markdown parser in the browser would be a second opinion about what
somebody's text means. Choosing a picture now shows a thumbnail with its name and
size before anything is uploaded, which is the only reason `blob:` joins img-src:
a handle to bytes already in the page, reaching no network.
All of it ours, no dependency added, under a policy that loads scripts from this
origin only. Verified by building the site with Hugo 0.135, the version the
pipeline runs: the nested menu, the page's share tags, the FAQ schema matching
the rendered section, and llms.txt listing the pages.
463 tests pass.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NizVpJ2dwzCbjCrTLCjeHn
148 lines
6.0 KiB
Python
148 lines
6.0 KiB
Python
"""Threads, comments, and who may touch them."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
|
|
@pytest.fixture
|
|
def thread(db, make_member):
|
|
author = make_member("autora")
|
|
cursor = db.execute(
|
|
"INSERT INTO threads (author_id, title, body_md) VALUES (?, 'Original', 'Cuerpo')",
|
|
(author,),
|
|
)
|
|
return {"id": cursor.lastrowid, "author": author}
|
|
|
|
|
|
def test_a_member_can_post_and_read_it_back(client, post, make_member, sign_in):
|
|
sign_in(make_member("maria"))
|
|
post("/comunidad/nuevo", {"title": "Reunión del jueves", "body": "A las 19h en el café."})
|
|
body = client.get("/comunidad/").get_data(as_text=True)
|
|
assert "Reunión del jueves" in body
|
|
|
|
|
|
def test_markdown_is_rendered_but_html_is_not(client, post, make_member, sign_in):
|
|
sign_in(make_member("maria"))
|
|
post("/comunidad/nuevo", {
|
|
"title": "Prueba",
|
|
"body": "**fuerte** y <script>alert(1)</script>",
|
|
})
|
|
body = client.get("/comunidad/tema/1").get_data(as_text=True)
|
|
assert "<strong>fuerte</strong>" in body
|
|
assert "<script>alert(1)</script>" not in body
|
|
assert "<script>" in body
|
|
|
|
|
|
def test_nobody_edits_someone_elses_words(client, post, thread, make_member, sign_in):
|
|
"""Not even an admin. Removing a post is visible to its author; quietly
|
|
rewriting it is not, which is why moderation here means deletion."""
|
|
sign_in(make_member("admina", role="admin"))
|
|
response = post(f"/comunidad/tema/{thread['id']}/editar",
|
|
{"title": "Reescrito", "body": "Otra cosa"})
|
|
assert response.status_code == 403
|
|
|
|
|
|
def test_a_user_cannot_delete_someone_elses_thread(client, post, thread, make_member, sign_in):
|
|
sign_in(make_member("ajena"))
|
|
assert post(f"/comunidad/tema/{thread['id']}/eliminar").status_code == 403
|
|
|
|
|
|
def test_an_admin_can_delete_any_thread(client, db, post, thread, make_member, sign_in):
|
|
sign_in(make_member("admina", role="admin"))
|
|
post(f"/comunidad/tema/{thread['id']}/eliminar")
|
|
row = db.execute("SELECT deleted_at FROM threads WHERE id = ?", (thread["id"],)).fetchone()
|
|
assert row["deleted_at"] is not None
|
|
|
|
|
|
def test_the_author_can_edit_their_own(client, db, post, thread, sign_in):
|
|
sign_in(thread["author"])
|
|
post(f"/comunidad/tema/{thread['id']}/editar", {"title": "Corregido", "body": "Mejor"})
|
|
row = db.execute("SELECT title, edited_at FROM threads WHERE id = ?",
|
|
(thread["id"],)).fetchone()
|
|
assert row["title"] == "Corregido"
|
|
assert row["edited_at"] is not None
|
|
|
|
|
|
def test_a_deleted_thread_disappears_from_the_list_and_the_page(
|
|
client, db, post, thread, sign_in):
|
|
sign_in(thread["author"])
|
|
post(f"/comunidad/tema/{thread['id']}/eliminar")
|
|
assert "Original" not in client.get("/comunidad/").get_data(as_text=True)
|
|
assert client.get(f"/comunidad/tema/{thread['id']}").status_code == 404
|
|
|
|
|
|
def test_a_deleted_thread_is_left_out_of_the_export(client, db, post, thread, sign_in):
|
|
sign_in(thread["author"])
|
|
post(f"/comunidad/tema/{thread['id']}/eliminar")
|
|
assert "Original" not in client.get("/comunidad/mis-datos").get_data(as_text=True)
|
|
|
|
|
|
def test_a_closed_thread_refuses_replies(client, db, post, thread, make_member, sign_in):
|
|
db.execute("UPDATE threads SET locked = 1 WHERE id = ?", (thread["id"],))
|
|
sign_in(make_member("maria"))
|
|
post(f"/comunidad/tema/{thread['id']}/comentar", {"body": "¿Hola?"})
|
|
count = db.execute("SELECT COUNT(*) AS n FROM comments WHERE thread_id = ?",
|
|
(thread["id"],)).fetchone()["n"]
|
|
assert count == 0
|
|
|
|
|
|
def test_only_admins_pin(client, post, thread, make_member, sign_in):
|
|
sign_in(make_member("maria"))
|
|
assert post(f"/comunidad/tema/{thread['id']}/estado",
|
|
{"field": "pinned", "value": "1"}).status_code == 403
|
|
|
|
|
|
def test_the_state_field_is_not_a_way_into_the_query(client, post, thread, make_member, sign_in):
|
|
"""`field` is interpolated into the UPDATE, so it is checked against a fixed
|
|
pair first. This asserts the check, not the interpolation."""
|
|
sign_in(make_member("admina", role="admin"))
|
|
assert post(f"/comunidad/tema/{thread['id']}/estado",
|
|
{"field": "role", "value": "1"}).status_code == 400
|
|
|
|
|
|
def test_the_cooldown_stops_a_double_submit(app, client, post, make_member, sign_in, db):
|
|
app.config["COOLDOWN_SECONDS"] = 60
|
|
sign_in(make_member("rapida"))
|
|
post("/comunidad/nuevo", {"title": "Primero", "body": "Uno"})
|
|
post("/comunidad/nuevo", {"title": "Segundo", "body": "Dos"})
|
|
count = db.execute("SELECT COUNT(*) AS n FROM threads").fetchone()["n"]
|
|
assert count == 1
|
|
|
|
|
|
# --- the preview behind the Vista previa button ---------------------------
|
|
#
|
|
# A round trip rather than a markdown parser in the browser: the preview has to
|
|
# agree with what gets published, and the only way to guarantee that is to
|
|
# render it with the same function the board and the site use.
|
|
|
|
def test_the_preview_renders_the_same_markdown_the_board_does(post, make_member, sign_in):
|
|
sign_in(make_member("maria"))
|
|
|
|
response = post("/comunidad/previsualizar", {"body": "**hola** y _adiós_"})
|
|
|
|
assert response.status_code == 200
|
|
assert response.get_json()["html"] == "<p><strong>hola</strong> y <em>adiós</em></p>\n"
|
|
|
|
|
|
def test_the_preview_drops_html_exactly_as_the_board_does(post, make_member, sign_in):
|
|
"""`html=False` in render.py is the whole XSS defence. A preview that
|
|
rendered raw HTML would be a hole in the members area and a lie about what
|
|
posting is going to do."""
|
|
sign_in(make_member("maria"))
|
|
|
|
html = post("/comunidad/previsualizar",
|
|
{"body": "<script>alert(1)</script>"}).get_json()["html"]
|
|
|
|
assert "<script>" not in html
|
|
|
|
|
|
def test_the_preview_needs_a_session(client):
|
|
assert client.post("/comunidad/previsualizar", data={"body": "hola"}).status_code in (302, 400)
|
|
|
|
|
|
def test_the_preview_is_never_cached(post, make_member, sign_in):
|
|
sign_in(make_member("maria"))
|
|
response = post("/comunidad/previsualizar", {"body": "hola"})
|
|
assert response.headers["Cache-Control"] == "no-store"
|