fix(admin): 用户字段唯一性校验
This commit is contained in:
1 parent
d46d61bfa1
commit
6757ad869f
2 files changed
+109
-21
No files matched your search
@@ -1,7 +1,21 @@
|
||||
import pytest
|
||||
from app.models import User
|
||||
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
|
||||
@@ -40,7 +54,7 @@ def test_register_fields_validation(client):
|
||||
}
|
||||
response = client.post('/api/user/register', json=dup_user)
|
||||
assert response.status_code == 400
|
||||
assert "Employee ID already exists" in response.json['error']
|
||||
assert "Employee ID already exists" in _error_message(response)
|
||||
|
||||
def test_admin_duplicate_employee_id(client, admin_headers):
|
||||
"""
|
||||
@@ -68,8 +82,8 @@ def test_admin_duplicate_employee_id(client, admin_headers):
|
||||
"phone_number": "13800000012"
|
||||
}
|
||||
response = client.post('/api/admin/users', json=u2, headers=admin_headers)
|
||||
assert response.status_code == 400
|
||||
assert "Employee ID already exists" in response.json['error']
|
||||
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
|
||||
@@ -86,5 +100,44 @@ def test_admin_duplicate_employee_id(client, admin_headers):
|
||||
|
||||
# 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 == 400
|
||||
assert "Employee ID already exists" in res.json['error']
|
||||
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)
|
||||
Reference in new issue
Block a user