From 1ef6188c027146f5e8cf6324dfa5b37f745cb0a1 Mon Sep 17 00:00:00 2001 From: liumangmang Date: Wed, 1 Jul 2026 21:15:55 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=AD=A3=20websites=20router?= =?UTF-8?q?=20=E4=B8=AD=E9=94=99=E8=AF=AF=E7=9A=84=20website.accounts=5Fen?= =?UTF-8?q?dpoint=20=E5=B1=9E=E6=80=A7=E8=B0=83=E7=94=A8=E4=B8=BA=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E5=80=BC=EF=BC=8C=E5=B9=B6=E5=A2=9E=E5=8A=A0=E5=AF=B9?= =?UTF-8?q?=E5=B7=B2=E5=AD=98=E5=9C=A8=E8=B4=A6=E5=8F=B7=E5=90=8D=E7=A7=B0?= =?UTF-8?q?=E5=9B=9E=E6=98=BE=E4=B8=8E=E9=BB=98=E8=AE=A4=E7=AB=AF=E7=82=B9?= =?UTF-8?q?=E5=BC=95=E7=94=A8=E7=9A=84=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/routers/websites.py | 2 +- backend/test_priority_algorithm_correction.py | 63 +++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/backend/app/routers/websites.py b/backend/app/routers/websites.py index 7891244..1c9e6fc 100644 --- a/backend/app/routers/websites.py +++ b/backend/app/routers/websites.py @@ -668,7 +668,7 @@ def import_upstream_keys_as_accounts( has_list_accounts = hasattr(c, "list_accounts") if has_list_accounts: try: - remote_accounts_list = c.list_accounts(endpoint=website.accounts_endpoint) + remote_accounts_list = c.list_accounts() if remote_accounts_list is not None: for acc in remote_accounts_list: aid = c.extract_id(acc) diff --git a/backend/test_priority_algorithm_correction.py b/backend/test_priority_algorithm_correction.py index e54fb74..e5c001a 100644 --- a/backend/test_priority_algorithm_correction.py +++ b/backend/test_priority_algorithm_correction.py @@ -231,3 +231,66 @@ def test_naming_format_on_manual_import(db_session, monkeypatch): # 优先使用 group_name,若为空使用 group_id assert acc1["name"] == f"{u.name}-G1-Name-{k1.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"