vienalatina/apps/board/tests/test_navigation.py
Claude 5e8ee5ad20
Two calendars: one for everybody, one for the members
**Public events are posts.** A new Evento category brings three fields with it —
event_date, event_time, event_location — and the first is required, because a
post's own `date` is when it was announced and an event announced three weeks
early would otherwise land in the agenda on the wrong day. Members propose
events through the same queue as everything else, and the date survives approval
untouched: the event the member described is the event that gets published.

An event's page leads with the day, the time and the place before the title,
shows its picture full width, and offers "añadir a mi calendario" — a real .ics
beside the page, generated because the editor writes `outputs: ["HTML", "ics"]`
into an event's frontmatter and nothing else asks for that format. It carries
Event structured data as well as the BlogPosting it already had, which is what
puts a date beside the result in a search listing.

/page/agenda/ is a month grid per month, every month from the first event to the
last built at build time, with a script that shows one and moves between them.
That arrangement is the point: with JavaScript off every month is on the page as
a list, with it on nothing is fetched to change months, and the month it opens on
is the reader's rather than the one the site was built in. Hugo exposes no
AddDate, so the length of a month is a table plus the leap rule and the first
weekday falls out of formatting the date — it looks like something simpler was
missed, and it is not.

**The private calendar** is a sixth section: rows in SQLite, written by admins,
read by every member, never published. It shows the public events too, marked,
so a member has one place to look rather than two — they come from the
content_cache the editor's listing already fills, which is why migration 5 adds
three columns to it and to submissions. The grid is Python's own
calendar.Calendar: no date arithmetic of mine anywhere in it.

Two things the tests found on the way:

gitea.py let `requests`' own exceptions through. They inherit from OSError, not
from GiteaError, so the calendar's careful "keep working while the git server is
down" would have been defeated by a refused connection — the page would have
500ed instead. Every transport failure is now a GiteaError, raised at the one
place that touches the wire. This is the third bug of exactly that shape in this
module.

An admin's erasure clears events.created_by rather than reassigning it. The
meeting still happens after they leave; the event belongs to the association.

509 tests pass, and the site builds with Hugo 0.135.0 under the pipeline's own
--panicOnWarning.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NizVpJ2dwzCbjCrTLCjeHn
2026-10-03 09:53:54 +00:00

220 lines
8.7 KiB
Python

