vienalatina/apps/board/tests/test_reactions.py
Pablo Volenski 1ff3522c56
A superadministrator above the responsable
You asked for a super-admin, and most of it already existed as the `owner` role
— labelled «Responsable», seeded from BOARD_OWNER, already the only account that
could create an admin, demote one, erase anybody or hand the role on, with a
partial unique index making "exactly one" a database rule. You chose a genuine
fifth tier anyway, and there is a reading in which that is the better answer, so
this builds it with that reading made explicit:

  superadministrador — the platform: admin accounts, the look, the identity
  responsable        — the association: members, moderators, invitations

The split is worth more than a rename because of where this is going. Installed
for a second organisation, the superadministrator is whoever maintains the box
and the responsable is their president — so the responsable chair is safe to
hand to a customer, and an admin account is not theirs to grant.

Both chairs are unreachable, for different reasons. The superadministrator
because there is nothing above them. The responsable because the chair belongs
to the association: a platform administrator quietly removing the president of
the organisation they host is exactly the move two chairs exist to prevent.
Each steps down by handing their own chair on, and whoever gives one up becomes
an administrator — there is no role above the one they just gave away.

Migration 7 is the rebuild migration 4 already did, for the same reason (SQLite
cannot alter a CHECK), and it promotes the existing owner — Pablo — to
superadministrator, leaving the responsable chair empty for the association to
fill. Nothing is erased; you offered and it was not needed.

Two things in here would have shipped silently without a test looking for them:

`_seed_superadmin` ends `ON CONFLICT(gitea_login) DO UPDATE SET role = …`. That
line was right while owner was the top of the tree. Left as it was, it would
have matched BOARD_OWNER's login and demoted the superadministrator back to
owner **on every container restart** — a privilege reduction caused by nothing
anybody did, freeing the chair for somebody else. It takes a second start-up to
catch, so the test starts the app three times.

And seven places outside the role modules asked "is this person at least an
admin" as a literal `("owner", "admin")` tuple — the wall's delete, the editor's
page collection, the calendar's edit flag, the dashboard, the moderation
predicate. Every one of them silently excluded the superadministrator the moment
a tier appeared above owner. They are now one ordered LADDER and one
`is_at_least`, so the next tier is a word in a list rather than an audit.

Marca moves from admin-only to superadmin-only, which is the distinction you
asked for: the look and the identity are the platform's.

564 tests pass, including the predicate table per actor and target, each of the
four doors into "make an admin" closed against an admin and against the
responsable, and both single-chair indexes surviving the rebuild.

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

151 lines
5.5 KiB
Python

