vienalatina/apps/board/tests/test_board.py
Claude ff115c1d6b
A post can say that a machine wrote it
A switch beside the button that publishes — on the editor, on a member's
proposal and on a wall thread — and a pill next to the date wherever the
post is read: the article, the cards in the listings, the thread, the
wall's list, and the moderator's review screen. Next to the date rather
than under the text, because a disclosure somebody meets after reading
the thing is not a disclosure.

Where it lives follows what the thing is. A public post carries
`ai_generated: true` in its own frontmatter, written only when true —
the file in the repository is the record, and `ai_generated: false` on
every post ever written is noise in git and a question in the mind of
whoever reads one. A thread carries a column. A proposal carries a
column too, and that is the one place it has to be moved by hand: a
proposal is a draft in SQLite until it is approved, so the frontmatter
it will end up in does not exist while the decision is being made, and
losing it there would lose it exactly when the text stops being private.

`content.edit` loads the flag back into the form, for the same reason
the byline is carried through: a field read but not written back is a
field that one corrected typo silently removes.

The pipeline needs nothing. translate.py copies the whole frontmatter
and rewrites only TRANSLATED_KEYS, so the flag reaches the German and
Portuguese siblings by itself — right rather than lucky, since a
translated post was still machine-written. Asserted against the real
list, so a key quietly added to it fails the suite.

Migration 9: three plain ADD COLUMNs, tested against a database with a
thread, a proposal and a cached post already in it.

The three words are in partials/t.html with the theme's other labels.
This is also the shape the EU AI Act's Article 50 asks for, which is
worth knowing for whoever installs this for a second organisation.

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

186 lines
7.9 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 "&lt;script&gt;" 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
# --- the preview behind the Vista previa button ---------------------------
#
# A round trip rather than a markdown parser in the browser: the preview has to
# agree with what gets published, and the only way to guarantee that is to
# render it with the same function the board and the site use.
def test_the_preview_renders_the_same_markdown_the_board_does(post, make_member, sign_in):
sign_in(make_member("maria"))
response = post("/comunidad/previsualizar", {"body": "**hola** y _adiós_"})
assert response.status_code == 200
assert response.get_json()["html"] == "<p><strong>hola</strong> y <em>adiós</em></p>\n"
def test_the_preview_drops_html_exactly_as_the_board_does(post, make_member, sign_in):
"""`html=False` in render.py is the whole XSS defence. A preview that
rendered raw HTML would be a hole in the members area and a lie about what
posting is going to do."""
sign_in(make_member("maria"))
html = post("/comunidad/previsualizar",
{"body": "<script>alert(1)</script>"}).get_json()["html"]
assert "<script>" not in html
def test_the_preview_needs_a_session(client):
assert client.post("/comunidad/previsualizar", data={"body": "hola"}).status_code in (302, 400)
def test_the_preview_is_never_cached(post, make_member, sign_in):
sign_in(make_member("maria"))
response = post("/comunidad/previsualizar", {"body": "hola"})
assert response.headers["Cache-Control"] == "no-store"
# --- said out loud on the wall too ----------------------------------------
def test_a_thread_can_say_a_machine_wrote_it(client, post, make_member, sign_in):
"""The same disclosure inside the members area as on the public site. The
people here know each other, which makes it more useful rather than less:
nobody wants to answer a machine thinking they are answering a neighbour.
"""
sign_in(make_member("maria"))
post("/comunidad/nuevo", {"title": "Resumen de la reunión",
"body": "Lo que se decidió.", "ai_generated": "on"})
page = client.get("/comunidad/muro").get_data(as_text=True)
assert "Generado con IA" in page
assert "Generado con IA" in client.get("/comunidad/tema/1").get_data(as_text=True)
def test_an_ordinary_thread_says_nothing_of_the_kind(client, post, make_member, sign_in):
sign_in(make_member("maria"))
post("/comunidad/nuevo", {"title": "Resumen", "body": "Escrito a mano."})
assert "Generado con IA" not in client.get("/comunidad/muro").get_data(as_text=True)
def test_editing_a_thread_can_add_or_remove_the_disclosure(client, post, make_member, sign_in):
"""It is the author's statement about their own text, so it stays theirs
to change — and an edit that forgets the field must not silently keep a
mark the author has taken off."""
member = make_member("maria")
sign_in(member)
post("/comunidad/nuevo", {"title": "Resumen", "body": "Primera versión.",
"ai_generated": "on"})
post("/comunidad/tema/1/editar", {"title": "Resumen", "body": "Reescrito a mano."})
assert "Generado con IA" not in client.get("/comunidad/tema/1").get_data(as_text=True)
post("/comunidad/tema/1/editar", {"title": "Resumen", "body": "Otra vez con ayuda.",
"ai_generated": "on"})
assert "Generado con IA" in client.get("/comunidad/tema/1").get_data(as_text=True)