vienalatina/apps/board/tests/conftest.py
Claude 6ac814475e
Add a members area: roles and an internal board
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
2026-09-22 10:57:18 +00:00

90 lines
2.5 KiB
Python

"""Shared fixtures.
Tests drive the app through Flask's test client rather than poking functions
directly, because most of what is worth checking here is a decision about who
may do what — and that decision is only real once it has survived routing,
the session and the CSRF hook.
Sign-in is faked by writing the session cookie: the OAuth round trip belongs to
Gitea, and the tests that care about it stub the two network calls instead.
"""
from __future__ import annotations
import sys
from pathlib import Path
import pytest
sys.path.insert(0, str(Path(__file__).resolve().parents[3]))
from apps.board.app import create_app # noqa: E402
from apps.board.db import connect # noqa: E402
@pytest.fixture
def app(tmp_path):
application = create_app({
"SECRET_KEY": "test-secret",
"DB_PATH": str(tmp_path / "board.db"),
"OWNER_LOGIN": "owner",
"SESSION_COOKIE_SECURE": False,
"COOLDOWN_SECONDS": 0,
"BASE_URL": "http://localhost",
"OAUTH_CLIENT_ID": "cid",
"OAUTH_CLIENT_SECRET": "secret",
"ADMIN_TOKEN": "admintoken",
"TESTING": True,
})
yield application
@pytest.fixture
def db(app):
conn = connect(app.config["DB_PATH"])
yield conn
conn.close()
@pytest.fixture
def client(app):
return app.test_client()
@pytest.fixture
def make_member(db):
"""Insert a member and return its id. The owner already exists, seeded."""
def _make(login, role="user", active=1):
cursor = db.execute(
"""INSERT INTO members (gitea_login, display_name, email, role, active)
VALUES (?, ?, ?, ?, ?)""",
(login, login.title(), f"{login}@example.com", role, active),
)
return cursor.lastrowid
return _make
@pytest.fixture
def owner_id(db):
return db.execute("SELECT id FROM members WHERE role = 'owner'").fetchone()["id"]
@pytest.fixture
def sign_in(client):
def _sign_in(member_id):
with client.session_transaction() as session:
session["member_id"] = member_id
session["csrf"] = "token-for-tests"
return _sign_in
@pytest.fixture
def post(client):
"""POST with a valid CSRF token, so tests exercise authorisation rather
than repeatedly rediscovering that the CSRF hook works."""
def _post(url, data=None, **kwargs):
payload = dict(data or {})
payload.setdefault("csrf_token", "token-for-tests")
return client.post(url, data=payload, **kwargs)
return _post