2026-11-10 23:56:50 +08:00
|
|
|
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):
|
2026-11-14 14:07:26 +08:00
|
|
|
from app.core.public_preflight import require_public_ready
|
|
|
|
|
|
2026-11-10 23:56:50 +08:00
|
|
|
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:
|
2026-11-14 14:07:26 +08:00
|
|
|
from app.core.security import hash_password
|
|
|
|
|
|
2026-11-10 23:56:50 +08:00
|
|
|
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):
|
2026-11-14 14:07:26 +08:00
|
|
|
from app.core.public_preflight import require_public_ready
|
|
|
|
|
|
2026-11-10 23:56:50 +08:00
|
|
|
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):
|
2026-11-14 14:07:26 +08:00
|
|
|
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)
|