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
504 lines
18 KiB
Python
504 lines
18 KiB
Python
"""The moderation queue.
|
|
|
|
The property every test here circles is the same one: **nothing a member writes
|
|
reaches the site repository until a moderator approves it.** A commit to that
|
|
repository is publication — Gitea's webhook starts the translate, build and
|
|
deploy pipeline — so "queued in git with a flag" would already be live in three
|
|
languages. The Gitea layer is therefore replaced by a dictionary, and several
|
|
tests assert it was *not* written to, which is the assertion that matters most.
|
|
|
|
The second property is attribution. Approval commits under the author's name and
|
|
email, not the moderator's, because git history is where somebody looks months
|
|
later to ask who wrote a post.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import io
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from apps.board import gitea
|
|
|
|
PNG = b"\x89PNG\r\n\x1a\n" + b"\x00" * 64
|
|
|
|
|
|
class FakeRepo:
|
|
"""Only what a submission needs: writes, with who made them."""
|
|
|
|
def __init__(self):
|
|
self.files: dict[str, bytes] = {}
|
|
self.commits: list[str] = []
|
|
self.authors: list[dict] = []
|
|
|
|
def write_file(self, path, data, message, token=None, sha=None, member=None):
|
|
self.files[path] = data
|
|
self.commits.append(message)
|
|
self.authors.append(gitea._identity(member))
|
|
return hashlib.sha1(data).hexdigest()
|
|
|
|
|
|
@pytest.fixture
|
|
def repo(monkeypatch):
|
|
fake = FakeRepo()
|
|
monkeypatch.setattr(gitea, "write_file", fake.write_file)
|
|
return fake
|
|
|
|
|
|
@pytest.fixture
|
|
def maria(make_member):
|
|
return make_member("maria", role="user")
|
|
|
|
|
|
@pytest.fixture
|
|
def luisa(make_member):
|
|
return make_member("luisa", role="moderator")
|
|
|
|
|
|
def propose(post, title="Feria en Ottakring", body="Habrá **empanadas**.", **extra):
|
|
data = {"title": title, "body": body}
|
|
data.update(extra)
|
|
return post("/comunidad/publicaciones/nueva", data)
|
|
|
|
|
|
def only_submission(db):
|
|
return db.execute("SELECT * FROM submissions").fetchone()
|
|
|
|
|
|
# --- a member proposes ----------------------------------------------------
|
|
|
|
def test_a_proposal_is_a_row_and_not_a_commit(repo, db, post, maria, sign_in):
|
|
"""The whole point of the table. If this ever commits, the post is on
|
|
vienalatina.com before anybody has read it."""
|
|
sign_in(maria)
|
|
propose(post, description="Una feria")
|
|
|
|
row = only_submission(db)
|
|
assert (row["state"], row["title"]) == ("pending", "Feria en Ottakring")
|
|
assert repo.files == {}
|
|
assert repo.commits == []
|
|
|
|
|
|
def test_an_empty_proposal_is_refused(db, post, maria, sign_in):
|
|
sign_in(maria)
|
|
assert propose(post, title=" ", body=" ").status_code == 400
|
|
assert db.execute("SELECT COUNT(*) AS n FROM submissions").fetchone()["n"] == 0
|
|
|
|
|
|
def test_only_so_many_may_wait_at_once(db, post, maria, sign_in):
|
|
"""A bound on how much unread work one person can put on the moderators
|
|
without talking to them."""
|
|
from apps.board.submissions import PENDING_MAX
|
|
sign_in(maria)
|
|
for n in range(PENDING_MAX):
|
|
propose(post, title=f"Propuesta {n}")
|
|
|
|
refused = propose(post, title="Una más")
|
|
|
|
assert refused.status_code == 400
|
|
assert db.execute("SELECT COUNT(*) AS n FROM submissions").fetchone()["n"] == PENDING_MAX
|
|
|
|
|
|
def test_a_moderator_is_sent_to_the_editor_instead(client, luisa, sign_in):
|
|
"""Their posts do not queue, so a form that would wait for their own
|
|
approval is a detour with nothing at the end of it."""
|
|
sign_in(luisa)
|
|
response = client.get("/comunidad/publicaciones/nueva")
|
|
|
|
assert response.status_code == 302
|
|
assert "/comunidad/contenido/post/nuevo" in response.headers["Location"]
|
|
|
|
|
|
def test_a_moderator_publishing_directly_does_commit(repo, db, post, luisa, sign_in):
|
|
"""The other half of the policy: curation applies to members' posts, and a
|
|
moderator is the person doing the curating."""
|
|
sign_in(luisa)
|
|
post("/comunidad/contenido/post/nuevo",
|
|
{"title": "Aviso", "body": "Texto.", "date": "2026-09-25"})
|
|
|
|
assert list(repo.files) == ["content/post/2026-09-25-aviso.es.md"]
|
|
assert db.execute("SELECT COUNT(*) AS n FROM submissions").fetchone()["n"] == 0
|
|
|
|
|
|
# --- who may read an unapproved post -------------------------------------
|
|
|
|
def test_a_stranger_cannot_read_somebody_else_is_draft(client, db, post, maria,
|
|
make_member, sign_in):
|
|
sign_in(maria)
|
|
propose(post)
|
|
submission_id = only_submission(db)["id"]
|
|
|
|
sign_in(make_member("curiosa"))
|
|
assert client.get(f"/comunidad/publicaciones/{submission_id}").status_code == 403
|
|
|
|
|
|
def test_the_author_and_the_moderators_can(client, db, post, maria, luisa, sign_in):
|
|
sign_in(maria)
|
|
propose(post)
|
|
submission_id = only_submission(db)["id"]
|
|
|
|
assert client.get(f"/comunidad/publicaciones/{submission_id}").status_code == 200
|
|
sign_in(luisa)
|
|
assert client.get(f"/comunidad/publicaciones/{submission_id}").status_code == 200
|
|
|
|
|
|
def test_the_queue_is_only_listed_for_moderators(client, db, post, maria,
|
|
luisa, make_member, sign_in):
|
|
sign_in(maria)
|
|
propose(post, title="Feria de Ottakring")
|
|
|
|
sign_in(make_member("otro"))
|
|
assert "Feria de Ottakring" not in client.get(
|
|
"/comunidad/publicaciones").get_data(as_text=True)
|
|
|
|
sign_in(luisa)
|
|
assert "Feria de Ottakring" in client.get(
|
|
"/comunidad/publicaciones").get_data(as_text=True)
|
|
|
|
|
|
def test_a_member_cannot_approve_their_own(repo, client, db, post, maria, sign_in):
|
|
"""The one that would make the whole queue decorative."""
|
|
sign_in(maria)
|
|
propose(post)
|
|
submission_id = only_submission(db)["id"]
|
|
|
|
response = post(f"/comunidad/publicaciones/{submission_id}/aprobar")
|
|
|
|
assert response.status_code == 403
|
|
assert repo.files == {}
|
|
assert only_submission(db)["state"] == "pending"
|
|
|
|
|
|
def test_a_member_cannot_reject_either(client, db, post, maria, make_member, sign_in):
|
|
sign_in(maria)
|
|
propose(post)
|
|
submission_id = only_submission(db)["id"]
|
|
|
|
sign_in(make_member("otro"))
|
|
assert post(f"/comunidad/publicaciones/{submission_id}/rechazar",
|
|
{"note": "No me gusta"}).status_code == 403
|
|
|
|
|
|
# --- approval -------------------------------------------------------------
|
|
|
|
def test_approving_commits_once_under_the_author_is_name(
|
|
repo, db, post, maria, luisa, sign_in):
|
|
"""Attribution is the reason `write_file` takes an author at all. A post
|
|
approved by Luisa but written by María has to say María in git."""
|
|
sign_in(maria)
|
|
propose(post)
|
|
submission_id = only_submission(db)["id"]
|
|
|
|
sign_in(luisa)
|
|
post(f"/comunidad/publicaciones/{submission_id}/aprobar")
|
|
|
|
path, = repo.files
|
|
assert path.startswith("content/post/") and path.endswith("-feria-en-ottakring.es.md")
|
|
assert repo.authors == [{"name": "Maria", "email": "maria@example.com"}]
|
|
row = only_submission(db)
|
|
assert (row["state"], row["published_path"], row["reviewed_by"]) == (
|
|
"approved", path, luisa)
|
|
|
|
|
|
def test_the_approved_file_is_what_the_pipeline_expects(repo, db, post, maria,
|
|
luisa, sign_in):
|
|
"""The same contract the editor's own tests hold it to: a Spanish source
|
|
with no `translated_from`."""
|
|
import yaml
|
|
sign_in(maria)
|
|
propose(post, description="Una feria")
|
|
submission_id = only_submission(db)["id"]
|
|
|
|
sign_in(luisa)
|
|
post(f"/comunidad/publicaciones/{submission_id}/aprobar")
|
|
|
|
path, = repo.files
|
|
text = repo.files[path].decode("utf-8")
|
|
front = yaml.safe_load(text.split("---")[1])
|
|
assert front["lang"] == "es"
|
|
assert front["manual_translation"] is False
|
|
assert "categories" not in front
|
|
assert front["description"] == "Una feria"
|
|
assert "translated_from" not in front
|
|
assert "empanadas" in text
|
|
|
|
|
|
def test_approving_twice_publishes_once(repo, db, post, maria, luisa, sign_in):
|
|
sign_in(maria)
|
|
propose(post)
|
|
submission_id = only_submission(db)["id"]
|
|
|
|
sign_in(luisa)
|
|
post(f"/comunidad/publicaciones/{submission_id}/aprobar")
|
|
post(f"/comunidad/publicaciones/{submission_id}/aprobar")
|
|
|
|
assert len(repo.commits) == 1
|
|
|
|
|
|
def test_a_picture_is_private_until_approval_and_committed_after(
|
|
app, repo, client, db, post, maria, luisa, make_member, sign_in):
|
|
"""Three things at once, because they are one story: the picture is readable
|
|
by the author and the moderators and nobody else, it is not in the
|
|
repository, and approving puts it there and takes the private copy away."""
|
|
sign_in(maria)
|
|
propose(post, picture=(io.BytesIO(PNG), "feria.png"))
|
|
row = only_submission(db)
|
|
stored = row["photo_name"]
|
|
url = f"/comunidad/publicaciones/foto/{stored}"
|
|
|
|
assert (Path(app.config["UPLOAD_DIR"]) / stored).exists()
|
|
assert client.get(url).status_code == 200 # the author
|
|
sign_in(make_member("curiosa"))
|
|
assert client.get(url).status_code == 404 # anybody else
|
|
sign_in(luisa)
|
|
assert client.get(url).status_code == 200 # a moderator
|
|
assert repo.files == {} # still nothing published
|
|
|
|
post(f"/comunidad/publicaciones/{row['id']}/aprobar")
|
|
|
|
assert any(path.startswith("static/uploads/") for path in repo.files)
|
|
assert only_submission(db)["photo_name"] is None
|
|
assert not (Path(app.config["UPLOAD_DIR"]) / stored).exists()
|
|
assert client.get(url).status_code == 404
|
|
|
|
|
|
def test_a_file_that_is_not_an_image_is_refused(db, post, maria, sign_in):
|
|
sign_in(maria)
|
|
response = propose(post, picture=(io.BytesIO(b"<html>nope</html>"), "foto.png"))
|
|
|
|
assert response.status_code == 400
|
|
assert db.execute("SELECT COUNT(*) AS n FROM submissions").fetchone()["n"] == 0
|
|
|
|
|
|
# --- rejection ------------------------------------------------------------
|
|
|
|
def test_rejecting_needs_a_reason_and_shows_it_to_the_author(
|
|
repo, client, db, post, maria, luisa, sign_in):
|
|
sign_in(maria)
|
|
propose(post)
|
|
submission_id = only_submission(db)["id"]
|
|
|
|
sign_in(luisa)
|
|
post(f"/comunidad/publicaciones/{submission_id}/rechazar", {"note": " "})
|
|
assert only_submission(db)["state"] == "pending" # no reason, no rejection
|
|
|
|
post(f"/comunidad/publicaciones/{submission_id}/rechazar",
|
|
{"note": "Falta decir dónde es."})
|
|
|
|
assert only_submission(db)["state"] == "rejected"
|
|
assert repo.files == {}
|
|
sign_in(maria)
|
|
assert "Falta decir dónde es." in client.get(
|
|
"/comunidad/publicaciones").get_data(as_text=True)
|
|
|
|
|
|
def test_editing_a_rejected_one_puts_it_back_in_the_queue(
|
|
client, db, post, maria, luisa, sign_in):
|
|
"""Otherwise a rewritten post stays marked "devuelto" and never reaches the
|
|
queue again — the author waits for a review that cannot happen."""
|
|
sign_in(maria)
|
|
propose(post)
|
|
submission_id = only_submission(db)["id"]
|
|
sign_in(luisa)
|
|
post(f"/comunidad/publicaciones/{submission_id}/rechazar", {"note": "Falta el dónde."})
|
|
|
|
sign_in(maria)
|
|
post(f"/comunidad/publicaciones/{submission_id}/editar",
|
|
{"title": "Feria en Ottakring", "body": "En la plaza, el sábado."})
|
|
|
|
row = only_submission(db)
|
|
assert (row["state"], row["note"], row["reviewed_by"]) == ("pending", None, None)
|
|
sign_in(luisa)
|
|
assert "Feria en Ottakring" in client.get(
|
|
"/comunidad/publicaciones").get_data(as_text=True)
|
|
|
|
|
|
def test_nobody_else_may_edit_or_withdraw_it(client, db, post, maria,
|
|
luisa, make_member, sign_in):
|
|
"""A moderator approves or returns a submission. Rewriting somebody's words
|
|
and publishing them under their name is not one of the two — the same line
|
|
the board draws between deleting a post and editing it."""
|
|
sign_in(maria)
|
|
propose(post)
|
|
submission_id = only_submission(db)["id"]
|
|
|
|
for other in (luisa, make_member("otro")):
|
|
sign_in(other)
|
|
assert post(f"/comunidad/publicaciones/{submission_id}/editar",
|
|
{"title": "Otra cosa", "body": "Otro texto"}).status_code == 403
|
|
assert post(f"/comunidad/publicaciones/{submission_id}/retirar").status_code == 403
|
|
|
|
|
|
def test_withdrawing_takes_the_picture_with_it(app, db, post, maria, sign_in):
|
|
sign_in(maria)
|
|
propose(post, picture=(io.BytesIO(PNG), "feria.png"))
|
|
row = only_submission(db)
|
|
|
|
post(f"/comunidad/publicaciones/{row['id']}/retirar")
|
|
|
|
assert db.execute("SELECT COUNT(*) AS n FROM submissions").fetchone()["n"] == 0
|
|
assert not (Path(app.config["UPLOAD_DIR"]) / row["photo_name"]).exists()
|
|
|
|
|
|
def test_an_approved_one_cannot_be_edited_or_withdrawn(
|
|
repo, db, post, maria, luisa, sign_in):
|
|
"""It is in the repository now. Changing it there is what the editor is
|
|
for, and withdrawing a row would not unpublish anything."""
|
|
sign_in(maria)
|
|
propose(post)
|
|
submission_id = only_submission(db)["id"]
|
|
sign_in(luisa)
|
|
post(f"/comunidad/publicaciones/{submission_id}/aprobar")
|
|
|
|
sign_in(maria)
|
|
post(f"/comunidad/publicaciones/{submission_id}/editar",
|
|
{"title": "Otra cosa", "body": "Otro texto"})
|
|
post(f"/comunidad/publicaciones/{submission_id}/retirar")
|
|
|
|
row = only_submission(db)
|
|
assert (row["state"], row["title"]) == ("approved", "Feria en Ottakring")
|
|
|
|
|
|
# --- erasure --------------------------------------------------------------
|
|
|
|
def test_erasing_the_author_takes_their_submissions_and_photos(
|
|
app, repo, db, post, superadmin_id, maria, sign_in):
|
|
"""A submission is private writing, so it goes the way private messages do.
|
|
What remains of an approved one is the post in the repository, which carries
|
|
their name in a commit that is not ours to rewrite from here."""
|
|
sign_in(maria)
|
|
propose(post, picture=(io.BytesIO(PNG), "feria.png"))
|
|
stored = only_submission(db)["photo_name"]
|
|
|
|
sign_in(superadmin_id)
|
|
post(f"/comunidad/miembros/{maria}/eliminar")
|
|
|
|
assert db.execute("SELECT COUNT(*) AS n FROM submissions").fetchone()["n"] == 0
|
|
assert not (Path(app.config["UPLOAD_DIR"]) / stored).exists()
|
|
|
|
|
|
def test_erasing_a_reviewer_keeps_the_submission(db, post, superadmin_id, maria,
|
|
make_member, sign_in):
|
|
"""Who reviewed something is a fact about the submission, not personal data
|
|
of the reviewer, so the name goes and the row stays."""
|
|
reviewer = make_member("luisa", role="moderator")
|
|
sign_in(maria)
|
|
propose(post)
|
|
submission_id = only_submission(db)["id"]
|
|
sign_in(reviewer)
|
|
post(f"/comunidad/publicaciones/{submission_id}/rechazar", {"note": "Falta el dónde."})
|
|
|
|
sign_in(superadmin_id)
|
|
post(f"/comunidad/miembros/{reviewer}/eliminar")
|
|
|
|
row = only_submission(db)
|
|
assert (row["state"], row["reviewed_by"], row["note"]) == (
|
|
"rejected", None, "Falta el dónde.")
|
|
|
|
|
|
# --- the two ways approving can fail, told apart --------------------------
|
|
|
|
def test_a_refused_token_is_not_reported_as_a_missing_picture(
|
|
app, db, post, maria, luisa, sign_in, monkeypatch):
|
|
"""The bug in the screenshot. `except OSError` caught PermissionError from
|
|
the git client and blamed the photograph — on a page that was displaying the
|
|
photograph. The message has to name the thing that is actually wrong."""
|
|
sign_in(maria)
|
|
propose(post, picture=(io.BytesIO(PNG), "feria.png"))
|
|
row = only_submission(db)
|
|
|
|
class Refused:
|
|
class Response:
|
|
status_code = 403
|
|
|
|
@staticmethod
|
|
def json():
|
|
return {}
|
|
|
|
def request(self, *args, **kwargs):
|
|
return self.Response()
|
|
|
|
monkeypatch.setattr(gitea, "requests", Refused())
|
|
sign_in(luisa)
|
|
response = post(f"/comunidad/publicaciones/{row['id']}/aprobar",
|
|
follow_redirects=True)
|
|
|
|
body = response.get_data(as_text=True)
|
|
assert "rechazó el token" in body
|
|
assert "imagen en el disco" not in body
|
|
assert only_submission(db)["state"] == "pending" # nothing was recorded
|
|
# And the picture is still there, so a second attempt can succeed.
|
|
assert (Path(app.config["UPLOAD_DIR"]) / row["photo_name"]).exists()
|
|
|
|
|
|
def test_a_genuinely_missing_picture_still_says_so(
|
|
app, repo, db, post, maria, luisa, sign_in):
|
|
sign_in(maria)
|
|
propose(post, picture=(io.BytesIO(PNG), "feria.png"))
|
|
row = only_submission(db)
|
|
(Path(app.config["UPLOAD_DIR"]) / row["photo_name"]).unlink()
|
|
|
|
sign_in(luisa)
|
|
response = post(f"/comunidad/publicaciones/{row['id']}/aprobar",
|
|
follow_redirects=True)
|
|
|
|
assert "imagen en el disco" in response.get_data(as_text=True)
|
|
assert repo.files == {}
|
|
assert only_submission(db)["state"] == "pending"
|
|
|
|
|
|
def test_an_approved_post_is_signed_by_the_member_not_the_moderator(
|
|
repo, db, post, maria, luisa, sign_in):
|
|
"""Both halves of attribution now: the commit names María, and so does the
|
|
page — `author` in the frontmatter, with a link only if she has published
|
|
her profile."""
|
|
import yaml
|
|
db.execute("UPDATE members SET profile_published = 1 WHERE id = ?", (maria,))
|
|
sign_in(maria)
|
|
propose(post)
|
|
submission_id = only_submission(db)["id"]
|
|
|
|
sign_in(luisa)
|
|
post(f"/comunidad/publicaciones/{submission_id}/aprobar")
|
|
|
|
path, = repo.files
|
|
front = yaml.safe_load(repo.files[path].decode().split("---")[1])
|
|
assert front["author"] == "Maria"
|
|
assert front["author_url"] == "/maria"
|
|
assert repo.authors == [{"name": "Maria", "email": "maria@example.com"}]
|
|
|
|
|
|
def test_a_member_can_propose_an_event_and_it_keeps_its_date(
|
|
repo, db, post, maria, luisa, sign_in):
|
|
"""Same form, same queue, same approval — and the event that reaches the
|
|
site is the one the member described, dated when it happens."""
|
|
import yaml
|
|
sign_in(maria)
|
|
propose(post, event_date="2026-11-07",
|
|
event_time="20:00", event_location="Sala Ottakring")
|
|
submission_id = only_submission(db)["id"]
|
|
|
|
sign_in(luisa)
|
|
post(f"/comunidad/publicaciones/{submission_id}/aprobar")
|
|
|
|
path, = repo.files
|
|
front = yaml.safe_load(repo.files[path].decode().split("---")[1])
|
|
assert front["event_date"] == "2026-11-07"
|
|
assert front["event_time"] == "20:00"
|
|
assert front["event_location"] == "Sala Ottakring"
|
|
assert front["author"] == "Maria"
|
|
|
|
|
|
def test_a_proposal_with_a_nonsense_event_date_is_refused(db, post, maria, sign_in):
|
|
"""A proposal with no date at all is just an article, so there is nothing
|
|
left to refuse there — the date is the declaration. A date that is not a
|
|
date still is."""
|
|
sign_in(maria)
|
|
|
|
response = propose(post, event_date="el martes")
|
|
|
|
assert response.status_code == 400
|
|
assert db.execute("SELECT COUNT(*) AS n FROM submissions").fetchone()["n"] == 0
|