import pytest from sqlalchemy import select from app.models import User from conftest import PASSWORD async def test_public_preflight_refuses_insecure_cookie_and_legacy_default(system): from app.core.public_preflight import require_public_ready settings = system.settings settings.public_access = True with pytest.raises(ValueError, match="COOKIE_SECURE"): await require_public_ready(system.app.state.db, settings) settings.cookie_secure = True async with system.app.state.db.session() as session: from app.core.security import hash_password user = await session.scalar(select(User).where(User.username == "admin")) user.must_change_password = False # Legacy users predate this flag. user.password_hash = hash_password("123456") await session.commit() settings.admin_password = "" # Clearing bootstrap env must not bypass it. with pytest.raises(ValueError, match="administrator password"): await require_public_ready(system.app.state.db, settings) async def test_public_preflight_allows_changed_admin_and_private_initial_login(system): from app.core.public_preflight import require_public_ready await require_public_ready(system.app.state.db, system.settings) system.settings.public_access = True system.settings.cookie_secure = True system.settings.admin_password = "123456" async with system.app.state.db.session() as session: user = await session.scalar(select(User).where(User.username == "admin")) user.must_change_password = False await session.commit() await require_public_ready(system.app.state.db, system.settings) async def test_public_mode_refuses_mysql_root_but_private_mode_remains_usable(system): from app.core.public_preflight import require_public_ready system.settings.cookie_secure = True system.settings.database_url = "mysql+aiomysql://root:unused@isolated/cmvr-visual" await require_public_ready(system.app.state.db, system.settings) system.settings.public_access = True with pytest.raises(ValueError, match="dedicated MySQL"): await require_public_ready(system.app.state.db, system.settings)