dify_admin/api/tests/test_accounts.py

55 lines
1.9 KiB
Python
Executable File

import pytest
from fastapi import status
from account_manager import AccountManager
def test_create_account(client, mocker_fixture):
"""测试创建账户"""
# 获取mock对象
mock_create = AccountManager.create_account
response = client.post("/api/accounts/", json={
"username": "newuser",
"email": "new@example.com",
"password": "newpass123"
})
# 验证mock调用
mock_create.assert_called_once_with("newuser", "new@example.com", "newpass123")
assert response.status_code == status.HTTP_200_OK
assert response.json()["user_id"] == "550e8400-e29b-41d4-a716-446655440000"
def test_get_account(client, auth_headers, mocker_fixture):
"""测试获取账户信息"""
# 获取mock对象
mock_get = AccountManager.get_user_by_username
response = client.get("/api/accounts/testuser", headers=auth_headers)
# 验证mock调用
mock_get.assert_called_once_with("testuser")
assert response.status_code == status.HTTP_200_OK
assert response.json()["username"] == "testuser"
def test_change_password(client, auth_headers, mocker_fixture):
"""测试修改密码"""
# 获取mock对象
mock_update = AccountManager.update_password
response = client.put("/api/accounts/password", json={
"current_password": "testpass",
"new_password": "newpassword123"
}, headers=auth_headers)
# 验证mock调用
mock_update.assert_called_once()
assert response.status_code == status.HTTP_200_OK
assert response.json()["message"] == "密码修改成功"
def test_account_not_found(client, auth_headers, mocker_fixture):
"""测试账户不存在"""
# 修改mock抛出404异常
AccountManager.get_user_by_username.side_effect = Exception("404: 账户不存在")
response = client.get("/api/accounts/nonexistent", headers=auth_headers)
assert response.status_code == status.HTTP_404_NOT_FOUND