Add account auth with websocket login/register and sessions

This commit is contained in:
Jage9
2026-02-24 22:03:10 -05:00
parent 1938f239e6
commit bf3bc90f2a
21 changed files with 1053 additions and 24 deletions

View File

@@ -0,0 +1,52 @@
from __future__ import annotations
from pathlib import Path
import pytest
from app.auth_service import AuthError, AuthService
def make_auth_service(tmp_path: Path) -> AuthService:
return AuthService(
db_path=tmp_path / "chatgrid.db",
token_hash_secret="test-secret",
password_min_length=8,
password_max_length=32,
username_min_length=2,
username_max_length=32,
)
def test_register_and_resume_session(tmp_path: Path) -> None:
service = make_auth_service(tmp_path)
try:
session = service.register("User_One", "password99", email="a@example.com")
assert session.user.username == "user_one"
resumed = service.resume(session.token)
assert resumed.user.id == session.user.id
assert resumed.user.role == "user"
finally:
service.close()
def test_login_rejects_invalid_password(tmp_path: Path) -> None:
service = make_auth_service(tmp_path)
try:
service.register("alpha", "password99")
with pytest.raises(AuthError):
service.login("alpha", "wrong-pass")
finally:
service.close()
def test_bootstrap_admin_once(tmp_path: Path) -> None:
service = make_auth_service(tmp_path)
try:
admin = service.bootstrap_admin("root-admin", "password99", email=None)
assert admin.role == "admin"
with pytest.raises(AuthError):
service.bootstrap_admin("another-admin", "password99")
finally:
service.close()