使用 curl 创建测试账号(快速方法)

快速创建所有测试账号

方法一:使用 PowerShell(Windows)

在 PowerShell 中执行以下命令:

# 设置变量
$API_BASE_URL = "http://localhost:8000"
$PASSWORD = "test123"

# 测试账号列表
$testUsers = @(
    "test_dev@example.com",
    "test_user@example.com",
    "test_enterprise@example.com",
    "test_operator@example.com",
    "test_admin@example.com",
    "test_super@example.com"
)

# 创建所有账号
foreach ($email in $testUsers) {
    Write-Host "创建账号: $email"
    $body = @{
        email = $email
        password = $PASSWORD
    } | ConvertTo-Json
    
    try {
        $response = Invoke-RestMethod -Uri "$API_BASE_URL/api/v1/users/register" `
            -Method Post `
            -Body $body `
            -ContentType "application/json"
        Write-Host "  [OK] 创建成功" -ForegroundColor Green
    } catch {
        if ($_.Exception.Response.StatusCode -eq 400) {
            Write-Host "  [跳过] 账号已存在" -ForegroundColor Yellow
        } else {
            Write-Host "  [失败] $($_.Exception.Message)" -ForegroundColor Red
        }
    }
    Write-Host ""
}

方法二:使用 curl(Linux/Mac/Windows Git Bash)

#!/bin/bash
API_BASE_URL="http://localhost:8000"
PASSWORD="test123"

# 测试账号列表
test_users=(
    "test_dev@example.com"
    "test_user@example.com"
    "test_enterprise@example.com"
    "test_operator@example.com"
    "test_admin@example.com"
    "test_super@example.com"
)

# 创建所有账号
for email in "${test_users[@]}"; do
    echo "创建账号: $email"
    response=$(curl -s -X POST "$API_BASE_URL/api/v1/users/register" \
        -H "Content-Type: application/json" \
        -d "{\"email\":\"$email\",\"password\":\"$PASSWORD\"}")
    
    if echo "$response" | grep -q "注册成功\|access_token"; then
        echo "  [OK] 创建成功"
    elif echo "$response" | grep -q "已注册\|已存在"; then
        echo "  [跳过] 账号已存在"
    else
        echo "  [失败] $response"
    fi
    echo ""
done

方法三:逐个创建(手动)

1. 创建开发者账号

curl -X POST http://localhost:8000/api/v1/users/register \
  -H "Content-Type: application/json" \
  -d '{"email":"test_dev@example.com","password":"test123"}'

2. 创建注册用户账号

curl -X POST http://localhost:8000/api/v1/users/register \
  -H "Content-Type: application/json" \
  -d '{"email":"test_user@example.com","password":"test123"}'

3. 创建企业用户账号

curl -X POST http://localhost:8000/api/v1/users/register \
  -H "Content-Type: application/json" \
  -d '{"email":"test_enterprise@example.com","password":"test123"}'

4. 创建运营员账号

curl -X POST http://localhost:8000/api/v1/users/register \
  -H "Content-Type: application/json" \
  -d '{"email":"test_operator@example.com","password":"test123"}'

5. 创建管理员账号

curl -X POST http://localhost:8000/api/v1/users/register \
  -H "Content-Type: application/json" \
  -d '{"email":"test_admin@example.com","password":"test123"}'

6. 创建超级管理员账号

curl -X POST http://localhost:8000/api/v1/users/register \
  -H "Content-Type: application/json" \
  -d '{"email":"test_super@example.com","password":"test123"}'

验证账号创建

测试登录

curl -X POST http://localhost:8000/api/v1/users/login \
  -H "Content-Type: application/json" \
  -d '{"email":"test_dev@example.com","password":"test123"}'

如果返回 access_token,说明账号创建成功。


修改用户角色

注册 API 创建的账号默认角色为 user。要修改为其他角色,需要:

  1. 使用管理员账号登录后台
  2. 在用户管理页面修改角色

或者使用管理员 API(需要管理员权限):

# 1. 管理员登录,获取 admin_session
ADMIN_SESSION="your_admin_session_cookie"

# 2. 获取用户ID
USER_ID=$(curl -s -X GET "http://localhost:8000/admin/api/users?search=test_dev@example.com" \
  -H "Cookie: admin_session=$ADMIN_SESSION" | jq -r '.users[0].user_id')

# 3. 修改角色为 developer
curl -X POST "http://localhost:8000/admin/api/users/$USER_ID/role?role=developer" \
  -H "Cookie: admin_session=$ADMIN_SESSION"

测试账号列表

创建完成后,使用以下账号登录 MBE Desktop:

角色 邮箱 密码
开发者 test_dev@example.com test123
注册用户 test_user@example.com test123
企业用户 test_enterprise@example.com test123
运营员 test_operator@example.com test123
管理员 test_admin@example.com test123
超级管理员 test_super@example.com test123

注意: 通过注册 API 创建的账号默认角色为 user,需要管理员权限修改为其他角色。