fix: 平台登录失败后刷新验证码

This commit is contained in:
2026-10-30 12:09:20 +08:00
parent 559548d577
commit 811dc448cb
3 changed files with 51 additions and 11 deletions
+39
View File
@@ -142,6 +142,45 @@ def test_admin_is_judged_locally_when_users_use_nqi(client: TestClient, monkeypa
assert calls == ["open"]
def test_platform_login_failure_refreshes_captcha(client: TestClient, monkeypatch):
from app.nqi.client import LoginChallenge, NqiError
logged = client.post("/api/auth/local", json={"username": "admin", "password": "admin-pass"})
headers = {"Authorization": f"Bearer {logged.json()['token']}"}
saved = client.put(
"/api/admin/settings",
json={"loginMode": "nqi", "credentialRefreshSeconds": 300, "workerConcurrency": 4},
headers=headers,
)
assert saved.status_code == 200
async def open_login(self):
return LoginChallenge(execution="exec", public_key=PUBLIC_KEY, captcha_png=b"old", page_url="https://nqi.example/login")
async def prepare_login(self, challenge, username, password, captcha):
raise NqiError("认证失败2!")
async def refresh_captcha(self, challenge):
challenge.captcha_png = b"new"
challenge.captcha_type = "image/png"
return b"new"
monkeypatch.setattr("app.api.http.NqiClient.open_login", open_login)
monkeypatch.setattr("app.api.http.NqiClient.prepare_login", prepare_login)
monkeypatch.setattr("app.api.http.NqiClient.refresh_captcha", refresh_captcha)
started = client.post("/api/auth/nqi/start", json={"purpose": "login"})
assert started.status_code == 200
failed = client.post(
"/api/auth/login",
json={"username": "platform-user", "password": "secret", "captcha": "abcd", "sessionId": started.json()["sessionId"]},
)
assert failed.status_code == 200
body = failed.json()
assert body["status"] == "need_captcha"
assert body["message"] == "认证失败2!"
assert body["captcha"].startswith("data:image/png;base64,")
def test_pool_add_is_allowed_when_login_is_local(client: TestClient, monkeypatch):
from app.nqi.client import NqiError