5
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
"""Phase 15 访问日志测试。"""
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from app.main import app
|
||||
|
||||
|
||||
def _client() -> TestClient:
|
||||
return TestClient(app, raise_server_exceptions=False)
|
||||
|
||||
|
||||
def test_access_log_recorded() -> None:
|
||||
"""访问公共页面后应有访问日志。"""
|
||||
with _client() as client:
|
||||
# 注册+创建知识库
|
||||
client.post("/api/auth/register", json={"username": "log_user", "email": "log@example.com", "password": "password123"})
|
||||
resp = client.post("/api/knowledge-bases", json={"name": "Log Test"})
|
||||
kb_id = resp.json()["id"]
|
||||
|
||||
# 获取 AI 链接
|
||||
resp = client.get(f"/api/knowledge-bases/{kb_id}/link")
|
||||
token = resp.json()["token"]
|
||||
|
||||
# 访问公共页面
|
||||
client.get(f"/k/{token}")
|
||||
client.get(f"/k/{token}.json")
|
||||
|
||||
# 检查访问日志
|
||||
from app.core.db import get_session_factory
|
||||
from sqlalchemy import text
|
||||
|
||||
factory = get_session_factory()
|
||||
with factory() as session:
|
||||
result = session.execute(text("SELECT COUNT(*) FROM access_logs WHERE knowledge_base_id = :kb_id"), {"kb_id": kb_id})
|
||||
count = result.scalar()
|
||||
assert count >= 2
|
||||
|
||||
|
||||
def test_access_log_with_kb() -> None:
|
||||
"""访问知识库首页应记录访问日志。"""
|
||||
with _client() as client:
|
||||
client.post("/api/auth/register", json={"username": "log_doc", "email": "log_doc@example.com", "password": "password123"})
|
||||
resp = client.post("/api/knowledge-bases", json={"name": "Log Test"})
|
||||
kb_id = resp.json()["id"]
|
||||
|
||||
# 获取 AI 链接
|
||||
resp = client.get(f"/api/knowledge-bases/{kb_id}/link")
|
||||
token = resp.json()["token"]
|
||||
|
||||
# 访问公共页面
|
||||
client.get(f"/k/{token}")
|
||||
|
||||
# 检查访问日志
|
||||
from app.core.db import get_session_factory
|
||||
from sqlalchemy import text
|
||||
|
||||
factory = get_session_factory()
|
||||
with factory() as session:
|
||||
result = session.execute(text("SELECT COUNT(*) FROM access_logs WHERE knowledge_base_id = :kb_id"), {"kb_id": kb_id})
|
||||
count = result.scalar()
|
||||
assert count >= 1
|
||||
Reference in New Issue
Block a user