91 lines
2.9 KiB
Python
91 lines
2.9 KiB
Python
import pytest
|
|
from app.models import User
|
|
from app import db
|
|
|
|
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 response.json['error']
|
|
|
|
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 == 400
|
|
assert "Employee ID already exists" in response.json['error']
|
|
|
|
# 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 == 400
|
|
assert "Employee ID already exists" in res.json['error']
|