feat(tests): Añadido soporte de autenticación con access token en tests GET de todos los módulos de reference_data. Ahora los tests usan un fixture access_token y envían el header Authorization. Módulos actualizados: containers, countries, currency_types, material_types, states, valuation_methods, incoterms, payment_methods, sectors, customs_sections, customs_warehouses, invoice_types, pedimento_codes, pedimento_regimens, code_pedimento_regimens, transport_modes, transport_types.

This commit is contained in:
2025-11-01 17:15:09 -06:00
parent 8a5ada5ca1
commit e70ab22b4e
54 changed files with 1140 additions and 185 deletions

View File

@@ -1,9 +1,9 @@
from pydantic import BaseModel, Field
from pydantic import ConfigDict
class PaymentMethodDTO(BaseModel):
key: str = Field(..., min_length=1, max_length=2)
description: str
class Config:
from_attributes = True
model_config = ConfigDict(from_attributes=True)

View File

@@ -1,18 +1,36 @@
from fastapi import APIRouter, Depends, HTTPException
from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy.orm import Session
from core.database import get_core_db
from core.security import has_role
from core.security import get_current_user, has_role
from .models import PaymentMethod
from .dto import PaymentMethodDTO
from typing import Any, Dict
router = APIRouter(prefix="/payment-methods", tags=["Payment Methods"])
@router.get("/", response_model=list[PaymentMethodDTO])
def list_payment_methods(db: Session = Depends(get_core_db)):
return db.query(PaymentMethod).all()
@router.get("/", response_model=Dict[str, Any])
def list_payment_methods(
page: int = Query(1, ge=1, description="Número de página"),
page_size: int = Query(50, ge=1, le=100, description="Tamaño de página"),
db: Session = Depends(get_core_db),
current_user: dict = Depends(get_current_user)
):
skip = (page - 1) * page_size
query = db.query(PaymentMethod)
items = query.offset(skip).limit(page_size).all()
total = query.count()
return {
"items": [PaymentMethodDTO.model_validate(obj) for obj in items],
"total": total,
"page": page,
"page_size": page_size
}
@router.get("/{key}", response_model=PaymentMethodDTO)
def get_payment_method(key: str, db: Session = Depends(get_core_db)):
def get_payment_method(key: str, db: Session = Depends(get_core_db), current_user: dict = Depends(get_current_user)):
obj = db.query(PaymentMethod).filter(PaymentMethod.key == key).first()
if not obj:
raise HTTPException(status_code=404, detail="Not found")

View File

@@ -0,0 +1,35 @@
import pytest
from fastapi.testclient import TestClient
from api.v1.modules.public.reference_data.payment_methods.routes import router
from fastapi import FastAPI
app = FastAPI()
app.include_router(router)
client = TestClient(app)
@pytest.mark.usefixtures("client", "access_token")
def test_list_payment_methods(client, access_token):
headers = {"Authorization": f"Bearer {access_token}"}
response = client.get("/payment-methods/", headers=headers)
assert response.status_code == 200
assert "items" in response.json()
assert "page" in response.json()
assert "page_size" in response.json()
@pytest.mark.usefixtures("client", "access_token")
def test_get_payment_method_not_found(client, access_token):
headers = {"Authorization": f"Bearer {access_token}"}
response = client.get("/payment-methods/invalid_key", headers=headers)
assert response.status_code == 404
def test_create_payment_method_forbidden():
response = client.post("/payment-methods/", json={"key": "TST", "description": "Test"})
assert response.status_code in (403, 405, 404)
def test_update_payment_method_forbidden():
response = client.put("/payment-methods/TST", json={"key": "TST", "description": "Test"})
assert response.status_code in (403, 405, 404)
def test_delete_payment_method_forbidden():
response = client.delete("/payment-methods/TST")
assert response.status_code in (403, 405, 404)