fix: 修正 websites router 中错误的 website.accounts_endpoint 属性调用为默认值,并增加对已存在账号名称回显与默认端点引用的单元测试
This commit is contained in:
@@ -668,7 +668,7 @@ def import_upstream_keys_as_accounts(
|
|||||||
has_list_accounts = hasattr(c, "list_accounts")
|
has_list_accounts = hasattr(c, "list_accounts")
|
||||||
if has_list_accounts:
|
if has_list_accounts:
|
||||||
try:
|
try:
|
||||||
remote_accounts_list = c.list_accounts(endpoint=website.accounts_endpoint)
|
remote_accounts_list = c.list_accounts()
|
||||||
if remote_accounts_list is not None:
|
if remote_accounts_list is not None:
|
||||||
for acc in remote_accounts_list:
|
for acc in remote_accounts_list:
|
||||||
aid = c.extract_id(acc)
|
aid = c.extract_id(acc)
|
||||||
|
|||||||
@@ -231,3 +231,66 @@ def test_naming_format_on_manual_import(db_session, monkeypatch):
|
|||||||
# 优先使用 group_name,若为空使用 group_id
|
# 优先使用 group_name,若为空使用 group_id
|
||||||
assert acc1["name"] == f"{u.name}-G1-Name-{k1.id}"
|
assert acc1["name"] == f"{u.name}-G1-Name-{k1.id}"
|
||||||
assert acc2["name"] == f"{u.name}-G2-{k2.id}"
|
assert acc2["name"] == f"{u.name}-G2-{k2.id}"
|
||||||
|
|
||||||
|
|
||||||
|
def test_import_already_exists_displays_remote_name_and_uses_default_endpoint(db_session, monkeypatch):
|
||||||
|
"""验证当导入已存在账号时,FakeClient 实现了 list_accounts,断言其使用默认的 /accounts,并返回 exists 状态且 account_name 为远端已有名称。"""
|
||||||
|
w = Website(name="W1", base_url="http://w1", enabled=True, site_type="sub2api", auth_config_json="{}", timeout_seconds=30)
|
||||||
|
u = Upstream(name="U1", base_url="http://u1")
|
||||||
|
db_session.add_all([w, u])
|
||||||
|
db_session.commit()
|
||||||
|
db_session.refresh(w); db_session.refresh(u)
|
||||||
|
|
||||||
|
k1 = UpstreamGeneratedKey(
|
||||||
|
upstream_id=u.id,
|
||||||
|
group_id="G1",
|
||||||
|
group_name="G1-Name",
|
||||||
|
key_name="K1",
|
||||||
|
key_value="V1",
|
||||||
|
imported_website_id=w.id,
|
||||||
|
imported_account_id="existing-remote-id",
|
||||||
|
status="imported"
|
||||||
|
)
|
||||||
|
db_session.add(k1)
|
||||||
|
db_session.commit()
|
||||||
|
|
||||||
|
list_accounts_called_with = []
|
||||||
|
|
||||||
|
class FakeClient:
|
||||||
|
def __init__(self, *args, **kwargs): pass
|
||||||
|
def __enter__(self): return self
|
||||||
|
def __exit__(self, *args): pass
|
||||||
|
def get_groups(self, *args, **kwargs):
|
||||||
|
return []
|
||||||
|
def list_accounts(self, endpoint="/accounts"):
|
||||||
|
list_accounts_called_with.append(endpoint)
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
"id": "existing-remote-id",
|
||||||
|
"name": "My-Existing-Remote-Name",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
def extract_id(self, data):
|
||||||
|
return data.get("id")
|
||||||
|
|
||||||
|
monkeypatch.setattr("app.routers.websites._client", lambda website: FakeClient())
|
||||||
|
monkeypatch.setattr("app.services.website_client.Sub2ApiWebsiteClient", FakeClient)
|
||||||
|
|
||||||
|
req = ImportAccountsRequest(
|
||||||
|
upstream_key_ids=[k1.id],
|
||||||
|
target_group_map={},
|
||||||
|
auto_priority_by_rate=False,
|
||||||
|
priority=10,
|
||||||
|
account_name_prefix="some-prefix",
|
||||||
|
default_platform="openai",
|
||||||
|
)
|
||||||
|
|
||||||
|
response = import_upstream_keys_as_accounts(w.id, req, db_session)
|
||||||
|
|
||||||
|
# 验证 list_accounts 调用使用了默认的 /accounts 端点
|
||||||
|
assert list_accounts_called_with == ["/accounts"]
|
||||||
|
|
||||||
|
# 验证返回 exists 状态,且 account_name 使用了远端已有的名字 "My-Existing-Remote-Name"
|
||||||
|
assert len(response.items) == 1
|
||||||
|
assert response.items[0].status == "exists"
|
||||||
|
assert response.items[0].account_name == "My-Existing-Remote-Name"
|
||||||
|
|||||||
Reference in New Issue
Block a user