88 lines
2.7 KiB
Python
88 lines
2.7 KiB
Python
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
_backend = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(_backend))
|
|
_root = _backend.parent / "Temp" / "pytest"
|
|
_root.mkdir(parents=True, exist_ok=True)
|
|
os.environ["EUNIV_SECRET_KEY"] = "test-secret-key-0123456789abcdef"
|
|
os.environ["EUNIV_DB_PATH"] = str(_root / f"euniv-{os.getpid()}.db")
|
|
os.environ["EUNIV_DISABLE_REFRESH"] = "1"
|
|
os.environ["EUNIV_ADMIN_PASSWORD"] = "test-admin-pass"
|
|
os.environ["EUNIV_COOKIE_SECURE"] = "0"
|
|
os.environ.pop("EUNIV_ADMIN_RESET", None)
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def fresh_database():
|
|
from sqlalchemy import text
|
|
|
|
from admin.settings import ensure_admin_seed, ensure_key_check, load_admin_identity
|
|
from euniv.db import engine, init_db
|
|
from euniv.models import Base
|
|
import admin.models # noqa: F401
|
|
|
|
Base.metadata.drop_all(bind=engine)
|
|
with engine.begin() as connection:
|
|
connection.execute(text("DROP TABLE IF EXISTS alembic_version"))
|
|
init_db()
|
|
ensure_admin_seed()
|
|
ensure_key_check()
|
|
load_admin_identity()
|
|
from admin.login_limit import reset
|
|
|
|
reset()
|
|
from euniv.login_limit import reset as reset_login
|
|
|
|
reset_login()
|
|
from euniv.proxy_pool import _holds
|
|
|
|
_holds.clear()
|
|
from euniv import autostudy as autostudy_mod
|
|
|
|
autostudy_mod._wanted.clear()
|
|
autostudy_mod._running.clear()
|
|
autostudy_mod._retry_at.clear()
|
|
autostudy_mod._retry_count.clear()
|
|
autostudy_mod._done_month.clear()
|
|
autostudy_mod._targets.clear()
|
|
autostudy_mod._paces.clear()
|
|
autostudy_mod._user_halts.clear()
|
|
users = autostudy_mod._status.get("users")
|
|
if isinstance(users, dict):
|
|
users.clear()
|
|
from euniv import encrypt as encrypt_mod
|
|
from euniv import service as service_mod
|
|
from euniv import study_push
|
|
|
|
for pending in list(service_mod._pending.values()) + list(service_mod._pending_qr.values()):
|
|
client = getattr(pending, "client", None)
|
|
if client is not None:
|
|
try:
|
|
client.close()
|
|
except Exception:
|
|
pass
|
|
service_mod._pending.clear()
|
|
service_mod._pending_qr.clear()
|
|
service_mod._pending_busy.clear()
|
|
study_push._links.clear()
|
|
study_push._loop = None
|
|
from euniv import study_control
|
|
|
|
study_control._snaps.clear()
|
|
study_control._snap_marks.clear()
|
|
autostudy_mod.forget_courses()
|
|
with encrypt_mod._lock:
|
|
encrypt_mod._config.clear()
|
|
encrypt_mod._config.update(
|
|
{
|
|
"path": encrypt_mod.DEFAULT_ENCRYPT_PATH,
|
|
"js_url": encrypt_mod.DEFAULT_JS_URL,
|
|
"loaded_at": 0.0,
|
|
"retry_at": 0.0,
|
|
}
|
|
)
|