mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
Importing any handler.auth submodule ran the package __init__, which built the auth handlers and so loaded authlib, joserfc and Socket.IO. models.user imports handler.auth.constants, so every process that touched the models paid for the whole auth stack. The handler instances now live next to their classes in handler.auth.base_handler. utils.context imported fastapi only for annotations; that import now sits under TYPE_CHECKING. An idle RQ worker drops from about 118 MB to 89 MB RSS. Each job's forked work horse now imports a little more on its own, about 0.2s on an x86 host. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Gh1SRMfuK7BZmf6YxMqSyT
106 lines
3.4 KiB
Python
106 lines
3.4 KiB
Python
"""Seed (or reset) the fixture users the Playwright e2e suite logs in as.
|
|
|
|
Idempotent: re-running resets the passwords and group assignment rather than
|
|
erroring on the existing rows. Pass ``--remove`` to delete the fixtures again.
|
|
|
|
uv run python .github/scripts/seed_e2e_users.py
|
|
uv run python .github/scripts/seed_e2e_users.py --remove
|
|
|
|
Two accounts, matching the two sides of every permission assertion:
|
|
|
|
* ``e2e_admin``: role admin, so `useCan` short-circuits to true and every
|
|
gated affordance must be present.
|
|
* ``e2e_viewer``: the seeded Viewer group, i.e. library read plus
|
|
own collections/assets. Every ROM write affordance must be absent.
|
|
|
|
Never point this at a real library: it writes users with a known password.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import os
|
|
import sys
|
|
|
|
# The app package lives in backend/; add it to the path so these can import
|
|
# `handler.*` / `models.*` the same way the backend does.
|
|
sys.path.insert(
|
|
0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "backend"))
|
|
)
|
|
|
|
from handler.auth.base_handler import auth_handler # noqa: E402
|
|
from handler.database import db_permission_handler, db_user_handler # noqa: E402
|
|
from models.permission import SystemGroupKey # noqa: E402
|
|
from models.user import Role, User # noqa: E402
|
|
|
|
E2E_ADMIN_USERNAME = "e2e_admin"
|
|
E2E_VIEWER_USERNAME = "e2e_viewer"
|
|
# Fixture-only credential for a throwaway dev instance
|
|
PASSWORD = os.environ.get("E2E_PASSWORD", "e2e-Passw0rd!") # nosec B105
|
|
|
|
|
|
def _viewer_group_id() -> int:
|
|
group = db_permission_handler.get_system_group(SystemGroupKey.VIEWER)
|
|
if group is None:
|
|
raise SystemExit("No Viewer group found, run `alembic upgrade head` first.")
|
|
return group.id
|
|
|
|
|
|
def _upsert(username: str, role: Role, permission_group_id: int | None) -> None:
|
|
hashed = auth_handler.get_password_hash(PASSWORD)
|
|
existing = db_user_handler.get_user_by_username(username)
|
|
if existing:
|
|
db_user_handler.update_user(
|
|
existing.id,
|
|
{
|
|
"hashed_password": hashed,
|
|
"role": role,
|
|
"enabled": True,
|
|
"permission_group_id": permission_group_id,
|
|
},
|
|
)
|
|
print(f"reset {username} (id={existing.id}, role={role})")
|
|
return
|
|
|
|
user = db_user_handler.add_user(
|
|
User(
|
|
username=username,
|
|
hashed_password=hashed,
|
|
email=f"{username}@example.invalid",
|
|
role=role,
|
|
enabled=True,
|
|
permission_group_id=permission_group_id,
|
|
)
|
|
)
|
|
print(f"create {username} (id={user.id}, role={role})")
|
|
|
|
|
|
def _remove() -> None:
|
|
for username in (E2E_ADMIN_USERNAME, E2E_VIEWER_USERNAME):
|
|
user = db_user_handler.get_user_by_username(username)
|
|
if user:
|
|
db_user_handler.delete_user(user.id)
|
|
print(f"delete {username} (id={user.id})")
|
|
else:
|
|
print(f"absent {username}")
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument(
|
|
"--remove", action="store_true", help="delete the fixture users instead"
|
|
)
|
|
args = parser.parse_args()
|
|
|
|
if args.remove:
|
|
_remove()
|
|
return 0
|
|
|
|
_upsert(E2E_ADMIN_USERNAME, Role.ADMIN, None)
|
|
_upsert(E2E_VIEWER_USERNAME, Role.USER, _viewer_group_id())
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|