This commit is contained in:
amb
2026-09-01 14:49:07 +08:00
parent 4db2d1a411
commit 70ece67910
8 changed files with 495 additions and 9 deletions
+61
View File
@@ -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
+77
View File
@@ -0,0 +1,77 @@
"""Phase 15 分类测试。"""
from fastapi.testclient import TestClient
from app.main import app
def _client() -> TestClient:
return TestClient(app, raise_server_exceptions=False)
def _setup(client: TestClient) -> str:
"""注册+创建知识库,返回 kb_id。"""
client.post("/api/auth/register", json={
"username": "cat_user",
"email": "cat@example.com",
"password": "password123",
})
resp = client.post("/api/knowledge-bases", json={"name": "Cat Test KB"})
return resp.json()["id"]
def test_create_category() -> None:
with _client() as client:
kb_id = _setup(client)
resp = client.post(f"/api/knowledge-bases/{kb_id}/categories", json={
"name": "技术文档",
"sort_order": 1,
})
assert resp.status_code == 201
assert resp.json()["name"] == "技术文档"
assert resp.json()["sort_order"] == 1
def test_list_categories() -> None:
with _client() as client:
kb_id = _setup(client)
client.post(f"/api/knowledge-bases/{kb_id}/categories", json={"name": "A", "sort_order": 2})
client.post(f"/api/knowledge-bases/{kb_id}/categories", json={"name": "B", "sort_order": 1})
resp = client.get(f"/api/knowledge-bases/{kb_id}/categories")
assert resp.status_code == 200
cats = resp.json()
assert len(cats) == 2
# 按 sort_order 排序
assert cats[0]["name"] == "B"
assert cats[1]["name"] == "A"
def test_update_category() -> None:
with _client() as client:
kb_id = _setup(client)
resp = client.post(f"/api/knowledge-bases/{kb_id}/categories", json={"name": "Old"})
cat_id = resp.json()["id"]
resp = client.put(f"/api/knowledge-bases/{kb_id}/categories/{cat_id}", json={"name": "New"})
assert resp.status_code == 200
assert resp.json()["name"] == "New"
def test_delete_category() -> None:
with _client() as client:
kb_id = _setup(client)
resp = client.post(f"/api/knowledge-bases/{kb_id}/categories", json={"name": "To Delete"})
cat_id = resp.json()["id"]
resp = client.delete(f"/api/knowledge-bases/{kb_id}/categories/{cat_id}")
assert resp.status_code == 204
def test_category_idor() -> None:
"""用户A 不能操作用户B 的分类。"""
with _client() as client:
kb_id = _setup(client)
resp = client.post(f"/api/knowledge-bases/{kb_id}/categories", json={"name": "A's Cat"})
cat_id = resp.json()["id"]
# 用户 B 登录
client.post("/api/auth/register", json={"username": "cat_b", "email": "cat_b@example.com", "password": "password123"})
resp = client.get(f"/api/knowledge-bases/{kb_id}/categories")
assert resp.status_code == 404