138 lines
3.9 KiB
Python
138 lines
3.9 KiB
Python
"""
|
|
Smoke Tests - ServiceManagerWeb
|
|
|
|
Verifican que el stack completo funciona:
|
|
- Conexión a BD Docker
|
|
- Fixtures de tenant y usuarios
|
|
- Login vía HTTP (httpx + FastAPI en memoria)
|
|
- Endpoint protegido con token
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
# ============================================================
|
|
# BD + FIXTURES
|
|
# ============================================================
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_db_connected(db):
|
|
"""La sesión de BD está activa y responde."""
|
|
from sqlalchemy import text
|
|
result = await db.execute(text("SELECT 1"))
|
|
assert result.scalar() == 1
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_tenant_a_existe(tenant_a):
|
|
"""El tenant 'aduanasoft' existe y tiene datos válidos."""
|
|
assert tenant_a.slug == "aduanasoft"
|
|
assert tenant_a.name is not None
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_admin_user_existe(admin_user):
|
|
"""El usuario ADMIN existe y pertenece al tenant correcto."""
|
|
from app.models.user import UserRole
|
|
assert admin_user.email == "admin@aduanasoft.com"
|
|
assert admin_user.role == UserRole.ADMIN
|
|
assert admin_user.is_active is True
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_manager_user_existe(manager_user):
|
|
"""El usuario SUPPORT_MANAGER existe."""
|
|
from app.models.user import UserRole
|
|
assert manager_user.email == "manager@aduanasoft.com"
|
|
assert manager_user.role == UserRole.SUPPORT_MANAGER
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_agent_user_existe(agent_user):
|
|
"""El usuario AGENT existe."""
|
|
from app.models.user import UserRole
|
|
assert agent_user.email == "agente@aduanasoft.com"
|
|
assert agent_user.role == UserRole.AGENT
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_client_user_existe(user_tenant_a):
|
|
"""El CLIENT_USER existe."""
|
|
from app.models.user import UserRole
|
|
assert user_tenant_a.email == "test_user@aduanasoft.com"
|
|
assert user_tenant_a.role == UserRole.CLIENT_USER
|
|
|
|
|
|
# ============================================================
|
|
# HTTP — LOGIN
|
|
# ============================================================
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_login_admin_ok(client):
|
|
"""Login con credenciales de admin devuelve access_token."""
|
|
response = await client.post(
|
|
"/v1/auth/login",
|
|
json={
|
|
"email": "admin@aduanasoft.com",
|
|
"password": "admin123",
|
|
"tenant_slug": "aduanasoft",
|
|
},
|
|
)
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert "access_token" in data
|
|
assert data["token_type"] == "bearer"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_login_credenciales_invalidas(client):
|
|
"""Login con contraseña incorrecta devuelve 401."""
|
|
response = await client.post(
|
|
"/v1/auth/login",
|
|
json={
|
|
"email": "admin@aduanasoft.com",
|
|
"password": "wrongpassword",
|
|
"tenant_slug": "aduanasoft",
|
|
},
|
|
)
|
|
assert response.status_code == 401
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_endpoint_sin_token_devuelve_401(client):
|
|
"""Acceder a un endpoint protegido sin token devuelve 401."""
|
|
response = await client.get(
|
|
"/v1/users/me",
|
|
headers={"X-Tenant-Slug": "aduanasoft"},
|
|
)
|
|
assert response.status_code == 401
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_login_y_me(client):
|
|
"""Login exitoso → /users/me devuelve el usuario correcto."""
|
|
# Login
|
|
login = await client.post(
|
|
"/v1/auth/login",
|
|
json={
|
|
"email": "admin@aduanasoft.com",
|
|
"password": "admin123",
|
|
"tenant_slug": "aduanasoft",
|
|
},
|
|
)
|
|
assert login.status_code == 200
|
|
token = login.json()["access_token"]
|
|
|
|
# Endpoint protegido
|
|
me = await client.get(
|
|
"/v1/users/me",
|
|
headers={
|
|
"Authorization": f"Bearer {token}",
|
|
"X-Tenant-Slug": "aduanasoft",
|
|
},
|
|
)
|
|
assert me.status_code == 200
|
|
data = me.json()
|
|
assert data["email"] == "admin@aduanasoft.com"
|
|
assert data["role"] == "ADMIN"
|