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:
@@ -1,9 +1,9 @@
|
||||
from pydantic import BaseModel, Field
|
||||
from pydantic import ConfigDict
|
||||
|
||||
class CustomsSectionDTO(BaseModel):
|
||||
customs_code: str = Field(..., min_length=1, max_length=3)
|
||||
section_name: str
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
|
||||
@@ -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 CustomsSection
|
||||
from .dto import CustomsSectionDTO
|
||||
from typing import Any, Dict
|
||||
|
||||
|
||||
router = APIRouter(prefix="/customs-sections", tags=["Customs Sections"])
|
||||
|
||||
@router.get("/", response_model=list[CustomsSectionDTO])
|
||||
def list_customs_sections(db: Session = Depends(get_core_db)):
|
||||
return db.query(CustomsSection).all()
|
||||
|
||||
@router.get("/", response_model=Dict[str, Any])
|
||||
def list_customs_sections(
|
||||
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(CustomsSection)
|
||||
items = query.offset(skip).limit(page_size).all()
|
||||
total = query.count()
|
||||
return {
|
||||
"items": [CustomsSectionDTO.model_validate(obj) for obj in items],
|
||||
"total": total,
|
||||
"page": page,
|
||||
"page_size": page_size
|
||||
}
|
||||
|
||||
@router.get("/{customs_code}", response_model=CustomsSectionDTO)
|
||||
def get_customs_section(customs_code: str, db: Session = Depends(get_core_db)):
|
||||
def get_customs_section(customs_code: str, db: Session = Depends(get_core_db), current_user: dict = Depends(get_current_user)):
|
||||
obj = db.query(CustomsSection).filter(CustomsSection.customs_code == customs_code).first()
|
||||
if not obj:
|
||||
raise HTTPException(status_code=404, detail="Not found")
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
from api.v1.modules.public.reference_data.customs_sections.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_customs_sections(client, access_token):
|
||||
headers = {"Authorization": f"Bearer {access_token}"}
|
||||
response = client.get("/customs-sections/", 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_customs_section_not_found(client, access_token):
|
||||
headers = {"Authorization": f"Bearer {access_token}"}
|
||||
response = client.get("/customs-sections/invalid_code", headers=headers)
|
||||
assert response.status_code == 404
|
||||
|
||||
def test_create_customs_section_forbidden():
|
||||
response = client.post("/customs-sections/", json={"customs_code": "TST", "description": "Test"})
|
||||
assert response.status_code in (403, 405, 404)
|
||||
|
||||
def test_update_customs_section_forbidden():
|
||||
response = client.put("/customs-sections/TST", json={"customs_code": "TST", "description": "Test"})
|
||||
assert response.status_code in (403, 405, 404)
|
||||
|
||||
def test_delete_customs_section_forbidden():
|
||||
response = client.delete("/customs-sections/TST")
|
||||
assert response.status_code in (403, 405, 404)
|
||||
Reference in New Issue
Block a user