Everything on this site so far has been a file built from git. This is the first component that runs code to answer a request and the first whose data git does not hold, so the trade is stated in the README and the backup script is not optional. Roles are owner, admin and user. The owner is seeded once from BOARD_OWNER and cannot be seeded again, because otherwise editing a compose file would be a quieter way to take the top role than asking for it; ownership moves only by transfer, inside the app. There is exactly one owner and a partial unique index enforces it, so the invariant holds even when a handler is wrong. The owner is beyond suspension and demotion by everyone, themselves included. Only the owner makes admins; admins make users. Sign-in goes through Gitea as a confidential OAuth client — the opposite of Decap, which has to be public because it runs in the browser. The rule the whole thing rests on is that a Gitea account is not a membership: entry needs an active row in `members`, or every account on the instance is a member, starting with the translations bot. Admins can delete any post; nobody can edit anyone else's, admins included. Taking a post down is visible to its author. Quietly rewriting it is not, and an admin who could do that could leave a sentence attributed to someone who never wrote it. The plan said admins could do both; this is the one place the implementation departs from it. Markdown renders with raw HTML disabled, which is the entire XSS defence and the reason there is no sanitiser: the renderer emits only its own tags and escapes the rest. The CSP carries no 'unsafe-inline', which makes an inline onsubmit silently inert rather than broken, so the confirmation dialogs live in a static file and a test fails any template that grows an inline handler. GDPR is in scope rather than deferred: erasure removes the member row and moves their authorship to a tombstone so the conversations around them still read, and any member can download their own writing. Verified: 63 checks pass, covering the membership gate, every role predicate, a direct insert of a second owner being refused by the index, atomic ownership transfer, CSRF, an offsite login redirect, script tags rendering as text, soft deletes leaving both listings and exports, and the member screens rendering for each role. Smoke-tested live: headers, both static assets, and the bare /comunidad redirect that the Caddy matcher has to cover. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NizVpJ2dwzCbjCrTLCjeHn
111 lines
4.5 KiB
Python
111 lines
4.5 KiB
Python
"""Threads, comments, and who may touch them."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
|
|
@pytest.fixture
|
|
def thread(db, make_member):
|
|
author = make_member("autora")
|
|
cursor = db.execute(
|
|
"INSERT INTO threads (author_id, title, body_md) VALUES (?, 'Original', 'Cuerpo')",
|
|
(author,),
|
|
)
|
|
return {"id": cursor.lastrowid, "author": author}
|
|
|
|
|
|
def test_a_member_can_post_and_read_it_back(client, post, make_member, sign_in):
|
|
sign_in(make_member("maria"))
|
|
post("/comunidad/nuevo", {"title": "Reunión del jueves", "body": "A las 19h en el café."})
|
|
body = client.get("/comunidad/").get_data(as_text=True)
|
|
assert "Reunión del jueves" in body
|
|
|
|
|
|
def test_markdown_is_rendered_but_html_is_not(client, post, make_member, sign_in):
|
|
sign_in(make_member("maria"))
|
|
post("/comunidad/nuevo", {
|
|
"title": "Prueba",
|
|
"body": "**fuerte** y <script>alert(1)</script>",
|
|
})
|
|
body = client.get("/comunidad/tema/1").get_data(as_text=True)
|
|
assert "<strong>fuerte</strong>" in body
|
|
assert "<script>alert(1)</script>" not in body
|
|
assert "<script>" in body
|
|
|
|
|
|
def test_nobody_edits_someone_elses_words(client, post, thread, make_member, sign_in):
|
|
"""Not even an admin. Removing a post is visible to its author; quietly
|
|
rewriting it is not, which is why moderation here means deletion."""
|
|
sign_in(make_member("admina", role="admin"))
|
|
response = post(f"/comunidad/tema/{thread['id']}/editar",
|
|
{"title": "Reescrito", "body": "Otra cosa"})
|
|
assert response.status_code == 403
|
|
|
|
|
|
def test_a_user_cannot_delete_someone_elses_thread(client, post, thread, make_member, sign_in):
|
|
sign_in(make_member("ajena"))
|
|
assert post(f"/comunidad/tema/{thread['id']}/eliminar").status_code == 403
|
|
|
|
|
|
def test_an_admin_can_delete_any_thread(client, db, post, thread, make_member, sign_in):
|
|
sign_in(make_member("admina", role="admin"))
|
|
post(f"/comunidad/tema/{thread['id']}/eliminar")
|
|
row = db.execute("SELECT deleted_at FROM threads WHERE id = ?", (thread["id"],)).fetchone()
|
|
assert row["deleted_at"] is not None
|
|
|
|
|
|
def test_the_author_can_edit_their_own(client, db, post, thread, sign_in):
|
|
sign_in(thread["author"])
|
|
post(f"/comunidad/tema/{thread['id']}/editar", {"title": "Corregido", "body": "Mejor"})
|
|
row = db.execute("SELECT title, edited_at FROM threads WHERE id = ?",
|
|
(thread["id"],)).fetchone()
|
|
assert row["title"] == "Corregido"
|
|
assert row["edited_at"] is not None
|
|
|
|
|
|
def test_a_deleted_thread_disappears_from_the_list_and_the_page(
|
|
client, db, post, thread, sign_in):
|
|
sign_in(thread["author"])
|
|
post(f"/comunidad/tema/{thread['id']}/eliminar")
|
|
assert "Original" not in client.get("/comunidad/").get_data(as_text=True)
|
|
assert client.get(f"/comunidad/tema/{thread['id']}").status_code == 404
|
|
|
|
|
|
def test_a_deleted_thread_is_left_out_of_the_export(client, db, post, thread, sign_in):
|
|
sign_in(thread["author"])
|
|
post(f"/comunidad/tema/{thread['id']}/eliminar")
|
|
assert "Original" not in client.get("/comunidad/mis-datos").get_data(as_text=True)
|
|
|
|
|
|
def test_a_closed_thread_refuses_replies(client, db, post, thread, make_member, sign_in):
|
|
db.execute("UPDATE threads SET locked = 1 WHERE id = ?", (thread["id"],))
|
|
sign_in(make_member("maria"))
|
|
post(f"/comunidad/tema/{thread['id']}/comentar", {"body": "¿Hola?"})
|
|
count = db.execute("SELECT COUNT(*) AS n FROM comments WHERE thread_id = ?",
|
|
(thread["id"],)).fetchone()["n"]
|
|
assert count == 0
|
|
|
|
|
|
def test_only_admins_pin(client, post, thread, make_member, sign_in):
|
|
sign_in(make_member("maria"))
|
|
assert post(f"/comunidad/tema/{thread['id']}/estado",
|
|
{"field": "pinned", "value": "1"}).status_code == 403
|
|
|
|
|
|
def test_the_state_field_is_not_a_way_into_the_query(client, post, thread, make_member, sign_in):
|
|
"""`field` is interpolated into the UPDATE, so it is checked against a fixed
|
|
pair first. This asserts the check, not the interpolation."""
|
|
sign_in(make_member("admina", role="admin"))
|
|
assert post(f"/comunidad/tema/{thread['id']}/estado",
|
|
{"field": "role", "value": "1"}).status_code == 400
|
|
|
|
|
|
def test_the_cooldown_stops_a_double_submit(app, client, post, make_member, sign_in, db):
|
|
app.config["COOLDOWN_SECONDS"] = 60
|
|
sign_in(make_member("rapida"))
|
|
post("/comunidad/nuevo", {"title": "Primero", "body": "Uno"})
|
|
post("/comunidad/nuevo", {"title": "Segundo", "body": "Dos"})
|
|
count = db.execute("SELECT COUNT(*) AS n FROM threads").fetchone()["n"]
|
|
assert count == 1
|