vienalatina/apps/board/tests/test_navigation.py
Claude 5ea6b1ac98
One dashboard, five sections, and a moderation queue
The members area had grown one navigation entry per phase, and three of the
five were inside an admin-only block in base.html — so an ordinary member
signed in to a wall and a member list, with their own profile, their own
private messages and the content editor all invisible. Everything worked and
nothing was reachable. That is the bug this starts from.

The navigation is now Inicio · Muro · Privados · Publicaciones, plus Gestión
for admins, and the account items (Mi perfil, Descargar mis datos, Salir) moved
to a menu under the member's own name — a <details> element, because the CSP has
no 'unsafe-inline' and a menu needing onclick="" silently never opens. Inicio is
a new dashboard; the wall moved from /comunidad/ to /comunidad/muro and kept its
thread URLs. test_navigation.py asserts the navigation per role from the
rendered page, so this cannot come back quietly.

Public posts are now curated. A member proposes one and it waits in a new
`submissions` table; a moderator or admin approves it — which commits it
through content.publish, the same function the editor uses, with the author
named per commit so git history says who wrote it, not who approved it — or
returns it with a reason the author reads. Moderators and admins do not queue.
Nothing a member writes reaches the repository before approval, because a
commit to it *is* publication: the webhook starts the pipeline. A proposal's
picture stays private in /data/uploads until then, and the private copy goes
when the public one is committed. There is deliberately no way to promote a wall
thread into a proposal.

The moderator role needed the role CHECK widened, and SQLite has no DROP
CONSTRAINT, so migration 4 rebuilds `members` — eight tables reference it, the
one-owner index has to be recreated by hand, and PRAGMA foreign_key_check runs
before the commit. One trap worth recording: the allowed roles can only be read
back out of sqlite_master's verbatim CREATE TABLE text, comments included, so
schema.sql's own comment explaining the rebuild made the step decide it had
already run. The start-up test caught it; comments are stripped now.

A moderator curates content and has no power over people: no creating,
suspending, promoting or erasing anybody. set_role moved from owner-only to
admin-level so moderators can be appointed routinely, and checks may_create on
the new role as well, so admins still cannot mint admins. content.py splits per
collection — posts need a moderator, the site's static pages need an admin.

Also: erasure deletes a member's submissions and their pictures and clears
reviewed_by, which the existing foreign-key guard forced; deploy-board.sh takes
a backup before restarting, since two migrations now rebuild a table in place
and one of them holds your own account; README and docs/server-setup.md updated
for the roles, the sections and migration 4.

370 tests pass.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NizVpJ2dwzCbjCrTLCjeHn
2026-09-30 11:09:55 +00:00

119 lines
4.6 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",
}
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