dify_admin/api/tests/test_tenants.py

45 lines
1.6 KiB
Python
Executable File

import pytest
from fastapi import status
from tenant_manager import TenantManager
def test_create_tenant(client, auth_headers, mocker_fixture):
"""测试创建租户"""
# 获取mock对象
mock_create = TenantManager.create_tenant
response = client.post("/api/tenants/", json={
"name": "testtenant",
"description": "测试租户"
}, headers=auth_headers)
# 验证mock调用
mock_create.assert_called_once_with("testtenant")
assert response.status_code == status.HTTP_200_OK
assert response.json()["message"] == "租户创建成功"
def test_list_tenants(client, auth_headers, mocker_fixture):
"""测试获取租户列表"""
response = client.get("/api/tenants/", headers=auth_headers)
assert response.status_code == status.HTTP_200_OK
assert isinstance(response.json(), list)
def test_get_tenant(client, auth_headers, mocker_fixture):
"""测试获取特定租户"""
# 获取mock对象
mock_get = TenantManager.get_tenant_by_name
response = client.get("/api/tenants/testtenant", headers=auth_headers)
# 验证mock调用
mock_get.assert_called_once_with("testtenant")
assert response.status_code == status.HTTP_200_OK
assert "name" in response.json()["tenant"]
def test_tenant_not_found(client, auth_headers, mocker_fixture):
"""测试租户不存在"""
# 修改mock抛出404异常
TenantManager.get_tenant_by_name.side_effect = Exception("404: 租户不存在")
response = client.get("/api/tenants/nonexistent", headers=auth_headers)
assert response.status_code == status.HTTP_404_NOT_FOUND