新增管理员创建用户接口 POST /admin/users,创建后邮箱自动确认

This commit is contained in:
2026-07-26 02:20:03 +08:00
parent 8149505f1b
commit 0776e91735
3 changed files with 116 additions and 0 deletions
+72
View File
@@ -52,6 +52,78 @@ def test_list_users(client: TestClient) -> None:
assert r.json()["total"] >= 1
# ---------------------------------------------------------------------------
# create user
# ---------------------------------------------------------------------------
def test_create_user(client: TestClient) -> None:
adm = admin_token(client)
r = client.post(
"/api/v1/admin/users",
headers=bearer(adm),
json={
"email": f"admin-created-{uuid.uuid4().hex[:8]}@example.com",
"password": "createdpass12",
"username": "管理员创建的用户",
},
)
assert r.status_code == 201
body = r.json()
assert body["user"]["email_verified"] is True
assert body["profile"]["role"] == "user"
def test_create_user_with_role(client: TestClient) -> None:
adm = admin_token(client)
r = client.post(
"/api/v1/admin/users",
headers=bearer(adm),
json={
"email": f"newadmin-{uuid.uuid4().hex[:8]}@example.com",
"password": "newadmin12",
"username": "新建管理员",
"role": "admin",
},
)
assert r.status_code == 201
assert r.json()["profile"]["role"] == "admin"
def test_create_user_duplicate_email(client: TestClient) -> None:
adm = admin_token(client)
email = f"dup-admin-{uuid.uuid4().hex[:8]}@example.com"
client.post(
"/api/v1/admin/users",
headers=bearer(adm),
json={"email": email, "password": "dup1pass12", "username": "第一人"},
)
r = client.post(
"/api/v1/admin/users",
headers=bearer(adm),
json={"email": email, "password": "dup2pass12", "username": "第二人"},
)
assert r.status_code == 409
def test_create_user_non_admin(client: TestClient, monkeypatch) -> None:
info = register_and_confirm(
client, monkeypatch,
f"na-create-{uuid.uuid4().hex[:8]}@example.com",
"nonadmin1", "非管理员创建",
)
r = client.post(
"/api/v1/admin/users",
headers=bearer(info["token"]),
json={
"email": f"should-fail-{uuid.uuid4().hex[:8]}@example.com",
"password": "failpass12",
"username": "失败的用户",
},
)
assert r.status_code == 403
# ---------------------------------------------------------------------------
# update user
# ---------------------------------------------------------------------------