feat: 用户入口改用自有账号,管理入口只从应用托管进入

This commit is contained in:
2026-11-06 12:06:37 +08:00
parent 1eccaa0fe2
commit 1c20a9e85b
15 changed files with 170 additions and 174 deletions
+32 -23
View File
@@ -11,6 +11,8 @@ from fastapi.testclient import TestClient
TEST_DATA = Path(os.environ["DECOM_DATA_DIR"])
from sqlalchemy import select # noqa: E402
from app.auth.platform import base_href, platform_identity # noqa: E402
from app.db import reset_engine, session_scope # noqa: E402
from app.main import create_app # noqa: E402
@@ -163,8 +165,8 @@ def test_platform_login_three_cases(hosted: TestClient):
assert admin.json()["user"] == {"kind": "platform", "id": "a-1", "role": "admin", "name": "管理员甲"}
user = hosted.post("/api/auth/platform", headers=identity("user"))
assert user.status_code == 200
assert user.json()["user"] == {"kind": "platform", "id": "u-1", "role": "user", "name": "张三"}
assert user.status_code == 403
assert user.json()["detail"] == "请从应用托管进入管理"
saved = hosted.put(
"/api/admin/settings",
@@ -174,7 +176,7 @@ def test_platform_login_three_cases(hosted: TestClient):
assert saved.status_code == 200
refused = hosted.post("/api/auth/platform", headers=identity("user"))
assert refused.status_code == 403
assert refused.json()["detail"] == "当前使用大数据平台账号登录"
assert refused.json()["detail"] == "请从应用托管进入管理"
still_admin = hosted.post("/api/auth/platform", headers=identity("admin", user_id="a-1"))
assert still_admin.status_code == 200
@@ -208,20 +210,23 @@ def test_local_password_login_closed_when_hosted(hosted: TestClient):
with session_scope() as session:
session.add(User(username="admin", password_hash=hash_password("admin-pass"), role="admin"))
session.add(User(username="worker", password_hash=hash_password("worker-pass"), role="user"))
for username, password in (("admin", "admin-pass"), ("worker", "worker-pass")):
local = hosted.post("/api/auth/local", json={"username": username, "password": password})
assert local.status_code == 400
assert local.json()["detail"] == "请从平台登录"
combined = hosted.post("/api/auth/login", json={"username": username, "password": password})
assert combined.status_code == 400
assert combined.json()["detail"] == "请从平台登录"
for path in ("/api/auth/local", "/api/auth/login"):
blocked = hosted.post(path, json={"username": "admin", "password": "admin-pass"})
assert blocked.status_code == 400
assert blocked.json()["detail"] == "账号或密码不正确"
allowed = hosted.post("/api/auth/login", json={"username": "worker", "password": "worker-pass"})
assert allowed.status_code == 200
assert allowed.json()["user"]["role"] == "user"
admin_headers = identity("admin", user_id="a-1", username="boss", display="管理员甲")
headers = bearer(admin_token(hosted), admin_headers)
assert hosted.get("/api/admin/users", headers=headers).status_code == 404
assert hosted.post("/api/admin/users", json={"username": "x", "password": "y"}, headers=headers).status_code == 404
assert hosted.put("/api/admin/users/1/password", json={"password": "z"}, headers=headers).status_code == 404
assert hosted.delete("/api/admin/users/1", headers=headers).status_code == 404
listed = hosted.get("/api/admin/users", headers=headers)
assert listed.status_code == 200
created = hosted.post("/api/admin/users", json={"username": "new", "password": "new-pass", "role": "admin"}, headers=headers)
assert created.status_code == 200
with session_scope() as session:
created_user = session.scalar(select(User).where(User.username == "new"))
assert created_user is not None and created_user.role == "user"
def test_nqi_admin_name_goes_to_platform_when_hosted(hosted: TestClient, monkeypatch):
@@ -272,15 +277,19 @@ def test_switch_to_local_needs_no_local_admin_when_hosted(hosted: TestClient):
assert back.json()["loginMode"] == "local"
def test_platform_user_runs_export(hosted: TestClient):
from app.models import ExportTask
def test_local_user_runs_export(hosted: TestClient):
from app.models import ExportTask, User
from app.security import hash_password
user_headers = identity("user")
login = hosted.post("/api/auth/platform", headers=user_headers)
headers = bearer(login.json()["token"], user_headers)
with session_scope() as session:
session.add(User(username="worker", password_hash=hash_password("worker-pass"), role="user"))
session.add(User(username="other", password_hash=hash_password("other-pass"), role="user"))
login = hosted.post("/api/auth/login", json={"username": "worker", "password": "worker-pass"})
headers = bearer(login.json()["token"])
created = hosted.post("/api/tasks", json={"format": "csv", "cellKey": ""}, headers=headers)
assert created.status_code == 200
task_id = created.json()["task"]["id"]
listing = []
for _ in range(30):
listing = hosted.get("/api/tasks", headers=headers).json()["tasks"]
if listing[0]["status"] == "done":
@@ -289,12 +298,12 @@ def test_platform_user_runs_export(hosted: TestClient):
assert listing[0]["status"] == "done"
with session_scope() as session:
task = session.get(ExportTask, task_id)
assert (task.owner_kind, task.owner_id) == ("platform", "u-1")
owner = session.scalar(select(User).where(User.username == "worker"))
assert (task.owner_kind, task.owner_id) == ("local", str(owner.id))
downloaded = hosted.get(f"/api/tasks/{task_id}/file", headers=headers)
assert downloaded.status_code == 200
other = identity("user", user_id="u-2")
other_login = hosted.post("/api/auth/platform", headers=other)
assert hosted.get(f"/api/tasks/{task_id}/file", headers=bearer(other_login.json()["token"], other)).status_code == 404
other_login = hosted.post("/api/auth/login", json={"username": "other", "password": "other-pass"})
assert hosted.get(f"/api/tasks/{task_id}/file", headers=bearer(other_login.json()["token"])).status_code == 404
admin_headers = identity("admin", user_id="a-1")
assert hosted.get("/api/tasks", headers=bearer(admin_token(hosted), admin_headers)).status_code == 403