149 lines
4.6 KiB
Python
149 lines
4.6 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import requests
|
|
import traceback
|
|
import time
|
|
import sys
|
|
|
|
# API配置
|
|
API_BASE_URL = "http://localhost:5000/api"
|
|
|
|
def print_separator(title):
|
|
"""打印分隔线"""
|
|
print("\n" + "=" * 80)
|
|
print(title.center(80))
|
|
print("=" * 80 + "\n")
|
|
|
|
def create_domain(name, description=""):
|
|
"""创建域名"""
|
|
print(f"创建域名: {name}")
|
|
try:
|
|
data = {
|
|
"name": name,
|
|
"description": description
|
|
}
|
|
response = requests.post(f"{API_BASE_URL}/domains", json=data)
|
|
print(f"响应状态码: {response.status_code}")
|
|
|
|
if response.status_code in (200, 201):
|
|
result = response.json()
|
|
print("响应内容:")
|
|
print(result)
|
|
return result.get("domain")
|
|
else:
|
|
print(f"创建域名失败: {response.text}")
|
|
return None
|
|
except Exception as e:
|
|
print(f"创建域名时出错: {str(e)}")
|
|
traceback.print_exc()
|
|
return None
|
|
|
|
def create_mailbox(domain_id, address):
|
|
"""创建邮箱"""
|
|
print(f"创建邮箱: {address} (域名ID: {domain_id})")
|
|
try:
|
|
data = {
|
|
"domain_id": domain_id,
|
|
"address": address
|
|
}
|
|
response = requests.post(f"{API_BASE_URL}/mailboxes", json=data)
|
|
print(f"响应状态码: {response.status_code}")
|
|
|
|
if response.status_code in (200, 201):
|
|
result = response.json()
|
|
print("响应内容:")
|
|
print(result)
|
|
return result.get("mailbox")
|
|
else:
|
|
print(f"创建邮箱失败: {response.text}")
|
|
return None
|
|
except Exception as e:
|
|
print(f"创建邮箱时出错: {str(e)}")
|
|
traceback.print_exc()
|
|
return None
|
|
|
|
def get_all_domains():
|
|
"""获取所有域名"""
|
|
print("获取所有域名")
|
|
try:
|
|
response = requests.get(f"{API_BASE_URL}/domains")
|
|
print(f"响应状态码: {response.status_code}")
|
|
|
|
if response.status_code == 200:
|
|
result = response.json()
|
|
domains = result.get("domains", [])
|
|
print(f"找到 {len(domains)} 个域名:")
|
|
for domain in domains:
|
|
print(f" - {domain.get('name')} (ID: {domain.get('id')})")
|
|
return domains
|
|
else:
|
|
print(f"获取域名失败: {response.text}")
|
|
return []
|
|
except Exception as e:
|
|
print(f"获取域名时出错: {str(e)}")
|
|
traceback.print_exc()
|
|
return []
|
|
|
|
def get_all_mailboxes():
|
|
"""获取所有邮箱"""
|
|
print("获取所有邮箱")
|
|
try:
|
|
response = requests.get(f"{API_BASE_URL}/mailboxes")
|
|
print(f"响应状态码: {response.status_code}")
|
|
|
|
if response.status_code == 200:
|
|
result = response.json()
|
|
mailboxes = result.get("mailboxes", [])
|
|
print(f"找到 {len(mailboxes)} 个邮箱:")
|
|
for mailbox in mailboxes:
|
|
print(f" - {mailbox.get('full_address')} (ID: {mailbox.get('id')})")
|
|
return mailboxes
|
|
else:
|
|
print(f"获取邮箱失败: {response.text}")
|
|
return []
|
|
except Exception as e:
|
|
print(f"获取邮箱时出错: {str(e)}")
|
|
traceback.print_exc()
|
|
return []
|
|
|
|
def main():
|
|
try:
|
|
print_separator("基础功能测试")
|
|
|
|
# 测试获取所有域名
|
|
print_separator("测试获取域名")
|
|
domains = get_all_domains()
|
|
|
|
# 测试创建新域名
|
|
print_separator("测试创建域名")
|
|
test_domain = create_domain("test-domain.com", "测试域名")
|
|
|
|
if test_domain:
|
|
print(f"成功创建域名: {test_domain.get('name')} (ID: {test_domain.get('id')})")
|
|
|
|
# 测试获取所有邮箱
|
|
print_separator("测试获取邮箱")
|
|
mailboxes = get_all_mailboxes()
|
|
|
|
# 测试创建新邮箱
|
|
print_separator("测试创建邮箱")
|
|
test_mailbox = create_mailbox(test_domain.get('id'), "testuser")
|
|
|
|
if test_mailbox:
|
|
print(f"成功创建邮箱: {test_mailbox.get('full_address')} (ID: {test_mailbox.get('id')})")
|
|
else:
|
|
print("创建邮箱失败")
|
|
else:
|
|
print("创建域名失败")
|
|
|
|
print_separator("测试完成")
|
|
return 0
|
|
|
|
except Exception as e:
|
|
print(f"测试过程中发生错误: {str(e)}")
|
|
traceback.print_exc()
|
|
return 1
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main()) |