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 IncotermDTO(BaseModel):
code: str = Field(..., min_length=1, max_length=5)
description_es: str
description_en: str
class Config:
from_attributes = True
model_config = ConfigDict(from_attributes=True)

View File

@@ -1,26 +1,46 @@
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 Incoterm
from .dto import IncotermDTO
from typing import Any, Dict
router = APIRouter(prefix="/incoterms", tags=["Incoterms"])
@router.get("/", response_model=list[IncotermDTO])
def list_incoterms(db: Session = Depends(get_core_db)):
objs = db.query(Incoterm).all()
return [IncotermDTO.model_validate(obj) for obj in objs]
@router.get("/", response_model=Dict[str, Any])
async def list_incoterms(
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(Incoterm)
items = query.offset(skip).limit(page_size).all()
total = query.count()
return {
"items": [IncotermDTO.model_validate(obj) for obj in items],
"total": total,
"page": page,
"page_size": page_size
}
@router.get("/{key}", response_model=IncotermDTO)
def get_incoterm(key: str, db: Session = Depends(get_core_db)):
async def get_incoterm(key: str, db: Session = Depends(get_core_db), current_user: dict = Depends(get_current_user)):
obj = db.query(Incoterm).filter(Incoterm.code == key).first()
if not obj:
raise HTTPException(status_code=404, detail="Not found")
return IncotermDTO.model_validate(obj)
@router.post("/", response_model=IncotermDTO, status_code=201)
def create_incoterm(
async def create_incoterm(
data: IncotermDTO,
db: Session = Depends(get_core_db),
current_user: dict = Depends(has_role("admin"))
@@ -31,8 +51,9 @@ def create_incoterm(
db.refresh(obj)
return IncotermDTO.model_validate(obj)
@router.put("/{key}", response_model=IncotermDTO)
def update_incoterm(
async def update_incoterm(
key: str,
data: IncotermDTO,
db: Session = Depends(get_core_db),
@@ -47,8 +68,9 @@ def update_incoterm(
db.refresh(obj)
return IncotermDTO.model_validate(obj)
@router.delete("/{key}", status_code=204)
def delete_incoterm(
async def delete_incoterm(
key: str,
db: Session = Depends(get_core_db),
current_user: dict = Depends(has_role("admin"))

View File

@@ -0,0 +1,35 @@
import pytest
from fastapi.testclient import TestClient
from api.v1.modules.public.reference_data.incoterms.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_incoterms(client, access_token):
headers = {"Authorization": f"Bearer {access_token}"}
response = client.get("/incoterms/", 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_incoterm_not_found(client, access_token):
headers = {"Authorization": f"Bearer {access_token}"}
response = client.get("/incoterms/invalid_key", headers=headers)
assert response.status_code == 404
def test_create_incoterm_forbidden():
response = client.post("/incoterms/", json={"key": "TST", "description": "Test"})
assert response.status_code in (403, 405, 404)
def test_update_incoterm_forbidden():
response = client.put("/incoterms/TST", json={"key": "TST", "description": "Test"})
assert response.status_code in (403, 405, 404)
def test_delete_incoterm_forbidden():
response = client.delete("/incoterms/TST")
assert response.status_code in (403, 405, 404)