vienalatina/apps/board/tests/test_submissions.py
Pablo Volenski c2154b6f06
A phone can use this site, and the way in is a button
Your screenshot had no navigation at all: brand, three language letters, posts.
The cause is one missing button, and the CSS has been waiting for it since the
WordPress port. `main.css` hid `.site-bar__pages` below 768px and showed it
again on a class, and styled a `.site-bar__hamburger` — the panel's styles, the
button's styles and the animated ✕ were all ported, and the button itself never
was. Nothing in the theme has ever set that class. So for three phases every
phone saw the site with no menu, with every rule involved correct and every
selector pointing at an element that did not exist.

The toggle is a visually hidden checkbox immediately before the menu, with the ☰
as a `<label>` for it in the bar, and one rule doing the work. No JavaScript.
Not `<details>`, which is this project's usual answer and does not work here: on
desktop the menu has to be open and horizontal, and current Chrome hides a
closed `<details>`'s content with `content-visibility` on `::details-content`,
which a `display` rule on the child no longer overrides. A checkbox behaves the
same in every engine, keeps its place in the tab order and keeps its label.

`test_templates.py` now fails if the stylesheet hides the menu behind a state
the header does not carry — the shape of bug this suite keeps meeting, and the
first time it is guarded rather than found by looking.

**Comunidad is a button in the bar** at every width, brand-coloured through the
tokens Marca writes, instead of a word in a menu that on a phone would not open.
Its three translations moved from the `menus.main` blocks into `t.html`: the URL
is the same in every language, so the menu entry was only carrying a label. The
language switcher is rendered twice, in the bar and at the foot of the panel —
a brand, a button, a ☰ and three language letters do not fit across 360px and
CSS cannot move an element between the two.

**Categories and tags are gone**, both taxonomies. Nothing downstream had to
change: the agenda, the event page, the .ics and the private calendar already
keyed on `event_date` rather than on the category, which I checked before
removing anything. The category was only a gate in the editor, so the gate goes
too — **a post carrying a date is an event**, with nothing to tick first and no
way left to say "this is an event" and then not say when. Migration 6 drops the
two columns that held the terms.

`taxonomies: {}` rather than no `taxonomies` key: leaving the key out restores
Hugo's own defaults, which are tags and categories, so deleting the block built
the very pages it was meant to remove.

Also here, found while looking at the output rather than assumed: the breadcrumb
schema emitted a subpage's parent and the page itself both at position 2, and
the members area's six-section nav had no wrap and no scroll, so `/comunidad/`
on a phone scrolled sideways. It wraps now — two lines showing all six, where a
swipe strip would have left Calendario and Gestión off the edge with nothing to
say they were there.

Verified by rendering: headless Chromium at 320px, 390px and 1280px, before and
after opening the menu, and the members area at 390px. No horizontal overflow at
any of them. 548 tests pass; the build is clean with the pipeline's own flags.

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

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, owner_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(owner_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, owner_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(owner_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