Files
orion/tests/unit/models/schema/test_auth.py
Samir Boulahtit 9920430b9e fix: correct tojson|safe usage in templates and update validator
- Remove |safe from |tojson in HTML attributes (x-data) - quotes must
  become " for browsers to parse correctly
- Update LANG-002 and LANG-003 architecture rules to document correct
  |tojson usage patterns:
  - HTML attributes: |tojson (no |safe)
  - Script blocks: |tojson|safe
- Fix validator to warn when |tojson|safe is used in x-data (breaks
  HTML attribute parsing)
- Improve code quality across services, APIs, and tests

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-13 22:59:51 +01:00

187 lines
5.6 KiB
Python

# tests/unit/models/schema/test_auth.py
"""Unit tests for auth Pydantic schemas."""
import pytest
from pydantic import ValidationError
from models.schema.auth import (
UserCreate,
UserLogin,
UserResponse,
UserUpdate,
)
@pytest.mark.unit
@pytest.mark.schema
class TestUserLoginSchema:
"""Test UserLogin schema validation."""
def test_valid_login(self):
"""Test valid login data."""
login = UserLogin(
email_or_username="testuser",
password="password123",
)
assert login.email_or_username == "testuser"
assert login.password == "password123"
def test_login_with_email(self):
"""Test login with email."""
login = UserLogin(
email_or_username="test@example.com",
password="password123",
)
assert login.email_or_username == "test@example.com"
def test_login_with_vendor_code(self):
"""Test login with optional vendor code."""
login = UserLogin(
email_or_username="testuser",
password="password123",
vendor_code="VENDOR001",
)
assert login.vendor_code == "VENDOR001"
def test_email_or_username_stripped(self):
"""Test email_or_username is stripped of whitespace."""
login = UserLogin(
email_or_username=" testuser ",
password="password123",
)
assert login.email_or_username == "testuser"
@pytest.mark.unit
@pytest.mark.schema
class TestUserCreateSchema:
"""Test UserCreate schema validation."""
def test_valid_user_create(self):
"""Test valid user creation data."""
user = UserCreate(
email="admin@example.com",
username="adminuser",
password="securepass",
first_name="Admin",
last_name="User",
role="admin",
)
assert user.email == "admin@example.com"
assert user.role == "admin"
def test_default_role_is_vendor(self):
"""Test default role is vendor."""
user = UserCreate(
email="vendor@example.com",
username="vendoruser",
password="securepass",
)
assert user.role == "vendor"
def test_invalid_role(self):
"""Test invalid role raises ValidationError."""
with pytest.raises(ValidationError) as exc_info:
UserCreate(
email="test@example.com",
username="testuser",
password="securepass",
role="superadmin",
)
assert "role" in str(exc_info.value).lower()
def test_username_too_short(self):
"""Test username shorter than 3 characters raises ValidationError."""
with pytest.raises(ValidationError) as exc_info:
UserCreate(
email="test@example.com",
username="ab",
password="securepass",
)
assert "username" in str(exc_info.value).lower()
def test_password_min_length(self):
"""Test password minimum length validation."""
with pytest.raises(ValidationError) as exc_info:
UserCreate(
email="test@example.com",
username="testuser",
password="12345",
)
assert "password" in str(exc_info.value).lower()
@pytest.mark.unit
@pytest.mark.schema
class TestUserUpdateSchema:
"""Test UserUpdate schema validation."""
def test_partial_update(self):
"""Test partial update with only some fields."""
update = UserUpdate(first_name="NewName")
assert update.first_name == "NewName"
assert update.username is None
assert update.email is None
def test_username_update_normalized(self):
"""Test username in update is normalized."""
update = UserUpdate(username="NewUser")
assert update.username == "newuser"
def test_invalid_role_update(self):
"""Test invalid role in update raises ValidationError."""
with pytest.raises(ValidationError):
UserUpdate(role="superadmin")
def test_valid_role_update(self):
"""Test valid role values."""
admin_update = UserUpdate(role="admin")
vendor_update = UserUpdate(role="vendor")
assert admin_update.role == "admin"
assert vendor_update.role == "vendor"
def test_empty_update(self):
"""Test empty update is valid (all fields optional)."""
update = UserUpdate()
assert update.model_dump(exclude_unset=True) == {}
@pytest.mark.unit
@pytest.mark.schema
class TestUserResponseSchema:
"""Test UserResponse schema."""
def test_from_dict(self):
"""Test creating response from dict."""
from datetime import datetime
data = {
"id": 1,
"email": "test@example.com",
"username": "testuser",
"role": "vendor",
"is_active": True,
"created_at": datetime.now(),
"updated_at": datetime.now(),
}
response = UserResponse(**data)
assert response.id == 1
assert response.email == "test@example.com"
assert response.is_active is True
def test_optional_last_login(self):
"""Test last_login is optional."""
from datetime import datetime
data = {
"id": 1,
"email": "test@example.com",
"username": "testuser",
"role": "vendor",
"is_active": True,
"created_at": datetime.now(),
"updated_at": datetime.now(),
}
response = UserResponse(**data)
assert response.last_login is None