Initial commit: 浼佷笟寰俊 AI 鏈哄櫒浜哄姪鐞?MVP

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
bujie9527
2026-02-05 16:36:32 +08:00
commit 59275ed4dc
126 changed files with 9120 additions and 0 deletions

View File

@@ -0,0 +1 @@
# tests

View File

@@ -0,0 +1,26 @@
"""登录接口测试。"""
import pytest
from fastapi.testclient import TestClient
from app.main import app
client = TestClient(app)
def test_login_fail_wrong_password():
r = client.post("/api/auth/login", json={"username": "admin", "password": "wrong"})
assert r.status_code == 401
def test_login_fail_wrong_user():
r = client.post("/api/auth/login", json={"username": "nobody", "password": "admin"})
assert r.status_code == 401
def test_login_returns_json():
"""无 DB 时可能 401有 DB 且 admin 存在时 200。仅断言响应为 JSON 且含 code。"""
r = client.post("/api/auth/login", json={"username": "admin", "password": "admin"})
assert r.headers.get("content-type", "").startswith("application/json")
data = r.json()
assert "code" in data
assert "trace_id" in data

View File

@@ -0,0 +1,16 @@
"""Health 接口测试。"""
import pytest
from fastapi.testclient import TestClient
from app.main import app
client = TestClient(app)
def test_health():
r = client.get("/api/health")
assert r.status_code == 200
data = r.json()
assert data.get("code") == 0
assert data.get("data", {}).get("status") == "up"
assert "trace_id" in data

View File

@@ -0,0 +1,26 @@
"""企业微信回调验签逻辑测试(不依赖真实 Token/Key"""
import pytest
from unittest.mock import patch
from app.services.wecom_crypto import (
verify_signature,
verify_and_decrypt_echostr,
_sha1,
)
def test_sha1():
h = _sha1("abc")
assert len(h) == 40
assert h == "a9993e364706816aba3e25717850c26c9cd0d89d"
def test_verify_signature():
# 用固定 token 时,签名为 sha1(sort(token, ts, nonce, encrypt))
with patch("app.services.wecom_crypto.settings") as s:
s.wecom_token = "mytoken"
lst = ["mytoken", "123", "456", "echostr"]
lst.sort()
expected = _sha1("".join(lst))
assert verify_signature(expected, "123", "456", "echostr") is True
assert verify_signature("wrong", "123", "456", "echostr") is False