feat: 增加管理首页运行面板并支持用户改密
This commit is contained in:
@@ -205,6 +205,43 @@ def test_concurrency_bounds():
|
||||
check_concurrency(33)
|
||||
|
||||
|
||||
def test_admin_dashboard_and_user_password(client: TestClient):
|
||||
from datetime import timedelta
|
||||
|
||||
from app.db import session_scope
|
||||
from app.models import NqiAccount, utcnow
|
||||
|
||||
logged = client.post("/api/auth/local", json={"username": "admin", "password": "admin-pass"})
|
||||
headers = {"Authorization": f"Bearer {logged.json()['token']}"}
|
||||
admin_id = logged.json()["user"]["id"]
|
||||
client.post("/api/admin/users", json={"username": "user", "password": "user-pass", "role": "user"}, headers=headers)
|
||||
client.post("/api/admin/users", json={"username": "boss", "password": "boss-pass", "role": "admin"}, headers=headers)
|
||||
rows = client.get("/api/admin/users", headers=headers).json()["users"]
|
||||
user_id = next(row["id"] for row in rows if row["username"] == "user")
|
||||
other_id = next(row["id"] for row in rows if row["username"] == "boss")
|
||||
denied = client.put(f"/api/admin/users/{other_id}/password", json={"password": "hacked"}, headers=headers)
|
||||
assert denied.status_code == 400
|
||||
changed = client.put(f"/api/admin/users/{user_id}/password", json={"password": "new-user-pass"}, headers=headers)
|
||||
assert changed.status_code == 200
|
||||
assert client.post("/api/auth/local", json={"username": "user", "password": "new-user-pass"}).status_code == 200
|
||||
self_ok = client.put(f"/api/admin/users/{admin_id}/password", json={"password": "admin-pass-2"}, headers=headers)
|
||||
assert self_ok.status_code == 200
|
||||
empty = client.get("/api/admin/dashboard", headers=headers)
|
||||
assert empty.status_code == 200
|
||||
assert empty.json()["pool"]["total"] == 0
|
||||
now = utcnow()
|
||||
with session_scope() as session:
|
||||
session.add(NqiAccount(login_id="a1", city_name="广州", credential_valid=True, session_open=True, saved_at=now))
|
||||
session.add(NqiAccount(login_id="a2", city_name="深圳", credential_valid=False, session_open=False, saved_at=now - timedelta(days=2)))
|
||||
stats = client.get("/api/admin/dashboard", headers=headers).json()["pool"]
|
||||
assert stats["total"] == 2
|
||||
assert stats["valid"] == 1
|
||||
assert stats["invalid"] == 1
|
||||
assert stats["addedToday"] == 1
|
||||
assert stats["addedWeek"] == 2
|
||||
assert stats["sessionOpen"] == 1
|
||||
|
||||
|
||||
def test_admin_can_set_concurrency(client: TestClient):
|
||||
logged = client.post("/api/auth/local", json={"username": "admin", "password": "admin-pass"})
|
||||
headers = {"Authorization": f"Bearer {logged.json()['token']}"}
|
||||
|
||||
Reference in New Issue
Block a user