"""What each role can see and reach.
This file exists because of a bug that no other test in this suite could have
caught: *Privados*, *Mi perfil* and *Contenido* were all inside one
`{% if g.member.role in ('owner', 'admin') %}` block in base.html, so an
ordinary member signed in to a wall and a member list and nothing else. Every
feature worked. Every test passed. Most of the members area was simply invisible
to the people it was built for.
So the navigation is now asserted per role, from the rendered page, and every
section is asserted from its route as well — because a link nobody can see and a
link that 403s are the same thing to whoever needed it.
"""
from __future__ import annotations
import pytest
# Label -> the path it leads to. Both halves matter: the label is what a member
# looks for, the path is what has to answer.
SECTIONS = {
"Inicio": "/comunidad/",
"Muro": "/comunidad/muro",
"Privados": "/comunidad/privados",
"Publicaciones": "/comunidad/publicaciones",
"Calendario": "/comunidad/calendario",
}
ADMIN_ONLY = {"Gestión": "/comunidad/gestion"}
@pytest.fixture
def nav(client, sign_in):
def _nav(member_id):
sign_in(member_id)
page = client.get("/comunidad/")
assert page.status_code == 200
return page.get_data(as_text=True)
return _nav
@pytest.mark.parametrize("role", ["user", "moderator"])
def test_a_member_sees_the_four_shared_sections(role, nav, make_member):
"""The regression. Not one of these may depend on a role again."""
body = nav(make_member("maria", role=role))
for label in SECTIONS:
assert f">{label}" in body or f"{label}<" in body, label
@pytest.mark.parametrize("role", ["user", "moderator"])
def test_a_member_does_not_see_gestion(role, nav, make_member):
assert "Gestión" not in nav(make_member("maria", role=role))
def test_an_admin_sees_everything(nav, make_member):
body = nav(make_member("admina", role="admin"))
for label in list(SECTIONS) + list(ADMIN_ONLY):
assert label in body, label
@pytest.mark.parametrize("role", ["user", "moderator", "admin"])
def test_the_account_items_are_in_the_corner_for_everybody(role, nav, make_member):
"""They used to be navigation entries, and two of them admin-only. A member
with no way to reach their own profile or their own data export is the same
bug wearing a different hat."""
body = nav(make_member("maria", role=role))
assert "/comunidad/mi-perfil" in body
assert "/comunidad/mis-datos" in body
assert "Salir" in body
@pytest.mark.parametrize("role", ["user", "moderator", "admin"])
@pytest.mark.parametrize("path", sorted(SECTIONS.values()))
def test_every_shared_section_answers_for_every_role(role, path, client, make_member, sign_in):
sign_in(make_member("maria", role=role))
assert client.get(path).status_code == 200
@pytest.mark.parametrize("role", ["user", "moderator"])
def test_gestion_is_refused_below_admin(role, client, make_member, sign_in):
"""Refused by the route, not hidden by the template. A moderator who types
the address meets the same answer as one who cannot see the link."""
sign_in(make_member("maria", role=role))
assert client.get("/comunidad/gestion").status_code == 403
def test_gestion_opens_for_an_admin(client, make_member, sign_in):
sign_in(make_member("admina", role="admin"))
assert client.get("/comunidad/gestion").status_code == 200
def test_signing_in_lands_on_inicio(client, db, post, make_member):
"""It used to land on the wall, which is one of five places something may
have happened since the last visit."""
from apps.board import passwords
member_id = make_member("maria")
db.execute("UPDATE members SET password_hash = ? WHERE id = ?",
(passwords.hash_password("una-contraseña-larga"), member_id))
response = post("/comunidad/login",
{"identifier": "maria", "password": "una-contraseña-larga"})
assert response.status_code == 302
assert response.headers["Location"].rstrip("/").endswith("/comunidad")
def test_the_wall_keeps_its_thread_urls(client, post, make_member, sign_in):
"""The wall moved from /comunidad/ to /comunidad/muro. Thread addresses did
not, because people have linked to them from private messages."""
sign_in(make_member("maria"))
post("/comunidad/nuevo", {"title": "Hola", "body": "Qué tal"})
assert client.get("/comunidad/tema/1").status_code == 200
assert client.get("/comunidad/muro").status_code == 200
def test_signed_out_visitors_get_no_navigation(client):
body = client.get("/comunidad/login").get_data(as_text=True)
for label in list(SECTIONS) + list(ADMIN_ONLY):
assert f'>{label}<' not in body, label
# --- reaching the published articles --------------------------------------
#
# The screen worked and nothing linked to it: Gestión offered only the static
# pages, and the Artículos tab exists only once you are already on that screen.
# A moderator, who cannot open Gestión at all, had no route to it.
def test_an_admin_reaches_the_articles_from_gestion(client, make_member, sign_in):
sign_in(make_member("admina", role="admin"))
body = client.get("/comunidad/gestion").get_data(as_text=True)
assert "/comunidad/contenido/post" in body
assert "Artículos publicados" in body
def test_a_moderator_reaches_them_from_publicaciones(client, make_member, sign_in):
"""Their only way in, so it is a link in the heading rather than a clause in
a paragraph."""
sign_in(make_member("luisa", role="moderator"))
body = client.get("/comunidad/publicaciones").get_data(as_text=True)
assert "/comunidad/contenido/post" in body
assert "Ya publicado" in body
def test_a_plain_member_is_offered_neither(client, make_member, sign_in):
sign_in(make_member("maria"))
body = client.get("/comunidad/publicaciones").get_data(as_text=True)
assert "/comunidad/contenido" not in body
assert "Ya publicado" not in body
# --- the strip the public site draws --------------------------------------
#
# vienalatina.com is files on disk, so the bar is drawn by a script that asks
# this app who is reading. That makes this endpoint the whole privacy surface:
# it is the only thing on the public side that knows anything about anybody.
def test_a_visitor_is_told_nothing(client):
response = client.get("/comunidad/sesion.json")
assert response.status_code == 200
assert response.get_json() == {"signed_in": False}
assert response.headers["Cache-Control"] == "no-store"
def test_a_member_gets_their_name_and_their_sections(client, make_member, sign_in):
sign_in(make_member("maria"))
state = client.get("/comunidad/sesion.json").get_json()
assert state["signed_in"] is True
assert state["name"] == "Maria"
assert [s["label"] for s in state["sections"]] == [
"Inicio", "Muro", "Privados", "Publicaciones", "Calendario"]
assert state["csrf"] # the bar renders a real logout form
def test_the_sections_follow_the_same_rule_as_the_navigation(
client, make_member, sign_in):
"""Two places listing the sections would drift the first time one changed;
the navigation and this endpoint have to agree about Gestión."""
sign_in(make_member("admina", role="admin"))
labels = [s["label"] for s in client.get("/comunidad/sesion.json").get_json()["sections"]]
assert "Gestión" in labels
sign_in(make_member("luisa", role="moderator"))
labels = [s["label"] for s in client.get("/comunidad/sesion.json").get_json()["sections"]]
assert "Gestión" not in labels
def test_the_answer_is_never_cached(client, make_member, sign_in):
"""A cached answer is somebody else's name on a shared machine, or a bar
that stays up after signing out."""
sign_in(make_member("maria"))
assert client.get("/comunidad/sesion.json").headers["Cache-Control"] == "no-store"
def test_signing_in_sets_the_hint_and_signing_out_clears_it(client, db, post, make_member):
"""The hint is what keeps a stranger reading one article from costing a
request to this app on every page view."""
from apps.board import passwords
member_id = make_member("maria")
db.execute("UPDATE members SET password_hash = ? WHERE id = ?",
(passwords.hash_password("una-contraseña-larga"), member_id))
signed_in = post("/comunidad/login",
{"identifier": "maria", "password": "una-contraseña-larga"})
cookie = signed_in.headers.get_all("Set-Cookie")
assert any("vl_sesion=1" in header for header in cookie)
assert any("HttpOnly" not in header for header in cookie if "vl_sesion" in header)
signed_out = post("/comunidad/logout")
assert any("vl_sesion=;" in header or "vl_sesion=\"\"" in header
for header in signed_out.headers.get_all("Set-Cookie"))