Files
leaudit-platform-backend/tests/test_qichacha_config_client.py
2026-05-25 09:50:01 +08:00

67 lines
2.6 KiB
Python

"""企查查配置与客户端测试。"""
from __future__ import annotations
import hashlib
import pytest
from fastapi_admin import config
def test_qichacha_settings_are_exported_from_app_toml():
"""企查查配置应通过 fastapi_admin.config 导出。"""
assert config.QICHACHA_BASE_URL == "https://api.qichacha.com"
assert config.QICHACHA_ENTERPRISE_PATH == "/ECIV4/GetBasicDetailsByName"
assert config.QICHACHA_DISHONESTY_PATH == "/ShixinCheck/GetList"
assert config.QICHACHA_TIMEOUT == 30
assert config.QICHACHA_CACHE_DAYS == 30
def test_qichacha_client_builds_expected_signature_headers(monkeypatch):
"""企查查客户端应按 AppKey + Timespan + SecretKey 生成大写 MD5 Token。"""
from fastapi_modules.fastapi_leaudit.services.impl.qichachaClient import QichachaClient
monkeypatch.setattr("time.time", lambda: 1779433125)
client = QichachaClient(AppKey="app-key", SecretKey="secret-key")
headers = client.BuildHeaders()
expected = hashlib.md5("app-key1779433125secret-key".encode("utf-8")).hexdigest().upper()
assert headers == {"Token": expected, "Timespan": "1779433125"}
@pytest.mark.asyncio
async def test_qichacha_client_parses_enterprise_and_dishonesty(monkeypatch):
"""企查查客户端应分别解析工商与失信接口结果。"""
from fastapi_modules.fastapi_leaudit.services.impl.qichachaClient import QichachaClient
calls: list[tuple[str, dict[str, str]]] = []
async def fake_request(self, Url: str, Params: dict[str, str]) -> dict:
calls.append((Url, Params))
if "GetBasicDetailsByName" in Url:
return {
"Status": "200",
"Message": "成功",
"Result": {"Name": "广州测试有限公司", "CreditCode": "91440000TEST"},
}
return {
"Status": "200",
"Message": "成功",
"Result": {"VerifyResult": 1, "Data": [{"Anno": "案号1"}]},
}
monkeypatch.setattr(QichachaClient, "Request", fake_request)
client = QichachaClient(AppKey="app-key", SecretKey="secret-key")
enterprise = await client.GetEnterpriseInfo("广州测试有限公司")
dishonesty = await client.GetDishonestyInfo("广州测试有限公司")
assert enterprise["Name"] == "广州测试有限公司"
assert enterprise["CreditCode"] == "91440000TEST"
assert dishonesty["VerifyResult"] == 1
assert dishonesty["Data"][0]["Anno"] == "案号1"
assert calls[0][1] == {"key": "app-key", "keyword": "广州测试有限公司"}
assert calls[1][1] == {"key": "app-key", "searchKey": "广州测试有限公司"}