feat: 自动拼图失败三次后改为手动拖动
This commit is contained in:
@@ -3,6 +3,7 @@ import json
|
||||
from fastapi.testclient import TestClient
|
||||
from sqlalchemy import text
|
||||
|
||||
from euniv.client import JigsawRejected, ManualJigsaw
|
||||
from euniv.crypto import decrypt_secret
|
||||
from euniv.db import SessionLocal, engine, init_db
|
||||
from euniv.models import Account
|
||||
@@ -110,3 +111,92 @@ def test_login_stores_password_and_credential_only() -> None:
|
||||
assert updated["yxt_token"] == "yxt-2"
|
||||
assert "测试用户" not in decrypt_secret(account.credential_cipher)
|
||||
assert "测试用户" not in decrypt_secret(account.password_cipher)
|
||||
|
||||
|
||||
def test_manual_jigsaw_keeps_image_pixels_and_retries() -> None:
|
||||
import euniv.api as api_module
|
||||
import euniv.service as service_module
|
||||
|
||||
service_module._pending.clear()
|
||||
|
||||
class ManualClient:
|
||||
def __init__(self, login_name: str, password: str) -> None:
|
||||
self.login_name = login_name
|
||||
self.password = password
|
||||
self._profile = {
|
||||
"token": "ucs-1",
|
||||
"accountId": login_name,
|
||||
"userId": "u1",
|
||||
"ssoinfo": "sso",
|
||||
"unionId": "",
|
||||
}
|
||||
|
||||
def close(self) -> None:
|
||||
return None
|
||||
|
||||
def login(self) -> dict:
|
||||
raise ManualJigsaw({
|
||||
"big_img": "data:image/png;base64,YQ==",
|
||||
"small_img": "data:image/png;base64,Yg==",
|
||||
"y": 56,
|
||||
"width": 280,
|
||||
"height": 172,
|
||||
"piece_width": 48,
|
||||
"piece_height": 48,
|
||||
})
|
||||
|
||||
def next_jigsaw(self) -> dict:
|
||||
return {
|
||||
"big_img": "data:image/png;base64,Yw==",
|
||||
"small_img": "data:image/png;base64,ZA==",
|
||||
"y": 60,
|
||||
"width": 280,
|
||||
"height": 172,
|
||||
"piece_width": 48,
|
||||
"piece_height": 48,
|
||||
}
|
||||
|
||||
def submit_jigsaw(self, offset: int) -> dict:
|
||||
if offset != 157:
|
||||
raise JigsawRejected("拼图未通过")
|
||||
return dict(self._profile)
|
||||
|
||||
def authorize(self, profile: dict) -> None:
|
||||
self._profile = dict(profile)
|
||||
|
||||
def export_credential(self) -> dict:
|
||||
return {
|
||||
"ucs_token": "ucs-1",
|
||||
"ucs_account_id": self.login_name,
|
||||
"ucs_user_id": "u1",
|
||||
"ucs_ssoinfo": "sso",
|
||||
"ucs_union_id": "",
|
||||
"yxt_token": "yxt-manual",
|
||||
}
|
||||
|
||||
api_module.login_and_store = lambda db, employee_id, password: service_module.login_and_store(
|
||||
db, employee_id, password, ManualClient
|
||||
)
|
||||
api_module.submit_jigsaw = service_module.submit_jigsaw
|
||||
client = TestClient(api_module.app)
|
||||
started = client.post("/api/login", json={"employee_id": "6650000146", "password": "secret-pass"})
|
||||
assert started.status_code == 200
|
||||
body = started.json()
|
||||
assert body["jigsaw"]["width"] == 280
|
||||
assert body["jigsaw"]["height"] == 172
|
||||
assert body["jigsaw"]["y"] == 56
|
||||
assert "password" not in body["jigsaw"]
|
||||
with SessionLocal() as db:
|
||||
assert db.get(Account, "6650000146") is None
|
||||
|
||||
missed = client.post("/api/login/jigsaw", json={"attempt_id": body["attempt_id"], "offset": 10})
|
||||
assert missed.status_code == 200
|
||||
assert missed.json()["jigsaw"]["y"] == 60
|
||||
|
||||
done = client.post("/api/login/jigsaw", json={"attempt_id": body["attempt_id"], "offset": 157})
|
||||
assert done.status_code == 200
|
||||
assert done.json() == {"employee_id": "6650000146"}
|
||||
with SessionLocal() as db:
|
||||
account = db.get(Account, "6650000146")
|
||||
assert account is not None
|
||||
assert decrypt_secret(account.password_cipher) == "secret-pass"
|
||||
|
||||
Reference in New Issue
Block a user