feat: 账号可配置 HTTP/HTTPS/SOCKS 代理
This commit is contained in:
@@ -10,9 +10,10 @@ from euniv.models import Account
|
||||
|
||||
|
||||
class FakeClient:
|
||||
def __init__(self, login_name: str, password: str) -> None:
|
||||
def __init__(self, login_name: str, password: str, proxy: str | None = None) -> None:
|
||||
self.login_name = login_name
|
||||
self.password = password
|
||||
self.proxy = proxy
|
||||
self._token = "yxt-1"
|
||||
self._profile = {
|
||||
"token": "ucs-1",
|
||||
@@ -111,6 +112,11 @@ def test_login_stores_password_and_credential_only() -> None:
|
||||
"credential_valid",
|
||||
"month_hours",
|
||||
"monthly_target",
|
||||
"proxy_scheme",
|
||||
"proxy_host",
|
||||
"proxy_port",
|
||||
"proxy_username",
|
||||
"proxy_password_cipher",
|
||||
}
|
||||
|
||||
home = client.get("/api/home")
|
||||
@@ -434,6 +440,54 @@ def test_period_page_is_not_fetched_twice() -> None:
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
def test_user_can_set_proxy_with_or_without_password() -> None:
|
||||
import euniv.api as api_module
|
||||
import euniv.service as service_module
|
||||
|
||||
api_module.login_and_store = lambda db, employee_id, password, manual=False, auto_attempts=3: service_module.login_and_store(
|
||||
db, employee_id, password, FakeClient, manual=manual, auto_attempts=auto_attempts
|
||||
)
|
||||
client = TestClient(api_module.app)
|
||||
assert client.post("/api/login", json={"employee_id": "6650000146", "password": "secret-pass"}).status_code == 200
|
||||
empty = client.get("/api/settings/proxy")
|
||||
assert empty.status_code == 200
|
||||
assert empty.json()["enabled"] is False
|
||||
assert "password" not in empty.json()
|
||||
saved = client.put(
|
||||
"/api/settings/proxy",
|
||||
json={"scheme": "http", "host": "10.0.0.1", "port": 8080, "username": "", "password": ""},
|
||||
)
|
||||
assert saved.status_code == 200
|
||||
assert saved.json() == {
|
||||
"enabled": True,
|
||||
"scheme": "http",
|
||||
"host": "10.0.0.1",
|
||||
"port": 8080,
|
||||
"username": "",
|
||||
"has_password": False,
|
||||
}
|
||||
with_auth = client.put(
|
||||
"/api/settings/proxy",
|
||||
json={"scheme": "socks5", "host": "127.0.0.1", "port": 1080, "username": "u", "password": "p@ss"},
|
||||
)
|
||||
assert with_auth.status_code == 200
|
||||
assert with_auth.json()["has_password"] is True
|
||||
assert "password" not in with_auth.json()
|
||||
kept = client.put(
|
||||
"/api/settings/proxy",
|
||||
json={"scheme": "socks5", "host": "127.0.0.1", "port": 1080, "username": "u", "password": None},
|
||||
)
|
||||
assert kept.status_code == 200
|
||||
assert kept.json()["has_password"] is True
|
||||
with SessionLocal() as db:
|
||||
account = db.get(Account, "6650000146")
|
||||
assert account is not None
|
||||
assert decrypt_secret(account.proxy_password_cipher) == "p@ss"
|
||||
cleared = client.delete("/api/settings/proxy")
|
||||
assert cleared.status_code == 200
|
||||
assert cleared.json()["enabled"] is False
|
||||
|
||||
|
||||
def test_exam_list_rejects_unrecognized_page() -> None:
|
||||
from euniv.client import EunivClient
|
||||
|
||||
|
||||
Reference in New Issue
Block a user