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
167 lines
5.5 KiB
Python
167 lines
5.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 hashlib
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[3]))
|
|
|
|
from apps.board import gitea # noqa: E402
|
|
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",
|
|
"ADMIN_TOKEN": "admintoken",
|
|
"UPLOAD_DIR": str(tmp_path / "uploads"),
|
|
"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 superadmin_id(db):
|
|
"""The account BOARD_OWNER seeds: the superadministrator, who runs the
|
|
platform and passes every check below them."""
|
|
return db.execute(
|
|
"SELECT id FROM members WHERE role = 'superadmin'").fetchone()["id"]
|
|
|
|
|
|
@pytest.fixture
|
|
def owner_id(db):
|
|
"""A responsable — the association's chair, which is empty on a fresh
|
|
install. Minted here because the two roles are deliberately different
|
|
people, and a test that means one should not reach for the other."""
|
|
return db.execute(
|
|
"""INSERT INTO members (gitea_login, display_name, email, role)
|
|
VALUES ('presidenta', 'Presidenta', 'presidenta@example.com', 'owner')"""
|
|
).lastrowid
|
|
|
|
|
|
@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):
|
|
# A real anonymous visitor gets a CSRF token when the form renders, so
|
|
# signed-out pages (invitations, password recovery) need one here too —
|
|
# otherwise every such test fails on the hook rather than on its subject.
|
|
with client.session_transaction() as session:
|
|
session.setdefault("csrf", "token-for-tests")
|
|
payload = dict(data or {})
|
|
payload.setdefault("csrf_token", "token-for-tests")
|
|
return client.post(url, data=payload, **kwargs)
|
|
return _post
|
|
|
|
|
|
# --- the git server, as a dictionary --------------------------------------
|
|
#
|
|
# Shared because two features commit to the content repository now: the editor
|
|
# (test_content.py) and the brand (test_brand.py). What matters in both is the
|
|
# payload — which files, with which bytes, under whose name — and `requests` can
|
|
# be trusted to do HTTP.
|
|
|
|
class FakeRepo:
|
|
"""A repository in a dict, with shas that change when content does."""
|
|
|
|
def __init__(self):
|
|
self.files: dict[str, bytes] = {}
|
|
self.commits: list[str] = []
|
|
self.authors: list[dict] = []
|
|
self.reads = 0
|
|
|
|
@staticmethod
|
|
def _sha(data: bytes) -> str:
|
|
return hashlib.sha1(data).hexdigest()
|
|
|
|
def list_directory(self, path, token=None):
|
|
out = []
|
|
for name, data in self.files.items():
|
|
if name.startswith(path + "/") and "/" not in name[len(path) + 1:]:
|
|
out.append({"name": name.rsplit("/", 1)[1], "path": name,
|
|
"type": "file", "sha": self._sha(data)})
|
|
return out
|
|
|
|
def read_file(self, path, token=None):
|
|
self.reads += 1
|
|
if path not in self.files:
|
|
raise gitea.GiteaError("Ese archivo ya no existe.")
|
|
return self.files[path].decode("utf-8"), self._sha(self.files[path])
|
|
|
|
def write_file(self, path, data, message, token=None, sha=None, member=None):
|
|
if sha and self.files.get(path) is not None and self._sha(self.files[path]) != sha:
|
|
raise gitea.StaleFile("Alguien más guardó este archivo mientras lo editabas.")
|
|
self.files[path] = data
|
|
self.commits.append(message)
|
|
self.authors.append(gitea._identity(member))
|
|
return self._sha(data)
|
|
|
|
def delete_file(self, path, sha, message, token=None, member=None):
|
|
self.files.pop(path, None)
|
|
self.commits.append(message)
|
|
|
|
|
|
@pytest.fixture
|
|
def repo(monkeypatch):
|
|
fake = FakeRepo()
|
|
for name in ("list_directory", "read_file", "write_file", "delete_file"):
|
|
monkeypatch.setattr(gitea, name, getattr(fake, name))
|
|
return fake
|
|
|
|
|