80 lines
2.4 KiB
Python
80 lines
2.4 KiB
Python
# ================================================================
|
|
# services/auth/tests/test_auth.py
|
|
# ================================================================
|
|
"""Authentication tests"""
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.services.auth_service import AuthService
|
|
from app.schemas.auth import UserRegistration, UserLogin
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_register_user(db: AsyncSession):
|
|
"""Test user registration"""
|
|
user_data = UserRegistration(
|
|
email="test@bakery.es",
|
|
password="TestPass123",
|
|
full_name="Test User",
|
|
language="es"
|
|
)
|
|
|
|
result = await AuthService.register_user(user_data, db)
|
|
|
|
assert result.email == "test@bakery.es"
|
|
assert result.full_name == "Test User"
|
|
assert result.is_active is True
|
|
assert result.is_verified is False
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_login_user(db: AsyncSession):
|
|
"""Test user login"""
|
|
# First register a user
|
|
user_data = UserRegistration(
|
|
email="test@bakery.es",
|
|
password="TestPass123",
|
|
full_name="Test User",
|
|
language="es"
|
|
)
|
|
await AuthService.register_user(user_data, db)
|
|
|
|
# Then login
|
|
login_data = UserLogin(
|
|
email="test@bakery.es",
|
|
password="TestPass123"
|
|
)
|
|
|
|
result = await AuthService.login_user(login_data, db, "127.0.0.1", "test-agent")
|
|
|
|
assert result.access_token is not None
|
|
assert result.refresh_token is not None
|
|
assert result.token_type == "bearer"
|
|
|
|
def test_register_endpoint(client: TestClient, test_user_data):
|
|
"""Test registration endpoint"""
|
|
response = client.post("/auth/register", json=test_user_data)
|
|
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["email"] == test_user_data["email"]
|
|
assert "id" in data
|
|
|
|
def test_login_endpoint(client: TestClient, test_user_data):
|
|
"""Test login endpoint"""
|
|
# First register
|
|
client.post("/auth/register", json=test_user_data)
|
|
|
|
# Then login
|
|
login_data = {
|
|
"email": test_user_data["email"],
|
|
"password": test_user_data["password"]
|
|
}
|
|
response = client.post("/auth/login", json=login_data)
|
|
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert "access_token" in data
|
|
assert "refresh_token" in data
|
|
assert data["token_type"] == "bearer"
|