"""One 👍 per member per thing, and clicking it again takes it back.
The whole feature is a count and a toggle. What is worth testing is not that a
row appears — it is that a member cannot react twice (the database says so, not
the handler), that the button goes back where it was pressed without becoming an
open redirect, and that reacting to something that has been deleted is a 404
rather than a row pointing at nothing.
"""
from __future__ import annotations
import sqlite3
import pytest
def react(post, kind, target_id, back=None):
data = {"kind": kind, "id": target_id}
if back is not None:
data["back"] = back
return post("/comunidad/reaccion", data)
@pytest.fixture
def thread(post, make_member, sign_in):
"""A thread by José, for somebody else to react to."""
jose = make_member("jose")
sign_in(jose)
post("/comunidad/nuevo", {"title": "Hola", "body": "Qué tal"})
return 1
def test_reacting_adds_one_and_reacting_again_takes_it_back(
db, post, thread, make_member, sign_in):
sign_in(make_member("maria"))
react(post, "thread", thread)
assert db.execute("SELECT COUNT(*) AS n FROM reactions").fetchone()["n"] == 1
react(post, "thread", thread)
assert db.execute("SELECT COUNT(*) AS n FROM reactions").fetchone()["n"] == 0
def test_two_members_each_get_one(db, post, thread, make_member, sign_in):
for name in ("maria", "luisa"):
sign_in(make_member(name))
react(post, "thread", thread)
assert db.execute("SELECT COUNT(*) AS n FROM reactions").fetchone()["n"] == 2
def test_the_database_refuses_a_second_one(db, thread, make_member):
"""A partial unique index rather than a check in the handler, because the
handler is one bug away from inserting twice and the index is not.
UNIQUE(member_id, thread_id) on its own would not do it: SQLite treats NULLs
as distinct, so every comment reaction — all with a NULL thread_id — would
be unique by accident."""
maria = make_member("maria")
db.execute("INSERT INTO reactions (thread_id, member_id) VALUES (?, ?)", (thread, maria))
with pytest.raises(sqlite3.IntegrityError):
db.execute("INSERT INTO reactions (thread_id, member_id) VALUES (?, ?)",
(thread, maria))
def test_a_reaction_belongs_to_exactly_one_thing(db, thread, make_member):
maria = make_member("maria")
for columns, values in (("", ""), ("thread_id, comment_id", f"{thread}, 1")):
with pytest.raises(sqlite3.IntegrityError):
db.execute(
f"INSERT INTO reactions ({columns + ', ' if columns else ''}member_id) "
f"VALUES ({values + ', ' if values else ''}{maria})")
def test_comments_can_be_reacted_to_as_well(db, post, thread, make_member, sign_in):
sign_in(make_member("maria"))
post(f"/comunidad/tema/{thread}/comentar", {"body": "Yo también"})
react(post, "comment", 1)
row = db.execute("SELECT thread_id, comment_id FROM reactions").fetchone()
assert (row["thread_id"], row["comment_id"]) == (None, 1)
def test_the_count_and_my_own_mark_show_on_the_page(client, post, thread,
make_member, sign_in):
sign_in(make_member("maria"))
react(post, "thread", thread)
for url in (f"/comunidad/tema/{thread}", "/comunidad/muro"):
body = client.get(url).get_data(as_text=True)
assert "reaction__button--mine" in body, url
assert ">1<" in body, url
# Somebody else sees the count but not the filled button.
sign_in(make_member("luisa"))
body = client.get("/comunidad/muro").get_data(as_text=True)
assert "reaction__button--mine" not in body
assert ">1<" in body
def test_reacting_to_something_that_is_gone_is_a_404(db, post, thread,
superadmin_id, make_member, sign_in):
sign_in(superadmin_id)
post(f"/comunidad/tema/{thread}/eliminar")
sign_in(make_member("maria"))
assert react(post, "thread", thread).status_code == 404
assert react(post, "thread", 9999).status_code == 404
assert db.execute("SELECT COUNT(*) AS n FROM reactions").fetchone()["n"] == 0
def test_an_unknown_kind_is_refused(post, thread, make_member, sign_in):
sign_in(make_member("maria"))
assert react(post, "members", thread).status_code == 400
def test_the_back_field_cannot_leave_the_site(post, thread, make_member, sign_in):
"""`back` is a redirect somebody can type, which is an open redirect unless
it is checked — the same hole the login form's `next` has, so it goes
through the same function."""
sign_in(make_member("maria"))
response = react(post, "thread", thread, back="https://ejemplo.invalid/premio")
assert response.status_code == 302
assert "ejemplo.invalid" not in response.headers["Location"]
def test_the_button_comes_back_to_the_comment_it_was_pressed_on(
post, thread, make_member, sign_in):
sign_in(make_member("maria"))
post(f"/comunidad/tema/{thread}/comentar", {"body": "Yo también"})
response = react(post, "comment", 1, back=f"/comunidad/tema/{thread}#c1")
assert response.headers["Location"].endswith(f"/comunidad/tema/{thread}#c1")
def test_erasing_a_member_takes_their_reactions(db, post, thread, superadmin_id,
make_member, sign_in):
maria = make_member("maria")
sign_in(maria)
react(post, "thread", thread)
sign_in(superadmin_id)
post(f"/comunidad/miembros/{maria}/eliminar")
assert db.execute("SELECT COUNT(*) AS n FROM reactions").fetchone()["n"] == 0