144 lines
4.7 KiB
Python
144 lines
4.7 KiB
Python
import pytest
|
|
from app.models import User, Agent
|
|
from app import db
|
|
|
|
def _error_message(response):
|
|
body = response.get_json() or {}
|
|
error = body.get('error')
|
|
if isinstance(error, dict):
|
|
return str(error.get('message') or '')
|
|
return str(error or '')
|
|
|
|
def _error_code(response):
|
|
body = response.get_json() or {}
|
|
error = body.get('error')
|
|
if isinstance(error, dict):
|
|
return str(error.get('code') or '')
|
|
return ''
|
|
|
|
def test_register_fields_validation(client):
|
|
"""
|
|
Test user registration field validation
|
|
"""
|
|
# 1. Missing fields
|
|
response = client.post('/api/user/register', json={
|
|
"username": "missing",
|
|
"password": "pwd",
|
|
"email": "m@e.com"
|
|
})
|
|
assert response.status_code == 400
|
|
assert "Real name and Employee ID are required" in response.json['error']
|
|
|
|
# 2. Duplicate Employee ID
|
|
# First create a user (via admin to bypass check or register with valid data)
|
|
# Let's register one valid user
|
|
valid_user = {
|
|
"username": "user1",
|
|
"password": "pwd",
|
|
"email": "u1@e.com",
|
|
"real_name": "User One",
|
|
"employee_id": "EMP100",
|
|
"phone_number": "13800000001"
|
|
}
|
|
response = client.post('/api/user/register', json=valid_user)
|
|
assert response.status_code == 201
|
|
|
|
# Now try to register another user with SAME employee_id
|
|
dup_user = {
|
|
"username": "user2",
|
|
"password": "pwd",
|
|
"email": "u2@e.com",
|
|
"real_name": "User Two",
|
|
"employee_id": "EMP100",
|
|
"phone_number": "13800000002"
|
|
}
|
|
response = client.post('/api/user/register', json=dup_user)
|
|
assert response.status_code == 400
|
|
assert "Employee ID already exists" in _error_message(response)
|
|
|
|
def test_admin_duplicate_employee_id(client, admin_headers):
|
|
"""
|
|
Test admin user creation duplicate employee_id check
|
|
"""
|
|
# 1. Create first user
|
|
u1 = {
|
|
"username": "adm_u1",
|
|
"password": "pwd",
|
|
"email": "au1@e.com",
|
|
"real_name": "Admin User 1",
|
|
"employee_id": "ADM001",
|
|
"phone_number": "13800000011"
|
|
}
|
|
response = client.post('/api/admin/users', json=u1, headers=admin_headers)
|
|
assert response.status_code == 201
|
|
|
|
# 2. Create second user with same ID
|
|
u2 = {
|
|
"username": "adm_u2",
|
|
"password": "pwd",
|
|
"email": "au2@e.com",
|
|
"real_name": "Admin User 2",
|
|
"employee_id": "ADM001",
|
|
"phone_number": "13800000012"
|
|
}
|
|
response = client.post('/api/admin/users', json=u2, headers=admin_headers)
|
|
assert response.status_code == 409
|
|
assert "Employee ID already exists" in _error_message(response)
|
|
|
|
# 3. Update user to duplicate ID
|
|
# Create u3 with valid ID
|
|
u3 = {
|
|
"username": "adm_u3",
|
|
"password": "pwd",
|
|
"email": "au3@e.com",
|
|
"real_name": "Admin User 3",
|
|
"employee_id": "ADM003",
|
|
"phone_number": "13800000013"
|
|
}
|
|
res = client.post('/api/admin/users', json=u3, headers=admin_headers)
|
|
u3_id = res.json['id']
|
|
|
|
# Update u3 to ADM001 (which exists)
|
|
res = client.put(f'/api/admin/users/{u3_id}', json={"employee_id": "ADM001"}, headers=admin_headers)
|
|
assert res.status_code == 409
|
|
assert "Employee ID already exists" in _error_message(res)
|
|
assert _error_code(res) == 'USER.EMPLOYEE_ID_EXISTS'
|
|
|
|
def test_admin_duplicate_phone_on_update_with_agent_ids(client, admin_headers):
|
|
"""
|
|
编辑用户时若手机号重复且同时提交 agent_ids,应在 autoflush 前返回冲突而非 500。
|
|
"""
|
|
existing = {
|
|
"username": "phone_owner",
|
|
"password": "pwd",
|
|
"email": "phone_owner@e.com",
|
|
"real_name": "Phone Owner",
|
|
"employee_id": "PHN001",
|
|
"phone_number": "123456",
|
|
}
|
|
target = {
|
|
"username": "phone_target",
|
|
"password": "pwd",
|
|
"email": "phone_target@e.com",
|
|
"real_name": "Phone Target",
|
|
"employee_id": "PHN002",
|
|
"phone_number": "13800009999",
|
|
}
|
|
assert client.post('/api/admin/users', json=existing, headers=admin_headers).status_code == 201
|
|
target_res = client.post('/api/admin/users', json=target, headers=admin_headers)
|
|
assert target_res.status_code == 201
|
|
target_id = target_res.json['id']
|
|
|
|
agent = Agent(name='Dup Phone Agent', dify_app_id='dup-phone-agent', type='chat')
|
|
db.session.add(agent)
|
|
db.session.commit()
|
|
|
|
res = client.put(
|
|
f'/api/admin/users/{target_id}',
|
|
json={'phone_number': '123456', 'agent_ids': [agent.id]},
|
|
headers=admin_headers,
|
|
)
|
|
assert res.status_code == 409
|
|
assert _error_code(res) == 'USER.PHONE_EXISTS'
|
|
assert "Phone number already exists" in _error_message(res)
|