Initial commit: 浼佷笟寰俊 AI 鏈哄櫒浜哄姪鐞?MVP
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1
backend/tests/__init__.py
Normal file
1
backend/tests/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
# tests
|
||||
26
backend/tests/test_auth.py
Normal file
26
backend/tests/test_auth.py
Normal 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
|
||||
16
backend/tests/test_health.py
Normal file
16
backend/tests/test_health.py
Normal 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
|
||||
26
backend/tests/test_wecom.py
Normal file
26
backend/tests/test_wecom.py
Normal 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
|
||||
Reference in New Issue
Block a user