cmvr-visual/backend/tests/test_auth.py
zhanghao 88cf14d794 feat: consolidate story studio and editable automatic production
Restructure backend and frontend modules; add story, library, and editor schema migrations.

Support editable automatic production, manual confirmation modes, account and user lifecycle management, compact container scrolling, consistent forms, and per-task result previews.

Update inference and TTS scheduling, deployment guidance, and regression coverage; remove obsolete standalone pages and temporary acceptance artifacts.
2026-11-14 14:07:26 +08:00

105 lines
4.2 KiB
Python

import httpx
from pathlib import Path
from tempfile import TemporaryDirectory
import pytest
from conftest import PASSWORD
@pytest.fixture
def tmp_path():
with TemporaryDirectory(prefix="cmvr-auth-") as directory:
yield Path(directory)
async def test_login_cookie_csrf_and_revocation(system):
async with httpx.AsyncClient(
transport=httpx.ASGITransport(app=system.app), base_url="http://test"
) as client:
assert (await client.get("/api/auth/me")).status_code == 401
denied = await client.post(
"/api/auth/login", json={"username": "admin", "password": "wrong"}
)
assert denied.status_code == 401
login = await client.post(
"/api/auth/login", json={"username": "admin", "password": PASSWORD}
)
assert login.status_code == 200 and "HttpOnly" in login.headers["set-cookie"]
assert (await client.post("/api/auth/logout")).status_code == 403
assert (
await client.post("/api/auth/logout", headers={"X-CSRF-Token": b"\xc3\xa9"})
).status_code == 403
csrf = login.json()["csrf_token"]
assert (await client.get("/api/auth/me")).json()["csrf_token"] == csrf
cookie = client.cookies.get("cmvr_session")
assert (
await client.post("/api/auth/logout", headers={"X-CSRF-Token": csrf})
).status_code == 204
client.cookies.set("cmvr_session", cookie)
assert (await client.get("/api/auth/me")).status_code == 401
async def test_non_admin_cannot_create_users_and_public_registration_is_absent(other_client):
assert (
await other_client.post("/api/auth/users", json={"username": "bob", "password": PASSWORD})
).status_code == 403
assert (await other_client.post("/api/auth/register", json={})).status_code == 404
async def test_requested_initial_admin_password_is_hashed_and_not_reset_on_restart(tmp_path):
from sqlalchemy import select
from app.core.security import verify_password
from app.core.config import Settings
from app.main import create_app
from app.models import User
from conftest import FakeProvider
settings = Settings(
mode="test",
database_url=f"sqlite+aiosqlite:///{tmp_path / 'admin.sqlite3'}",
workspace_root=tmp_path,
admin_username="admin",
admin_password="123456",
run_coordinator=False,
cookie_secure=False,
)
app = create_app(settings, provider=FakeProvider())
async with app.router.lifespan_context(app):
async with httpx.AsyncClient(
transport=httpx.ASGITransport(app=app), base_url="http://test"
) as client:
login = await client.post(
"/api/auth/login", json={"username": "admin", "password": "123456"}
)
assert login.status_code == 200 and login.json()["user"]["role"] == "admin"
async with app.state.db.session() as session:
user = await session.scalar(select(User).where(User.username == "admin"))
assert user.password_hash != "123456" and verify_password(
"123456", user.password_hash
)
client.headers["X-CSRF-Token"] = login.json()["csrf_token"]
created = await client.post(
"/api/auth/users", json={"username": "ordinary", "password": "123456"}
)
assert created.status_code == 201
denied = await client.post(
"/api/auth/users", json={"username": "too-short", "password": "12345"}
)
assert denied.status_code == 422
settings.admin_password = PASSWORD
restarted = create_app(settings, provider=FakeProvider())
async with restarted.router.lifespan_context(restarted):
async with httpx.AsyncClient(
transport=httpx.ASGITransport(app=restarted), base_url="http://test"
) as client:
assert (
await client.post(
"/api/auth/login", json={"username": "admin", "password": "123456"}
)
).status_code == 200
assert (
await client.post(
"/api/auth/login", json={"username": "admin", "password": PASSWORD}
)
).status_code == 401