feat: 账号可设置同时学习课程数并支持关闭代理
This commit is contained in:
@@ -452,6 +452,7 @@ def test_check_skips_empty_credential_and_updates_saved_one() -> None:
|
||||
"study_start_hour",
|
||||
"study_start_minute",
|
||||
"study_start_second",
|
||||
"course_workers",
|
||||
}
|
||||
assert saved["has_credential"] is True
|
||||
assert saved["refresh_paused"] is False
|
||||
@@ -726,3 +727,67 @@ def test_admin_can_set_study_start_for_selected_users() -> None:
|
||||
assert two["study_start"] == "每月5日 16:08:09"
|
||||
detail = client.get("/api/admin/users/s1-user/study-start")
|
||||
assert detail.json()["day"] == 20
|
||||
|
||||
|
||||
def test_admin_can_set_course_workers_for_selected_users() -> None:
|
||||
_add_account("w1-user")
|
||||
_add_account("w2-user")
|
||||
api = _app()
|
||||
client = TestClient(api.app)
|
||||
assert client.post("/api/login", json={"employee_id": "admin", "password": INITIAL_PASSWORD}).status_code == 200
|
||||
listed = client.get("/api/admin/users").json()
|
||||
one = next(item for item in listed if item["employee_id"] == "w1-user")
|
||||
assert one["course_workers"] == 1
|
||||
saved = client.put(
|
||||
"/api/admin/users/course-workers",
|
||||
json={"employee_ids": ["w1-user", "w2-user"], "workers": 4},
|
||||
)
|
||||
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"] == "w1-user")
|
||||
two = next(item for item in listed if item["employee_id"] == "w2-user")
|
||||
assert one["course_workers"] == 4
|
||||
assert two["course_workers"] == 4
|
||||
one_only = client.put(
|
||||
"/api/admin/users/course-workers",
|
||||
json={"employee_ids": ["w1-user"], "workers": 1},
|
||||
)
|
||||
assert one_only.status_code == 200
|
||||
listed = client.get("/api/admin/users").json()
|
||||
one = next(item for item in listed if item["employee_id"] == "w1-user")
|
||||
two = next(item for item in listed if item["employee_id"] == "w2-user")
|
||||
assert one["course_workers"] == 1
|
||||
assert two["course_workers"] == 4
|
||||
bad = client.put(
|
||||
"/api/admin/users/course-workers",
|
||||
json={"employee_ids": ["w1-user"], "workers": 9},
|
||||
)
|
||||
assert bad.status_code == 400
|
||||
detail = client.get("/api/admin/users/w1-user/course-workers")
|
||||
assert detail.json()["workers"] == 1
|
||||
|
||||
|
||||
def test_admin_can_turn_off_proxy_with_scheme() -> None:
|
||||
_add_account("off-user")
|
||||
api = _app()
|
||||
client = TestClient(api.app)
|
||||
assert client.post("/api/login", json={"employee_id": "admin", "password": INITIAL_PASSWORD}).status_code == 200
|
||||
client.put(
|
||||
"/api/admin/users/proxy",
|
||||
json={"employee_ids": ["off-user"], "scheme": "http", "host": "10.0.0.1", "port": 8080},
|
||||
)
|
||||
listed = client.get("/api/admin/users").json()
|
||||
one = next(item for item in listed if item["employee_id"] == "off-user")
|
||||
assert one["has_proxy"] is True
|
||||
closed = client.put(
|
||||
"/api/admin/users/proxy",
|
||||
json={"employee_ids": ["off-user"], "scheme": "off"},
|
||||
)
|
||||
assert closed.status_code == 200
|
||||
listed = client.get("/api/admin/users").json()
|
||||
one = next(item for item in listed if item["employee_id"] == "off-user")
|
||||
assert one["has_proxy"] is False
|
||||
detail = client.get("/api/admin/users/off-user/proxy")
|
||||
assert detail.json()["enabled"] is False
|
||||
assert detail.json()["scheme"] == "off"
|
||||
|
||||
@@ -121,6 +121,7 @@ def test_login_stores_password_and_credential_only() -> None:
|
||||
"study_start_hour",
|
||||
"study_start_minute",
|
||||
"study_start_second",
|
||||
"course_workers",
|
||||
}
|
||||
|
||||
home = client.get("/api/home")
|
||||
@@ -456,6 +457,7 @@ def test_user_can_set_proxy_with_or_without_password() -> None:
|
||||
empty = client.get("/api/settings/proxy")
|
||||
assert empty.status_code == 200
|
||||
assert empty.json()["enabled"] is False
|
||||
assert empty.json()["scheme"] == "off"
|
||||
assert "password" not in empty.json()
|
||||
saved = client.put(
|
||||
"/api/settings/proxy",
|
||||
@@ -490,6 +492,30 @@ def test_user_can_set_proxy_with_or_without_password() -> None:
|
||||
cleared = client.delete("/api/settings/proxy")
|
||||
assert cleared.status_code == 200
|
||||
assert cleared.json()["enabled"] is False
|
||||
assert cleared.json()["scheme"] == "off"
|
||||
off = client.put("/api/settings/proxy", json={"scheme": "off"})
|
||||
assert off.status_code == 200
|
||||
assert off.json()["enabled"] is False
|
||||
assert off.json()["scheme"] == "off"
|
||||
|
||||
|
||||
def test_user_can_set_course_workers() -> 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/course-workers")
|
||||
assert empty.status_code == 200
|
||||
assert empty.json() == {"workers": 1, "max": 8, "recommended": True}
|
||||
saved = client.put("/api/settings/course-workers", json={"workers": 3})
|
||||
assert saved.status_code == 200
|
||||
assert saved.json() == {"workers": 3, "max": 8, "recommended": False}
|
||||
bad = client.put("/api/settings/course-workers", json={"workers": 9})
|
||||
assert bad.status_code == 400
|
||||
|
||||
|
||||
def test_user_can_set_monthly_study_start() -> None:
|
||||
|
||||
@@ -4,8 +4,9 @@ import pytest
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from euniv.autostudy import HoursWatcher, _parse_minutes, _study_video, wait_stop
|
||||
from euniv.proxy import ProxyError, build_proxy_url, parse_proxy
|
||||
from euniv.autostudy import CourseCursor, HoursWatcher, _parse_minutes, _study_video, wait_stop
|
||||
from euniv.course_workers import CourseWorkersError, of as workers_of, validate as validate_workers
|
||||
from euniv.proxy import ProxyError, apply as apply_proxy, build_proxy_url, parse_proxy, view_of as proxy_view
|
||||
from euniv.study_window import CN_TZ, StudyWindowError, validate_start, window_open
|
||||
|
||||
|
||||
@@ -192,3 +193,54 @@ def test_study_window_rejects_invalid_parts() -> None:
|
||||
validate_start(32, 0, 0, 0)
|
||||
with pytest.raises(StudyWindowError):
|
||||
validate_start(1, 24, 0, 0)
|
||||
|
||||
|
||||
def test_course_workers_default_and_range() -> None:
|
||||
class Account:
|
||||
course_workers = 1
|
||||
|
||||
assert workers_of(Account()) == 1
|
||||
assert validate_workers(8) == 8
|
||||
with pytest.raises(CourseWorkersError):
|
||||
validate_workers(0)
|
||||
with pytest.raises(CourseWorkersError):
|
||||
validate_workers(9)
|
||||
|
||||
|
||||
def test_course_cursor_hands_each_item_once() -> None:
|
||||
async def main() -> None:
|
||||
cursor = CourseCursor([("a", "A", "视频"), ("b", "B", "文档"), ("c", "C", "课程")])
|
||||
seen: list[str] = []
|
||||
|
||||
async def worker() -> None:
|
||||
while True:
|
||||
item = await cursor.take()
|
||||
if item is None:
|
||||
return
|
||||
seen.append(item[0])
|
||||
await asyncio.sleep(0)
|
||||
|
||||
await asyncio.gather(worker(), worker(), worker())
|
||||
assert sorted(seen) == ["a", "b", "c"]
|
||||
|
||||
asyncio.run(main())
|
||||
|
||||
|
||||
class _ProxyAccount:
|
||||
def __init__(self) -> None:
|
||||
self.proxy_scheme = "http"
|
||||
self.proxy_host = "10.0.0.1"
|
||||
self.proxy_port = 8080
|
||||
self.proxy_username = "u"
|
||||
self.proxy_password_cipher = "cipher"
|
||||
|
||||
|
||||
def test_proxy_off_scheme_clears_settings() -> None:
|
||||
account = _ProxyAccount()
|
||||
apply_proxy(account, "off")
|
||||
view = proxy_view(account)
|
||||
assert view["enabled"] is False
|
||||
assert view["scheme"] == "off"
|
||||
assert account.proxy_scheme == ""
|
||||
assert account.proxy_host == ""
|
||||
assert account.proxy_port is None
|
||||
|
||||
Reference in New Issue
Block a user