mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 06:09:33 +08:00
The identity store had two bugs that combined into a lockout for deployments whose admin comes from auth.json rather than /register. load_users() returned the bootstrap admin only when the store was empty, so writing the first real account dropped it and the operator's login started returning 401. save_user() then decided first-admin promotion from load_users() called with no env arguments, an env-blind view, so with an empty store the first account an admin created from /admin/users was silently promoted to admin even though that endpoint documents role="user". Together: create one student account, the student is the only admin, the operator is locked out. load_users() now merges the bootstrap admin whenever it is configured; a stored record with the same username still wins, so the account can be adopted into the store. The merge stays in memory because no write path passes the env arguments, so a rotation of auth.json is never shadowed by a stale persisted hash. save_user() counts a configured bootstrap admin as an existing account, except when saving that same username, which is an adoption and keeps the admin role. The genuinely-empty deployment is unchanged: with no bootstrap credentials the first /register user is still promoted. A username configured with an empty hash cannot authenticate and so does not count, which matters because that is the shipped default. Fixes #849
128 lines
4.3 KiB
Python
128 lines
4.3 KiB
Python
"""Shared fixtures for the multi_user test suite.
|
|
|
|
These fixtures isolate each test under ``tmp_path`` so we never read or write
|
|
the developer's real ``data/`` or ``multi-user/`` directories. They also
|
|
provide a context manager that pushes a ``CurrentUser`` onto the contextvar
|
|
for tests that need to call user-scoped code without going through HTTP.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from contextlib import contextmanager
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from deeptutor.multi_user.context import reset_current_user, set_current_user
|
|
from deeptutor.multi_user.models import CurrentUser, UserScope
|
|
|
|
|
|
@pytest.fixture
|
|
def mu_isolated_root(tmp_path, monkeypatch) -> Path:
|
|
"""Redirect every ``multi_user`` global path under ``tmp_path``.
|
|
|
|
Also clears the ``_path_services`` cache so ``get_path_service()`` can be
|
|
re-resolved per test without leaking instances created in earlier tests.
|
|
"""
|
|
from deeptutor.multi_user import grants, identity, paths
|
|
|
|
project_root = tmp_path
|
|
admin_root = (project_root / "data").resolve()
|
|
users_root = admin_root / "users"
|
|
system_root = admin_root / "system"
|
|
|
|
monkeypatch.setattr(paths, "PROJECT_ROOT", project_root)
|
|
monkeypatch.setattr(paths, "USERS_ROOT", users_root)
|
|
monkeypatch.setattr(paths, "SYSTEM_ROOT", system_root)
|
|
monkeypatch.setattr(paths, "ADMIN_WORKSPACE_ROOT", admin_root)
|
|
monkeypatch.setattr(paths, "LEGACY_MULTI_USER_ROOT", project_root / "multi-user")
|
|
monkeypatch.setattr(paths, "_path_services", {})
|
|
|
|
monkeypatch.setattr(identity, "PROJECT_ROOT", project_root)
|
|
monkeypatch.setattr(identity, "SYSTEM_ROOT", system_root)
|
|
monkeypatch.setattr(identity, "AUTH_DIR", system_root / "auth")
|
|
monkeypatch.setattr(identity, "USERS_FILE", system_root / "auth" / "users.json")
|
|
monkeypatch.setattr(identity, "SECRET_FILE", system_root / "auth" / "auth_secret")
|
|
monkeypatch.setattr(
|
|
identity,
|
|
"LEGACY_USERS_FILE",
|
|
project_root / "data" / "user" / "auth_users.json",
|
|
)
|
|
monkeypatch.setattr(
|
|
identity,
|
|
"LEGACY_SECRET_FILE",
|
|
project_root / "data" / "user" / "auth_secret",
|
|
)
|
|
|
|
monkeypatch.setattr(grants, "GRANTS_DIR", system_root / "grants")
|
|
|
|
# The ``auth.json`` bootstrap admin is process-global state rather than a
|
|
# path, and it now takes part in the first-user promotion decision (#849).
|
|
# Clear it so a developer with real credentials configured sees the same
|
|
# results as CI; tests that need one patch these back explicitly.
|
|
from deeptutor.services import auth as auth_service
|
|
|
|
monkeypatch.setattr(auth_service, "AUTH_USERNAME", "")
|
|
monkeypatch.setattr(auth_service, "AUTH_PASSWORD_HASH", "")
|
|
|
|
admin_root.mkdir(parents=True, exist_ok=True)
|
|
return tmp_path
|
|
|
|
|
|
@pytest.fixture
|
|
def make_user(mu_isolated_root):
|
|
"""Build a ``CurrentUser`` rooted under the isolated tmp_path."""
|
|
|
|
def _make(uid: str, *, role: str = "user", username: str | None = None) -> CurrentUser:
|
|
from deeptutor.multi_user.paths import admin_scope
|
|
|
|
if role == "admin":
|
|
scope = admin_scope()
|
|
else:
|
|
scope = UserScope(
|
|
kind="user",
|
|
user_id=uid,
|
|
root=(mu_isolated_root / "data" / "users" / uid).resolve(),
|
|
)
|
|
return CurrentUser(
|
|
id=uid,
|
|
username=username or uid,
|
|
role=role,
|
|
scope=scope,
|
|
)
|
|
|
|
return _make
|
|
|
|
|
|
@pytest.fixture
|
|
def as_user(make_user):
|
|
"""Context manager that pushes a CurrentUser onto the contextvar.
|
|
|
|
Usage:
|
|
with as_user("u_alice", role="user"):
|
|
...
|
|
"""
|
|
|
|
@contextmanager
|
|
def _scope(uid: str, *, role: str = "user", username: str | None = None):
|
|
token = set_current_user(make_user(uid, role=role, username=username))
|
|
try:
|
|
yield
|
|
finally:
|
|
reset_current_user(token)
|
|
|
|
return _scope
|
|
|
|
|
|
@pytest.fixture
|
|
def seed_user(mu_isolated_root):
|
|
"""Create a user record on disk and return the resulting record dict."""
|
|
|
|
def _seed(username: str, password: str = "password1234", role: str = "user") -> dict:
|
|
from deeptutor.multi_user.identity import save_user
|
|
from deeptutor.services.auth import hash_password
|
|
|
|
return save_user(username, hash_password(password), role=role) # type: ignore[arg-type]
|
|
|
|
return _seed
|