78 lines
2.5 KiB
Python
78 lines
2.5 KiB
Python
"""
|
|
Test suite validating FastAPI ASGI Core & Routers.
|
|
"""
|
|
from fastapi.testclient import TestClient
|
|
from main import app
|
|
from app.models.user import User
|
|
from app.models.classroom import Classroom
|
|
from app.services.jwt_service import JWTService
|
|
from werkzeug.security import generate_password_hash
|
|
|
|
|
|
def test_fastapi_version_endpoint():
|
|
client = TestClient(app)
|
|
response = client.get('/api/v1/version')
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data['status'] == 'success'
|
|
assert 'version' in data
|
|
assert data['engine'] == 'FastAPI ASGI'
|
|
|
|
|
|
def test_fastapi_docs_endpoint():
|
|
client = TestClient(app)
|
|
response = client.get('/docs')
|
|
assert response.status_code == 200
|
|
assert 'swagger-ui' in response.text.lower() or 'html' in response.text.lower()
|
|
|
|
|
|
def test_fastapi_auth_providers(init_database):
|
|
client = TestClient(app)
|
|
response = client.get('/api/v1/auth/providers')
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data['status'] == 'success'
|
|
assert 'providers' in data
|
|
assert 'local' in data['providers']
|
|
|
|
|
|
def test_fastapi_auth_login_invalid(init_database):
|
|
client = TestClient(app)
|
|
response = client.post('/api/v1/auth/login', json={'email': 'inexistente@unicaba.edu.ar', 'password': 'wrong'})
|
|
assert response.status_code == 401
|
|
|
|
|
|
def test_fastapi_full_flow_auth_and_classrooms(init_database):
|
|
client = TestClient(app)
|
|
|
|
# 1. Crear usuario Admin de prueba
|
|
u = User(
|
|
email="admin.fastapi@unicaba.edu.ar",
|
|
name="Admin FastAPI",
|
|
password_hash=generate_password_hash("Secret123!"),
|
|
role="ADMIN",
|
|
is_active=True
|
|
)
|
|
init_database.session.add(u)
|
|
init_database.session.commit()
|
|
|
|
# 2. Login
|
|
login_res = client.post('/api/v1/auth/login', json={
|
|
'email': 'admin.fastapi@unicaba.edu.ar',
|
|
'password': 'Secret123!'
|
|
})
|
|
assert login_res.status_code == 200
|
|
login_data = login_res.json()
|
|
token = login_data['access_token']
|
|
assert token is not None
|
|
|
|
# 3. GET /me con Bearer token
|
|
me_res = client.get('/api/v1/auth/me', headers={'Authorization': f'Bearer {token}'})
|
|
assert me_res.status_code == 200
|
|
assert me_res.json()['user']['email'] == 'admin.fastapi@unicaba.edu.ar'
|
|
|
|
# 4. GET /classrooms
|
|
classrooms_res = client.get('/api/v1/classrooms', headers={'Authorization': f'Bearer {token}'})
|
|
assert classrooms_res.status_code == 200
|
|
assert 'classrooms' in classrooms_res.json()
|