fix: Sub2ApiWebsiteClient.list_accounts 支持分页拉取,避免只拿到默认第一页 20 条导致误建重复账号

This commit is contained in:
SmartUp Developer
2026-07-02 11:04:46 +08:00
parent fe3f9f4a4c
commit 00af34c487
2 changed files with 131 additions and 11 deletions
+54
View File
@@ -272,3 +272,57 @@ def test_get_groups_all_404_no_raw_url(monkeypatch):
assert "接口不存在" in msg
assert "http://" not in msg
assert "MDN" not in msg
def test_list_accounts_pagination():
from app.services.website_client import Sub2ApiWebsiteClient
requests_called = []
def handler(req: httpx.Request) -> httpx.Response:
requests_called.append(req)
url_str = str(req.url)
if "page=1" in url_str:
return httpx.Response(200, json={
"total": 3,
"page": 1,
"page_size": 2,
"pages": 2,
"data": [
{"id": "a1", "name": "acc1"},
{"id": "a2", "name": "acc2"},
]
}, request=req)
elif "page=2" in url_str:
return httpx.Response(200, json={
"total": 3,
"page": 2,
"page_size": 2,
"pages": 2,
"data": [
{"id": "a3", "name": "acc3"},
]
}, request=req)
return httpx.Response(404, json={"error": "Not Found"}, request=req)
client = Sub2ApiWebsiteClient(
base_url="https://target.example",
api_prefix="/api/v1/admin",
auth_type="api_key",
auth_config={"key": "admin-key"},
)
client._client = httpx.Client(transport=httpx.MockTransport(handler))
try:
accounts = client.list_accounts("/accounts")
assert accounts == [
{"id": "a1", "name": "acc1"},
{"id": "a2", "name": "acc2"},
{"id": "a3", "name": "acc3"},
]
assert len(requests_called) == 2
assert "page_size=100" in str(requests_called[0].url)
assert "page=1" in str(requests_called[0].url)
assert "page=2" in str(requests_called[1].url)
finally:
client.close()