feat: 账号可配置 HTTP/HTTPS/SOCKS 代理
This commit is contained in:
@@ -291,9 +291,10 @@ def test_saving_new_interval_restarts_wait_without_refresh(monkeypatch) -> None:
|
||||
|
||||
|
||||
class _Alive:
|
||||
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.login_called = False
|
||||
|
||||
def close(self) -> None:
|
||||
@@ -444,6 +445,8 @@ def test_check_skips_empty_credential_and_updates_saved_one() -> None:
|
||||
"last_refresh_at",
|
||||
"credential_valid",
|
||||
"month_hours",
|
||||
"has_proxy",
|
||||
"proxy",
|
||||
}
|
||||
assert saved["has_credential"] is True
|
||||
assert saved["refresh_paused"] is False
|
||||
@@ -632,3 +635,58 @@ def test_settings_do_not_expose_process_switches() -> None:
|
||||
}
|
||||
assert identity.password_cipher()
|
||||
assert decrypt_secret(identity.password_cipher()) == INITIAL_PASSWORD
|
||||
|
||||
|
||||
def test_admin_can_set_and_clear_proxy_for_selected_users() -> None:
|
||||
_add_account("p1-user")
|
||||
_add_account("p2-user")
|
||||
api = _app()
|
||||
client = TestClient(api.app)
|
||||
assert client.post("/api/login", json={"employee_id": "admin", "password": INITIAL_PASSWORD}).status_code == 200
|
||||
saved = client.put(
|
||||
"/api/admin/users/proxy",
|
||||
json={
|
||||
"employee_ids": ["p1-user", "p2-user"],
|
||||
"scheme": "socks5",
|
||||
"host": "127.0.0.1",
|
||||
"port": 1080,
|
||||
"username": "u",
|
||||
"password": "secret",
|
||||
},
|
||||
)
|
||||
assert saved.status_code == 200
|
||||
assert saved.json()["count"] == 2
|
||||
listed = client.get("/api/admin/users").json()
|
||||
one = next(item for item in listed if item["employee_id"] == "p1-user")
|
||||
assert one["has_proxy"] is True
|
||||
assert one["proxy"] == "SOCKS5 127.0.0.1:1080"
|
||||
detail = client.get("/api/admin/users/p1-user/proxy")
|
||||
assert detail.status_code == 200
|
||||
assert detail.json()["username"] == "u"
|
||||
assert detail.json()["has_password"] is True
|
||||
assert "password" not in detail.json()
|
||||
cleared = client.post("/api/admin/users/proxy/clear", json={"employee_ids": ["p1-user"]})
|
||||
assert cleared.status_code == 200
|
||||
listed = client.get("/api/admin/users").json()
|
||||
one = next(item for item in listed if item["employee_id"] == "p1-user")
|
||||
two = next(item for item in listed if item["employee_id"] == "p2-user")
|
||||
assert one["has_proxy"] is False
|
||||
assert two["has_proxy"] is True
|
||||
no_auth = client.put(
|
||||
"/api/admin/users/proxy",
|
||||
json={
|
||||
"employee_ids": ["p1-user"],
|
||||
"scheme": "http",
|
||||
"host": "10.0.0.1",
|
||||
"port": 8080,
|
||||
"username": "",
|
||||
"password": "",
|
||||
},
|
||||
)
|
||||
assert no_auth.status_code == 200
|
||||
listed = client.get("/api/admin/users").json()
|
||||
one = next(item for item in listed if item["employee_id"] == "p1-user")
|
||||
assert one["proxy"] == "HTTP 10.0.0.1:8080"
|
||||
detail = client.get("/api/admin/users/p1-user/proxy")
|
||||
assert detail.json()["has_password"] is False
|
||||
assert detail.json()["username"] == ""
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import asyncio
|
||||
import pytest
|
||||
|
||||
from euniv.autostudy import HoursWatcher, _parse_minutes, _study_video, wait_stop
|
||||
from euniv.proxy import parse_proxy
|
||||
from euniv.proxy import ProxyError, build_proxy_url, parse_proxy
|
||||
|
||||
|
||||
def test_parse_proxy_accepts_http_and_socks() -> None:
|
||||
@@ -16,6 +16,15 @@ def test_parse_proxy_accepts_http_and_socks() -> None:
|
||||
assert parse_proxy("socks5://user:pass@127.0.0.1:1080") == "socks5://user:pass@127.0.0.1:1080"
|
||||
|
||||
|
||||
def test_build_proxy_url_auth_is_optional_and_quoted() -> None:
|
||||
assert build_proxy_url("http", "10.0.0.1", 8080) == "http://10.0.0.1:8080"
|
||||
assert build_proxy_url("socks5", "127.0.0.1", 1080, "u", "p@ss") == "socks5://u:p%40ss@127.0.0.1:1080"
|
||||
assert build_proxy_url("http", "10.0.0.1", 8080, "u", "") == "http://u:@10.0.0.1:8080"
|
||||
assert build_proxy_url("socks4", "127.0.0.1", 1080, "", "s3cret") == "socks4://:s3cret@127.0.0.1:1080"
|
||||
with pytest.raises(ProxyError):
|
||||
build_proxy_url("ss", "127.0.0.1", 1080)
|
||||
|
||||
|
||||
def test_parse_proxy_rejects_other_schemes() -> None:
|
||||
with pytest.raises(ValueError):
|
||||
parse_proxy("ss://secret")
|
||||
|
||||
Reference in New Issue
Block a user